com.castis.xylophone.adsmadapter.filePolling.MapperListPolling.java Source code

Java tutorial

Introduction

Here is the source code for com.castis.xylophone.adsmadapter.filePolling.MapperListPolling.java

Source

package com.castis.xylophone.adsmadapter.filePolling;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.comparator.LastModifiedFileComparator;
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.bizobject.mapper.Mapper;
import com.castis.tambourine.dto.ktadsm.ADSMSchedulerLogDTO;
import com.castis.tambourine.enumeration.MapperAxisType;
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.mapper.ParsedMapperData;
import com.castis.xylophone.adsmadapter.thread.pasing.MapperParsing;

/**
 * @author  iuwe1126
 */
public class MapperListPolling extends FilePolling {

    static final Log log = LogFactory.getLog(MapperListPolling.class);
    static final String name = "MapperListPolling";

    static final String MAPPER_START_DATE = "mapperStartDate";

    MapperParsing mapperParsing;

    TambourineConnector tambourineConnector;

    //----------------------------------------------------------
    // Constructor and destroyer

    public MapperListPolling() {
        log.info(" ### " + name + " was created.");
        triggerInitialization();
    }

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

    // - - - - - - - - - - - - - - - -
    // Getter/setter methods
    public MapperParsing getMapperParsing() {
        return mapperParsing;
    }

    public void setMapperParsing(MapperParsing mapperParsing) {
        this.mapperParsing = mapperParsing;
    }

    public void setTambourineConnector(TambourineConnector tambourineConnector) {
        this.tambourineConnector = tambourineConnector;
    }

    //----------------------------------------------------------
    // Protected/private methods   

    public boolean loadFileList() {

        for (int i = 0; i < oneCycleTryCount; i++) {
            long start = System.currentTimeMillis();

            List<File> fileList = getFileListByCondition("mapper", null,
                    LastModifiedFileComparator.LASTMODIFIED_COMPARATOR, true);

            if (fileList == null || fileList.isEmpty())
                return true;

            ADSMSchedulerLogDTO schedulerLog = null;
            File selectFile = fileList.get(0);
            if (selectFile == null)
                continue;

            try {
                if (integrityFileSize(selectFile) == false) {
                    moveIgnoreFile(selectFile, InputDataType.INVENTORY_DAT, "File size is smaller than limit size");
                    continue;
                }

                log.info("MapperListPolling start(" + selectFile.getAbsolutePath() + ")");

                //? 
                ParsedMapperData parsedMapperData = (ParsedMapperData) mapperParsing.parseList(selectFile);

                //DB   
                String siteMessage = FilePolling.PARAM_SITE_PREFIX + ":" + parsedMapperData.getMappingIdDomain();
                String jobName = getJobName(parsedMapperData.getMapperAxisType()).name();
                String param = "fileName:" + selectFile.getName() + "/" + siteMessage + "/" + MAPPER_START_DATE
                        + ":" + parsedMapperData.getMapperStartDateStr();

                int previousSuccessSchedulerLogId = 0;
                // ? ?
                ADSMSchedulerLogDTO previousSuccessLog = tambourineConnector
                        .getLatestADSMSchedulerLogwithMessage(jobName, siteMessage);
                if (previousSuccessLog != null) {
                    String lastestSuccessStart = (String) previousSuccessLog.getParamValue(MAPPER_START_DATE);
                    Date lastestSuccessStartDate = CiDateUtil.convertDate(lastestSuccessStart, "yyyy-MM-dd");
                    //2-2.   ?? ?  dat?
                    if (lastestSuccessStartDate != null && lastestSuccessStartDate.getTime() > start) {
                        if (parsedMapperData.getMapperStartDateStr().compareTo(lastestSuccessStart) > 0) {
                            //  ?.
                            moveIgnoreFile(selectFile, InputDataType.INVENTORY_DAT,
                                    "License start date after [" + lastestSuccessStart + "] can not be processed.");
                            continue;
                        }
                    }
                    previousSuccessSchedulerLogId = previousSuccessLog.getId();
                }

                schedulerLog = tambourineConnector.startLog(jobName, param, siteMessage);
                if (schedulerLog == null)
                    return false;

                /*?*/
                int mergeListSize = mergeMapperList(parsedMapperData, previousSuccessSchedulerLogId);

                //DB    
                schedulerLog.setSchedulerStatus(ADSMSchedulerStatus.SUCCESS);

                //?  
                moveProcessedFile(selectFile, true);
                ResultFileUtil.writeResultFile(selectFile.getName(),
                        "File processing Success. [size:" + mergeListSize + "]",
                        FileResultCode.RESULT_OK.getValue(), fileResDirectory, InputDataType.INVENTORY_DAT);

                myProcessStatus.addTransactionCount();
            } catch (UnreadableFileException e) {
                log.info(e.getMessage());
                break;
            } catch (WrongFileNameException e) {
                log.warn(e.getMessage());
                moveProcessedFile(selectFile, false);
                ResultFileUtil.writeResultFile(selectFile.getName(), e.getMessage(),
                        FileResultCode.RESULT_FAIL.getValue(), fileResDirectory, InputDataType.INVENTORY_DAT);
                myProcessStatus.addErrorTransactionCount();
                return false;
            } catch (Exception e) {
                String errorLog = "Mapper Info File(" + selectFile.getName() + ")processing fail.";
                TheLogger.getWriter().error(errorLog);
                log.error(errorLog, 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);
                    }
                }

                moveProcessedFile(selectFile, false);
                ResultFileUtil.writeResultFile(selectFile.getName(), "File processing fail.[" + e + "]",
                        FileResultCode.RESULT_FAIL.getValue(), fileResDirectory, InputDataType.INVENTORY_DAT);
                myProcessStatus.addErrorTransactionCount();
            }

            tambourineConnector.endLog(schedulerLog);

            long end = System.currentTimeMillis();
            log.info("MapperListPolling end - T(" + (end - start) + ")");
        }

        return true;
    }

    private ADSMJobNameType getJobName(MapperAxisType mapperAxisType) {
        switch (mapperAxisType) {
        case REGION:
            return ADSMJobNameType.REGION_MAPPER;
        case CATEGORY:
            return ADSMJobNameType.CATEGORY_MAPPER;
        case CHANNEL:
            return ADSMJobNameType.CHANNEL_MAPPER;
        case CONTENT:
            return ADSMJobNameType.CONTENT_MAPPER;
        case CONTENT_PROVIDER:
            return ADSMJobNameType.CONTENT_PROVIDER_MAPPER;
        case TIME:
            return ADSMJobNameType.TIME_MAPPER;
        case USER:
            return ADSMJobNameType.USER_MAPPER;
        }
        return null;
    }

    /*
       private int   getPreviousSuccessSchedulerLogId(String jobName, String siteMessage) {
          int previousSuccessSchedulerLogId = -1;
              
          try {
     ADSMSchedulerLogDTO previousSuccessSchedulerLog = tambourineConnector.getLatestADSMSchedulerLogwithMessage(jobName, siteMessage);
     if(previousSuccessSchedulerLog != null)
        previousSuccessSchedulerLogId = previousSuccessSchedulerLog.getId();
          } catch (Exception e) {
     log.error("?? ? [jobName:" + jobName + ", siteMessage:" + siteMessage + "] ?    ? .");
          }
              
          return previousSuccessSchedulerLogId;
       }
       */
    private int mergeMapperList(ParsedMapperData parsedMapperData, int previousSuccessSchedulerLogId) {
        List<Mapper> mapperList = new ArrayList<Mapper>(parsedMapperData.getMapperList());
        List<Mapper> persistentMapperList = new ArrayList<Mapper>();
        List<Mapper> removeMapperList = new ArrayList<Mapper>();
        for (Mapper mapper : mapperList) {
            Mapper persistentMapper = tambourineConnector.getMapper(mapper.getMapperAxisType(),
                    mapper.getMappingIdDomain(), mapper.getMappingId(), mapper.getExternalId());
            if (persistentMapper != null) {
                if (persistentMapper.getMapperStartDate().after(
                        mapper.getMapperStartDate())) /*?? ?  ? */
                    persistentMapper.setMapperStartDate(mapper.getMapperStartDate());

                if (mapper.getMapperStartDate().after(persistentMapper
                        .getMapperEndDate())) /*?   ? */
                    persistentMapper.setMapperStartDate(mapper.getMapperStartDate());

                persistentMapper.setMapperEndDate(mapper.getMapperEndDate());
                persistentMapperList.add(persistentMapper);
                removeMapperList.add(mapper);
            }
        }
        mapperList.removeAll(removeMapperList);
        mapperList.addAll(persistentMapperList);

        tambourineConnector.saveMapper(mapperList, parsedMapperData.getMapperAxisType(),
                parsedMapperData.getMappingIdDomain(), null, parsedMapperData.getMapperStartDate(),
                parsedMapperData.getSyncStartDate(), previousSuccessSchedulerLogId);

        return mapperList.size();
    }

}