본문 바로가기

개발중/Java

카테고리 완성 기록

728x90
반응형
	/**
	 * 경우 1 ) 전체 코드 반환
	 */
	public List<CodeVo> getParentCodeList( int sysCode ) {
		
		return  sysCode > 0 ? separateSyscode( this.commonParentCodeList , sysCode ) : this.commonParentCodeList;
	}

 

 

	/**
	 * 경우 2 ) 전체 코드 + depth 반환
	 */
	public List<CodeVo> getParentCodeListFilterDepth( int sysCode, int depth ) {

		List<CodeVo> codeList = generateCodeList();
		codeList = mappingParentAndChild( codeList );
		codeList = separateParent( codeList );
		codeList = sysCode > 0 ? separateSyscode( codeList , sysCode ) : codeList ;
		codeList = cuttingDepth( codeList, depth );
		
		return codeList;
	}

 

	/**
	 * 경우 3 ) FiledName 조건 반환
	 */
	public List<CodeVo> getParentCodeList( int sysCode, String filedName, String value ) {

		List<CodeVo> codeList = sysCode > 0 ? separateSyscode( this.commonCodeList , sysCode ) : this.commonCodeList;
		codeList = mappingParentAndChild( codeList );
		codeList = separateParent( codeList );
		codeList = mappingFieldAndValue( codeList, filedName, value ); 
		
		return codeList;
	}

 

 

	/**
	 * 경우 4 ) FiledName + depth 조건 반환
	 */
	public List<CodeVo> getParentCodeListFilterDepth ( int sysCode, int depth, String filedName, String value ) {
		
		List<CodeVo> codeList = generateCodeList();
		codeList = sysCode > 0 ? separateSyscode( codeList , sysCode ) : codeList;
		codeList = mappingParentAndChild( codeList );
		codeList = separateParent( codeList );
		codeList = mappingFieldAndValue( codeList, filedName, value);
		codeList = cuttingDepth( codeList, depth );
		return codeList;
	}

 

	/**
	 * 경우 5 ) codeSeq 하위 트리 구조로 반환
	 */
	public List<CodeVo> getSeqCodeList ( int codeSeq ) {
		
		List<CodeVo> codeList = this.commonCodeList;
		codeList = mappingFieldAndValue( codeList, "code_seq", Integer.toString(codeSeq) );
		return codeList;
	}

 

 


 

	/**
	 * 기능 1 ) System Code 구분
	 */
	public List<CodeVo> separateSyscode( List<CodeVo> codeList , int sysCode ) {
		
		codeList = codeList.stream()
				           .filter( x-> x.getSys_code() == sysCode )
				           .collect(Collectors.toList());
		return codeList;
	}

 

 

	/**
	 * 기능 2 ) List copy
	 */
	public List<CodeVo> generateCodeList() {
		
		List<CodeVo> newCodeList = new ArrayList<>();

		for( CodeVo codeVo : commonCodeList ) {
			CodeVo codeCopyVo = codeVo.clone();
			codeCopyVo.setChildList(new ArrayList<>());
			newCodeList.add(codeCopyVo);
		}
		return newCodeList;
	}

 

 

	/**
	 * 기능 3 ) depth cutting
	 */
	public List<CodeVo> cuttingDepth( List<CodeVo> codeList , int depth ) {
		
		if ( depth == 1 ) {
			for ( CodeVo vo : codeList ) {
				List<CodeVo> childList = vo.getChildList();
				for (Iterator<CodeVo> it = childList.iterator(); it.hasNext();) {
					it.next();
					it.remove();
				}
			}
			return codeList;
		} else {
			for (CodeVo vo : codeList) {
				cuttingDepth( vo.getChildList(), depth - 1);
			}
		}
		return codeList;
	}

 

 

	/**
	 * 기능 4 ) Field == Value Mapping
	 */
	public List<CodeVo> mappingFieldAndValue( List<CodeVo> codeList , String fieldName, String value ) {
		
		try {
			Field field = CodeVo.class.getDeclaredField(fieldName);
			field.setAccessible(true);
			
			if( field.getType() == int.class ) {
				codeList = mappingFieldAndValueSecond( codeList, field, Integer.parseInt(value) );
				
			} else if( field.getType() == String.class) {
				codeList = mappingFieldAndValueSecond( codeList, field, value );
				
			} else {
				throw new RuntimeException("code value type error");
			}
			
		} catch (NoSuchFieldException e) {
			throw new RuntimeException("지원하지 않는 fieldName 입니다 ", e);
			
		} catch (SecurityException e) {
			throw new RuntimeException("권한 없음 에러 입니다 : " + e);
		}
		return codeList;
	}

 

 

	/**
	 * 기능 4-1 ) 하위 요소 탐색 String
	 */
	public List<CodeVo> mappingFieldAndValueSecond(  List<CodeVo> codeList, Field field, String paramValue ) {
		
		codeList = codeList.stream()
						   .filter(code -> {
					   	                 Object fieldValue = fieldGet( field, code );
						                 if( !Optional.ofNullable(fieldValue).isEmpty() && fieldValue.equals(paramValue)) {
							                 return true;
						                 }
						                 return false;
						   }).collect(Collectors.toList());
		return codeList;
	}

 

 

	/**
	 * 기능 4-2 ) 하위 요소 탐색 Integer
	 */
	public List<CodeVo> mappingFieldAndValueSecond(  List<CodeVo> codeList, Field field, int paramValue ) {
		
		codeList = codeList.stream()
						   .filter(code -> {
					   	                 int codeSeq = (int)fieldGet( field, code );
						                 if( codeSeq == paramValue) {
							                 return true;
						                 }
						                 return false;
						   }).collect(Collectors.toList());
		return codeList;
	}

 

 

	public Object fieldGet( Field field, CodeVo vo ) {
		
		Object fieldValue = null;
		try {
			fieldValue = field.get(vo);
			return fieldValue;
			
		} catch ( IllegalArgumentException e) {
			throw new RuntimeException("부적절한 인수 입니다 : " + e);
			
		} catch ( IllegalAccessException e) {
			throw new RuntimeException("참조할 수 없는 클래스 입니다 : " + e);
		}
	}

 

 

	/**
	 * 기능 5 ) 최상위 요소만 반환
	 */
	public List<CodeVo> separateParent( List<CodeVo> codeList ) {
		codeList = codeList.stream()
	               .filter( x -> x.getCode_p_seq() == 0)
	               .collect(Collectors.toList());
		return codeList;
	}

 

 

	/**
	 * 기능 6 ) Parent == Child Mapping 
	 */
	public List<CodeVo> mappingParentAndChild( List<CodeVo> codeList ) {

		for (CodeVo vo : codeList) {
			vo.setChildList(new ArrayList<>());
		}

		for (CodeVo children : codeList) {
			for (CodeVo parent : codeList) {
				if (children.getCode_p_seq() == parent.getCode_seq()) {
					
					if (parent.getChildList().isEmpty()) {
						parent.setChildList(new ArrayList<>());
						parent.getChildList().add(children);
						
					} else {
						parent.getChildList().add(children);
					}
					break;
				}
			}
		}
		
		return codeList;
	}

 

 

	/**
	 * 기능 6-1 ) Parent == Child Mapping 지정 Copy 
	 */
	public List<CodeVo> mappingParentAndChild( List<CodeVo> oneSelfList, List<CodeVo> childrenList ) {

		for (CodeVo vo : oneSelfList) {
			vo.setChildList(new ArrayList<>());
		}

		for (CodeVo self : oneSelfList ) {
			for (CodeVo child : childrenList ) {
				if ( self.getCode_seq() == child.getCode_p_seq() ) {
					
					if ( self.getChildList().isEmpty()) {
						 self.setChildList(new ArrayList<>());
						 self.getChildList().add(child.clone());
						
					} else {
						self.getChildList().add(child.clone());
					}
					break;
				}
			}
		}
		oneSelfList = oneSelfList.stream().collect(Collectors.toList());
		return oneSelfList;
	}

 

 

	/**
	 * TEST Print
	 */
	public void testPrint( List<CodeVo> codeList ) {

		for (CodeVo v1 : codeList) {
			List<CodeVo> child1 = v1.getChildList();

			System.out.printf("⇨ " + v1.getCode_seq() + " / ( p : " + v1.getCode_p_seq() + ") : %10s" + " / sys : "
					+ v1.getSys_code() + "\n", v1.getCode_name());

			for (CodeVo v2 : child1) {
				List<CodeVo> child2 = v2.getChildList();

				System.out.printf("     ⇨ " + v2.getCode_seq() + " / ( p : " + v2.getCode_p_seq() + ") : %10s  / sys : "
						+ v2.getSys_code() + "\n", v2.getCode_name());

				for (CodeVo v3 : child2) {
					List<CodeVo> child3 = v3.getChildList();

					System.out.printf("          ⇨ " + v3.getCode_seq() + " / ( p : " + v3.getCode_p_seq()
							+ ") : %10s / sys : " + v3.getSys_code() + "\n", v3.getCode_name());
					for (CodeVo v4 : child3) {
						System.out.printf("               ⇨ " + v4.getCode_seq() + " / ( p : " + v4.getCode_p_seq()
								+ ") : %10s  / sys : " + v4.getSys_code() + "\n", v4.getCode_name());
					}
				}
			}
		}
	}
728x90
반응형