com.pactera.edg.am.metamanager.extractor.dao.helper.CreateMetadataAlterHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.pactera.edg.am.metamanager.extractor.dao.helper.CreateMetadataAlterHelper.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.dao.helper;

import java.sql.PreparedStatement;
import java.sql.SQLException;
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 org.springframework.dao.DataAccessException;

import com.pactera.edg.am.metamanager.extractor.bo.mm.AbstractMetadata;
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.ISequenceDao;
import com.pactera.edg.am.metamanager.extractor.util.AdapterExtractorContext;
import com.pactera.edg.am.metamanager.extractor.util.Constants;

/**
 * ????
 * 
 * @author hqchen
 * @version 1.0 Date: Oct 9, 2009
 */
public class CreateMetadataAlterHelper extends PreparedStatementCallbackHelper {

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

    protected Set<MMMetaModel> metaModels;

    protected ISequenceDao sequenceDao;

    protected String taskInstanceId;

    // ID
    protected String userId;

    protected long startTime;

    /**
     * ?ID??,key:?ID,value:namespace
     */
    protected Map<String, String> namespaceCache = new HashMap<String, String>();

    /**
     * ID?
     * @param sequenceDao
     */
    public void setSequenceDao(ISequenceDao sequenceDao) {
        this.sequenceDao = sequenceDao;
    }

    public void setMetaModels(Set<MMMetaModel> metaModels) {
        this.metaModels = metaModels;
    }

    public CreateMetadataAlterHelper(int batchSize) {
        super(batchSize);
        userId = AdapterExtractorContext.getInstance().getUserId();
    }

    public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
        startTime = AdapterExtractorContext.getInstance().getGlobalTime();
        taskInstanceId = AdapterExtractorContext.getInstance().getTaskInstanceId();

        for (MMMetaModel metaModel : metaModels) {
            batchLoadCreate(ps, metaModel);
        }

        if (super.count % super.batchSize != 0) {
            ps.executeBatch();
            ps.clearBatch();

        }
        return null;
    }

    private void batchLoadCreate(PreparedStatement ps, MMMetaModel metaModel) throws SQLException {
        if (metaModel.isHasMetadata()) {
            // ?
            List<AbstractMetadata> metadatas = metaModel.getMetadatas();
            doInPreparedStatement(ps, metaModel.getCode(), metaModel.isHasChildMetaModel(), metadatas);
        }

        if (metaModel.isHasChildMetaModel()) {
            // ?
            for (MMMetaModel childMetaModel : metaModel.getChildMetaModels()) {
                batchLoadCreate(ps, childMetaModel);
            }
        }

    }

    protected void doInPreparedStatement(PreparedStatement ps, String metaModelCode, boolean hasChildMetaModel,
            List<AbstractMetadata> metadatas) throws SQLException {
        try {
            for (AbstractMetadata metadata : metadatas) {
                // ?ID
                String sequenceId = sequenceDao.getUuid();
                ps.setString(1, sequenceId);

                // ID
                ps.setString(3, taskInstanceId);
                // ?ID
                ps.setString(4, metadata.getId());
                // 
                ps.setString(5, metaModelCode);
                // ID
                ps.setString(7, userId);

                // : ALTERATION_TIME
                ps.setLong(9, startTime);

                // ? ? 2010-05-18 fbchen
                //ps.setString(3, genAttrs(metadata));

                setPs(ps, metadata, metaModelCode, hasChildMetaModel);

                String parentId = metadata.getParentMetadata().getId();
                if (parentId == null || parentId.equals("")) {
                    parentId = "0";
                }
                ps.setString(11, parentId);
                ps.addBatch();
                ps.clearParameters();

                if (++super.count % super.batchSize == 0) {
                    ps.executeBatch();
                    ps.clearBatch();
                }

            }
        } catch (SQLException e) {
            // ??,????,,??
            log.warn("??!", e);
        }

    }

    protected void setPs(PreparedStatement ps, AbstractMetadata metadata, String metaModelCode,
            boolean hasChildMetaModel) throws SQLException {
        // ?,0
        ps.setString(2, "0");

        // ??
        String namespace = metadata.getNamespace();
        ps.setString(6,
                namespace == null ? genNamespace(metadata.getParentMetadata(), metadata.getId(), hasChildMetaModel)
                        : namespace);

        // ?START_TIME
        // ?startTime
        ps.setLong(8, startTime); //metadata.getStartTime());

        // OLD_START_TIME ???OLD_START_TIME??
        ps.setNull(10, java.sql.Types.BIGINT);
    }

    /**
     * ????,??,?
     * 
     * @param parentId
     *            ID
     * @param metadataId
     *            ?ID
     * @param hasChildMetaModel
     *            metadataId??
     * @return
     */
    protected String genNamespace(MMMetadata parentMetadata, String metadataId, boolean hasChildMetaModel) {
        String parentId = parentMetadata.getId();
        String parentNamespace = "";
        if (parentId == null || "".equals(parentId)) { // ,??"/"
            parentNamespace = Constants.METADATA_PATH_SEPARATOR;
        } else if (namespaceCache.containsKey(parentId)) {
            if (namespaceCache.get(parentId).equals(Constants.METADATA_PATH_SEPARATOR)) {
                // ?,??,?"/","/"
                parentNamespace = Constants.METADATA_PATH_SEPARATOR + parentId;
            } else { // ??value/parentId
                parentNamespace = new StringBuilder().append(namespaceCache.get(parentId))
                        .append(Constants.METADATA_PATH_SEPARATOR).append(parentId).toString();

            }
        } else {
            parentNamespace = parentMetadata.genNamespace();
        }
        if (hasChildMetaModel) {
            // metadataId??,
            namespaceCache.put(metadataId, parentNamespace);
        }

        String metadataNamespace = "";
        // ???"/"
        if (parentNamespace.equals(Constants.METADATA_PATH_SEPARATOR)) {
            metadataNamespace = parentNamespace + metadataId;
        } else {
            metadataNamespace = new StringBuilder().append(parentNamespace)
                    .append(Constants.METADATA_PATH_SEPARATOR).append(metadataId).toString();
        }
        return metadataNamespace;
    }

}