Java tutorial
/** * PureInfo Command2 * @(#)Index3SCIStatistic.java 1.0 2007-12-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.reports.table.data; import java.text.ParseException; import java.util.Date; import java.util.HashMap; import java.util.Iterator; 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.model.IObjects; import com.pureinfo.dolphin.persister.IStatement; import com.pureinfo.force.exception.PureException; import com.pureinfo.srm.SRMConstants; import com.pureinfo.srm.org.domain.IOrganizationMgr; import com.pureinfo.srm.org.model.Organization; import com.pureinfo.srm.product.domain.IProductMgr; import com.pureinfo.srm.product.model.Product; import com.pureinfo.srm.reports.table.ITableDatabuilder; import com.pureinfo.srm.reports.table.data.weight.WeightStatistic; /** * <P> * Created on 2007-12-19 01:55:04<BR> * Last modified on 2007-12-19 * </P> * TODO describe Index3SCIStatistic here ... * * @author opensky.ma * @version 1.0, 2007-12-19 * @since Command2 1.0 */ public class Index3SCIStatistic implements ITableDatabuilder { private IOrganizationMgr mgr; private IProductMgr productMgr; private Map m_codeRowIndexMap = new TreeMap(); private Map m_codeNameMap = new TreeMap(); private Map m_yearColIndexMap = new TreeMap(); private int m_otherRowIndex; private int m_totalRowIndex; private int m_rowOtherColIndex; private static final int TITLE_HEIGHT = 1; private static final int LABEL_WIDTH = 1; private static final int COL_IDX_TITLE = 0; private int m_nStartYear; private int m_nEndYear; private int m_nColLength; private String m_sAudit; private String m_sUnAudit; private String m_sAll; private String m_sIsFirst; private int m_nStart; private int m_nEnd; private IObjects m_3Indexes; private static final String SUM = "_SUM"; private static final String COLLEGE = "_COLLEGE"; private static final String INDEX_DATE = "_INDEX_INDEX_DATE"; private Object[][] m_datas; /** * @see com.pureinfo.srm.reports.table.ITableDatabuilder#buildDatas(boolean, * boolean) */ public Object[][] buildDatas(boolean _bAscOrder, boolean _bOrderByValue) throws PureException { //title&label initObjects(); // doQuery(); // renderData(); return m_datas; } private void renderData() throws PureException { for (Iterator iter = m_3Indexes.iterator(); iter.hasNext();) { Product product = (Product) iter.next(); setUpData(product); } } /** * product * @param _product * @throws PureException */ private void setUpData(Product _product) throws PureException { int nCollegeId = _product.getIntProperty(COLLEGE, 0); int nSum = _product.getIntProperty(SUM, 0); Integer sDate = _product.getIntegerProperty(INDEX_DATE); int nColIndex = 0; if (sDate == null) { nColIndex = m_rowOtherColIndex; } else { nColIndex = getColIndex(sDate); } int nRowIndex = 0; if (nCollegeId == 0) { nRowIndex = m_otherRowIndex; } else { nRowIndex = getRowIndex(nCollegeId); } // setValue(nColIndex, nRowIndex, nSum); } /** * * @param _nColIndex * @param _nRowIndex * @param _nSum */ private void setValue(int _nColIndex, int _nRowIndex, int _nSum) { if (m_datas[_nRowIndex][_nColIndex] == null) { m_datas[_nRowIndex][_nColIndex] = new Integer(_nSum); } else { Integer value = new Integer(String.valueOf(m_datas[_nRowIndex][_nColIndex])); int nValue = value.intValue() + _nSum; m_datas[_nRowIndex][_nColIndex] = new Integer(nValue); } } /** * Index * @param _sDate * @return */ private int getColIndex(Integer _sDate) { return ((Integer) m_yearColIndexMap.get(_sDate)) != null ? ((Integer) m_yearColIndexMap.get(_sDate)).intValue() : m_rowOtherColIndex; } /** * Index * @param _nCollegeId Id * @return * @throws PureException */ private int getRowIndex(int _nCollegeId) throws PureException { Organization org = getCollege(_nCollegeId); String sCode = org.getCode(); return ((Integer) m_codeRowIndexMap.get(sCode)) != null ? ((Integer) m_codeRowIndexMap.get(sCode)).intValue() : m_otherRowIndex; } /** * collegeId * @param _nCollegeId * @return * @throws PureException */ private Organization getCollege(int _nCollegeId) throws PureException { mgr = getOrgMgr(); Organization org = (Organization) mgr.lookupById(_nCollegeId); return org; } /** * @see com.pureinfo.srm.reports.table.ITableDatabuilder#setParameters(java.util.Map) */ public void setParameters(Map _sParams) throws ParseException { String[] sYear = (String[]) _sParams.get("start"); m_nStartYear = StringUtils.isNotEmpty(sYear[0]) ? Integer.parseInt(sYear[0]) : 0; m_nStart = m_nStartYear; String[] sEnd = (String[]) _sParams.get("end"); m_nEndYear = StringUtils.isNotEmpty(sEnd[0]) ? Integer.parseInt(sEnd[0]) : 0; m_nEnd = m_nEndYear; String[] sAudit = (String[]) _sParams.get("audit"); m_sAudit = StringUtils.isNotEmpty(sAudit[0]) ? sAudit[0] : null; String[] sIsFirst = (String[]) _sParams.get("isFirst"); m_sIsFirst = StringUtils.isNotEmpty(sIsFirst[0]) ? sIsFirst[0] : null; } /** * * @throws PureException */ private void initColleges() throws PureException { IOrganizationMgr orgMgr = getOrgMgr(); List list = orgMgr.findAllByType(SRMConstants.ORG_TYPE.COLLEGE, false); int i = COL_IDX_TITLE + 1; for (Iterator iter = list.iterator(); iter.hasNext(); i++) { Organization org = (Organization) iter.next(); String sCode = org.getCode(); String sName = org.getName(); m_codeRowIndexMap.put(sCode, new Integer(i)); m_codeNameMap.put(sCode, sName); } m_otherRowIndex = i++; m_totalRowIndex = i++; } private void initYears() throws PureException { m_rowOtherColIndex = LABEL_WIDTH + m_nEndYear - m_nStartYear + 1; m_nColLength = LABEL_WIDTH + m_nEndYear - m_nStartYear + 1 + 1; } /** * * @throws PureException */ private void doQuery() throws PureException { if (m_sAudit.equalsIgnoreCase("a")) { m_3Indexes = doAllAuditQuery(); } else { m_3Indexes = doAuditQuery(m_sAudit); } } /** * * @return Iobjects * @throws PureException */ private IObjects doAllAuditQuery() throws PureException { IStatement query = null; try { IProductMgr prdMgr = getProductMgr(); String sSql = "(SELECT COUNT({this.id}) AS " + SUM + ", {this.college} AS " + COLLEGE + ",1 AS _TEMP,{this.indexIndexDate} AS " + INDEX_DATE + " FROM {this}0 "; sSql += " WHERE {this.paperSort} in ('A','B','D')"; sSql += " AND {this.productForm} = '3' AND {this.indexIndexDate} >= ? AND {this.indexIndexDate} <= ? "; if (m_sIsFirst.equalsIgnoreCase("y")) { sSql += " AND {this.isZju} = 1 "; } else if (m_sIsFirst.equalsIgnoreCase("n")) { sSql += " AND {this.isZju} = 0 "; } sSql += "GROUP BY {this.college}, {this.indexIndexDate}) "; sSql += " UNION "; sSql += "(SELECT COUNT({this.id}) AS " + SUM + ", {this.college} AS " + COLLEGE + ",0 AS _TEMP,{this.indexIndexDate} AS " + INDEX_DATE + " FROM {this} "; sSql += " WHERE {this.paperSort} in ('A','B','D')"; sSql += " AND {this.productForm} = '3' AND {this.indexIndexDate} >= ? AND {this.indexIndexDate} <= ? "; if (m_sIsFirst.equalsIgnoreCase("y")) { sSql += " AND {this.isZju} = 1 "; } else if (m_sIsFirst.equalsIgnoreCase("n")) { sSql += " AND {this.isZju} = 0 "; } sSql += "GROUP BY {this.college}, {this.indexIndexDate}) "; query = prdMgr.createQuery(sSql, 0); query.setInt(0, m_nStart); query.setInt(1, m_nEnd); query.setInt(2, m_nStart); query.setInt(3, m_nEnd); return query.executeQuery(false); } finally { if (query != null) query.clear(); } } /** * * @param _sTag yn * @return IObjects * @throws PureException */ private IObjects doAuditQuery(String _sTag) throws PureException { IStatement query = null; IObjects objs = null; String sTable = _sTag.equalsIgnoreCase("n") ? "{this}0" : "{this}"; try { IProductMgr prdMgr = (IProductMgr) ArkContentHelper.getContentMgrOf(Product.class); String sSql = "SELECT COUNT({this.id}) AS " + SUM + ", {this.college} AS " + COLLEGE + ",{this.indexIndexDate} AS " + INDEX_DATE + " FROM " + sTable + ""; sSql += " WHERE {this.paperSort} in ('A','B','D')"; sSql += " AND {this.productForm} = '3' AND {this.indexIndexDate} >= " + m_nStart + " AND {this.indexIndexDate} <= " + m_nEnd + ""; if (m_sIsFirst.equalsIgnoreCase("y")) { sSql += " AND {this.isZju} = 1 "; } else if (m_sIsFirst.equalsIgnoreCase("n")) { sSql += " AND {this.isZju} = 0 "; } sSql += " GROUP BY {this.college}, {this.indexIndexDate}"; query = prdMgr.createQuery(sSql, 0); objs = query.executeQuery(false); return objs; } finally { if (query != null) query.clear(); } } /** * title&label * @throws PureException */ private void initObjects() throws PureException { //label initColleges(); //title initYears(); //title&label begin m_datas = new Object[TITLE_HEIGHT + m_codeRowIndexMap.size() + 1 + 1][m_nColLength]; m_datas[0][0] = ""; //title begin for (int i = 1; i < m_nColLength - 1; i++) { m_datas[COL_IDX_TITLE][i] = String.valueOf(m_nStartYear); m_yearColIndexMap.put(new Integer(m_nStartYear), new Integer(i)); m_nStartYear++; } m_datas[COL_IDX_TITLE][m_nColLength - 1] = ""; //end renderLabels(); //end } private void renderLabels() { for (Iterator iter = m_codeRowIndexMap.entrySet().iterator(); iter.hasNext();) { Map.Entry en = (Map.Entry) iter.next(); String sCode = (String) en.getKey(); Integer rowIndex = (Integer) en.getValue(); m_datas[rowIndex.intValue()][0] = m_codeNameMap.get(sCode); } m_datas[m_otherRowIndex][0] = ""; m_datas[m_totalRowIndex][0] = ""; } private IOrganizationMgr getOrgMgr() throws PureException { if (mgr != null) return mgr; return (IOrganizationMgr) ArkContentHelper.getContentMgrOf(Organization.class); } private IProductMgr getProductMgr() throws PureException { if (productMgr != null) return productMgr; return (IProductMgr) ArkContentHelper.getContentMgrOf(Product.class); } public static void main(String[] args) { try { Index3SCIStatistic s = new Index3SCIStatistic(); Map m = new HashMap(); String[] start = { "2006" }; m.put("start", start); String[] end = { "2007" }; m.put("end", end); String[] audit = { "a" }; m.put("audit", audit); String[] isFirst = { "y" }; m.put("isFirst", isFirst); s.setParameters(m); // s.doOutlayQuery(); Object[][] o = s.buildDatas(false, false); for (int i = 0; i < o.length; i++) { for (int j = 0; j < o[i].length; j++) { System.out.print("\t" + "\t" + "\t" + o[i][j]); } System.out.println(); } } catch (Exception ex) { // TODO Auto-generated catch block ex.printStackTrace(System.err); } finally { } } }