package com.leanwo.activiti.util;

public class FieldUtil {
	
	/**
	 * 获取组合ID
	 * @param fieldId
	 * @param fieldIndex
	 * @return
	 */
	public static String getCompositeId(Long fieldId, int fieldIndex){
		return fieldId.toString() + "_" + fieldIndex;
	}
	
	/**
	 * 从组合ID中获取FieldId
	 * @param compositeId
	 * @return
	 */
	public static Long getFieldId(String compositeId){
		String fieldIdStr = compositeId.substring(0, compositeId.indexOf("_"));		
		Long fieldId = Long.parseLong(fieldIdStr);
		return fieldId;
	}
	
	
	/**
	 * 从组合ID中获取FieldIndex
	 * @param compositeId
	 * @return
	 */
	public static int getFieldIndex(String compositeId){
		String fieldIndexStr = compositeId.substring(compositeId.lastIndexOf("_") + 1);
		Integer fieldIndex = Integer.parseInt(fieldIndexStr);
		return fieldIndex;
	}
	
	/**
	 * 获取包含的子栏位数量
	 * @param fieldName
	 * @return
	 */
	public static int getFieldMaxIndex(String fieldName){
		if(fieldName == null){
			return 0;
		}
		return fieldName.split(",").length; 
	}
	
	/**
	 * 获取字段名称
	 * @param name
	 * @param index
	 * @return
	 */
	public static String getFieldName(String name, int index){
		if(name == null || index < 0){
			return null;
		}
		String[] names = name.split(",");
		if(index >= names.length){
			return null;
		}else{
			return names[index];
		}
	}
	
	public static String getDisplayName(String[] displayNames){
		if(displayNames == null || displayNames.length == 0){
			return null;
		}
		StringBuilder stringBuilder = new StringBuilder();
		for(int i = 0; i < displayNames.length; i++){
			String displayName = displayNames[i];
			if(i == (displayNames.length - 1)){
				if(displayName != null){
					stringBuilder.append(displayName);
				}
			}else{
				if(displayName == null){
					stringBuilder.append(",");
				}else{
					stringBuilder.append(displayName).append(",");
				}
			}
		}
		return stringBuilder.toString();
	}
}
