📄 chartdatamanager.java
字号:
/*
* Copyright 2006-2007 Queplix Corp.
*
* Licensed under the Queplix Public License, Version 1.1.1 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.queplix.com/solutions/commercial-open-source/queplix-public-license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.queplix.core.integrator;
import java.io.CharArrayReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import com.queplix.core.client.app.vo.CheckBoxData;
import com.queplix.core.client.app.vo.EntityReferenceData;
import com.queplix.core.client.app.vo.FieldData;
import com.queplix.core.client.app.vo.FieldMeta;
import com.queplix.core.client.app.vo.ListboxFieldData;
import com.queplix.core.client.app.vo.MemoFieldData;
import com.queplix.core.client.app.vo.SubsetData;
import com.queplix.core.client.app.vo.TextareaFieldData;
import com.queplix.core.client.app.vo.TextboxFieldData;
import com.queplix.core.client.app.vo.chart.ChartDetails;
import com.queplix.core.client.app.vo.chart.ChartMeta;
import com.queplix.core.client.app.vo.chart.ChartModel;
import com.queplix.core.client.app.vo.chart.ChartOrientation;
import com.queplix.core.client.app.vo.chart.ChartType;
import com.queplix.core.client.app.vo.chart.DefaultChartModel;
import com.queplix.core.integrator.entity.EntityFacade;
import com.queplix.core.integrator.entity.EntitySerializeHelper;
import com.queplix.core.integrator.entity.EntityViewHelper;
import com.queplix.core.integrator.entity.RequestProperties;
import com.queplix.core.integrator.security.LogonSession;
import com.queplix.core.modules.config.utils.EntityHelper;
import com.queplix.core.modules.eql.EQLObject;
import com.queplix.core.modules.eql.EQLRes;
import com.queplix.core.modules.eql.EQLResRecord;
import com.queplix.core.modules.eql.error.EQLException;
import com.queplix.core.modules.eqlext.jxb.gr.Chart;
import com.queplix.core.modules.eqlext.jxb.gr.ChartCategoryField;
import com.queplix.core.modules.eqlext.jxb.gr.ChartDataField;
import com.queplix.core.modules.eqlext.jxb.gr.ChartEfieldType;
import com.queplix.core.modules.eqlext.jxb.gr.ChartParam;
import com.queplix.core.modules.eqlext.jxb.gr.ChartParams;
import com.queplix.core.modules.eqlext.jxb.gr.ChartReq;
import com.queplix.core.modules.eqlext.jxb.gr.Req;
import com.queplix.core.modules.eqlext.jxb.gr.ReqField;
import com.queplix.core.modules.eqlext.jxb.gr.ReqFilter;
import com.queplix.core.modules.eqlext.jxb.gr.ReqFilters;
import com.queplix.core.modules.eqlext.jxb.gr.ReqFiltersTypeItem;
import com.queplix.core.modules.eqlext.jxb.gr.Reqs;
import com.queplix.core.modules.eqlext.jxb.gr.types.ConditionSType;
import com.queplix.core.modules.eqlext.jxb.gr.types.OrderDirectionSType;
import com.queplix.core.modules.jeo.gen.ChartObject;
import com.queplix.core.modules.jeo.gen.ChartObjectHandler;
import com.queplix.core.utils.StringHelper;
import com.queplix.core.utils.log.AbstractLogger;
import com.queplix.core.utils.log.Log;
import com.queplix.core.utils.xml.XMLHelper;
/**
* Manages all chart-related data operations like add/edit/remove/build chart data
* @author Michael Trofimov
*/
public class ChartDataManager {
private static final AbstractLogger logger = Log.getLog(ChartDataManager.class);
private static final String PARAM_WIDTH = "width";
private static final String PARAM_HEIGHT = "height";
private static final String PARAM_COLORS = "colors";
private static final ChartOrientation DEFAULT_ORIENTATION = ChartOrientation.HORIZONTAL;
private static final int DEFAULT_HEIGHT = 300;
private static final int DEFAULT_WIDTH = 400;
private ChartDataManager() {
}
public static ChartDetails getChartDetails(LogonSession ls, ActionContext ctx, long id)
throws EQLException {
ChartObject obj = getChartObject(ls, ctx, id);
if (obj == null)
return null;
return transformJEOToDetails(ls, ctx, obj);
}
public static ChartModel getChart(LogonSession ls, ActionContext ctx, long id)
throws EQLException {
ChartObject obj = getChartObject(ls, ctx, id);
if (obj == null)
return null;
return new DefaultChartModel(transformJEOToMeta(ls, ctx, obj));
}
// TODO implement it
// public void saveChart(ChartMeta chartMeta);
/**
* Incapsulates all data to build chart's image
* and provides FieldData objects for each value
* of category field (for drilldown functionality)
*/
public static class IntegratedChartData {
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
Map<Comparable, FieldData> fieldData = new HashMap<Comparable, FieldData>();
public CategoryDataset getCategoryDataset(){
return dataSet;
}
public FieldData getFieldData(int column){
return fieldData.get(dataSet.getColumnKey(column));
}
}
public static IntegratedChartData getChartData(LogonSession ls, ActionContext ctx, ChartMeta meta)
throws EQLException {
if (meta == null)
throw new IllegalArgumentException("meta can't be a null");
if (meta.getID() == null)
throw new IllegalArgumentException("Unspecified chart id");
ChartObject obj = getChartObject(ls, ctx, meta.getID());
if (obj == null)
return null;
IntegratedChartData data = new IntegratedChartData();
Chart chart = transformJEOToJXB(obj);
ChartReq chartReq = chart.getChartReq();
ChartCategoryField categoryField = chartReq.getChartCategoryField();
// TODO cache field meta
FieldMeta categoryMeta = EntityViewHelper.getMetaForField(
categoryField.getEntity(), categoryField.getName(), false, ls, ctx);
Reqs reqs = createReqsFromChartReq(chartReq);
Req req = reqs.getReq();
ReqField categoryReqField = createReqFieldFromCategoryField(chartReq.getChartCategoryField());
for (ChartDataField dataField : chartReq.getChartDataField()) {
ReqField dataReqField = createReqFieldFromDataField(dataField);
req.addReqField(dataReqField);
req.addReqField(categoryReqField);
EQLRes res = ctx.getRecordsManager().process(reqs, ls).getEQLRes();
if(res == null)
continue;
fillChartData(ls, categoryMeta, dataField, res, data);
req.clearReqField();
}
return data;
}
private static ChartObject getChartObject(LogonSession ls, ActionContext ctx, long id)
throws EQLException {
ChartObject obj = ChartObjectHandler.findByID(ctx.getJEOManager(), ls, id);
if(obj == null){
logger.WARN("Can't retrieve chart JEO by ID=" + id);
return null;
}
return obj;
}
// private static void fillDataSet(DefaultCategoryDataset dataSet, ChartDataField dataField, EQLRes res) {
// Map<String, Integer> counts = getCountsFromEQLRes(res);
//
// String dataName = EntityHelper.getFieldId(dataField.getEntity(), dataField.getName());
//
// for (String categoryName : counts.keySet()) {
// dataSet.addValue(counts.get(categoryName), dataName, categoryName);
// }
// }
private static void fillChartData(LogonSession ls, FieldMeta categoryMeta,
ChartDataField dataField, EQLRes res, IntegratedChartData data) {
String dataName = EntityHelper.getFieldId(dataField.getEntity(), dataField.getName());
Map<String, Integer> counts = new LinkedHashMap<String, Integer>();
for (int i = 0; i < res.size(); i++) {
EQLResRecord record = res.getRecord(i);
// Process category field
EQLObject valueEqlObj = record.getEQLObject(1);
Object valueObj = valueEqlObj.getObject();
EQLObject textEqlObj = record.getEQLListObject(1);
Object textObj = textEqlObj != null ? textEqlObj.getObject() : null;
// Gets category field key
String categoryName = textObj != null ? textObj.toString() : (valueObj != null ? valueObj.toString() : "Empty");
if(!data.fieldData.containsKey(categoryName))
data.fieldData.put(categoryName, createFieldData(ls, categoryMeta, valueObj, textObj));
Integer count = counts.get(categoryName);
counts.put(categoryName, count != null ? count + 1 : 1);
}
for (String categoryName : counts.keySet()) {
data.dataSet.addValue(counts.get(categoryName), dataName, categoryName);
}
}
private static Reqs createReqsFromChartReq(ChartReq chartReq) {
RequestProperties requestProps = new RequestProperties(
false, // doCount = false,
0, // page = 0, first page
-1, // pageSize = -1, retrieve all records
null); // no sorting
Reqs reqs = EntityFacade.createRequest(requestProps);
reqs.setReqFilters(chartReq.getReqFilters());
reqs.setEqlFilters(chartReq.getEqlFilters());
logger.DEBUG("Created chart request: " + reqs);
return reqs;
}
private static ReqField createReqFieldFromDataField(ChartDataField dataField){
return createReqFieldFromChartEfield(dataField);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -