Java tutorial
package com.castis.xylophone.adsmadapter.filePolling; import java.io.File; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.commons.io.comparator.LastModifiedFileComparator; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 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.ktadsm.ADSMSchedulerLogDTO; import com.castis.tambourine.dto.ktadsm.InventoryDataDTO; import com.castis.tambourine.enumeration.Platform; import com.castis.tambourine.exception.UnacceptableStartDateException; import com.castis.tambourine.exception.UnreadableFileException; import com.castis.tambourine.exception.WrongFileNameException; import com.castis.tambourine.logger.TheLogger; import com.castis.xylophone.adsmadapter.common.enumeration.FileResultCode; import com.castis.xylophone.adsmadapter.common.enumeration.InputDataType; import com.castis.xylophone.adsmadapter.common.util.CiDateUtil; import com.castis.xylophone.adsmadapter.common.util.ResultFileUtil; import com.castis.xylophone.adsmadapter.connectors.TambourineConnector; import com.castis.xylophone.adsmadapter.setting.ADDSAdapterSetting; import com.castis.xylophone.adsmadapter.thread.pasing.InventoryDataListPasing; public class InventoryDataListPolling extends FilePolling { static final Log log = LogFactory.getLog(InventoryDataListPolling.class); static final String name = "InventoryDataListPolling"; public static final String TREE_NAME_MESSAGE_PREFIX = "treeName:"; class ParsedDatFileNameInfo { InputDataType dataType; String site; String treeName; String startDate; String jobNameType; Platform platform = null; } InventoryDataListPasing inventoryDataListPasing; TambourineConnector tambourineConnector; private boolean txtExtension = false; //---------------------------------------------------------- // Constructor and destroyer public InventoryDataListPolling() { log.info(" ### " + name + " was created."); triggerInitialization(); } public void close() { log.info(" # " + name + " is shutting down."); } // - - - - - - - - - - - - - - - - // Getter/setter methods public void setTxtExtension(boolean txtExtension) { this.txtExtension = txtExtension; } public InventoryDataListPasing getInventoryDataListPasing() { return inventoryDataListPasing; } public void setInventoryDataListPasing(InventoryDataListPasing inventoryDataListPasing) { this.inventoryDataListPasing = inventoryDataListPasing; } public void setTambourineConnector(TambourineConnector tambourineConnector) { this.tambourineConnector = tambourineConnector; } public TambourineConnector getTambourineConnector() { return tambourineConnector; } //---------------------------------------------------------- // Protected/private methods public boolean loadFileList(String targetDir, String filter) { for (int i = 0; i < oneCycleTryCount; i++) { long start = System.currentTimeMillis(); fileDirectory = targetDir; //? ? List<File> fileList = getFileListByCondition(filter, null, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR, true); if (fileList != null && !fileList.isEmpty()) { try { process(fileList.get(0), start); } catch (UnreadableFileException e) { break; } } else break; } return true; } private boolean process(File file, long currentTime) throws UnreadableFileException { if (file == null) return false; ADSMSchedulerLogDTO schedulerLog = null; try { /*? ? ?*/ if (integrityFileSize(file) == false) { moveIgnoreFile(file, InputDataType.INVENTORY_DAT, "File size is smaller than limit size"); return false; } log.info("InventoryDataListPolling start"); /*? ? */ ParsedDatFileNameInfo datFileInfo = parseFileName(file, null); if (datFileInfo == null) return false; /* */ String param = PARAM_FILE_NAME_PREFIX + ":" + file.getName() + "/" + PARAM_TREE_NAME + ":" + datFileInfo.treeName + "/" + PARAM_START_TIME_PREFIX + ":" + datFileInfo.startDate + "/" + PARAM_SITE_PREFIX + ":" + datFileInfo.site + "/" + PARAM_PLATFORM_PREFIX + ":" + datFileInfo.platform; String schedLogMessage = TREE_NAME_MESSAGE_PREFIX + datFileInfo.treeName; schedulerLog = tambourineConnector.startLog(datFileInfo.jobNameType, param, schedLogMessage); if (schedulerLog == null) return false; List<InventoryDataDTO> inventoryDataDTOList; /*? */ inventoryDataDTOList = inventoryDataListPasing.parseList(file, datFileInfo.dataType); /* DB */ saveInventoryDat(inventoryDataDTOList, datFileInfo, currentTime, schedulerLog); /*dat ? ? ?*/ moveProcessedFile(file, true); String msg = "The files were successfully processed. [size:" + inventoryDataDTOList.size() + "]"; ResultFileUtil.writeResultFile(file.getName(), msg, FileResultCode.RESULT_OK.getValue(), fileResDirectory, InputDataType.INVENTORY_DAT); myProcessStatus.addTransactionCount(); log.info(msg); } catch (UnreadableFileException e) { //log.info(e.getMessage()); throw e; } catch (WrongFileNameException e) { moveIgnoreFile(file, InputDataType.INVENTORY_DAT, e.getMessage()); ResultFileUtil.writeResultFile(file.getName(), e.getMessage(), FileResultCode.RESULT_IGNORE.getValue(), fileResDirectory, InputDataType.INVENTORY_DAT); log.info(e.getMessage()); return false; } catch (UnacceptableStartDateException e) { moveIgnoreFile(file, InputDataType.INVENTORY_DAT, e.getMessage()); ResultFileUtil.writeResultFile(file.getName(), e.getMessage(), FileResultCode.RESULT_IGNORE.getValue(), fileResDirectory, InputDataType.INVENTORY_DAT); log.info(e.getMessage()); return false; } catch (Exception e) { if (schedulerLog != null) { schedulerLog.setMessage(e.getMessage()); schedulerLog.setSchedulerStatus(ADSMSchedulerStatus.FAIL); StackTraceElement[] stackTraces = e.getStackTrace(); if (stackTraces.length > 0) { String failProcess = stackTraces[0].getClassName() + " - " + stackTraces[0].getMethodName() + "(" + stackTraces[0].getLineNumber() + ")"; schedulerLog.setFailProcess(failProcess); } } log.error("Fail to Processing InventoryData File(" + file.getName() + ")", e); moveProcessedFile(file, false); ResultFileUtil.writeResultFile(file.getName(), e.getMessage(), FileResultCode.RESULT_FAIL.getValue(), fileResDirectory, InputDataType.INVENTORY_DAT); myProcessStatus.addErrorTransactionCount(); } finally { if (schedulerLog != null) { tambourineConnector.endLog(schedulerLog); } } long end = System.currentTimeMillis(); log.info("InventoryDataListPolling end - T(" + (end - currentTime) + ")"); return true; } private void saveInventoryDat(List<InventoryDataDTO> inventoryDataDTOList, ParsedDatFileNameInfo datFileInfo, long currentTime, ADSMSchedulerLogDTO schedulerLog) throws Exception { if (inventoryDataDTOList != null && inventoryDataDTOList.size() > 0) { /* ?? / ?*/ boolean isPast = false; Date fileStartDate = CiDateUtil.convertDate(datFileInfo.startDate, "yyyy-MM-dd"); if (fileStartDate.getTime() <= currentTime) isPast = true; /* dat schedulerLogId */ //1. ?? ? : , dat ? List<ADSMSchedulerLogDTO> previousSuccessLogList = getADSMSchedulerLogListAfterBaseTime( datFileInfo.jobNameType, schedulerLog.getMessage(), 0); //2. ?? ? : ? ?? ?(curTime) ?, // ?? (treeLicenseStartTime) ? ??? ? . // schedulerLogId ? ? dat ?. if (!isPast && previousSuccessLogList != null && !previousSuccessLogList.isEmpty()) { //2-1. ? ?? ADSMSchedulerLogDTO latestSuccessLog = previousSuccessLogList .get(previousSuccessLogList.size() - 1); String lastestSuccessStart = (String) latestSuccessLog.getParamValue(PARAM_START_TIME_PREFIX); Date latestSuccessStartDate = CiDateUtil.convertDate(lastestSuccessStart, "yyyy-MM-dd"); //2-2. ?? ? dat? if (latestSuccessStartDate != null && latestSuccessStartDate.getTime() > currentTime) { //2-3. ?? ? ? if (datFileInfo.startDate.compareTo(lastestSuccessStart) > 0) { // ?. throw new UnacceptableStartDateException( "Since [" + lastestSuccessStart + "] start date is not available."); } else { //2-4. ?? ? ? previousSuccessLogList.clear(); previousSuccessLogList.add(latestSuccessLog); } } else { previousSuccessLogList.clear(); } } /*dat DB */ tambourineConnector.insertInventoryData(inventoryDataDTOList, schedulerLog, previousSuccessLogList); } } private List<ADSMSchedulerLogDTO> getADSMSchedulerLogListAfterBaseTime(String adsmJobNameType, String logMessage, long baseTime) { List<ADSMSchedulerLogDTO> previousSuccessLogList = tambourineConnector .getADSMSchedulerLogList(adsmJobNameType, logMessage); List<ADSMSchedulerLogDTO> resultLogList = new ArrayList<ADSMSchedulerLogDTO>(); for (ADSMSchedulerLogDTO log : previousSuccessLogList) { String fileStartDateStr = (String) log.getParamValue(PARAM_START_TIME_PREFIX); Date fileStartDate = CiDateUtil.convertDate(fileStartDateStr, "yyyy-MM-dd"); if (fileStartDate != null) { if (fileStartDate.getTime() >= baseTime) resultLogList.add(log); } } return resultLogList; } private void checkValidStartTime(String treeLicenseStartTime) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { sdf.parse(treeLicenseStartTime); } catch (ParseException pe) { throw new WrongFileNameException("Check ValidStarTime Rule(yyyy-MM-dd)"); } } public ParsedDatFileNameInfo parseFileName(File file, Platform platformType) throws WrongFileNameException { ParsedDatFileNameInfo result = new ParsedDatFileNameInfo(); String fileName = null; try { fileName = file.getName(); int count = StringUtils.countMatches(fileName, "."); if (count == INDEX_COUNT) { String[] values = fileName.split("\\."); result.dataType = InputDataType.valueof(values[INDEX_INPUT_DATA_TYPE]); result.site = values[INDEX_PUBLISHER]; result.treeName = values[INDEX_TREE_NAME]; result.startDate = values[INDEX_START_DATE]; result.platform = Platform.valueof(values[INDEX_PLATFORM_TYPE]); switch (result.dataType) { case CATEGORY_DATA: result.jobNameType = ADSMJobNameType.INVENTORY_CATEGORY_DATA.name(); break; case REGION_DATA: result.jobNameType = ADSMJobNameType.INVENTORY_REGION_DATA.name(); break; default: throw new WrongFileNameException("Check File name rule."); } // ? checkValidStartTime(result.startDate); } else if (txtExtension) { Date date = new Date(); SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); String[] values = fileName.split("\\_"); result.dataType = InputDataType.valueof(values[0]); result.site = ADDSAdapterSetting.defaultSiteName; result.startDate = fmt.format(date); result.treeName = Constants.tree.REGION_TREE_NAME; result.platform = platformType; switch (result.dataType) { case CATEGORY_DATA: result.jobNameType = ADSMJobNameType.INVENTORY_CATEGORY_DATA.name(); result.treeName = Constants.tree.CATEGORY_TREE_NAME; break; case REGION_DATA: result.jobNameType = ADSMJobNameType.INVENTORY_REGION_DATA.name(); result.treeName = Constants.tree.REGION_TREE_NAME; break; default: throw new WrongFileNameException("Check File name rule."); } // ? checkValidStartTime(result.startDate); } else throw new WrongFileNameException("Check File name rule."); } catch (WrongFileNameException e) { throw e; } catch (Exception e) { log.error("Fail to pasing FileName.(FileName:" + fileName + ")", e); return null; } return result; } }