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

Java tutorial

Introduction

Here is the source code for com.pactera.edg.am.metamanager.extractor.dao.helper.CreateCompositionHelper.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.Iterator;
import java.util.List;
import java.util.Set;

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

import com.pactera.edg.am.metamanager.core.util.DateUtil;
import com.pactera.edg.am.metamanager.extractor.bo.ExtractorLogLevel;
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.util.AdapterExtractorContext;

/**
 * ???PreparedStatementCallback
 * 
 * @author hqchen
 * @version 1.0 Date: Aug 21, 2009
 */
public class CreateCompositionHelper extends PreparedStatementCallbackHelper {

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

    private Set<MMMetaModel> metaModels;

    public CreateCompositionHelper(int batchSize) {
        super(batchSize);
    }

    public Object doInPreparedStatement(PreparedStatement ps) throws SQLException {
        String startTime = DateUtil.getFormatTime(System.currentTimeMillis(), DateUtil.DATA_FORMAT);

        for (Iterator<MMMetaModel> iter = metaModels.iterator(); iter.hasNext();) {
            // ??
            batchLoadCreate(iter.next(), ps);
        }
        // ??,???

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

        }

        String logMsg = new StringBuilder(getMsg()).append(count).append(",:").append(startTime)
                .append(",?:")
                .append(DateUtil.getFormatTime(System.currentTimeMillis(), DateUtil.DATA_FORMAT)).toString();
        log.info(logMsg);
        AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.INFO, logMsg);

        return null;
        // test for callback
        // throw new SQLException();
    }

    protected String getMsg() {
        return "??:";
    }

    private void batchLoadCreate(final MMMetaModel metaModel, PreparedStatement ps) throws SQLException {

        if (metaModel.isHasMetadata()) {
            // ?
            doInPreparedStatement(metaModel, ps);
        }

        if (metaModel.isHasChildMetaModel()) {
            // ?
            Set<MMMetaModel> childMetaModels = metaModel.getChildMetaModels();
            for (MMMetaModel childMetaModel : childMetaModels) {
                batchLoadCreate(childMetaModel, ps);
            }
        }

    }

    private void doInPreparedStatement(MMMetaModel metaModel, PreparedStatement ps) throws SQLException {
        List<AbstractMetadata> metadatas = metaModel.getMetadatas();
        String parentMetaModelId = metaModel.getParentMetaModel().getCode();
        String metaModelId = metaModel.getCode();
        String relationshipName = metaModel.getCompedRelationCode();
        long sysTime = AdapterExtractorContext.getInstance().getGlobalTime();

        for (int i = 0, size = metadatas.size(); i < size; i++) {

            AbstractMetadata metadata = metadatas.get(i);
            if (metadata.isHasExist()) {
                // ??,???
                continue;
            }

            // ?ID
            ps.setString(1, metadata.getParentMetadata().getId());
            // ?ID
            ps.setString(2, parentMetaModelId);
            // ?ID
            ps.setString(3, metadata.getId());
            // ?ID
            ps.setString(4, metaModelId);
            // ???
            ps.setString(5, relationshipName);
            // 
            ps.setLong(6, sysTime);

            setPs(ps, 6);

            ps.addBatch();
            ps.clearParameters();

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

    }

    protected void setPs(PreparedStatement ps, int index) throws SQLException {

    }

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