Java tutorial
package com.castis.xylophone.adsmadapter.convert.axistree; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.transaction.annotation.Transactional; import com.castis.tambourine.bizobject.common.Period; import com.castis.tambourine.bizobject.ktadsm.ADSMJobNameType; import com.castis.tambourine.bizobject.ktadsm.ADSMSchedulerStatus; import com.castis.tambourine.define.Constants; import com.castis.tambourine.dto.inventory.NodeDTO; import com.castis.tambourine.dto.ktadsm.ADSMSchedulerLogDTO; import com.castis.tambourine.dto.ktadsm.FeSyncNoticeboardDTO; import com.castis.tambourine.dto.ktadsm.InventoryDataDTO; import com.castis.tambourine.dto.ktadsm.NodeDataDTO; import com.castis.tambourine.dto.ktadsm.RegionDataDTO; import com.castis.tambourine.enumeration.FeSyncNoticeboardStatus; import com.castis.tambourine.enumeration.MapperAxisType; import com.castis.tambourine.enumeration.Platform; import com.castis.tambourine.enumeration.SyncDataType; import com.castis.tambourine.enumeration.TreeType; import com.castis.tambourine.logger.TheLogger; import com.castis.tambourine.util.DateUtil; import com.castis.xylophone.adsmadapter.common.exception.CiRuntimeException; import com.castis.xylophone.adsmadapter.common.util.CiDateUtil; import com.castis.xylophone.adsmadapter.filePolling.FilePolling; import com.castis.xylophone.adsmadapter.setting.ADDSAdapterSetting; public class ConvertRegionAxisTreeDTO extends ConvertTreeDTO { static final Log log = LogFactory.getLog(ConvertRegionAxisTreeDTO.class); static final String name = "ConvertNodeTreeDTO"; public static final String REGION_DATA_ROOT_NODE_NAME = "region_data_tree_root"; public static final String NODE_DATA_ROOT_NODE_NAME = "root_region_node"; public static final String NODE_DATA_TREE_NAME = "node_data_tree"; public static final String REGION_AXIS_TREE_PARTIAL_UPDATE = "REGION_AXIS_TREE_PARTIAL_UPDATE"; private static final String ROOT_REGION_PARENT = "*"; private static final int REGION_DATA_FIELD_INDEX_SI = 0; //private static final int REGION_DATA_FIELD_INDEX_GU = 1; private static final int REGION_DATA_FIELD_INDEX_DONG = 2; private static final int REGION_DATA_FIELD_INDEX_COUNT = 3; private ADSMJobNameType srcJobType; private String regionMapperDomain; public void setRegionMapperDomain(String regionMapperDomain) { this.regionMapperDomain = regionMapperDomain; } public ConvertRegionAxisTreeDTO() { log.info(" ### " + name + " was started."); } public void close() { log.info(" # " + name + " is shutting down."); } public boolean insertNodeData(List<NodeDataDTO> nodeDataList, ADSMSchedulerLogDTO schedulerLog) { return tambourineConnector.insertNodeData(nodeDataList, schedulerLog); } public boolean insertRegionData(List<RegionDataDTO> regionDataList, ADSMSchedulerLogDTO schedulerLog, String dataFileName) { return tambourineConnector.insertRegionData(regionDataList, schedulerLog, resultDirectory, dataFileName); } public void writeMapperFile(PrintWriter writer, NodeDTO nodeDTO, int treeID) { if (nodeDTO == null) return; List<NodeDTO> childNodeDTO = nodeDTO.getChildren(); if (childNodeDTO != null) { for (int i = 0; i < childNodeDTO.size(); i++) writeMapperFile(writer, childNodeDTO.get(i), treeID); } writer.println("Region" + "\t" + treeID + "\t" + nodeDTO.getNodeID() + "\t" + nodeDTO.getName() + "\t" + nodeDTO.getExternalNodeId()); } public List<DatInfomation> getNewSchedulerLogParam(ADSMJobNameType srcJopType) { Map<ADSMSchedulerLogDTO, String/*treeName*/> schedulerLogMap = tambourineConnector .getNewSchedulerLogByTreeName(srcJopType, Constants.tree.REGION_TREE_NAME); if (schedulerLogMap == null || schedulerLogMap.isEmpty()) { return null; } List<DatInfomation> datInfomationList = new ArrayList<DatInfomation>(); for (Map.Entry<ADSMSchedulerLogDTO, String> entry : schedulerLogMap.entrySet()) { ADSMSchedulerLogDTO dataLog = entry.getKey(); String treeName = entry.getValue(); int dataLogId = dataLog.getId(); String message = dataLog.getMessage(); String datStartTime = (String) dataLog.getParamValue(FilePolling.PARAM_START_TIME_PREFIX); String publisher = (String) dataLog.getParamValue(FilePolling.PARAM_SITE_PREFIX); String platformType = (String) dataLog.getParamValue(FilePolling.PARAM_PLATFORM_PREFIX); String extendParameter = ""; if (datStartTime != null && publisher != null) extendParameter = "/" + FilePolling.PARAM_TREE_NAME + ":" + treeName + "/" + FilePolling.PARAM_START_TIME_PREFIX + ":" + datStartTime + "/" + FilePolling.PARAM_SITE_PREFIX + ":" + publisher + "/" + FilePolling.PARAM_PLATFORM_PREFIX + ":" + platformType; String schedulerLogParam = "dataLogId:" + dataLogId;// + extendParameter; boolean isFurtureDate = CiDateUtil.checkFutureDate(datStartTime, "yyyyy-MM-dd"); ADSMSchedulerLogDTO treeLog = null; if (isFurtureDate == true) treeLog = tambourineConnector.getLatestADSMSchedulerLog(REGION_AXIS_TREE_PARTIAL_UPDATE, message, schedulerLogParam); else treeLog = tambourineConnector.getLatestADSMSchedulerLog(TreeType.REGION_AXIS_TREE.name(), message, schedulerLogParam); if (treeLog != null) continue; else datInfomationList.add(new DatInfomation(treeName, schedulerLogParam + extendParameter, isFurtureDate, publisher, platformType)); } return datInfomationList; } @Transactional(rollbackFor = Exception.class) public int convertRegionTree(Date date, ADSMSchedulerLogDTO schedulerLog, ADSMJobNameType srcJopType, String treeName, boolean isPartialUpdate, String publisher, Platform platformType) throws Exception { long start = System.currentTimeMillis(); String schedulerLogParam = schedulerLog.getParam(); log.info("Start Update Region Tree (" + schedulerLogParam + ")"); this.srcJobType = srcJopType; int treeID = 0; try { int dataLogID = Integer.parseInt((String) schedulerLog.getParamValue("dataLogId")); String licenseStartDateStr = (String) schedulerLog.getParamValue(FilePolling.PARAM_START_TIME_PREFIX); NodeDTO rootNodeDTO = getRootNode(treeName + "_root", dataLogID); if (rootNodeDTO == null) { throw new CiRuntimeException( "Fail to update Region Tree (Cannot Check root Info. " + schedulerLog.getParam() + ")"); } treeID = tambourineConnector.getAxisTreeId(TreeType.REGION_AXIS_TREE, treeName, publisher, platformType, false); //? if (treeID <= 0) treeID = tambourineConnector.registerRegionAxisTree( getAxisTreeDTO(treeName, rootNodeDTO, publisher, platformType), date); //? if (licenseStartDateStr != null) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date licenseStartDate = sdf.parse(licenseStartDateStr); Date syncStartDate = licenseStartDate; Date nowDate = new Date(); if (isPartialUpdate == false) syncStartDate = nowDate; makeTreeNode(treeID, licenseStartDate, rootNodeDTO.getExternalNodeId(), schedulerLogParam, treeName, dataLogID, isPartialUpdate); String param = "treeId:" + treeID + "/treeType:" + TreeType.REGION_AXIS_TREE.toString() + "/treeName:" + treeName; FeSyncNoticeboardDTO feSyncNoticeboardDTO = tambourineConnector.generateFeSyncNoticeboardData( SyncDataType.TREEDATA, syncStartDate, param, publisher, FeSyncNoticeboardStatus.NEWDATA, platformType, null, null); //? sync ,treetype,treeName? syncNoticeboard disable tambourineConnector.disableFeSyncNoticeboard(feSyncNoticeboardDTO); // syncNoticeboard? ? if (isPartialUpdate == false) tambourineConnector.insertTreeLogNSyncNoticeboard(treeID, TreeType.REGION_AXIS_TREE, treeName, feSyncNoticeboardDTO, publisher, platformType); } else { makeTreeNode(treeID, date, rootNodeDTO.getExternalNodeId(), schedulerLogParam, treeName, dataLogID, isPartialUpdate); Date nowDate = new Date(); publisher = ADDSAdapterSetting.defaultSiteName; String param = "treeId:" + treeID + "/treeType:" + TreeType.REGION_AXIS_TREE.toString() + "/treeName:" + treeName; FeSyncNoticeboardDTO feSyncNoticeboardDTO = tambourineConnector.generateFeSyncNoticeboardData( SyncDataType.TREEDATA, nowDate, param, publisher, FeSyncNoticeboardStatus.NEWDATA, platformType, null, null); // syncNoticeboard? ? tambourineConnector.insertTreeLogNSyncNoticeboard(treeID, TreeType.REGION_AXIS_TREE, treeName, feSyncNoticeboardDTO, publisher, platformType); } // ? schedulerLog.setSchedulerStatus(ADSMSchedulerStatus.SUCCESS); schedulerLog.setMessage(TREE_NAME_MESSAGE_PREFIX + treeName); } catch (Exception e) { TheLogger.getWriter().error("Fail to update Region Tree (" + schedulerLog.getParam() + ")"); long end = System.currentTimeMillis(); log.error("Fail to update Region Tree(" + schedulerLog.getParam() + ") - T(" + (end - start) + ")"); // ? ? schedulerLog.setSchedulerStatus(ADSMSchedulerStatus.FAIL); schedulerLog.setMessage(e.getMessage()); StackTraceElement[] stackTraces = e.getStackTrace(); if (stackTraces.length > 0) { schedulerLog.setFailProcess(stackTraces[0].getClassName() + " - " + stackTraces[0].getMethodName() + "(" + stackTraces[0].getLineNumber() + ")"); } throw e; } long end = System.currentTimeMillis(); log.info("Complete to update Region Tree (" + schedulerLog.getParam() + ") - T(" + (end - start) + ")"); TheLogger.getWriter().info("Update Region Tree Success(" + schedulerLog.getParam() + ")"); return treeID; } private boolean existNodeDTO(Map<String, NodeDTO> nodeMemory, String externalId) { NodeDTO nodeDTO = nodeMemory.get(externalId); return (nodeDTO != null); } private void mergeNodeWithInventoryRegionData(int treeID, String parentId, int level, Date date, int scheduleLogId, boolean isOnlyInsert) throws Exception { try { List<InventoryDataDTO> inventoryDataDTOList = tambourineConnector .getInventoryDataByParentId(scheduleLogId, parentId); for (InventoryDataDTO inventoryDataDTO : inventoryDataDTOList) { NodeDTO nodeDTO = convertInventoryData2Node(inventoryDataDTO, level); nodeDTO.setLicensingPeriod(new Period(date, DateUtil.string2Date("9999-12-31"))); if (isOnlyInsert == true) tambourineConnector.insertOnlyNewNodeUsingMemory(treeID, parentId, nodeDTO); else tambourineConnector.mergeNodeUsingMemory(treeID, parentId, nodeDTO); mergeNodeWithInventoryRegionData(treeID, nodeDTO.getExternalNodeId(), level + 1, date, scheduleLogId, isOnlyInsert); } } catch (Exception e) { throw e; } } private List<NodeDTO> mergeRegionNode(int treeID, String rootNodeId, int level, Date date, int dataLogId) throws Exception { List<RegionDataDTO> regionDataDTOList = tambourineConnector.getRegionDataBySchedulerLogId(dataLogId); String parentId = rootNodeId; Map<String, NodeDTO> nodeMemory = new HashMap<String, NodeDTO>(); List<NodeDTO> regionDongList = new ArrayList<NodeDTO>(); // index 0 | index 1 | index 2 //|GF3000||GS3011|?|302001|232323 //index 0 : , index 1 : , index 2 : ? + STB_CD //dat?? ? "" ? , ? "" ? , "?" Tree? for (int field_index = REGION_DATA_FIELD_INDEX_SI; field_index < REGION_DATA_FIELD_INDEX_COUNT; field_index++) { for (RegionDataDTO regionDataDTO : regionDataDTOList) { String newNodeExternalId = regionDataDTO.getRegionCD(field_index); if (useMapper == true && field_index == REGION_DATA_FIELD_INDEX_DONG) regionDongList.add(generateNodeDTO(regionDataDTO.getRegionName(field_index), regionDataDTO.getSTB_CD(), newNodeExternalId, level + field_index)); boolean existNode = false; if (field_index < REGION_DATA_FIELD_INDEX_DONG) existNode = existNodeDTO(nodeMemory, newNodeExternalId); if (existNode) continue; NodeDTO nodeDTO; if (field_index == REGION_DATA_FIELD_INDEX_DONG) nodeDTO = generateNodeDTO(regionDataDTO.getRegionName(field_index), regionDataDTO.getSTB_CD(), newNodeExternalId, level + field_index); else nodeDTO = generateNodeDTO(regionDataDTO.getRegionName(field_index), newNodeExternalId, newNodeExternalId, level + field_index); try { nodeDTO.setLicensingPeriod(new Period(date, DateUtil.string2Date("9999-12-31"))); if (field_index == REGION_DATA_FIELD_INDEX_SI) parentId = rootNodeId; else parentId = regionDataDTO.getRegionCD(field_index - 1); nodeDTO = tambourineConnector.mergeNodeUsingMemory(treeID, parentId, nodeDTO); nodeMemory.put(newNodeExternalId, nodeDTO); // } catch (Exception e) { throw e; } } } return regionDongList; } private void mergeNode(int treeID, String parentId, int level, Date date) { List<NodeDataDTO> regionDataDTOList = tambourineConnector.getNodeData(); for (NodeDataDTO regionDataDTO : regionDataDTOList) { NodeDTO nodeDTO = convertNodeData2Node(regionDataDTO, level); try { nodeDTO.setLicensingPeriod(new Period(date, DateUtil.string2Date("9999-12-31"))); tambourineConnector.mergeNode(treeID, parentId, nodeDTO); } catch (Exception e) { log.error("", e); } } } private NodeDTO generateNodeDTO(String nodeName, String mappingId, String externalId, int level) { NodeDTO nodeDTO = new NodeDTO(); nodeDTO.setLevel(level); nodeDTO.setName(nodeName); nodeDTO.setReflected(false); nodeDTO.setMappingId(mappingId); nodeDTO.setExternalNodeId(externalId); nodeDTO.setUseLicensingWindow(true); return nodeDTO; } private NodeDTO convertInventoryData2Node(InventoryDataDTO inventoryDataDTO, int level) { NodeDTO nodeDTO = new NodeDTO(); nodeDTO.setLevel(level); nodeDTO.setName(inventoryDataDTO.getItemName()); nodeDTO.setReflected(false); nodeDTO.setMappingId(inventoryDataDTO.getItemId()); nodeDTO.setExternalNodeId(inventoryDataDTO.getItemId()); nodeDTO.setUseLicensingWindow(true); return nodeDTO; } private NodeDTO convertNodeData2Node(NodeDataDTO regionDataDTO, int level) { return generateNodeDTO(regionDataDTO.getNode_Name(), regionDataDTO.getNode_ID(), regionDataDTO.getNode_ID(), level); } private NodeDTO getRootNode(String nodeName, int dataLogID) { if (srcJobType == ADSMJobNameType.REGION_DATA) return generateNodeDTO(nodeName, nodeName, nodeName, 0); else if (srcJobType == ADSMJobNameType.INVENTORY_REGION_DATA) { List<InventoryDataDTO> rootDTO = tambourineConnector.getInventoryDataByParentId(dataLogID, ROOT_REGION_PARENT); if (rootDTO != null && rootDTO.isEmpty() == false) return convertInventoryData2Node(rootDTO.get(0), 0); } return null; } private void makeTreeNode(int treeID, Date date, String externalNodeId, String schedulerLogParam, String treeName, int dataLogId, boolean isPartialUpdate) throws Exception { int level = 0; // ? license if (isPartialUpdate == false) tambourineConnector.invalidateAxisTreeNodeExceptRootNode(treeID, date); List<NodeDTO> regionDongList = null; try { //node merge // node . Dong ? Region tree . if (srcJobType == ADSMJobNameType.NODE_DATA) mergeNode(treeID, externalNodeId, level + 1, date); else if (srcJobType == ADSMJobNameType.REGION_DATA) regionDongList = mergeRegionNode(treeID, externalNodeId, level + 1, date, dataLogId); else if (srcJobType == ADSMJobNameType.INVENTORY_REGION_DATA) mergeNodeWithInventoryRegionData(treeID, externalNodeId, level + 1, date, dataLogId, isPartialUpdate); } finally { //mergeNode (fxpark) tambourineConnector.mergeNodeAfterMemoryClear(); } tambourineConnector.saveClosureNodes(treeID); if (useMapper == true && regionDongList != null && regionDongList.isEmpty() == false) mapperHandler.generateRegionMapper(regionDongList, schedulerLogParam, MapperAxisType.REGION, regionMapperDomain, treeName, date); } }