com.pureinfo.srm.sci.helper.SCIMappingHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.srm.sci.helper.SCIMappingHelper.java

Source

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

package com.pureinfo.srm.sci.helper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Element;

import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.exception.PureRuntimeException;
import com.pureinfo.force.io.ClassResourceUtil;
import com.pureinfo.force.xml.XMLUtil;
import com.pureinfo.srm.SRMExceptionTypes;
import com.pureinfo.srm.sci.model.SCIDeptObj;

/**
 * <P>
 * Created on 2007-7-19 03:20:23<BR>
 * Last modified on 2007-7-19
 * </P>
 * SCIMappingHelper
 * 
 * @author sunjie
 * @version 1.0, 2007-7-19
 * @since Command 1.0
 */
public class SCIMappingHelper {
    private static SCIMappingHelper s_instance = new SCIMappingHelper();

    private boolean m_bLoaded = false;

    private final String m_sConfigFileName = "school-sci-import-dept-mapping.xml";

    private SCIDeptObj m_deptObj;

    private Map map = new HashMap();

    private List all = new ArrayList();

    private String str = "";

    /**
     * Returns the all.
     * 
     * @return the all.
     */
    public List getAll() {
        return this.all;
    }

    public static SCIMappingHelper getInstance() {
        if (!s_instance.m_bLoaded) {
            s_instance.insureLoaded();
        }
        return s_instance;
    }

    /**
     * 
     */
    private void insureLoaded() {
        if (m_bLoaded)
            return;

        synchronized (this) {
            if (m_bLoaded)
                return;
            this.init();
            m_bLoaded = true;
        }
    }

    private void init() {
        try {
            String sResource = ClassResourceUtil.mapFullPath(m_sConfigFileName, true);
            Element root = XMLUtil.fileToElement(sResource);
            Element element;
            for (Iterator iter = root.elementIterator("dept"); iter.hasNext();) {
                element = (Element) iter.next();

                m_deptObj = new SCIDeptObj();
                m_deptObj.fromXML(element);
                map.put(m_deptObj.getCNName(), m_deptObj);
                all.add(m_deptObj);
            }

        } catch (Exception ex) {
            throw new PureRuntimeException(SRMExceptionTypes.CONFIG_LOAD, m_sConfigFileName, ex);
        }

    }

    /**
     * @param _sENName
     *            english name
     */
    public static String getCNName(String _sENName) {
        List list = getInstance().getAll();
        SCIDeptObj element;
        Map map;
        String result = null;
        for (Iterator iter = list.iterator(); iter.hasNext();) {
            element = (SCIDeptObj) iter.next();
            map = element.getHENNameMap();
            result = (String) map.get(_sENName.toLowerCase());
            if (result == null || result.length() == 0)
                continue;
            break;
        }
        return result;
    }

    public String isContainSchool(String _sName) throws PureException {
        List list = getInstance().getAll();
        String sEnglishNames = null;
        for (Iterator iter = list.iterator(); iter.hasNext();) {
            SCIDeptObj obj = (SCIDeptObj) iter.next();
            String sName = obj.getCNName();
            if (sName.equalsIgnoreCase(_sName)) {
                sEnglishNames = obj.getEnglishNames();
                break;
            }
        }
        return sEnglishNames;
    }

    public static void main(String[] args) {
        try {
            String s = SCIMappingHelper.getInstance().isContainSchool("");
            System.out.println(s);
            String[] a = s.split("\\|");
            System.out.println(a.length);
            for (int i = 0; i < a.length; i++) {
                System.out.println("---" + a[i]);
            }
        } catch (PureException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace(System.err);
        }
    }
}