com.castis.xylophone.adsmadapter.convert.axistree.ConvertCategoryAxisTreeDTO.java Source code

Java tutorial

Introduction

Here is the source code for com.castis.xylophone.adsmadapter.convert.axistree.ConvertCategoryAxisTreeDTO.java

Source

package com.castis.xylophone.adsmadapter.convert.axistree;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
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.apache.xmlbeans.impl.xb.xsdschema.Public;
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.CategoryIdForDisableContent;
import com.castis.tambourine.define.Constants;
import com.castis.tambourine.dto.inventory.AxisTreeDTO;
import com.castis.tambourine.dto.inventory.NodeDTO;
import com.castis.tambourine.dto.ktadsm.ADSMSchedulerLogDTO;
import com.castis.tambourine.dto.ktadsm.CategoryDTO;
import com.castis.tambourine.dto.ktadsm.CategoryDataDTO;
import com.castis.tambourine.dto.ktadsm.ChannelServiceDTO;
import com.castis.tambourine.dto.ktadsm.FeSyncNoticeboardDTO;
import com.castis.tambourine.dto.ktadsm.InventoryDataDTO;
import com.castis.tambourine.dto.ktadsm.VirtualCategoryDTO;
import com.castis.tambourine.dto.ktadsm.VirtualCategoryDefineDTO;
import com.castis.tambourine.dto.policy.CategoryInfoForDiableContentDTO;
import com.castis.tambourine.enumeration.DeleteDataType;
import com.castis.tambourine.enumeration.FeSyncNoticeboardStatus;
import com.castis.tambourine.enumeration.PlacementOpportunityTypeEnum;
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.enumeration.InputDataType;
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 ConvertCategoryAxisTreeDTO extends ConvertTreeDTO {
    static final Log log = LogFactory.getLog(ConvertCategoryAxisTreeDTO.class);
    static final String name = "ConvertCategoryAxisTreeDTO";
    static final String ROOT_NODE_NAME = "root_category";
    public static final String ROOT_CATEGORY_PARENT = "*";
    public static final String CATEGORY_AXIS_TREE_PARTIAL_UPDATE = "CATEGORY_AXIS_TREE_PARTIAL_UPDATE";
    private ADSMJobNameType srcJobType;
    public ConvertContentAxisTreeDTO convertContentAxisTreeDTO;

    public ConvertCategoryAxisTreeDTO() {
        log.info(" ### " + name + " was started.");
    }

    public void close() {
        log.info(" # " + name + " is shutting down.");
    }

    public ConvertContentAxisTreeDTO getConvertContentAxisTreeDTO() {
        return convertContentAxisTreeDTO;
    }

    public void setConvertContentAxisTreeDTO(ConvertContentAxisTreeDTO convertContentAxisTreeDTO) {
        this.convertContentAxisTreeDTO = convertContentAxisTreeDTO;
    }

    public boolean insertData(List<CategoryDataDTO> categoryDataList, ADSMSchedulerLogDTO schedulerLog,
            String dataFileName, InputDataType inputDataType, DeleteDataType deleteData, ADSMJobNameType jobType) {
        return tambourineConnector.insertCategoryData(categoryDataList, schedulerLog, resultDirectory, dataFileName,
                inputDataType, deleteData, jobType);
    }

    public void makeMapperFile(AxisTreeDTO axisTreeDTO) {
        if (axisTreeDTO == null)
            return;

        try {
            File file = new File("c:/dataTest/category.txt"); //  ? ?
            PrintWriter writer = new PrintWriter(new FileWriter(file, false));

            writer.println("Type\tTreeID\tNodeID\tPath_Name\tMappingID(to be filled in)");
            writer.println("----\t------\t------\t---------\t---------");
            writeMapperFile(writer, axisTreeDTO.getRootNode(), axisTreeDTO.getTreeID());
            writer.flush();
            writer.close();
        } catch (Exception e) {
            log.error("Fail to write MapperFile. Exception Type : Exception |", e);
        }
    }

    private NodeDTO getRootNode(int dataLogID) {
        //fxpark(getCategoryDataByParentId?  )
        //ADSMSchedulerLogDTO adsmScheduleLogDTO = tambourineConnector.getLatestADSMSchedulerLog(ADSMJobNameType.CATEGORY_DATA.name());

        if (srcJobType == ADSMJobNameType.CATEGORY_DATA || srcJobType == ADSMJobNameType.CATEGORY_STB_DATA
                || srcJobType == ADSMJobNameType.CATEGORY_OTT_DATA) {
            List<CategoryDataDTO> rootCategoryDTO = tambourineConnector.getCategoryDataByParentId(dataLogID,
                    ROOT_CATEGORY_PARENT);
            if (rootCategoryDTO != null && rootCategoryDTO.isEmpty() == false)
                return convertCategoryData2Node(rootCategoryDTO.get(0), 0);
        } else if (srcJobType == ADSMJobNameType.INVENTORY_CATEGORY_DATA) {
            List<InventoryDataDTO> rootCategoryDTO = tambourineConnector.getInventoryDataByParentId(dataLogID,
                    ROOT_CATEGORY_PARENT);
            if (rootCategoryDTO != null && rootCategoryDTO.isEmpty() == false)
                return convertInventoryData2Node(rootCategoryDTO.get(0), 0);
        }

        return null;
    }

    private void mergeNodePointOfEntry(int treeID, String parentId, int level, Date date,
            List<CategoryDTO> invenBoxCategoryDTOs, List<VirtualCategoryDTO> virtualCategoryList,
            List<NodeDTO> childNodeDTOList, int dataLogId, boolean isOnlyInsert) throws Exception {
        try {
            mergeNode(treeID, parentId, level, date, dataLogId, invenBoxCategoryDTOs, virtualCategoryList,
                    childNodeDTOList, isOnlyInsert);
        } finally {
            // leak?  .
            tambourineConnector.mergeNodeAfterMemoryClear();
        }
    }

    private void mergeNode(int treeID, String parentId, int level, Date date, int scheduleLogId,
            List<CategoryDTO> invenBoxCategoryDTOs, List<VirtualCategoryDTO> virtualCategoryList,
            List<NodeDTO> childNodeDTOList, boolean isOnlyInsert) throws Exception {

        List<NodeDTO> etcChildNodeDTOList = new ArrayList<NodeDTO>();

        try {

            if (childNodeDTOList != null) {
                for (NodeDTO nodeDTO : childNodeDTOList) {
                    nodeDTO.setLicensingPeriod(new Period(date, DateUtil.string2Date("9999-12-31")));
                    tambourineConnector.mergeNodeUsingMemory(treeID, parentId, nodeDTO);
                    mergeNode(treeID, nodeDTO.getExternalNodeId(), level + 1, date, scheduleLogId,
                            invenBoxCategoryDTOs, virtualCategoryList, etcChildNodeDTOList, isOnlyInsert);
                }
            }
            if (srcJobType == ADSMJobNameType.CATEGORY_DATA) {
                List<CategoryDataDTO> categoryDTOList = tambourineConnector.getCategoryDataByParentId(scheduleLogId,
                        parentId);

                for (CategoryDataDTO categoryDataDTO : categoryDTOList) {
                    boolean isContainInvenBoxCategory = isContainInvenBox(invenBoxCategoryDTOs, categoryDataDTO);
                    if (isContainInvenBoxCategory == true) {
                        //invenBox?  ? ??
                        NodeDTO nodeDTO = convertCategoryData2Node(categoryDataDTO, level);
                        //nodeDTOList.add(nodeDTO);
                        nodeDTO.setLicensingPeriod(new Period(date, DateUtil.string2Date("9999-12-31")));
                        tambourineConnector.mergeNodeUsingMemory(treeID, parentId, nodeDTO);
                        mergeNode(treeID, nodeDTO.getExternalNodeId(), level + 1, date, scheduleLogId,
                                invenBoxCategoryDTOs, virtualCategoryList, null, isOnlyInsert);
                    } else {
                        //  ETC? ??
                        NodeDTO nodeDTO = convertCategoryData2Node(categoryDataDTO, level + 1);
                        etcChildNodeDTOList.add(nodeDTO);
                    }
                }

            } else if (srcJobType == ADSMJobNameType.INVENTORY_CATEGORY_DATA) {
                List<InventoryDataDTO> inventoryDataDTOList = tambourineConnector
                        .getInventoryDataByParentId(scheduleLogId, parentId);

                for (InventoryDataDTO inventoryDataDTO : inventoryDataDTOList) {
                    boolean isContainInvenBoxCategory = isContainInvenBox(invenBoxCategoryDTOs, inventoryDataDTO);
                    if (isContainInvenBoxCategory == true) {
                        //invenBox?  ? ??
                        NodeDTO nodeDTO = convertInventoryData2Node(inventoryDataDTO, level);
                        //nodeDTOList.add(nodeDTO);
                        nodeDTO.setLicensingPeriod(new Period(date, DateUtil.string2Date("9999-12-31")));

                        if (isOnlyInsert == true)
                            tambourineConnector.insertOnlyNewNodeUsingMemory(treeID, parentId, nodeDTO);
                        else
                            tambourineConnector.mergeNodeUsingMemory(treeID, parentId, nodeDTO);

                        mergeNode(treeID, nodeDTO.getExternalNodeId(), level + 1, date, scheduleLogId,
                                invenBoxCategoryDTOs, virtualCategoryList, null, isOnlyInsert);
                    } else {
                        //  ETC? ??
                        NodeDTO nodeDTO = convertInventoryData2Node(inventoryDataDTO, level + 1);
                        etcChildNodeDTOList.add(nodeDTO);
                    }
                }
            } else if (srcJobType == ADSMJobNameType.CATEGORY_STB_DATA
                    || srcJobType == ADSMJobNameType.CATEGORY_OTT_DATA) {
                List<CategoryDataDTO> categoryDTOList = tambourineConnector.getCategoryDataByParentId(scheduleLogId,
                        parentId);

                for (CategoryDataDTO categoryDataDTO : categoryDTOList) {
                    NodeDTO nodeDTO = convertCategoryData2Node(categoryDataDTO, level);
                    nodeDTO.setLicensingPeriod(new Period(date, DateUtil.string2Date("9999-12-31")));
                    tambourineConnector.mergeNodeUsingMemory(treeID, parentId, nodeDTO);
                    mergeNode(treeID, nodeDTO.getExternalNodeId(), level + 1, date, scheduleLogId,
                            invenBoxCategoryDTOs, virtualCategoryList, null, isOnlyInsert);
                }
            }

            //ETC ?
            NodeDTO etcNodeDTO = getETCNode(virtualCategoryList, parentId);
            if (etcNodeDTO != null) {
                etcNodeDTO.setLicensingPeriod(new Period(date, DateUtil.string2Date("9999-12-31")));
                tambourineConnector.mergeNodeUsingMemory(treeID, parentId, etcNodeDTO);
                mergeNode(treeID, etcNodeDTO.getExternalNodeId(), level + 1, date, scheduleLogId,
                        invenBoxCategoryDTOs, virtualCategoryList, etcChildNodeDTOList, isOnlyInsert);
            } else {
                for (NodeDTO nodeDTO : etcChildNodeDTOList) {
                    //nodeDTOList.add(nodeDTO);
                    nodeDTO.setLicensingPeriod(new Period(date, DateUtil.string2Date("9999-12-31")));
                    tambourineConnector.mergeNodeUsingMemory(treeID, parentId, nodeDTO);
                    mergeNode(treeID, nodeDTO.getExternalNodeId(), level + 1, date, scheduleLogId,
                            invenBoxCategoryDTOs, virtualCategoryList, null, isOnlyInsert);
                }
            }
        } catch (Exception e) {
            throw e;
        }
    }

    private NodeDTO getETCNode(List<VirtualCategoryDTO> virtualCategoryList, String parentId) {
        if (virtualCategoryList == null)
            return null;
        for (VirtualCategoryDTO virtualCategoryDTO : virtualCategoryList) {
            if (virtualCategoryDTO.getParentCategoryID().equals(parentId)) {
                return getNodeDTO(null, virtualCategoryDTO.getVirtualcategory_ID(), -1,
                        virtualCategoryDTO.getVirtualcategory_NM());
            }
        }
        return null;
    }

    private boolean isContainInvenBox(List<CategoryDTO> invenBoxCategoryDTOs, CategoryDataDTO categoryDataDTO) {
        if (invenBoxCategoryDTOs == null)
            return true;
        for (CategoryDTO categoryDTO : invenBoxCategoryDTOs) {
            if (categoryDTO.getMenu_ID().equals(categoryDataDTO.getCatId())) {
                return true;
            }
        }
        return false;
    }

    private boolean isContainInvenBox(List<CategoryDTO> invenBoxCategoryDTOs, InventoryDataDTO inventoryDataDTO) {
        if (invenBoxCategoryDTOs == null)
            return true;
        for (CategoryDTO categoryDTO : invenBoxCategoryDTOs) {
            if (categoryDTO.getMenu_ID().equals(inventoryDataDTO.getItemId())) {
                return true;
            }
        }
        return false;
    }

    private NodeDTO convertCategoryData2Node(CategoryDataDTO categoryDataDTO, int level) {

        NodeDTO nodeDTO = new NodeDTO();
        nodeDTO.setLevel(level);
        nodeDTO.setName(categoryDataDTO.getCatName());
        nodeDTO.setReflected(false);
        nodeDTO.setMappingId(categoryDataDTO.getCatId());
        nodeDTO.setExternalNodeId(categoryDataDTO.getCatId());
        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;
    }

    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("" + "\t" + treeID + "\t" + nodeDTO.getNodeID() + "\t" + nodeDTO.getName() + "\t"
                + nodeDTO.getAggregationId());
    }

    public List<NodeDTO> getCategoryTree(List<CategoryDataDTO> categoryDataList,
            List<VirtualCategoryDTO> virtualCategoryList, List<CategoryDTO> invenBoxCategoryDTOs) {
        Map<String, List<CategoryDataDTO>> categoryDataMap = new HashMap<String, List<CategoryDataDTO>>();
        for (CategoryDataDTO categoryData : categoryDataList) {
            if (categoryDataMap.get(categoryData.getParentCatId()) != null) {
                categoryDataMap.get(categoryData.getParentCatId()).add(categoryData);
            } else {
                List<CategoryDataDTO> categoryDataDTOList = new ArrayList<CategoryDataDTO>();
                categoryDataDTOList.add(categoryData);
                categoryDataMap.put(categoryData.getParentCatId(), categoryDataDTOList);
            }
        }
        return getCategoryTree(categoryDataMap, virtualCategoryList, invenBoxCategoryDTOs, ROOT_CATEGORY_PARENT, 0);

    }

    public List<NodeDTO> getCategoryTree(Map<String, List<CategoryDataDTO>> categoryDataMap,
            List<VirtualCategoryDTO> virtualCategoryList, List<CategoryDTO> invenBoxCategoryDTOs, String parentId,
            int level) {
        List<NodeDTO> currentNodeDTOList = new ArrayList<NodeDTO>();
        List<NodeDTO> childNodeDTOList = new ArrayList<NodeDTO>();
        List<CategoryDataDTO> categoryListDataList = categoryDataMap.get(parentId);
        if (categoryListDataList == null)
            return currentNodeDTOList;

        int nodeSize = categoryListDataList.size();
        int currentTreeLevel = level;

        if (nodeSize > 0) {
            for (int i = 0; i < nodeSize; i++) {
                CategoryDataDTO categoryData = categoryListDataList.get(i);

                //? 
                childNodeDTOList = getCategoryTree(categoryDataMap, virtualCategoryList, invenBoxCategoryDTOs,
                        categoryData.getCatId(), currentTreeLevel + 1);

                //ETC 
                NodeDTO etcNode = getVirtualCategory(categoryData.getCatId(), virtualCategoryList);

                //? ETC?   
                if (etcNode != null) {
                    etcNode.setLevel(currentTreeLevel + 1);
                    etcNode = moveEtcNode(etcNode, childNodeDTOList, invenBoxCategoryDTOs);
                    childNodeDTOList.add(etcNode);
                }
                NodeDTO nodeDTO = getNodeDTO(childNodeDTOList, categoryData.getCatId(), currentTreeLevel,
                        categoryData.getCatName());

                currentNodeDTOList.add(nodeDTO);
            }
        }

        return currentNodeDTOList;
    }

    private NodeDTO moveEtcNode(NodeDTO etcNode, List<NodeDTO> childNodeDTOList,
            List<CategoryDTO> invenBoxCategoryDTOs) {

        for (Iterator<NodeDTO> it = childNodeDTOList.iterator(); it.hasNext();) {
            NodeDTO childNode = it.next();

            boolean isFind = false;
            for (CategoryDTO categoryDTO : invenBoxCategoryDTOs) {
                if (categoryDTO.getMenu_ID().equals(childNode.getExternalNodeId())) {
                    isFind = true;
                }
            }
            if (isFind == false && etcNode != null) {
                etcNode.addChild(childNode);
                it.remove();

            }
        }
        return etcNode;
    }

    private NodeDTO getVirtualCategory(String parentId, List<VirtualCategoryDTO> virtualCategoryList) {
        NodeDTO etcNode = null;
        for (VirtualCategoryDTO virtualCategoryDTO : virtualCategoryList) {
            if (virtualCategoryDTO.getParentCategoryID().equals(parentId)) {
                etcNode = getNodeDTO(null, virtualCategoryDTO.getVirtualcategory_ID(), 0,
                        virtualCategoryDTO.getVirtualcategory_ID());
                return etcNode;
            }
        }

        return etcNode;
    }

    NodeDTO getNodeDTO(List<NodeDTO> children, String aggregationId, Integer level, String name) {
        NodeDTO nodeDTO = new NodeDTO();
        nodeDTO.setAggregationId(aggregationId);
        if (children == null)
            children = new ArrayList<NodeDTO>();
        nodeDTO.setChildren(children);
        nodeDTO.setLevel(level);
        nodeDTO.setName(name);
        nodeDTO.setReflected(false);
        nodeDTO.setMappingId(aggregationId);
        nodeDTO.setExternalNodeId(aggregationId);
        return nodeDTO;
    }

    //TreeNode .
    //1.  ? license 
    //2. node Merge
    //3. closureNode 
    //?  ? ? Transaction      

    private void makeTreeNode(int treeID, Date date, String externalNodeId, List<CategoryDTO> invenBoxCategoryDTOs,
            List<VirtualCategoryDTO> virtualCategoryDTOs, int dataLogId, boolean isPartialUpdate) throws Exception {
        int level = 0;

        // ? license   
        if (isPartialUpdate == false)
            tambourineConnector.invalidateAxisTreeNodeExceptRootNode(treeID, date);

        //node merge
        mergeNodePointOfEntry(treeID, externalNodeId, level + 1, date, invenBoxCategoryDTOs, virtualCategoryDTOs,
                null, dataLogId, isPartialUpdate);

        tambourineConnector.saveClosureNodes(treeID);
    }

    private boolean updateDisableContentPolicy(int treeId, int deleteSchedulerLogId) throws Exception {
        long start = System.currentTimeMillis(), end;
        log.info("@@@ update DisablePolicy - start");

        boolean result = false;
        List<Boolean> booleanList = Arrays.asList(true, false);
        for (Boolean booleanValue : booleanList) {
            List<CategoryIdForDisableContent> disableCategoryContentList = tambourineConnector
                    .findCategoryIdForDisableContentByPpsSlotExceptYN(booleanValue);

            if (disableCategoryContentList != null && disableCategoryContentList.isEmpty() == false) {
                Map<Integer, CategoryInfoForDiableContentDTO> policyMap = new HashMap<Integer, CategoryInfoForDiableContentDTO>();
                List<String> familyCategoryList = new ArrayList<String>();
                Set<String> uniqueCategoryid = new HashSet<String>();
                for (CategoryIdForDisableContent disable : disableCategoryContentList) {
                    CategoryInfoForDiableContentDTO categoryInfo = policyMap.get(disable.getPolicyId());

                    if (categoryInfo == null) {
                        categoryInfo = new CategoryInfoForDiableContentDTO();
                        policyMap.put(disable.getPolicyId(), categoryInfo);
                    }

                    // ?    ID? ?    ID .
                    List<String> familyExternalIdList = tambourineConnector
                            .getFamilyExternalIdList(disable.getCategoryId(), treeId);

                    if (familyExternalIdList != null && familyExternalIdList.isEmpty() == false) {
                        for (String categoryId : familyExternalIdList) {
                            if (uniqueCategoryid.add(categoryId)) {
                                categoryInfo.addDisableFamilyCategoryId(categoryId);
                                familyCategoryList.add(categoryId);
                            }
                        }
                    } else
                        log.warn("  ?  categoryId : " + disable.getCategoryId()
                                + "  ?  .");

                    categoryInfo.addDisableCategoryId(disable.getCategoryId());
                    categoryInfo.setOpp(disable.getPlacementOpportunityType());
                }

                if (convertContentAxisTreeDTO.updateLinkNode(familyCategoryList, null, null)) {
                    tambourineConnector.updateDisableContentPolicy(policyMap, deleteSchedulerLogId, treeId,
                            booleanValue);
                    result = true;
                }
            }
        }

        end = System.currentTimeMillis();
        log.info("@@@ update DisablePolicy - end(" + (end - start) + " ms)");

        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    public int convertCategoryTree(Date date, ADSMSchedulerLogDTO schedulerLog, String treeName,
            ADSMJobNameType srcJopType, boolean isPartialUpdate, ADSMSchedulerLogDTO deleteSchedulerLog,
            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 CategoryTree");
        }

        long start = System.currentTimeMillis();
        this.srcJobType = srcJopType;

        log.info("TreeName : " + treeName + ", srcDataType : " + srcJopType + ". Start Update("
                + schedulerLog.getParam() + ")");

        int treeID = 0;
        int dataLogID = Integer.parseInt((String) schedulerLog.getParamValue("dataLogId"));

        String licenseStartDateStr = null;
        List<CategoryDTO> invenBoxCategoryDTOs = null;
        List<VirtualCategoryDTO> virtualCategoryDTOs = null;
        String addsSite = null;

        if (ADSMJobNameType.INVENTORY_CATEGORY_DATA == srcJopType || ADSMJobNameType.CATEGORY_DATA == srcJopType) {
            licenseStartDateStr = (String) schedulerLog.getParamValue(FilePolling.PARAM_START_TIME_PREFIX);
            addsSite = (String) schedulerLog.getParamValue(FilePolling.PARAM_SITE_PREFIX);

            invenBoxCategoryDTOs = tambourineConnector.getCategoryListFromInventoryBox(date);

            VirtualCategoryDefineDTO virtualCategoryDefineDTO = tambourineConnector
                    .getVirtualCategoryDefineList(date);

            if (virtualCategoryDefineDTO != null)
                virtualCategoryDTOs = virtualCategoryDefineDTO.getVirtualCategoryList();
            if (invenBoxCategoryDTOs == null || invenBoxCategoryDTOs.size() == 0)
                log.warn("Cannot find InventoryBox at " + date.toString());
        }

        try {
            //?, , ,  ?
            //tambourineConnector.initializeInventoryCube(date);
            NodeDTO rootNodeDTO = getRootNode(dataLogID);

            if (rootNodeDTO == null) {
                throw new CiRuntimeException("Fail to Update Category Tree (Cannot Check root node Info. "
                        + schedulerLog.getParam() + ")");
            }

            treeID = tambourineConnector.getAxisTreeId(TreeType.CATEGORY_AXIS_TREE, treeName, addsSite,
                    platformType, false);
            //?
            if (treeID <= 0)
                treeID = tambourineConnector.registerCategoryAxisTree(
                        getAxisTreeDTO(treeName, rootNodeDTO, addsSite, platformType), date);

            //TreeNode .
            //?
            if (ADSMJobNameType.INVENTORY_CATEGORY_DATA == srcJopType
                    || ADSMJobNameType.CATEGORY_DATA == srcJopType) {
                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(), invenBoxCategoryDTOs,
                            virtualCategoryDTOs, dataLogID, isPartialUpdate);

                    String param = "treeId:" + treeID + "/treeType:" + TreeType.CATEGORY_AXIS_TREE.toString()
                            + "/treeName:" + treeName;
                    FeSyncNoticeboardDTO feSyncNoticeboardDTO = tambourineConnector.generateFeSyncNoticeboardData(
                            SyncDataType.TREEDATA, syncStartDate, param, addsSite, FeSyncNoticeboardStatus.NEWDATA,
                            platformType, null, null);

                    //? sync ,treetype,treeName? syncNoticeboard disable
                    tambourineConnector.disableFeSyncNoticeboard(feSyncNoticeboardDTO);

                    //  syncNoticeboard? ?
                    if (isPartialUpdate == false)
                        tambourineConnector.insertTreeLogNSyncNoticeboard(treeID, TreeType.CATEGORY_AXIS_TREE,
                                treeName, feSyncNoticeboardDTO, addsSite, platformType);
                } else {
                    makeTreeNode(treeID, date, rootNodeDTO.getExternalNodeId(), invenBoxCategoryDTOs,
                            virtualCategoryDTOs, dataLogID, isPartialUpdate);
                    addsSite = ADDSAdapterSetting.defaultSiteName;
                    insertTreeLogNSyncNoticeboard(addsSite, treeID, treeName, null);
                }

                // ? ??   ? ?  ? ??    ? ?? .      
                if (isPartialUpdate == false) {
                    if (deleteSchedulerLog == null)
                        throw new CiRuntimeException("Cannot Find deleteSchedulerLog in generating CategoryTree.");

                    if (updateDisableContentPolicy(treeID, deleteSchedulerLog.getId()) == true) {
                        deleteSchedulerLog.setSchedulerStatus(ADSMSchedulerStatus.SUCCESS);
                    } else
                        deleteSchedulerLog.setSchedulerStatus(ADSMSchedulerStatus.DONE);
                }
            } else { //viettel STB, OTT   ? 
                makeTreeNode(treeID, date, rootNodeDTO.getExternalNodeId(), null, null, dataLogID, false);
                Platform platform = getPlatformTypeByADSMJobNameType(srcJopType);
                insertTreeLogNSyncNoticeboard(addsSite, treeID, treeName, platform);

            }
            // ? 
            schedulerLog.setSchedulerStatus(ADSMSchedulerStatus.SUCCESS);
            schedulerLog.setMessage(TREE_NAME_MESSAGE_PREFIX + treeName);
        } catch (Exception e) {
            long end = System.currentTimeMillis();
            TheLogger.getWriter().error("Fail to Update Category Tree");
            log.error("Fail to update Category Tree.(" + schedulerLog.getParam() + ") - T(" + (end - start) + ")");
            schedulerLog.setMessage(e.getMessage());
            schedulerLog.setSchedulerStatus(ADSMSchedulerStatus.FAIL);
            if (ADSMJobNameType.INVENTORY_CATEGORY_DATA == srcJopType
                    || ADSMJobNameType.CATEGORY_DATA == srcJopType) {
                deleteSchedulerLog.setSchedulerStatus(ADSMSchedulerStatus.FAIL);
            }
            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 Category Tree (" + schedulerLog.getParam() + ") - T(" + (end - start) + ")");
        TheLogger.getWriter().info("Complete to Update Category Tree(" + schedulerLog.getParam() + ")");
        return treeID;
    }

    private Platform getPlatformTypeByADSMJobNameType(ADSMJobNameType srcJopType) {
        switch (srcJopType) {
        case CATEGORY_OTT_DATA:
            return Platform.MOBILE;
        default:
            return Platform.STB;
        }
    }

    public void insertTreeLogNSyncNoticeboard(String addsSite, int treeID, String treeName, Platform platformType) {
        Date nowDate = new Date();
        String param = "treeId:" + treeID + "/treeType:" + TreeType.CATEGORY_AXIS_TREE.toString() + "/treeName:"
                + treeName;
        FeSyncNoticeboardDTO feSyncNoticeboardDTO = tambourineConnector.generateFeSyncNoticeboardData(
                SyncDataType.TREEDATA, nowDate, param, addsSite, FeSyncNoticeboardStatus.NEWDATA, platformType,
                null, null);
        feSyncNoticeboardDTO.setPlatform(platformType);
        //  syncNoticeboard? ?
        tambourineConnector.insertTreeLogNSyncNoticeboard(treeID, TreeType.CATEGORY_AXIS_TREE, treeName,
                feSyncNoticeboardDTO, addsSite, platformType);
    }

    // ?
    /*
    @Transactional(rollbackFor=Exception.class)
    public int convertCategoryTree(Date date,   ADSMSchedulerLogDTO schedulerLog, String treeName,      ADSMJobNameType srcJopType) throws Exception {
       if(schedulerLog == null) {
     throw new CiRuntimeException("ADSMSchedulerLog is null");
       }
       if(treeName == null || treeName.isEmpty()) {
     throw new CiRuntimeException("Cannot Find treeName in generating CategoryTree");
       }
       long start = System.currentTimeMillis();
       this.srcJobType = srcJopType;
           
       log.info("TreeName : " + treeName + ", srcDataType : " + srcJopType + ". Start update(" + schedulerLog.getParam() + ")");
           
       int treeID = 0;
       int dataLogID = Integer.parseInt((String) schedulerLog.getParamValue("dataLogId"));
       try {
     NodeDTO rootNodeDTO = getRootNode(dataLogID);
     if(rootNodeDTO == null) {
        throw new CiRuntimeException("Fail to update Category Tree (Cannot find root node Info. " + schedulerLog.getParam() + ")");
     }
           
     treeID = tambourineConnector.getAxisTreeId(TreeType.CATEGORY_AXIS_TREE,treeName);
     //?
     if(treeID <= 0)
        treeID = tambourineConnector.registerCategoryAxisTree(getAxisTreeDTO(treeName, rootNodeDTO, null), date);
           
     //
     makeTreeNode(treeID, date, rootNodeDTO.getExternalNodeId(),   null, null, dataLogID, false);
           
     Date nowDate = new Date();
     String param = "treeId:" + treeID + "/treeType:" + TreeType.CATEGORY_AXIS_TREE.toString() + "/treeName:" + treeName;
     FeSyncNoticeboardDTO feSyncNoticeboardDTO = tambourineConnector.generateFeSyncNoticeboardData(SyncDataType.TREEDATA, nowDate, param, null, FeSyncNoticeboardStatus.NEWDATA);
     //  syncNoticeboard? ?
     tambourineConnector.insertTreeLogNSyncNoticeboard(treeID, TreeType.CATEGORY_AXIS_TREE, treeName, feSyncNoticeboardDTO);
           
        
     // ? 
     schedulerLog.setSchedulerStatus(ADSMSchedulerStatus.SUCCESS);
     schedulerLog.setMessage(TREE_NAME_MESSAGE_PREFIX + treeName);
       } catch (Exception e) {
     long end = System.currentTimeMillis();
     TheLogger.getWriter().error("Fail to update Category Tree");
     log.error("Fail to Category Tree (" + schedulerLog.getParam() + ") - T(" + (end - start )+ ")");
     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;
    }
       long end = System.currentTimeMillis();
       log.info("Complete to update Category Tree(" + schedulerLog.getParam() + ") - T(" + (end - start )+ ")");
       TheLogger.getWriter().info("Update Category Tree Success. (" + schedulerLog.getParam() + ")");
       return treeID;
    }*/

    public List<NodeDTO> getCategoryNodeListFromDB(ADSMJobNameType srcJopType) {
        List<CategoryDataDTO> categoryDTOList = tambourineConnector.getCategoryData(srcJopType);
        List<NodeDTO> nodeList = new ArrayList<NodeDTO>();

        for (CategoryDataDTO c : categoryDTOList) {
            NodeDTO node = convertData2Node(c.getCatId(), 0);
            if (node != null) {
                node.setName(c.getCatName());
                nodeList.add(node);
            }

        }
        return nodeList;
    }

    public List<DatInfomation> getNewSchedulerLogParam(Date date, ADSMJobNameType srcJopType,
            String defaultCategoryTreeName) {

        Map<ADSMSchedulerLogDTO, String/*treeName*/> schedulerLogMap = tambourineConnector
                .getNewSchedulerLogByTreeName(srcJopType, defaultCategoryTreeName);

        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();
            String message = dataLog.getMessage();

            int dataLogId = dataLog.getId();

            boolean isFurtureDate = false;
            String schedulerLogParam;
            String publisher = null;
            String platformType = null;
            String extendParameter = "";
            if (ADSMJobNameType.CATEGORY_DATA == srcJopType
                    || ADSMJobNameType.INVENTORY_CATEGORY_DATA == srcJopType) {
                int inventoryLogId = tambourineConnector.getInventoryBoxLogId(date);
                String datStartTime = (String) dataLog.getParamValue(FilePolling.PARAM_START_TIME_PREFIX);
                publisher = (String) dataLog.getParamValue(FilePolling.PARAM_SITE_PREFIX);
                platformType = (String) dataLog.getParamValue(FilePolling.PARAM_PLATFORM_PREFIX);

                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;

                schedulerLogParam = "dataLogId:" + dataLogId + "/inventoryLogId:" + inventoryLogId;// + extendParameter;         
                isFurtureDate = CiDateUtil.checkFutureDate(datStartTime, "yyyy-MM-dd");
            } else {
                schedulerLogParam = "dataLogId:" + dataLogId;
            }

            ADSMSchedulerLogDTO treeLog = null;
            if (isFurtureDate == true)
                treeLog = tambourineConnector.getLatestADSMSchedulerLog(CATEGORY_AXIS_TREE_PARTIAL_UPDATE, message,
                        schedulerLogParam);
            else
                treeLog = tambourineConnector.getLatestADSMSchedulerLog(TreeType.CATEGORY_AXIS_TREE.name(), message,
                        schedulerLogParam);

            if (treeLog != null)
                continue;
            else
                datInfomationList.add(new DatInfomation(treeName, schedulerLogParam + extendParameter,
                        isFurtureDate, publisher, platformType));
        }
        return datInfomationList;
    }

}