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

Java tutorial

Introduction

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

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

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

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

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

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

    protected MMMetaModel metaModel;

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

    public void setMetaModel(MMMetaModel metaModel) {
        this.metaModel = metaModel;
    }

    protected Long getGlobalTime() {
        return AdapterExtractorContext.getInstance().getGlobalTime();
    }

    public Object doInPreparedStatement(PreparedStatement ps) throws SQLException {
        // ?

        Map<String, String> mAttrs = metaModel.getMAttrs();
        boolean hasChildMetaModel = metaModel.isHasChildMetaModel();

        // ???
        List<AbstractMetadata> metadatas = metaModel.getMetadatas();
        int size = metadatas.size();
        String code = "";
        String metaModelCode = "";
        MMMetadata parentMetadata = null;
        String logMsg = "";
        try {
            for (int i = 0; i < size; i++) {

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

                parentMetadata = metadata.getParentMetadata();
                if (parentMetadata == null) {
                    String error = new StringBuilder("?:").append(metadata.getCode())
                            .append(" ,??!!").toString();
                    log.error(error);
                    throw new SQLException(error);
                }
                String metadataNamespace = genNamespace(parentMetadata, metadata.getId(), hasChildMetaModel);

                // ?ID
                ps.setString(1, metadata.getId());
                code = metadata.getCode();
                // ???
                ps.setString(2, code);
                // ???
                ps.setString(3,
                        (metadata.getName() == null || metadata.getName().equals("")) ? code : metadata.getName());
                // ID
                metaModelCode = metaModel.getCode();
                ps.setString(4, metaModelCode);

                // namespaceID
                ps.setString(5, metadataNamespace);
                ps.setString(6, parentMetadata.getId());
                // START_TIME: 
                ps.setLong(7, this.getGlobalTime());

                int index = setAttrs(ps, metadata, mAttrs);

                setPs(ps, metadata, index + 7);

                if (log.isDebugEnabled()) {
                    log.debug(new StringBuilder().append(":parent_id:").append(parentMetadata.getId())
                            .append(",parent_code:").append(parentMetadata.getCode()).append(",instance_code:")
                            .append(code).append(",classifier_id:").append(metaModelCode).toString());
                }
                ps.addBatch();
                // ??
                ps.clearParameters();

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

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

            }
        } catch (SQLException e) {
            logMsg = new StringBuilder().append("?,?:parent_id:")
                    .append(parentMetadata.getId()).append(",parent_code:").append(parentMetadata.getCode())
                    .append(",instance_code:").append(code).append(",classifier_id:").append(metaModelCode)
                    .append("  ?:").append(e.getLocalizedMessage()).toString();
            log.error(logMsg);
            AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.ERROR, logMsg);
            throw e;
        }
        return null;
        // test for callback
        // throw new SQLException();
    }

    protected int setAttrs(PreparedStatement ps, MMMetadata metadata, Map<String, String> mAttrs)
            throws SQLException {
        Map<String, String> attrs = getMetadataAttrPrepared(metadata);

        int index = 0;
        for (Iterator<String> iter = mAttrs.keySet().iterator(); iter.hasNext(); index++) {
            String mAttrName = iter.next();
            if (attrs.containsKey(mAttrName)) {
                // ,?
                String val = attrs.get(mAttrName) == null ? "" : attrs.get(mAttrName);
                ps.setString(index + 8, val);
            } else {
                // ?NULL
                ps.setNull(index + 8, Types.VARCHAR);
            }
        }
        return index;
    }

    /**
     * ?
     * 
     * @return map
     */
    protected Map<String, String> getMetadataAttrPrepared(MMMetadata metadata) {
        return metadata.getAttrs();
    }

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

    }

    /**
     * ????,??,?
     * 
     * @param parentId
     *            ID
     * @param metadataId
     *            ?ID
     * @param hasChildMetaModel
     *            metadataId??
     * @return
     */
    private 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;
    }

}