com.pureinfo.srm.reports.table.data.institute.Index3Statitistic.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.srm.reports.table.data.institute.Index3Statitistic.java

Source

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

package com.pureinfo.srm.reports.table.data.institute;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;

import com.pureinfo.ark.content.ArkContentHelper;
import com.pureinfo.dolphin.MultipleObjects;
import com.pureinfo.dolphin.model.DolphinObject;
import com.pureinfo.dolphin.model.IObjects;
import com.pureinfo.dolphin.query.SQLOperator;
import com.pureinfo.dolphin.query.logic.ISQLLogic;
import com.pureinfo.dolphin.query.logic.SQLComparison;
import com.pureinfo.dolphin.query.logic.SQLCondition;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.object.DataTypes;
import com.pureinfo.srm.SRMConstants;
import com.pureinfo.srm.org.domain.IOrganizationMgr;
import com.pureinfo.srm.org.model.Organization;
import com.pureinfo.srm.product.ProductConstants;
import com.pureinfo.srm.product.domain.IProductMgr;
import com.pureinfo.srm.product.model.Product;

/**
 * <P>
 * Created on 2007-7-12 16:37:03 <BR>
 * Last modified on 2007-7-12
 * </P>
 * TODO describe ArrivedOutlayStatitistic here ...
 * 
 * @author elmar.chen
 * @version 1.0, 2007-7-12
 * @since Command 1.0
 */
public class Index3Statitistic extends InstituteReportBase {

    // private static Logger logger = Logger.getLogger(Index3Statitistic.class);

    public static final DecimalFormat NUM_FORMAT = new DecimalFormat("#.###");

    private static final int TABEL_WIDTH = 11;

    private static final int TABLE_SCI_WIDTH = 7;

    private static final int TABLE_EI_WIDTH = 4;

    private static final int TABLE_ISTP_WIDTH = 4;

    private static final Integer IDX_OTHER = new Integer(Integer.MAX_VALUE);

    private static final Integer IDX_TOTAL = new Integer(Integer.MAX_VALUE - 1);

    private static final int IDX_SCI = 1;

    private static final int IDX_SCI_TOTAL = 2;

    private static final int IDX_SCI_AWARD = 3;

    private static final int IDX_SCI_TOP = 4;

    private static final int IDX_SCI_3KIND_TOTAL = 5;

    private static final int IDX_SCI_3KIND_IF = 6;

    private static final int IDX_EI = 7;

    private static final int IDX_EI_TOTAL = 7;

    private static final int IDX_EI_AWARD = 8;

    private static final int IDX_ISTP = 9;

    private static final int IDX_ISTP_TOTAL = 9;

    private static final int IDX_ISTP_AWARD = 10;

    private static final String PARAM_TABLE = "table";

    private static final String PARAM_YEAR_START = "year_start";

    private static final String PARAM_YEAR_END = "year_end";

    private static final String DEPARTMENT = "_DEPARTMENT";

    private static final String INDEX_FORM = "_INDEX_FORM";

    private static final String ISTOPPAPER = "_ISTOPPAPER";

    private static final String PAPERSORT = "_PAPERSORT";

    private static final String COUNTID = "_COUNT_ID";

    private static final String ISSCHOOLHONOR = "_ISSCHOOLHONOR";

    private static final String SUMIMPACTFACTORNUM = "SUMIMPACTFACTORNUM";

    private static final int TITLE_HEIGHT = 3;

    private Map m_institutes = new TreeMap();

    private Map m_collges = new TreeMap();

    private Map m_colInsMap = new TreeMap();

    private Map m_InsColMap = new TreeMap();

    private int m_nTableHight;

    private Map m_orgIdxes;

    private boolean m_isNextQueryFormal;

    // 0:sci,1:ei,2:istp
    private boolean[] needShow = { false, false, false };

    /**
     * @see com.pureinfo.srm.reports.table.data.institute.InstituteReportBase#getMyCondition()
     */
    protected ISQLLogic getMyCondition() {
        SQLCondition condition = new SQLCondition();
        condition.append("{this.productForm}='3'");
        if (getParamValue(PARAM_YEAR_START, 0) != null) {
            condition.append(new SQLComparison("{this.indexIndexDate}", SQLOperator.GREATER_OR_EQUAL, DataTypes.INT,
                    Integer.valueOf(getParamValue(PARAM_YEAR_START, 0))));
        }
        if (getParamValue(PARAM_YEAR_END, 0) != null) {
            condition.append(new SQLComparison("{this.indexIndexDate}", SQLOperator.LESS_OR_EQUAL, DataTypes.INT,
                    Integer.valueOf(getParamValue(PARAM_YEAR_END, 0))));
        }
        return condition;
    }

    /**
     * @see com.pureinfo.srm.reports.table.data.institute.InstituteReportBase#getSelects()
     */
    protected Map getSelects() {
        Map map = new HashMap();
        map.put(DEPARTMENT, "{this.department}");
        map.put(INDEX_FORM, "{this.indexForm}");
        map.put(ISSCHOOLHONOR, "{this.isSchoolHonor}");
        map.put(ISTOPPAPER, "{this.isTopPaper}");
        map.put(PAPERSORT, "{this.paperSort}");
        map.put(COUNTID, "count({this.id})");
        map.put(SUMIMPACTFACTORNUM, "sum({this.ImpactFactorNum})");
        return map;
    }

    /**
     * @see com.pureinfo.srm.reports.table.data.institute.InstituteReportBase#getTables()
     */
    protected String[] getTables() {
        return new String[] { "product" };
    }

    /**
     * @see com.pureinfo.srm.reports.table.data.institute.InstituteReportBase#getCols()
     */
    protected LinkedHashMap getCols() {
        throw new RuntimeException("unimpleted method.");
    }

    /**
     * @throws PureException
     * @see com.pureinfo.srm.reports.table.data.institute.InstituteReportBase#myMakeCell(com.pureinfo.dolphin.model.DolphinObject,
     *      java.lang.String)
     */
    protected Object myMakeCell(DolphinObject _sData, String _sColKey) throws PureException {
        throw new RuntimeException("unimpleted method.");
    }

    /**
     * @see com.pureinfo.srm.reports.table.data.institute.IInstituteDataBuilder#descParam()
     */
    public ParamDesc descParam() {
        return new ParamDesc() {
            public String getDateProperty() {
                return "{this.arriveDate}";
            }

            public String getDateDesc() {
                return "";
            }

            public boolean isDateYearOnly() {
                return true;
            }

            public String getDepartmentProperty() {
                return null;
            }
        };
    }

    /**
     * @see com.pureinfo.srm.reports.table.data.institute.IInstituteDataBuilder#getTitle()
     */
    public String getTitle() {
        return "";
    }

    /**
     * @see com.pureinfo.srm.reports.table.data.institute.InstituteReportBase#buildDatas(boolean,
     *      boolean)
     */
    public Object[][] buildDatas(boolean _bAscOrder, boolean _bOrderByValue) throws PureException {
        IObjects results = doMyQuery();
        initOrgDatas();
        Object[][] datas = new Object[m_nTableHight][TABEL_WIDTH];

        for (int i = 0; i < TITLE_HEIGHT; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                datas[i][j] = "";
            }
        }
        datas[0][0] = "";
        datas[0][IDX_SCI] = "SCI";
        datas[0][IDX_EI] = "EI";
        datas[0][IDX_ISTP] = "ISTP";
        datas[1] = new String[] { "", "", "", "", "TOP", "", "", "",
                "", "", "" };
        for (int i = 0; i < datas[2].length; i++) {
            datas[2][i] = null;
        }
        datas[2][IDX_SCI_3KIND_TOTAL] = "";
        datas[2][IDX_SCI_3KIND_IF] = "IF";

        datas[m_nTableHight - 1][0] = "";
        datas[m_nTableHight - 2][0] = "";
        DolphinObject result = null;
        for (Iterator iter = m_collges.entrySet().iterator(); iter.hasNext();) {
            Map.Entry en = (Map.Entry) iter.next();
            Integer iId = (Integer) en.getKey();
            Organization org = (Organization) en.getValue();
            Integer idxCol = (Integer) m_orgIdxes.get(iId);
            Integer idxColOther = (Integer) m_orgIdxes.get(new Integer(-iId.intValue()));
            datas[idxCol.intValue()][0] = org.getCode() + "";
            datas[idxColOther.intValue()][0] = org.getCode() + "";
        }
        for (Iterator iter = m_institutes.entrySet().iterator(); iter.hasNext();) {
            Map.Entry en = (Map.Entry) iter.next();
            Integer iId = (Integer) en.getKey();
            Organization org = (Organization) en.getValue();
            Integer idxIns = (Integer) m_orgIdxes.get(iId);
            datas[idxIns.intValue()][0] = org.getCode();
            datas[idxIns.intValue()][1] = org.getName();
        }
        while ((result = results.next()) != null) {
            Integer iDept = result.getIntegerProperty(DEPARTMENT);
            if (m_institutes.containsKey(iDept)) {
                Integer iIdxIns = (Integer) m_orgIdxes.get(iDept);
                Integer iCol = (Integer) m_InsColMap.get(iDept);
                Integer iIdxCol = (Integer) m_orgIdxes.get(iCol);
                add(datas[iIdxIns.intValue()], result);
                add(datas[iIdxCol.intValue()], result);

            } else if (m_collges.containsKey(iDept)) {
                Integer iIdxIns = (Integer) m_orgIdxes.get(new Integer(-iDept.intValue()));
                Integer iIdxCol = (Integer) m_orgIdxes.get(iDept);
                add(datas[iIdxIns.intValue()], result);
                add(datas[iIdxCol.intValue()], result);
            } else {
                Integer iIdxCol = (Integer) m_orgIdxes.get(IDX_OTHER);
                add(datas[iIdxCol.intValue()], result);
            }
            Integer iIdxCol = (Integer) m_orgIdxes.get(IDX_TOTAL);
            add(datas[iIdxCol.intValue()], result);
        }

        for (int i = TITLE_HEIGHT; i < datas.length; i++) {
            if (datas[i][IDX_SCI_3KIND_IF] == null)
                continue;
            if (datas[i][IDX_SCI_3KIND_TOTAL] == null) {
                datas[i][IDX_SCI_3KIND_IF] = null;
                continue;
            }
            Number iTotal = (Number) datas[i][IDX_SCI_3KIND_TOTAL];
            Double oSum = (Double) datas[i][IDX_SCI_3KIND_IF];
            if (iTotal.intValue() == 0) {
                datas[i][IDX_SCI_3KIND_IF] = null;
            } else {
                datas[i][IDX_SCI_3KIND_IF] = NUM_FORMAT.format(oSum.doubleValue() / iTotal.intValue());
            }
        }
        return datas;
    }

    private IObjects doMyQuery() throws PureException {
        String[] tables = request.getParameterValues(PARAM_TABLE);
        boolean queryTemp = false;
        boolean queryFormal = false;

        if (tables != null) {
            for (int i = 0; i < tables.length; i++) {
                if ("formal".equals(tables[i])) {
                    queryFormal = true;
                } else if ("temp".equals(tables[i])) {
                    queryTemp = true;
                }
            }
        }
        IObjects[] objses = new IObjects[(queryTemp ? 1 : 0) + (queryFormal ? 1 : 0)];
        int i = 0;
        if (queryFormal) {
            m_isNextQueryFormal = true;
            objses[i++] = doQuery();
        }
        if (queryTemp) {
            m_isNextQueryFormal = false;
            objses[i++] = doQuery();
        }
        MultipleObjects objs = new MultipleObjects(objses);
        return objs;
    }

    private void add(Object[] _line, DolphinObject _result) {
        Number oCount = (Number) _result.getProperty(COUNTID);
        Number oSum = (Number) _result.getProperty(SUMIMPACTFACTORNUM);
        if (ProductConstants.INDEX_FORM_SCI.equals(_result.getProperty(INDEX_FORM))) {
            addInt(_line, IDX_SCI_TOTAL, oCount);
            if (Boolean.TRUE.equals(_result.getProperty(ISSCHOOLHONOR))) {
                addInt(_line, IDX_SCI_AWARD, oCount);
            }
            if (Boolean.TRUE.equals(_result.getProperty(ISTOPPAPER))) {
                addInt(_line, IDX_SCI_TOP, oCount);
            }
            String sPaperSort = _result.getStrProperty(PAPERSORT);
            if (ProductConstants.PAPER_SORG_ARTICLE.equals(sPaperSort)
                    || ProductConstants.PAPER_SORG_REVIEW.equals(sPaperSort)
                    || ProductConstants.PAPER_SORG_LETTER.equals(sPaperSort)) {
                addInt(_line, IDX_SCI_3KIND_TOTAL, oCount);
                addDouble(_line, IDX_SCI_3KIND_IF, oSum);
            }
        } else if (ProductConstants.INDEX_FORM_EI.equals(_result.getProperty(INDEX_FORM))) {
            addInt(_line, IDX_EI_TOTAL, oCount);
            if (Boolean.TRUE.equals(_result.getProperty(ISSCHOOLHONOR))) {
                addInt(_line, IDX_EI_AWARD, oCount);
            }
        } else if (ProductConstants.INDEX_FORM_ISTP.equals(_result.getProperty(INDEX_FORM))) {
            addInt(_line, IDX_ISTP_TOTAL, oCount);
            if (Boolean.TRUE.equals(_result.getProperty(ISSCHOOLHONOR))) {
                addInt(_line, IDX_ISTP_AWARD, oCount);
            }
        }
    }

    private void addInt(Object[] _line, int _nIdx, Number _oValue) {
        if (_oValue == null)
            return;
        if (_line[_nIdx] == null || null == _line[_nIdx]) {
            _line[_nIdx] = _oValue;
        } else {
            Number num = (Number) _line[_nIdx];
            _line[_nIdx] = new Integer(_oValue.intValue() + num.intValue());
        }
    }

    private void addDouble(Object[] _line, int _nIdx, Number _oValue) {
        if (_oValue == null)
            return;
        if (_line[_nIdx] == null || null == _line[_nIdx]) {
            _line[_nIdx] = _oValue;
        } else {
            Number num = (Number) _line[_nIdx];
            _line[_nIdx] = new Double(_oValue.doubleValue() + num.doubleValue());
        }
    }

    private void initOrgDatas() throws PureException {
        IOrganizationMgr mgr = (IOrganizationMgr) ArkContentHelper.getContentMgrOf(Organization.class);
        List orgs = mgr.findAllByType(SRMConstants.ORG_TYPE.INSTITUTE, false);
        for (Iterator iter = orgs.iterator(); iter.hasNext();) {
            Organization ins = (Organization) iter.next();
            Organization col = ins.getCollege();
            addMap(ins, col);
        }
        m_orgIdxes = new HashMap(m_collges.size() * 2 + m_institutes.size());
        int i = TITLE_HEIGHT;
        for (Iterator iter = m_colInsMap.entrySet().iterator(); iter.hasNext();) {
            Map.Entry en = (Map.Entry) iter.next();
            List subs = (List) en.getValue();
            for (Iterator iterator = subs.iterator(); iterator.hasNext();) {
                m_orgIdxes.put(iterator.next(), new Integer(i++));
            }
            Integer iColId = (Integer) en.getKey();
            m_orgIdxes.put(new Integer(-iColId.intValue()), new Integer(i++));
            m_orgIdxes.put(iColId, new Integer(i++));
            i++;
        }
        m_orgIdxes.put(IDX_OTHER, new Integer(i++));
        m_orgIdxes.put(IDX_TOTAL, new Integer(i++));
        m_nTableHight = i;
    }

    private void addMap(Organization _ins, Organization _col) {
        Integer iIns = new Integer(_ins.getId());
        m_institutes.put(iIns, _ins);
        Integer iCol = new Integer(_col.getId());
        m_collges.put(iCol, _col);
        m_InsColMap.put(iIns, iCol);
        List inses = (List) m_colInsMap.get(iCol);
        if (inses == null) {
            m_colInsMap.put(iCol, inses = new ArrayList());
        }
        inses.add(iIns);
    }

    public static void main(String[] args) throws PureException {
        Object[][] datas = new Index3Statitistic().buildDatas(false, false);
        for (int i = 0; i < datas.length; i++) {
            System.out.println(StringUtils.join(datas[i], "\t"));
        }
    }

    /**
     * @see com.pureinfo.srm.reports.table.data.institute.InstituteReportBase#getGroupBy()
     */
    protected String getGroupBy() {
        return StringUtils.join(new String[] { DEPARTMENT, ISTOPPAPER, PAPERSORT, INDEX_FORM }, ",");
    }

    protected String getFromStr() throws PureException {
        if (m_isNextQueryFormal) {
            return "{this}";
        }
        IProductMgr mgr = (IProductMgr) ArkContentHelper.getContentMgrOf(Product.class);
        return mgr.getTempTable(true) + " this";
    }
}