Java tutorial
package com.castis.xylophone.adsmadapter.convert.axistree; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; 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 com.castis.tambourine.bizobject.inventory.AxisTree; 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.AxisTreeDTO; import com.castis.tambourine.dto.ktadsm.ADInventoryDTO; import com.castis.tambourine.dto.ktadsm.ADSMSchedulerLogDTO; import com.castis.tambourine.dto.ktadsm.CategoryDTO; import com.castis.tambourine.dto.ktadsm.InventoryBoxDTO; import com.castis.tambourine.dto.ktadsm.InventoryDTO; import com.castis.tambourine.dto.ktadsm.InventorySizePolicyDTO; import com.castis.tambourine.dto.ktadsm.TimeDefineDTO; import com.castis.tambourine.dto.ktadsm.result.ResultInventory; import com.castis.tambourine.enumeration.InventoryBoxDayCode; import com.castis.tambourine.enumeration.PlacementOpportunityTypeEnum; import com.castis.tambourine.enumeration.TreeType; import com.castis.tambourine.inventorysysdomain.syscomp.export.IInventoryExportSysCompForAdapter; import com.castis.tambourine.logger.TheLogger; import com.castis.xylophone.adsmadapter.common.util.CiFileUtil; import com.castis.xylophone.adsmadapter.common.util.InventorySizePolicyGenerator; import com.castis.xylophone.adsmadapter.connectors.TambourineConnector; /** * @author Leftie */ public class ConvertInventoryBox { static final Log log = LogFactory.getLog(ConvertInventoryBox.class); TambourineConnector tambourineConnector; IInventoryExportSysCompForAdapter inventoryExportSysCompForAdapter; double defaultSize; private ResultInventory inventoryResult = null; private int errorCount = 0; // private String resultDirectory; private String fileName; public void setFileName(String name) { this.fileName = name; } // // public void setResultDirectory(String resultDirectory) { // this.resultDirectory = resultDirectory; // } public void setInventoryExportSysCompForAdapter( IInventoryExportSysCompForAdapter inventoryExportSysCompForAdapter) { this.inventoryExportSysCompForAdapter = inventoryExportSysCompForAdapter; } public void setTambourineConnector(TambourineConnector tambourineConnector) { this.tambourineConnector = tambourineConnector; } public void setDefaultSize(double defaultSize) { this.defaultSize = defaultSize; } //? public boolean generateInventorySizePolicy(ADSMJobNameType jobName, ADSMJobNameType inventorySizePolicy) { long start = System.currentTimeMillis(); InventoryBoxDTO inventoryBox = tambourineConnector.findNewInventoryBox(jobName, inventorySizePolicy.name()); if (inventoryBox == null) return true; log.info("generateInventorySizePolicy start [jobName : " + jobName.name() + "] "); // ? ?. tambourineConnector.generateKtTimeTableTree(inventoryBox.getTimeDefine(), inventoryBox.getStart_DT()); ADSMSchedulerLogDTO schedulerLog = null; try { //?? schedulerLog ?. schedulerLog = tambourineConnector.startLog(inventorySizePolicy.name(), "inventoryLogId:" + inventoryBox.getSchedulerLogId()); if (schedulerLog == null) return false; /*=============================================== * 1. ', , ?' . + 'OpportunityEvent' (?? preplay, midplay ? ) ================================================*/ AxisTreeDTO categoryAxisTreeDTO = null; AxisTreeDTO regionAxisTreeDTO = null; AxisTreeDTO weekAxisTreeDTO = null; try { String categoryTreeName = Constants.tree.CATEGORY_TREE_NAME; String regionTreeName = Constants.tree.REGION_TREE_NAME; String weekTreeName = AxisTree.wellKnownTreeNameTimeTableTree; categoryAxisTreeDTO = inventoryExportSysCompForAdapter .findAxisTreeByName(TreeType.CATEGORY_AXIS_TREE, categoryTreeName, new Date(), new Date()); if (categoryAxisTreeDTO == null) return false; regionAxisTreeDTO = inventoryExportSysCompForAdapter.findAxisTreeByName(TreeType.REGION_AXIS_TREE, regionTreeName, new Date(), new Date()); if (regionAxisTreeDTO == null) return false; weekAxisTreeDTO = inventoryExportSysCompForAdapter.findAxisTreeByName(TreeType.TIME_AXIS_TREE, weekTreeName, new Date(), new Date()); if (weekAxisTreeDTO == null) return false; log.info("tree load done"); } catch (Exception e) { log.error("tree load - fail", e); throw e; } /*=============================================== * 2. mapping ID mapper ================================================*/ InventorySizePolicyGenerator sizePolicyGenerator = new InventorySizePolicyGenerator(); try { int cateSize = sizePolicyGenerator.setCategoryMap(categoryAxisTreeDTO); int regionSize = sizePolicyGenerator.setRegionMap(regionAxisTreeDTO); int timeSize = sizePolicyGenerator.setWeekMap(weekAxisTreeDTO); // int opportunitySize = sizePolicyGenerator.setOpportunityMap(oppAxisTreeDTO); //time Define? size TimeDefineDTO timeDefine = inventoryBox.getTimeDefine(); sizePolicyGenerator.setTimeNodeCellCountMap(timeDefine); /*=============================================== * 3. license ? ? ? ? ? ================================================*/ sizePolicyGenerator.setInventoryBoxPeriod(inventoryBox.getStart_DT(), inventoryBox.getEnd_DT()); sizePolicyGenerator.setInventoryBoxVersion(inventoryBox.getInventoryVer()); log.info("mapper generate done(cateSize:" + cateSize + ",regionSize:" + regionSize + ",timeSize:" + timeSize /*+ ",opportunitySize:" + opportunitySize + ")"*/); } catch (Exception e) { log.error("InventorySizePolicyGenerator make - fail", e); throw e; } /*=============================================== * 4. ? list ================================================*/ List<InventorySizePolicyDTO> sizePolicyList = new ArrayList<InventorySizePolicyDTO>(); log.info("? ? ? "); ADInventoryDTO inventory = null; PlacementOpportunityTypeEnum oppType = null; try { if (inventoryBox.getAdInventory() != null && ADSMJobNameType.INVENTORY_BOX.equals(jobName)) { inventory = inventoryBox.getAdInventory(); oppType = PlacementOpportunityTypeEnum.PREROLL; } else if (inventoryBox.getMidplay_adInventory() != null && ADSMJobNameType.INVENTORY_BOX_MID.equals(jobName)) { inventory = inventoryBox.getMidplay_adInventory(); oppType = PlacementOpportunityTypeEnum.MIDROLL; } else if (inventoryBox.getPostplay_adInventory() != null && ADSMJobNameType.INVENTORY_BOX_POST.equals(jobName)) { inventory = inventoryBox.getPostplay_adInventory(); oppType = PlacementOpportunityTypeEnum.POSTROLL; } generateSizePolicyList(sizePolicyList, inventory, oppType, sizePolicyGenerator); sizePolicyList.addAll(sizePolicyGenerator.generateAutoInventorySizePolicy(oppType)); log.info("? ? ? ( :" + sizePolicyList.size() + ")"); } catch (Exception e) { log.error("InventorySizePolicy generate - fail", e); throw e; } /*=============================================== * 5. ================================================*/ log.info("? ? DB "); try { if (sizePolicyList.size() > 0) { Date startDate = sizePolicyGenerator.getSizePolicyStartDate(); Date endDate = sizePolicyGenerator.getSizePolicyEndDate(); //TODO: ? ? inventoryExportSysCompForAdapter.registerInventorySizePolicy(sizePolicyList, startDate, endDate, oppType); } } catch (Exception e) { log.error("InventorySizePolicy register - fail", e); throw e; } log.info("? ? DB (version-" + inventoryBox.getInventoryVer() + ")"); /*=============================================== * 6. ? join Table insert ================================================*/ log.info("InventorySizePolicy GenericAxisItem List ? ."); inventoryExportSysCompForAdapter.copyPolicyGenericAxisItem(oppType); log.info("InventorySizePolicy GenericAxisItem List ? ."); schedulerLog.setSchedulerStatus(ADSMSchedulerStatus.SUCCESS); } catch (Exception e) { //log.error("",e); schedulerLog.setMessage(e.toString()); schedulerLog.setSchedulerStatus(ADSMSchedulerStatus.FAIL); StackTraceElement[] stackTraces = e.getStackTrace(); if (stackTraces.length > 0) { schedulerLog.setFailProcess(stackTraces[0].getClassName() + " - " + stackTraces[0].getMethodName() + "(" + stackTraces[0].getLineNumber() + ")"); } TheLogger.getWriter().error(inventoryBox.getInventoryVer() + " Inventory Size "); log.error(inventoryBox.getInventoryVer() + " Inventory Size . ? ?? ?."); return false; } finally { //? ? ??? tambourineConnector.endLog(schedulerLog); } long end = System.currentTimeMillis(); log.info("generateInventorySizePolicy end T(" + (end - start) + ")"); TheLogger.getWriter().info(inventoryBox.getInventoryVer() + " Inventory Size "); return true; } private int getNodeLevel(Map<String, Integer> nodeLevelMap, TreeType treeType, String externalId) throws Exception { Integer level = nodeLevelMap.get(externalId); if (level == null) { level = inventoryExportSysCompForAdapter.getNodeLevelByTreeTypeAndExternalId(treeType, externalId); nodeLevelMap.put(externalId, level); } return level; } private void generateSizePolicyList(List<InventorySizePolicyDTO> sizePolicyList, ADInventoryDTO adInventory, PlacementOpportunityTypeEnum oppType, InventorySizePolicyGenerator sizePolicyGenerator) throws Exception { if (adInventory == null) return; Map<String, Integer> regionNodeLevelMap = new HashMap<String, Integer>(); List<CategoryDTO> categoryList = adInventory.getCategoryList(); for (CategoryDTO category : categoryList) { String menuId = category.getMenu_ID(); InventoryBoxDayCode dayCode = InventoryBoxDayCode.valueof(category.getDay_CD()); // ?, ? .(2014-06-25 fxpark) //?: ?, ? if (!sizePolicyGenerator.isExistCategoryAxisItemId(oppType, dayCode, menuId)) continue; int catNodeLevel = inventoryExportSysCompForAdapter .getNodeLevelByTreeTypeAndExternalId(TreeType.CATEGORY_AXIS_TREE, menuId); List<InventoryDTO> inventoryList = category.getInventoryList(); for (InventoryDTO inventory : inventoryList) { String regionId = inventory.getRegion(); int regionNodeLevel = getNodeLevel(regionNodeLevelMap, TreeType.REGION_AXIS_TREE, regionId); String timeId = inventory.getTime_ID(); //? InventorySizePolicyDTO sizePolicy = sizePolicyGenerator.getSizePolicy(inventory, oppType, dayCode, menuId, regionId, timeId, catNodeLevel, regionNodeLevel, true); if (sizePolicy == null) continue; sizePolicyList.add(sizePolicy); log.debug("create " + sizePolicy.getName() + ", unitSize:" + sizePolicy.getUnitSize()); } } } @SuppressWarnings("finally") public int generateInventoryBox(InventoryBoxDTO inventoryBox, ADSMSchedulerLogDTO schedulerLog, String fileResDirectory) { int resultCount = 0; try { if (inventoryBox != null) { tambourineConnector.insertInventoryBox(inventoryBox, schedulerLog); schedulerLog.setSchedulerStatus(ADSMSchedulerStatus.SUCCESS); resultCount = errorCount; } else { schedulerLog.setFailProcess("Parsing error"); schedulerLog.setSchedulerStatus(ADSMSchedulerStatus.FAIL); errorCount = -1; //Parsing error resultCount = -1; } } catch (Exception e) { //log.error("",e); 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() + ")"); } } finally { inventoryResult = new ResultInventory(); if (inventoryBox != null) { inventoryResult.setStart_DT(inventoryBox.getStart_DT()); inventoryResult.setEnd_DT(inventoryBox.getEnd_DT()); inventoryResult.setInventoryVer(inventoryBox.getInventoryVer()); } generateResult(fileResDirectory, inventoryResult, errorCount); return resultCount; } } public boolean generateResult(String fileResDirectory, ResultInventory inventoryResult, int errorCount) { inventoryResult.setError_cnt(errorCount); OutputStream os = null; try { boolean result = (inventoryResult != null && inventoryResult.getError_cnt() > -1); String resultString = result ? "OK_result" : "NOK_result"; int pos = this.fileName.lastIndexOf("."); String fileName = ""; fileName = this.fileName.substring(0, pos); fileName = fileName + "_" + resultString + ".xml"; File dir = new File(fileResDirectory); // ? if (!dir.mkdirs()) { log.debug("Direcory is exist"); } os = new FileOutputStream(CiFileUtil.getReplaceFullPath(fileResDirectory) + fileName); inventoryResult.marshaling(os); inventoryResult = new ResultInventory(); errorCount = 0; return result; } catch (Exception e) { log.error("", e); return false; } finally { try { if (os != null) os.close(); } catch (IOException e) { log.error("", e); } } } }