com.pureinfo.studio.db.xls2srm.ref.ResCenterMapRef.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.studio.db.xls2srm.ref.ResCenterMapRef.java

Source

/**
 * PureInfo SRM3.0
 * @(#)ResCenterMapRef.java   1.0 2007-4-10
 * 
 * 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.xls2srm.ref;

import org.apache.commons.lang.StringUtils;

import com.pureinfo.ark.content.ArkContentHelper;
import com.pureinfo.ark.content.domain.IContentMgr;
import com.pureinfo.ark.content.model.ArkContent;
import com.pureinfo.dolphin.DolphinHelper;
import com.pureinfo.dolphin.context.LocalContextHelper;
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.force.lang.StrUtil;
import com.pureinfo.importer.ref.IImportorRef;
import com.pureinfo.srm.SRMHelper;
import com.pureinfo.srm.auth.domain.ISRMUserMgr;
import com.pureinfo.srm.auth.domain.IUserOrgMapMgr;
import com.pureinfo.srm.auth.model.SRMUser;
import com.pureinfo.srm.auth.model.UserOrgMap;
import com.pureinfo.srm.org.OrganizationConstants;
import com.pureinfo.srm.org.model.Institute;
import com.pureinfo.srm.org.model.ResearchCenter;
import com.pureinfo.srm.xls2srm.model.Xls2srmForm;

/**
 * <P>
 * Created on 2007-4-10 11:37:47<BR>
 * Last modified on 2007-4-10
 * </P>
 * TODO describe ResCenterMapRef here ...
 * 
 * @author Administrator
 * @version 1.0, 2007-4-10
 * @since SRM3.0 1.0 
 */
public class ResCenterMapRef implements IImportorRef {

    public Object convert(DolphinObject _old, DolphinObject _new, String _sFromProperty, String _sToProperty,
            ISession _fromSession, ISession _toSession, EntityMetadata _metadata, String _sKey, String _sToTable)
            throws PureException {
        int nImportType = Integer.parseInt(_sToProperty);

        //   
        if (Xls2srmForm.JUSTFORCHECK == nImportType)
            return null;

        String sValue = _old.getStrProperty(_sFromProperty);
        int nOrgType = 1;
        if ("".equals(_sFromProperty))
            nOrgType = OrganizationConstants.ORG_TYPE_LAB;
        else
            nOrgType = OrganizationConstants.ORG_TYPE_RESERCH_CENTER;

        int nUserId = 0;
        // 
        if (Xls2srmForm.COVERDATAWHENREPEAT == nImportType || Xls2srmForm.LEAPDATAWHENREPEAT == nImportType) {
            if (sValue != null && sValue.trim().length() > 0) {
                //              _sKey*.xls.xml<match-properties/>;
                if (_sKey != null && _sKey.trim().length() > 0) {
                    StringBuffer sbuff = new StringBuffer();
                    String[] sPropertyNames = StrUtil.split(_sKey, ",");

                    ISRMUserMgr mgr = (ISRMUserMgr) ArkContentHelper.getContentMgrOf(SRMUser.class);
                    for (int i = 0; i < sPropertyNames.length; i++) {
                        sbuff.append(" AND {this.").append(sPropertyNames[i]).append("} = '");
                        sbuff.append(_new.getProperty(sPropertyNames[i])).append('\'');
                    }
                    String strSQL = sbuff.toString();
                    if (sbuff.length() > 5)
                        strSQL = strSQL.substring(5);
                    IStatement stat = null;
                    IObjects results = null;
                    try {
                        stat = mgr.createQuery("SELECT {this.id} FROM {this} WHERE " + strSQL, 1);
                        results = stat.executeQuery(false);
                        SRMUser user = (SRMUser) results.next();
                        results.clear();
                        stat.clear(false);

                        if (user != null) {
                            nUserId = user.getId();
                            if (Xls2srmForm.COVERDATAWHENREPEAT == nImportType) {
                                SRMHelper.deleteAllOf(UserOrgMap.class, "userId", nUserId);
                            }
                        }
                    } finally {
                        if (sbuff != null)
                            sbuff.setLength(0);
                        DolphinHelper.clear(results, stat);
                    }
                }
            }
        }
        // 
        if (sValue != null) {
            // 
            if (!(Xls2srmForm.LEAPDATAWHENREPEAT == nImportType && nUserId > 0)) {

                String[] names = StringUtils.split(sValue, ",");

                IUserOrgMapMgr mapMgr;
                try {
                    mapMgr = (IUserOrgMapMgr) ArkContentHelper.getContentMgrOf(UserOrgMap.class);
                    LocalContextHelper.currentSession().autoGenerateProperty(_new, "id");
                } catch (PureException e) {
                    e.printStackTrace();
                    return null;
                }
                int nId = _new.getIntProperty("id", -1);

                if (Xls2srmForm.COVERDATAWHENREPEAT == nImportType) {
                    if (nUserId > 0) {
                        nId = nUserId;
                    }
                }

                IContentMgr mgr = null;
                if (nOrgType == OrganizationConstants.ORG_TYPE_LAB) {
                    mgr = ArkContentHelper.getContentMgrOf(Institute.class);
                } else {
                    mgr = ArkContentHelper.getContentMgrOf(ResearchCenter.class);
                }
                for (int i = 0; i < names.length; i++) {
                    String sName = names[i].trim();
                    UserOrgMap uo = new UserOrgMap();
                    uo.setUserId(nId);
                    uo.setOrgType(nOrgType);
                    uo.setCreateUser("system");
                    try {
                        ArkContent obj;
                        IObjects result = null;
                        IStatement query = null;
                        try {
                            String strSQL;
                            if (nOrgType == OrganizationConstants.ORG_TYPE_LAB) {
                                strSQL = "SELECT * FROM {this} WHERE {this.name}=? AND {this.type}=1";
                            } else {
                                strSQL = "SELECT * FROM {this} WHERE {this.name}=?";
                            }
                            query = mgr.createQuery(strSQL, 1);
                            query.setString(0, sName);
                            result = query.executeQuery();
                            obj = (ArkContent) result.next();
                        } finally {
                            DolphinHelper.clear(result, query);
                        }

                        if (obj != null) {
                            uo.setOrgId(obj.getId());
                        } else {
                            uo.setOrgId(0);
                        }
                        mapMgr.save(uo);
                    } catch (PureException e) {
                        e.printStackTrace(System.err);
                    }
                }
                return null;
            }
        }
        return null;
    }
}