com.castis.xylophone.adsmadapter.common.util.InventorySizePolicyGenerator.java Source code

Java tutorial

Introduction

Here is the source code for com.castis.xylophone.adsmadapter.common.util.InventorySizePolicyGenerator.java

Source

package com.castis.xylophone.adsmadapter.common.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
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 com.castis.tambourine.bizobject.common.Period;
import com.castis.tambourine.dto.inventory.AxisTreeDTO;
import com.castis.tambourine.dto.ktadsm.GenericAxisItemIDDTO;
import com.castis.tambourine.dto.ktadsm.InventoryDTO;
import com.castis.tambourine.dto.ktadsm.InventorySizePolicyDTO;
import com.castis.tambourine.dto.ktadsm.SubInventoryDTO;
import com.castis.tambourine.dto.ktadsm.SubInventoryMemberDTO;
import com.castis.tambourine.dto.ktadsm.TimeDTO;
import com.castis.tambourine.dto.ktadsm.TimeDefineDTO;
import com.castis.tambourine.enumeration.EventType;
import com.castis.tambourine.enumeration.InventoryBoxDayCode;
import com.castis.tambourine.enumeration.PlacementOpportunityTypeEnum;
import com.castis.tambourine.enumeration.TreeType;
import com.castis.xylophone.adsmadapter.common.exception.CiRuntimeException;
import com.castis.xylophone.adsmadapter.common.util.data.ExternalIdMap;
import com.castis.xylophone.adsmadapter.common.util.data.ParentIdLink;
import com.castis.xylophone.adsmadapter.common.util.data.SizePolicyInfo;
import com.castis.xylophone.adsmadapter.common.util.data.SizePolicyInfoMap;
import com.castis.xylophone.adsmadapter.common.util.data.SubInventoryMemberMap;

public class InventorySizePolicyGenerator {

    protected static final Log log = LogFactory.getLog(InventorySizePolicyGenerator.class);

    Map<String/*time */, Set<GenericAxisItemIDDTO>> timeGenericAxisItemMap = null;
    Map<String/*time */, Integer> timeNodeCellCountMap = null;

    ExternalIdMap categoryMap;
    ExternalIdMap regionMap;
    ExternalIdMap weekMap;
    ExternalIdMap opportunityMap;

    //(ex., ,..),  ? ? ?  ..
    ParentIdLink regionParentIdLink; //  ??
    SubInventoryMemberMap subInventoryMapForMergeRegion = new SubInventoryMemberMap(); //''? SubInventory 
    SizePolicyInfoMap sizePolicyInfoMap = new SizePolicyInfoMap(); //''? subInven ? (??? ? ? ? .) 

    Period inventoryBoxPeriod;

    String inventoryBoxVersion;

    int weekdayCount; //inventoryBoxPeriod ??  ?(? ? 1? ??  ?)
    int weekendCount; //inventoryBoxPeriod ?? ? ?

    //fxpark
    @Deprecated
    public void setTimeGenericAxisItemMap(TimeDefineDTO timeDefine) {

        /*===============================================
         * time? genericAxisItemID  .
         * ?  ? Subinventory?  
         * ) "" + "_" + "T1", "" + "_" + "T2", "" + "_" + "T3", "?" + "_" + "T1", "?" + "_" + "T2", "?" + "_" + "T3"
         ================================================*/
        timeGenericAxisItemMap = new HashMap<String, Set<GenericAxisItemIDDTO>>();
        timeNodeCellCountMap = new HashMap<String, Integer>();

        for (TimeDTO timeDTO : timeDefine.getTimeList()) {

            String timeID = timeDTO.getTime_ID(); //T1, T2, T3
            List<String> timeValueList = timeDTO.getTimeValueList(); //ex.T1->22,23,0,1,2,3,4,5
            if (timeID == null || timeValueList == null || timeValueList.isEmpty())
                continue;

            for (InventoryBoxDayCode dayCode : InventoryBoxDayCode.values()) {
                List<String> timeKeyList = getTimeExternalID(dayCode, timeValueList);
                Set<GenericAxisItemIDDTO> timeGenericAxisItemList = weekMap.getGenericAxisItemIDList(timeKeyList);
                //subInventory?  time GenericAxisItem?  
                timeGenericAxisItemMap.put(getTimeGenericAxisItemMapKey(dayCode, timeID), timeGenericAxisItemList);
            }
            //? unitSize?   .(?  ? ? ?    )
            timeNodeCellCountMap.put(timeID, timeValueList.size());
        }
    }

    //fxpark (kt time table Tree  ? timeNodeCellCountMap?  ?? .)
    public void setTimeNodeCellCountMap(TimeDefineDTO timeDefine) {

        /*===============================================
         * time? genericAxisItemID  .
         * ?  ? Subinventory?  
         * ) "" + "_" + "T1", "" + "_" + "T2", "" + "_" + "T3", "?" + "_" + "T1", "?" + "_" + "T2", "?" + "_" + "T3"
         ================================================*/
        timeNodeCellCountMap = new HashMap<String, Integer>();

        for (TimeDTO timeDTO : timeDefine.getTimeList()) {

            String timeID = timeDTO.getTime_ID(); //T1, T2, T3
            List<String> timeValueList = timeDTO.getTimeValueList(); //ex.T1->22,23,0,1,2,3,4,5

            //? unitSize?   .(?  ? ? ?    )
            timeNodeCellCountMap.put(timeID, timeValueList.size());
        }
    }

    public void setRegionParentIdLink(AxisTreeDTO regionTreeDTO) {
        this.regionParentIdLink = new ParentIdLink(regionTreeDTO);
    }

    public int setCategoryMap(AxisTreeDTO axisTreeDTO) {
        this.categoryMap = new ExternalIdMap(TreeType.CATEGORY_AXIS_TREE, axisTreeDTO);
        return categoryMap.size();
    }

    public int setRegionMap(AxisTreeDTO axisTreeDTO) {
        this.regionMap = new ExternalIdMap(TreeType.REGION_AXIS_TREE, axisTreeDTO);
        // ? ? ,,   node  .
        setRegionParentIdLink(axisTreeDTO);
        return regionMap.size();
    }

    public int setWeekMap(AxisTreeDTO axisTreeDTO) {
        this.weekMap = new ExternalIdMap(TreeType.TIME_AXIS_TREE, axisTreeDTO);
        return weekMap.size();
    }

    public Date getSizePolicyStartDate() {
        if (inventoryBoxPeriod != null)
            return inventoryBoxPeriod.getStartDate();
        return null;
    }

    public Date getSizePolicyEndDate() {
        if (inventoryBoxPeriod != null)
            return inventoryBoxPeriod.getEndDate();
        return null;
    }

    public void setInventoryBoxPeriod(String startDate, String endDate) {
        inventoryBoxPeriod = new Period(startDate, endDate);

        weekdayCount = CiDateUtil.countDayInLicense(startDate, endDate, InventoryBoxDayCode.WEEKDAY);
        weekendCount = CiDateUtil.countDayInLicense(startDate, endDate, InventoryBoxDayCode.WEEKEND);
    }

    public String getInventoryBoxVersion() {
        return inventoryBoxVersion;
    }

    public void setInventoryBoxVersion(String inventoryBoxVersion) {
        this.inventoryBoxVersion = inventoryBoxVersion;
    }

    public InventorySizePolicyDTO getSizePolicy(InventoryDTO inventory, PlacementOpportunityTypeEnum oppType,
            InventoryBoxDayCode dayCode, String menuId, String regionId, String timeId, int catNodeLevel,
            int regNodeLevel, boolean isInterworkedData) {
        // ?
        String policyName = "[" + inventoryBoxVersion + "]" + oppType + "," + menuId + "," + timeId + "(day:"
                + dayCode + ")" + "," + regionId;

        if (inventory == null)
            log.warn(policyName + ":inventorySize not exists");

        //? ?  inventory size 
        Double inventorySizePerDay = Double.valueOf(inventory.getInventory());
        Double inventorySize = dayCode.equals(InventoryBoxDayCode.WEEKDAY) ? inventorySizePerDay * weekdayCount
                : inventorySizePerDay * weekendCount;
        int timeNodeCellCount = timeNodeCellCountMap.get(timeId);
        Double unitSize = inventorySizePerDay / timeNodeCellCount;

        //subInventory 
        SubInventoryDTO subInventory = new SubInventoryDTO();
        SubInventoryMemberDTO subInventoryMember = new SubInventoryMemberDTO();
        //String timeGenericAxisItemMapKey = getTimeGenericAxisItemMapKey(dayCode, timeId);
        String timeExternalId = getTimeExternalIdKey(dayCode, timeId);
        GenericAxisItemIDDTO regionAxisItemId = null, categoryAxisItemId = null,
                timeAxisItemId = null/*, oppTypeAxisItemId = null*/;
        try {
            //subInventoryMember.addAllAxisItemID ( timeGenericAxisItemMap.get(timeGenericAxisItemMapKey) );   // subinventory

            regionAxisItemId = regionMap.getGenericAxisItemID(regionId, subInventoryMember);
            categoryAxisItemId = categoryMap.getGenericAxisItemID(menuId, subInventoryMember);
            timeAxisItemId = weekMap.getGenericAxisItemID(timeExternalId, subInventoryMember);
            //         oppTypeAxisItemId = opportunityMap.getGenericAxisItemID(oppType.toString(), subInventoryMember);

            subInventoryMember.addAxisItemID(regionAxisItemId); // subinventory
            subInventoryMember.addAxisItemID(categoryAxisItemId); // subinventory
            subInventoryMember.addAxisItemID(timeAxisItemId); // subinventory
            //         subInventoryMember.addAxisItemID(oppTypeAxisItemId);   //OpportunityEvent subinventory

        } catch (CiRuntimeException e) {
            log.warn(policyName + ":" + e.getMessage());
            return null;
        }
        subInventoryMember.setSubInventory(subInventory);
        subInventory.addMember(subInventoryMember);

        int priority = ((10 - regNodeLevel * 2) * 100) - catNodeLevel;

        if (isInterworkedData) {
            String parentPolicyName = "[" + inventoryBoxVersion + "]" + oppType + "," + menuId + "," + timeId
                    + "(day:" + dayCode + ")" + ",";
            saveParentRegionInventorySizeInfo(parentPolicyName, regionId, categoryAxisItemId,
                    timeAxisItemId/*, oppTypeAxisItemId*/
                    , catNodeLevel, regNodeLevel, inventorySize, unitSize);
        }

        return getSizePolicy(false, policyName, inventorySize, unitSize, subInventory, priority, oppType);
    }

    public boolean isExistCategoryAxisItemId(PlacementOpportunityTypeEnum oppType, InventoryBoxDayCode dayCode,
            String menuId) {
        // ?
        String policyName = "[" + inventoryBoxVersion + "]" + oppType + "," + menuId + ", ALL_TIME (day:" + dayCode
                + ")" + ", ALL_REGION";

        try {

            categoryMap.getGenericAxisItemID(menuId);

        } catch (CiRuntimeException e) {
            log.warn(policyName + ":" + e.getMessage());
            return false;
        }

        return true;
    }

    public List<InventorySizePolicyDTO> generateAutoInventorySizePolicy(PlacementOpportunityTypeEnum oppType) {
        List<InventorySizePolicyDTO> result = new ArrayList<InventorySizePolicyDTO>();

        Set<String> policyNameList = subInventoryMapForMergeRegion.keySet();
        for (String policyName : policyNameList) {
            SubInventoryMemberDTO member = subInventoryMapForMergeRegion.get(policyName);
            SizePolicyInfo sizePolicyInfo = sizePolicyInfoMap.get(policyName);
            if (member == null || sizePolicyInfo == null)
                continue;
            SubInventoryDTO subInventory = new SubInventoryDTO();
            subInventory.addMember(member);
            member.setSubInventory(subInventory);

            InventorySizePolicyDTO policy = getSizePolicy(false, policyName, sizePolicyInfo.getInventorySize(),
                    sizePolicyInfo.getUnitSize(), subInventory, sizePolicyInfo.getPolicyPriority(), oppType);
            log.debug("create " + policy.getName() + ", unitSize:" + policy.getUnitSize());
            result.add(policy);
        }
        return result;
    }

    public InventorySizePolicyDTO getSizePolicy(boolean defaultType, String policyName, double inventorySize,
            double unitSize, SubInventoryDTO subInventory, int priority, PlacementOpportunityTypeEnum oppType) {

        InventorySizePolicyDTO sizePolicy = new InventorySizePolicyDTO();

        sizePolicy.setDefaultType(defaultType);
        sizePolicy.setName(policyName);
        sizePolicy.setCreateDate(new Date());
        sizePolicy.setSlotNumber(1);
        sizePolicy.setServicePeriod(inventoryBoxPeriod);
        sizePolicy.setEventType(EventType.CREATED);
        sizePolicy.setPlacementType(oppType);
        sizePolicy.setSubInventory(subInventory);
        sizePolicy.setInventorySize(inventorySize);
        sizePolicy.setUnitSize(unitSize);
        sizePolicy.setPriority(priority);
        return sizePolicy;
    }

    private void saveParentRegionInventorySizeInfo(String parentPolicyName, String regionExId,
            GenericAxisItemIDDTO categoryId, GenericAxisItemIDDTO timeId
            /*,GenericAxisItemIDDTO oppId*/, int catNodeLevel, int regNodeLevel, Double invenSize,
            Double unitSize) {

        //1.  
        List<String> regionParentExIds = regionParentIdLink.findParentIds(regionExId);

        //2.  ?   ?
        for (String regionParentExId : regionParentExIds) {
            String sizePolicyName = parentPolicyName + regionParentExId;
            GenericAxisItemIDDTO parentRegionId = regionMap.get(regionParentExId);
            //GenericAxisItemIDDTO parentRegionId = new GenericAxisItemIDDTO(TreeType.REGION_AXIS_TREE, regionId.getTreeId(), regionNodeParentId);

            subInventoryMapForMergeRegion.getOrPut(sizePolicyName, parentRegionId, categoryId, timeId/*, oppId*/);
            sizePolicyInfoMap.plusSize(sizePolicyName, catNodeLevel, --regNodeLevel, invenSize, unitSize);
        }
    }

    private String getTimeGenericAxisItemMapKey(InventoryBoxDayCode dayCode, String timeId) {
        return dayCode + "_" + timeId;
    }

    private String getTimeExternalIdKey(InventoryBoxDayCode dayCode, String timeId) {
        String externalId = null;
        switch (dayCode) {

        case WEEKDAY:
            externalId = "KT.*.WD." + timeId;
            break;

        case WEEKEND:
            externalId = "KT.*.WE." + timeId;
            break;

        }

        return externalId;
    }

    private List<String> getTimeExternalID(InventoryBoxDayCode dayCode, List<String> timeValueList) {
        List<String> externalIDList = new ArrayList<String>();
        switch (dayCode) {
        case WEEKDAY:
            for (String timeValue : timeValueList) {
                timeValue = timeValue.length() < 2 ? "0" + timeValue : timeValue;
                externalIDList.add("W.*.W" + Calendar.MONDAY + ".H" + timeValue);
                externalIDList.add("W.*.W" + Calendar.TUESDAY + ".H" + timeValue);
                externalIDList.add("W.*.W" + Calendar.WEDNESDAY + ".H" + timeValue);
                externalIDList.add("W.*.W" + Calendar.THURSDAY + ".H" + timeValue);
                externalIDList.add("W.*.W" + Calendar.FRIDAY + ".H" + timeValue);
            }
            break;
        case WEEKEND:
            for (String timeValue : timeValueList) {
                timeValue = timeValue.length() < 2 ? "0" + timeValue : timeValue;
                externalIDList.add("W.*.W" + Calendar.SUNDAY + ".H" + timeValue);
                externalIDList.add("W.*.W" + Calendar.SATURDAY + ".H" + timeValue);
            }
            break;
        }

        return externalIDList;
    }
}