Java tutorial
package com.castis.xylophone.adsmadapter.convert.axistree; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; 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.bizobject.ktadsm.ClientUIData; 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.ClientUIDataDTO; import com.castis.tambourine.dto.ktadsm.ContentDataDTO; import com.castis.tambourine.dto.ktadsm.NodeInfoLicenseDTO; import com.castis.tambourine.dto.ktadsm.NodeInfoWithLinkNodeDTO; import com.castis.tambourine.dto.ktadsm.InventoryData.TargetedContentInfo; import com.castis.tambourine.dto.ktadsm.axistree.AxisTreeNodeDTO; import com.castis.tambourine.dto.ktadsm.axistree.TreeNodeLinkDTO; import com.castis.tambourine.dto.ktadsm.axistree.TreeNodeLinkPeriodDTO; import com.castis.tambourine.dto.ktadsm.campaign.PlayInfo; import com.castis.tambourine.enumeration.ClientUIType; import com.castis.tambourine.enumeration.InventoryType; import com.castis.tambourine.enumeration.PlacementOpportunityTypeEnum; import com.castis.tambourine.enumeration.Platform; 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.filePolling.FilePolling; public class ConvertBannerAxisTreeDTO extends ConvertTreeDTO { static final Log log = LogFactory.getLog(ConvertBannerAxisTreeDTO.class); static final String name = "ConvertBannerAxisTreeDTO"; static final String ROOT_NODE_NAME = "root_banner"; private static final String ROOT_BANNER_PARENT = "*"; private static final String ROOT_LINK_NODE_NAME = "root_link_banner"; public void convertBannerTree(Collection<ClientUIDataDTO> bannerAxisTreeDTO, Date date, ADSMSchedulerLogDTO menuTreeSchedulerLog) { // TODO Auto-generated method stub } public List<DatInfomation> getNewSchedulerLogParam(ADSMJobNameType srcJobType, String defaultTreeName) { Map<ADSMSchedulerLogDTO, String/*treeName*/> schedulerLogMap = tambourineConnector .getNewSchedulerLogByTreeName(srcJobType, defaultTreeName); 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 && platformType != 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; ADSMSchedulerLogDTO treeLog = null; treeLog = tambourineConnector.getLatestADSMSchedulerLog(TreeType.BANNER_AXIS_TREE.name(), message, schedulerLogParam); if (treeLog != null) continue; else datInfomationList.add(new DatInfomation(treeName, schedulerLogParam + extendParameter, false, publisher, platformType)); } return datInfomationList; } @Transactional(rollbackFor = Exception.class) public int convertBannerTree(Date date, ADSMSchedulerLogDTO schedulerLog, ADSMJobNameType srcJobType, String treeName, String publisher, Platform platformType) throws Exception { long start = System.currentTimeMillis(); log.info("convertBannerTree start(UpdateTreeJob)"); int treeID = mergeBannerTreeNode(date, schedulerLog, srcJobType, treeName, publisher, platformType); long end = System.currentTimeMillis(); log.info("convertBannerTree end - T(" + (end - start) + ")"); return treeID; } public int mergeBannerTreeNode(Date date, ADSMSchedulerLogDTO schedulerLog, ADSMJobNameType srcJopType, String treeName, String publisher, Platform platformType) throws Exception { if (schedulerLog == null) { throw new CiRuntimeException("ADSMSchedulerLog is null"); } if (treeName == null || treeName.isEmpty()) { throw new CiRuntimeException("Cannot find treeName in generating MenuTree"); } log.info("TreeName : " + treeName + ", srcDataType : " + srcJopType + ". Start Update(" + schedulerLog.getParam() + ")"); int treeID = 0; try { //?, , , ? //tambourineConnector.initializeInventoryCube(date); NodeDTO rootNodeDTO = getRootNode(ROOT_NODE_NAME); if (rootNodeDTO == null) { throw new CiRuntimeException("Fail to Update Banner Tree (Cannot Check root node Info. " + schedulerLog.getParam() + ")"); } treeID = tambourineConnector.getAxisTreeId(TreeType.BANNER_AXIS_TREE, treeName, publisher, platformType, false); //? if (treeID <= 0) treeID = tambourineConnector.generateTree(treeName, ROOT_NODE_NAME, TreeType.BANNER_AXIS_TREE, publisher, platformType); List<ClientUIData> ClientUIDataList = tambourineConnector.getClientUIDataByType(ClientUIType.BANNER); int linkTreeId = -1; linkTreeId = makeTreeNode(treeID, date, rootNodeDTO.getExternalNodeId(), ClientUIDataList, publisher, platformType); tambourineConnector.insertTreeLogNSyncNoticeboard(linkTreeId, TreeType.BANNER_AXIS_TREE, Constants.tree.SCENE_TREE_NAME, null, publisher, platformType); tambourineConnector.insertTreeLogNSyncNoticeboard(treeID, TreeType.BANNER_AXIS_TREE, Constants.tree.BANNER_TREE_NAME, null, publisher, platformType); schedulerLog.setSchedulerStatus(ADSMSchedulerStatus.SUCCESS); TheLogger.getWriter().info("Update Banner Tree Success"); } catch (Exception e) { //log.error("",e); TheLogger.getWriter().error("Fail to Update Banner Tree"); log.error("Fail to Update Banner Tree"); schedulerLog.setMessage(e.getMessage()); schedulerLog.setSchedulerStatus(ADSMSchedulerStatus.FAIL); StackTraceElement[] stackTraces = e.getStackTrace(); if (stackTraces.length > 0) { schedulerLog.setFailProcess(stackTraces[0].getClassName() + " - " + stackTraces[0].getMethodName() + "(" + stackTraces[0].getLineNumber() + ")"); } throw e; } return treeID; } private NodeDTO getRootNode(String name) { NodeDTO nodeDTO = new NodeDTO(); nodeDTO.setLevel(0); nodeDTO.setName(name); nodeDTO.setReflected(false); nodeDTO.setMappingId(ROOT_BANNER_PARENT); nodeDTO.setExternalNodeId(ROOT_BANNER_PARENT); nodeDTO.setUseLicensingWindow(true); return nodeDTO; } private int makeTreeNode(int treeID, Date date, String externalNodeId, List<ClientUIData> ClientUIDataList, String publisher, Platform platformType) throws Exception { int level = 0; tambourineConnector.invalidateAxisTreeNodeExceptRootNode(treeID, date); log.info("invalidate BannerTree ExceptRootNode done"); //node merge int updateSize = mergeNode(treeID, externalNodeId, level + 1, date, ClientUIDataList); log.info("Banner Tree Update done Size: " + updateSize); int linkTreeId = generateBannerLinkTree(ClientUIDataList, date, publisher, platformType); //?? tambourineConnector.saveClosureNodes(linkTreeId); tambourineConnector.saveClosureNodes(treeID); return linkTreeId; } private int generateBannerLinkTree(List<ClientUIData> ClientUIDataList, Date date, String publisher, Platform platformType) throws Exception { int level = 0; NodeDTO rootNodeDTO = getRootNode(ROOT_LINK_NODE_NAME); int treeID = tambourineConnector.getAxisTreeId(TreeType.BANNER_AXIS_TREE, Constants.tree.SCENE_TREE_NAME, publisher, platformType, true); //? if (treeID <= 0) treeID = tambourineConnector.registerAxisTree(getAxisTreeDTO(Constants.tree.SCENE_TREE_NAME, rootNodeDTO, true, treeID, publisher, platformType), date, TreeType.BANNER_AXIS_TREE); tambourineConnector.invalidateAxisTreeNodeExceptRootNode(treeID, date); log.info("invalidate BannerLinkTree ExceptRootNode done"); mergeLinkNode(treeID, level + 1, date, ClientUIDataList); log.info("generateBannerLinkTree done"); return treeID; } private void mergeLinkNode(int treeID, int level, Date date, List<ClientUIData> ClientUIDataList) throws Exception { try { AxisTreeNodeDTO rootNodeDTO = getRootAxisTreeNode(ROOT_LINK_NODE_NAME); int rootNodeId = tambourineConnector.getNodeId(treeID, ROOT_BANNER_PARENT); rootNodeDTO.setNodeID(rootNodeId); Map<String, NodeInfoLicenseDTO> nodeInfoMap = tambourineConnector.getNodeInfoLicenseByTreeId(treeID); Set<String> menuIdSet = new HashSet<String>(); List<AxisTreeNodeDTO> nodeDTOList = new ArrayList<AxisTreeNodeDTO>(); //TBL_CLIENT_UI_DATA? Type? Banner? id bannerId parentId menuId?. for (ClientUIData clientUIData : ClientUIDataList) { if (clientUIData.getClientUIParentId() != null && menuIdSet.add(clientUIData.getClientUIParentId()) == true) { NodeInfoLicenseDTO nodeInfo = nodeInfoMap.get(clientUIData.getClientUIParentId()); AxisTreeNodeDTO nodeDTO = null; if (nodeInfo != null) nodeDTO = convertBannerLinkData2Node(clientUIData.getClientUIParentId(), level, new Period(nodeInfo.getLicenseStart(), DateUtil.string2Date("9999-12-31")), nodeInfo.getNodeId(), treeID, rootNodeDTO); else nodeDTO = convertBannerLinkData2Node(clientUIData.getClientUIParentId(), level, new Period(date, DateUtil.string2Date("9999-12-31")), null, treeID, rootNodeDTO); nodeDTOList.add(nodeDTO); } } if (nodeDTOList != null && nodeDTOList.size() > 0) { tambourineConnector.saveNodeList(nodeDTOList); } log.info("Banner Link Tree Update Done Size: " + menuIdSet.size()); } catch (Exception e) { log.error(e.getMessage()); throw e; } } private AxisTreeNodeDTO convertBannerLinkData2Node(String menuId, int level, Period period, Integer nodeId, int treeId, AxisTreeNodeDTO parent) { if (menuId == null || menuId.isEmpty()) return null; AxisTreeNodeDTO axisTreeNodeDTO = null; try { axisTreeNodeDTO = new AxisTreeNodeDTO(); axisTreeNodeDTO.setLevel(level); axisTreeNodeDTO.setName(menuId); axisTreeNodeDTO.setReflected(false); axisTreeNodeDTO.setMappingId(menuId); axisTreeNodeDTO.setExternalNodeId(menuId); axisTreeNodeDTO.setUseLicensingWindow(true); axisTreeNodeDTO.setLicensingPeriod(period); axisTreeNodeDTO.setTreeId(treeId); axisTreeNodeDTO.setParent(parent); if (nodeId != null && nodeId > 0) axisTreeNodeDTO.setNodeID(nodeId); //? menuId ? banner? Node . List<NodeInfoWithLinkNodeDTO> bannerNodes = tambourineConnector.getBannerNodeInfoByMenuId(menuId, (nodeId == null) ? -1 : nodeId); if (bannerNodes != null) { for (NodeInfoWithLinkNodeDTO bannerNode : bannerNodes) { List<TreeNodeLinkPeriodDTO> linkPeriods = new ArrayList<TreeNodeLinkPeriodDTO>(); TreeNodeLinkPeriodDTO treeNodeLinkPeriodDTO = null; if (bannerNode.getPeriodId() != null && bannerNode.getPeriodId() > 0) { treeNodeLinkPeriodDTO = new TreeNodeLinkPeriodDTO(bannerNode.getPeriodId(), period); } else treeNodeLinkPeriodDTO = new TreeNodeLinkPeriodDTO(period); linkPeriods.add(treeNodeLinkPeriodDTO); TreeNodeLinkDTO treeNodeLinkDTO = null; if (bannerNode.getLinkId() != null && bannerNode.getLinkId() > 0) { treeNodeLinkDTO = new TreeNodeLinkDTO(bannerNode.getLinkId(), bannerNode.getTreeId(), bannerNode.getNodeId(), null, linkPeriods); } else { treeNodeLinkDTO = new TreeNodeLinkDTO(bannerNode.getTreeId(), bannerNode.getNodeId(), null, linkPeriods); } axisTreeNodeDTO.addExternalLink(treeNodeLinkDTO); } } } catch (Exception e) { log.error("Fail to Generate Banner Link Node (menu Id = " + menuId + ", nodeId = " + nodeId + ") : ", e); } return axisTreeNodeDTO; } private int mergeNode(int treeID, String parentId, int level, Date date, List<ClientUIData> ClientUIDataList) throws Exception { try { Set<String> bannerIdSet = new HashSet<String>(); for (ClientUIData clientUIData : ClientUIDataList) { if (clientUIData.getClientUIId() != null && bannerIdSet.add(clientUIData.getClientUIId()) == true) { NodeDTO nodeDTO = convertClientUIData2Node(clientUIData, level); if (nodeDTO != null) { nodeDTO.setLicensingPeriod(new Period(date, DateUtil.string2Date("9999-12-31"))); tambourineConnector.mergeNodeUsingMemory(treeID, parentId, nodeDTO); } } } return bannerIdSet.size(); } catch (Exception e) { throw e; } finally { // mergeNode?? ? tambourineConnector.mergeNodeAfterMemoryClear(); } } private AxisTreeNodeDTO getRootAxisTreeNode(String name) { AxisTreeNodeDTO nodeDTO = new AxisTreeNodeDTO(); nodeDTO.setLevel(0); nodeDTO.setName(name); nodeDTO.setReflected(false); nodeDTO.setMappingId(ROOT_BANNER_PARENT); nodeDTO.setExternalNodeId(ROOT_BANNER_PARENT); nodeDTO.setUseLicensingWindow(true); return nodeDTO; } private NodeDTO convertClientUIData2Node(ClientUIData clientUIData, int level) { NodeDTO nodeDTO = new NodeDTO(); nodeDTO.setLevel(level); nodeDTO.setName(clientUIData.getClientUIName()); nodeDTO.setReflected(false); nodeDTO.setMappingId(clientUIData.getClientUIId()); nodeDTO.setExternalNodeId(clientUIData.getClientUIId()); nodeDTO.setUseLicensingWindow(true); return nodeDTO; } }