Java tutorial
/** * PureInfo Command * @(#)BySubjectStatistic.java 1.0 2007-7-26 * * 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.sci; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.apache.commons.collections.map.HashedMap; import com.pureinfo.ark.content.ArkContentHelper; import com.pureinfo.dolphin.model.DolphinObject; import com.pureinfo.dolphin.model.IObjects; import com.pureinfo.dolphin.persister.IStatement; import com.pureinfo.dolphin.query.logic.ISQLLogic; import com.pureinfo.force.exception.PureException; import com.pureinfo.srm.reports.table.data.institute.InstituteReportBase; import com.pureinfo.srm.sci.domain.ISCIMgr; import com.pureinfo.srm.sci.model.SCI; /** * <P> * Created on 2007-7-26 21:12:01 <BR> * Last modified on 2007-7-26 * </P> * TODO describe BySubjectStatistic here ... * * @author elmar.chen * @version 1.0, 2007-7-26 * @since Command 1.0 */ public class SCIBySubjectOnlyStatistic extends InstituteReportBase { private static final String PARAM_NAME_ORDER = "order"; public static final LinkedHashMap SCHOOLS = new LinkedHashMap(); static { SCHOOLS.put("", new Integer(0)); SCHOOLS.put("", new Integer(1)); SCHOOLS.put("", new Integer(2)); } /** * @see com.pureinfo.srm.reports.table.data.institute.InstituteReportBase#buildDatas(boolean, * boolean) */ public Object[][] buildDatas(boolean _bAscOrder, boolean _bOrderByValue) throws PureException { IObjects nums = doQuery(); DolphinObject num = null; Map hDatas = new HashedMap(); boolean hasOther = false; while ((num = nums.next()) != null) { String subest = num.getStrProperty("_SUB"); if (subest == null || subest.trim().length() == 0) continue; String[] subs = subest.split(";"); String sSchool = num.getStrProperty("_SCH"); int nNum = num.getIntProperty("_NUM", 0); for (int i = 0; i < subs.length; i++) { if (nNum < 1) continue; String sSubject = subs[i].trim(); Object[] hSubjectDatas = (Object[]) hDatas.get(sSubject); if (hSubjectDatas == null) { hDatas.put(sSubject, hSubjectDatas = new Object[SCHOOLS.size() * 2]); hSubjectDatas[0] = sSubject; for (int j = 1; j < hSubjectDatas.length; j++) { hSubjectDatas[j] = new Integer(0); } } Integer idxSchool = (Integer) SCHOOLS.get(sSchool); if (idxSchool == null) hasOther = true; int idxData = (idxSchool == null ? SCHOOLS.size() : idxSchool.intValue()) + 1; Integer odValue = (Integer) hSubjectDatas[idxData]; int sum = odValue == null ? nNum : (nNum + odValue.intValue()); hSubjectDatas[idxData] = new Integer(sum); } } List l = new ArrayList(hDatas.size()); for (Iterator iter = hDatas.entrySet().iterator(); iter.hasNext();) { Map.Entry en = (Map.Entry) iter.next(); Object[] hSubjectDatas = (Object[]) en.getValue(); if (!hasOther) { Object[] line = new Object[SCHOOLS.size() + 1]; System.arraycopy(hSubjectDatas, 0, line, 0, line.length); hSubjectDatas = line; } l.add(hSubjectDatas); } Collections.sort(l, new Comparator() { public int compare(Object _sO1, Object _sO2) { String sOrder = getParamValue(PARAM_NAME_ORDER, 0); boolean orderValid = sOrder != null && sOrder.matches("[0-9]+"); int orderIdx = orderValid ? Integer.parseInt(sOrder) : 0; Object[] arr1 = (Object[]) _sO1; Object[] arr2 = (Object[]) _sO2; if (orderIdx < 0 || orderIdx >= arr1.length) { orderIdx = 0; } Comparable s1 = (Comparable) arr1[orderIdx]; Comparable s2 = (Comparable) arr2[orderIdx]; int nComp = s1.compareTo(s2); if (nComp == 0) { Comparable ss1 = (Comparable) arr1[0]; Comparable ss2 = (Comparable) arr2[0]; return ss1.compareTo(ss2); } return nComp; } }); Object[][] datas = new Object[l.size() + 1][]; datas[0] = new Object[(hasOther ? 1 : 0) + SCHOOLS.size() + 1]; int j = 1; datas[0][0] = ""; for (Iterator iter = SCHOOLS.keySet().iterator(); iter.hasNext();) { datas[0][j++] = iter.next(); } if (hasOther) { datas[0][j++] = ""; } int rowIdx = 1; for (Iterator iter = l.iterator(); iter.hasNext();) { datas[rowIdx++] = (Object[]) iter.next(); } nums.clear(); return datas; } protected IObjects doQuery() throws PureException { IStatement stat = null; try { ISCIMgr mgr = (ISCIMgr) ArkContentHelper.getContentMgrOf(SCI.class); ISQLLogic con = makeDateCondtion(); String sWhere = ""; ArrayList params = new ArrayList(); String sCon = con == null ? null : con.toSQL(params); if (sCon != null && sCon.trim().length() > 0) { sWhere = " WHERE " + sCon; } String strSQL = "select count({this.id}) _NUM, {this.englishScience} AS _SUB,{this.schoolName} AS _SCH " + " from {this} " + sWhere + " group by _SUB"; stat = mgr.createQuery(strSQL, 0); if (!params.isEmpty()) { stat.setParameters(0, params); } IObjects nums = stat.executeQuery(false); return nums; } finally { if (stat != null) stat.clear(); } } /** * @see com.pureinfo.srm.reports.table.data.institute.InstituteReportBase#getMyCondition() */ protected ISQLLogic getMyCondition() { return null; } /** * @see com.pureinfo.srm.reports.table.data.institute.InstituteReportBase#getSelects() */ protected Map getSelects() { return null; } /** * @see com.pureinfo.srm.reports.table.data.institute.InstituteReportBase#getTables() */ protected String[] getTables() { return null; } /** * @see com.pureinfo.srm.reports.table.data.institute.InstituteReportBase#getCols() */ protected LinkedHashMap getCols() { return null; } /** * @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 { return null; } /** * @see com.pureinfo.srm.reports.table.data.institute.IInstituteDataBuilder#getTitle() */ public String getTitle() { return getDateRangeDesc() + ""; } /** * @see com.pureinfo.srm.reports.table.data.institute.IInstituteDataBuilder#descParam() */ public ParamDesc descParam() { return new ParamDesc() { public String getDateProperty() { return "{this.publishDate}"; } public String getDateDesc() { return ""; } public boolean isDateYearOnly() { return true; } public String[][] getOrderMap() { String[][] map = new String[SCHOOLS.size() + 1][]; int i = 0; map[i++] = new String[] { "0", "" }; for (Iterator iter = SCHOOLS.keySet().iterator(); iter.hasNext();) { map[i] = new String[] { i + "", (String) iter.next() }; i++; } return map; } public String getDepartmentProperty() { return null; } }; } public static void main(String[] args) throws PureException { Object[][] datas = new SCIBySubjectOnlyStatistic().buildDatas(false, false); for (int i = 0; i < datas.length; i++) { for (int j = 0; j < datas[i].length; j++) { System.out.print(datas[i][j] + "\t"); } System.out.println(); } } }