com.pactera.edg.am.metamanager.extractor.increment.impl.FullIncrementAnalysisServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.pactera.edg.am.metamanager.extractor.increment.impl.FullIncrementAnalysisServiceImpl.java

Source

/*
 * Copyright 2009 by pactera.edg.am Corporation. Address:HePingLi East Street No.11
 * 5-5, BeiJing,
 * 
 * All rights reserved.
 * 
 * This software is the confidential and proprietary information of pactera.edg.am
 * Corporation ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with pactera.edg.am.
 */

package com.pactera.edg.am.metamanager.extractor.increment.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.servlet.ServletException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.pactera.edg.am.metamanager.extractor.bo.mappingload.Operation;
import com.pactera.edg.am.metamanager.extractor.bo.mm.AppendMetadata;
import com.pactera.edg.am.metamanager.extractor.bo.mm.MMDDependency;
import com.pactera.edg.am.metamanager.extractor.bo.mm.MMMetaModel;
import com.pactera.edg.am.metamanager.extractor.bo.mm.MMMetadata;
import com.pactera.edg.am.metamanager.extractor.dao.IMetaModelDao;
import com.pactera.edg.am.metamanager.extractor.ex.MetaModelNotFoundException;
import com.pactera.edg.am.metamanager.extractor.increment.IGenMetadataService;
import com.pactera.edg.am.metamanager.extractor.increment.IIncrementAnalysisService;
import com.pactera.edg.am.metamanager.extractor.util.AdapterExtractorContext;

/**
 * ??,???,??.<br>
 * ?:??,???,
 * 
 * @author hqchen
 * @version 1.0 Date: Oct 4, 2009
 */
public class FullIncrementAnalysisServiceImpl implements IIncrementAnalysisService {

    private Log log = LogFactory.getLog(FullIncrementAnalysisServiceImpl.class);

    /**
     * ??DAO?
     */
    private IGenMetadataService genMetadataService;

    /**
     * ?DAO?
     */
    private IMetaModelDao metaModelDao;

    /**
     * Spring
     * 
     * @param metaModelDao
     */
    public void setMetaModelDao(IMetaModelDao metaModelDao) {
        this.metaModelDao = metaModelDao;
    }

    public IGenMetadataService getGenMetadataService() {
        return genMetadataService;
    }

    public void setGenMetadataService(IGenMetadataService genMetadataService) {
        this.genMetadataService = genMetadataService;
    }

    private final static String cgb_properties = "extractor/cgb_extractor.properties";

    private static Properties cgbPro;
    {
        InputStream in = null;
        cgbPro = new Properties();
        try {
            in = FullIncrementAnalysisServiceImpl.class.getClassLoader().getResourceAsStream(cgb_properties);
            cgbPro.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * ???,???,?,??
     * 
     * @param aNamespace
     *            ??
     * @return ?(?)
     * @throws MetaModelNotFoundException
     */
    private Set<MMMetaModel> genDeleteMetadatas(String aNamespace) throws MetaModelNotFoundException {
        // ??
        List<MMMetadata> dbMetadatas = genMetadataService.genChildrenMetadatasId("%" + aNamespace);
        if (dbMetadatas.size() == 0) {
            // ?,??
            return Collections.emptySet();
        }

        // ??
        Map<String, String> newMetadatas = AdapterExtractorContext.getInstance().getNewMetadatasId();

        // DB??
        Collections.sort(dbMetadatas, new Comparator<MMMetadata>() {
            public int compare(MMMetadata o1, MMMetadata o2) {
                return o1.getClassifierId().compareTo(o2.getClassifierId());
            }
        });

        MMMetaModel deleteMetaModel = null;
        Set<MMMetaModel> deleteMetaModels = new HashSet<MMMetaModel>();
        String metaModelCode = "";
        for (MMMetadata dbMetadata : dbMetadatas) {
            String id = dbMetadata.getId();
            if (!newMetadatas.containsKey(id)) {
                //???????
                if (!AdapterExtractorContext.getInstance().isFileUpload()
                        && cgbPro.get("is_del_codeItem").equals("1")
                        && (dbMetadata.getClassifierId().equals("AmColumnCodeItem")
                                || dbMetadata.getClassifierId().equals("AmCommonCodeItemMapping"))) {
                    String parId = genMetadataService.getParId(dbMetadata.getId());
                    if (parId != null && newMetadatas.containsKey(parId))
                        continue;
                }
                // ?,,?
                if (metaModelCode.equals("")) {
                    // 
                    metaModelCode = dbMetadata.getClassifierId();
                    deleteMetaModel = genMetaModel(metaModelCode);
                }

                if (!metaModelCode.equals(dbMetadata.getClassifierId())) {
                    // ??
                    metaModelCode = dbMetadata.getClassifierId();
                    deleteMetaModels.add(deleteMetaModel);
                    deleteMetaModel = genMetaModel(metaModelCode);
                }
                deleteMetaModel.addMetadata(dbMetadata);
                deleteMetaModel.setHasMetadata(true);
            }
        }

        if (deleteMetaModel != null) {
            deleteMetaModels.add(deleteMetaModel);
        }

        return deleteMetaModels;
    }

    private MMMetaModel genMetaModel(String metaModelCode) throws MetaModelNotFoundException {
        MMMetaModel metaModel = new MMMetaModel();
        metaModel.setCode(metaModelCode);

        if (AdapterExtractorContext.getInstance().getMetaModelsAttrs().containsKey(metaModelCode)) {
            // ?
            metaModel.setMAttrs(AdapterExtractorContext.getInstance().getMetaModelsAttrs().get(metaModelCode));
        } else {
            // ?
            metaModelDao.genStorePositionByApi(metaModel);
            AdapterExtractorContext.getInstance().addMetaModelAttrs(metaModelCode, metaModel.getMAttrs());
        }
        return metaModel;
    }

    /**
     * ???,???,?,???
     * 
     * @param aNamespace
     *            ??
     * @return ???
     */
    private List<MMDDependency> genDeleteDependencies(String aNamespace) {
        // ??
        List<MMDDependency> dbDependencies = genMetadataService.genDbDependencies("%" + aNamespace);
        if (dbDependencies == null || dbDependencies.size() == 0) {
            // ?,??
            return Collections.emptyList();
        }

        // ???,?
        List<MMDDependency> dependencies = AdapterExtractorContext.getInstance().getNewDependencies();
        if (dependencies.size() == 0) {
            // ?0,??
            return dbDependencies;
        }

        Collections.sort(dependencies);
        Collections.sort(dbDependencies);

        int newIndex = 0, dbIndex = 0, compareInt, newSize = dependencies.size(), dbSize = dbDependencies.size();
        List<MMDDependency> deleteDependencies = new ArrayList<MMDDependency>();

        while (dbIndex < dbSize && newIndex < newSize) {
            MMDDependency dbDependency = dbDependencies.get(dbIndex);
            compareInt = dbDependency.compareTo(dependencies.get(newIndex));
            if (compareInt > 0) {
                // NEW?,?
                newIndex++;
            } else if (compareInt < 0) {
                String fCId = dbDependency.getOwnerMetadata().getClassifierId();
                String tCId = dbDependency.getValueMetadata().getClassifierId();
                //???????
                if (!AdapterExtractorContext.getInstance().isFileUpload()
                        && cgbPro.get("is_del_codeItem").equals("1")
                        && (fCId.equals("AmCommonCodeItemMapping") || fCId.equals("AmCommonCodeItem")
                                || tCId.equals("AmCommonCodeItem") || tCId.equals("AmCommonCodeItemMapping"))) {
                    continue;
                }

                deleteDependencies.add(dbDependency);
                dbIndex++;
            } else {
                // ?,DB?,?NEW???,??
                newIndex++;
                dbIndex++;
            }
        }

        while (newIndex < newSize) {
            // ?,???,?!
            break;
        }
        while (dbIndex < dbSize) {
            // ?,?!
            deleteDependencies.add(dbDependencies.get(dbIndex++));
        }

        // for (dbIndex = 0, dbSize = dbDependencies.size(); dbIndex < dbSize;)
        // {
        // MMDDependency dbDependency = dbDependencies.get(dbIndex);
        //
        // if (newIndex >= newSize) {
        // // ???
        // deleteDependencies.add(dbDependency);
        // dbIndex++;
        // continue;
        // }
        //
        // for (; newIndex < newSize;) {
        // compareInt = dbDependency.compareTo(dependencies.get(newIndex));
        // if (compareInt > 0) {
        // // NEW?,?
        // newIndex++;
        // }
        // else if (compareInt < 0) {
        // deleteDependencies.add(dbDependency);
        // dbIndex++;
        // break;
        // }
        // else {
        // // ?,DB?,?NEW???,??
        // newIndex++;
        // dbIndex++;
        // break;
        // }
        // }
        // }

        return deleteDependencies;
    }

    /**
     * ????,?
     * 
     * @see com.pactera.edg.am.metamanager.extractor.increment.IIncrementAnalysisService#incrementAnalysis(com.pactera.edg.am.metamanager.extractor.bo.mm.AppendMetadata)
     */
    public Map<Operation, AppendMetadata> incrementAnalysis(AppendMetadata aMetadata) throws Throwable {

        // ?
        AppendMetadata deleteAMetadata = aMetadata.cloneAppendMetadata();

        String aNamespace = aMetadata.getMetadata().genNamespace();
        //??
        Map<String, String> newMetadatas = AdapterExtractorContext.getInstance().getNewMetadatasId();
        //??
        for (Iterator<String> sys = newMetadatas.keySet().iterator(); sys.hasNext();) {
            String id = sys.next();
            //?SchemaID
            if (newMetadatas.get(id).equals("AmModule")) {
                aNamespace = id;
            }
        }
        deleteAMetadata.setChildMetaModels(genDeleteMetadatas(aNamespace));
        deleteAMetadata.setDDependencies(genDeleteDependencies(aNamespace));

        Map<Operation, AppendMetadata> incrementMetadata = new HashMap<Operation, AppendMetadata>(1);
        incrementMetadata.put(Operation.DELETE, deleteAMetadata);
        return incrementMetadata;
    }

}