com.pureinfo.studio.db.cmd2srm.ref.impl.ChangeFormatter.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.studio.db.cmd2srm.ref.impl.ChangeFormatter.java

Source

/**
 * PureInfo Command
 * @(#)ChangeFormatter.java   1.0 2007-9-13
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.studio.db.cmd2srm.ref.impl;

import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.pureinfo.ark.content.ArkContentHelper;
import com.pureinfo.common.namedvalue.NamedValueHelper;
import com.pureinfo.common.namedvalue.domain.IListNamedValueMgr;
import com.pureinfo.common.namedvalue.model.ListNamedValue;
import com.pureinfo.dolphin.DolphinHelper;
import com.pureinfo.dolphin.mapping.EntityMetadata;
import com.pureinfo.dolphin.model.DolphinObject;
import com.pureinfo.dolphin.model.IObjects;
import com.pureinfo.dolphin.persister.ISession;
import com.pureinfo.dolphin.persister.IStatement;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.importer.ref.IImportorRef;
import com.pureinfo.srm.project.ProjectConstants;
import com.pureinfo.srm.project.model.Project;
import com.pureinfo.srm.project.model.ProjectFinish;

public class ChangeFormatter implements IImportorRef {

    private Logger logger = Logger.getLogger(ChangeFormatter.class);

    public Object convert(DolphinObject _old, DolphinObject _new, String _sFromProperty, String _sToProperty,
            ISession _fromSession, ISession _toSession, EntityMetadata _metadata, String _sKey, String _sToTable)
            throws PureException {

        String sNew = null;
        try {
            String sProjectModel = _old.getStrProperty(_sFromProperty);
            String[] formatter = sProjectModel.split(",");
            String[] result = new String[formatter.length];
            sNew = null;

            if (formatter.length == 1) {
                sNew = this.getListNamedValue(220, sProjectModel);
            } else {
                for (int i = 0; i < formatter.length; i++) {
                    logger.debug(formatter[i]);
                    result[i] = this.getListNamedValue(220, formatter[i]);
                    logger.debug(">>>>>>>>>>>>>" + result[i]);
                }

                for (int i = 0; i < result.length; i++) {
                    if (i == 0) {
                        sNew = result[0];
                    } else {
                        sNew = sNew + "$" + result[i];
                    }
                }
            }
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace(System.err);
        }
        logger.debug(sNew);
        return sNew;
    }

    public String getListNamedValue(int _nType, String _sName) throws PureException, UnsupportedEncodingException {
        IStatement query = null;
        IObjects objs = null;
        String sValue = null;
        ListNamedValue lv = null;
        String sResult = null;
        List objList = null;

        logger.debug(_sName);
        try {
            IListNamedValueMgr mgr = (IListNamedValueMgr) ArkContentHelper.getContentMgrOf(ListNamedValue.class);
            String sSql = "select {this.value} from {this} where {this.type} = ? and {this.name} like ?";
            query = mgr.createQuery(sSql, 0);
            query.setInt(0, 220);
            query.setString(1, _sName);
            objs = query.executeQuery();
            if (objs != null) {
                objList = objs.toList();
                if (objList != null && objList.size() > 0) {
                    Iterator iter = objList.iterator();
                    sValue = ((DolphinObject) iter.next()).getPropertyAsString("value");
                    lv = NamedValueHelper.getListNamedValue(_nType, sValue, false);
                    sResult = StringUtils.isNotEmpty(lv.getValue()) ? lv.getValue() : _sName;
                } else {
                    sResult = _sName;
                }
            }

            return sResult;
        } finally {
            DolphinHelper.clear(objs, query);
        }

    }

    public static void main(String[] args) {
        try {
            ChangeFormatter c = new ChangeFormatter();
            DolphinObject obj = new ProjectFinish();
            obj.setProperty("productModel",
                    ",,,,,,,,,,");
            String _sFromProperty = "productModel";
            String a = (String) c.convert(obj, null, _sFromProperty, null, null, null, null, null, null);
            System.out.println(a);
        } catch (PureException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace(System.err);
        }
    }
}