package com.bstek.bdf2.export; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import net.sf.ehcache.hibernate.management.impl.BeanUtils; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.lang.StringUtils; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.node.ObjectNode; import com.alibaba.fastjson.JSONObject; import com.bstek.bdf2.export.interceptor.IDataInterceptor; import com.bstek.bdf2.export.model.ReportFormData; import com.bstek.bdf2.export.model.ReportForm; import com.bstek.bdf2.export.model.ReportTitle; import com.bstek.bdf2.export.model.ReportTitleStyle; import com.bstek.bdf2.export.utils.ColorUtils; import com.bstek.bdf2.export.utils.CriterionUtils; import com.bstek.bdf2.export.view.SupportWidget; import com.bstek.dorado.data.JsonUtils; import com.bstek.dorado.data.ParameterWrapper; import com.bstek.dorado.data.entity.EntityCollection; import com.bstek.dorado.data.entity.EntityList; import com.bstek.dorado.data.entity.EntityUtils; import com.bstek.dorado.data.provider.DataProvider; import com.bstek.dorado.data.provider.Page; import com.bstek.dorado.data.type.DataType; import com.bstek.dorado.data.variant.MetaData; import com.bstek.dorado.web.DoradoContext; /** * @author matt.yao@bstek.com * @since 2.0 */ public abstract class AbstractReportModelGenerater { /** * 2020年7月8日 调整,在表头中使用JSON文本传递过来,然后扩展这个表头 * * @param titleInfos * @return */ public ReportTitle generateReportTitleModel(Map titleInfos) { ReportTitle reportTitle = new ReportTitle(); if (titleInfos == null) { return reportTitle; } Boolean showTitle = (Boolean) titleInfos.get("showTitle"); if (showTitle != null && showTitle) { String titleStr = (String) titleInfos.get("title"); if (titleStr.indexOf("{") >= 0) { JSONObject json = JSONObject.parseObject(titleStr); reportTitle.setTitle(json.getString("title")); reportTitle.setCreateUser(json.getString("createUser")); reportTitle.setDepotName(json.getString("depotName")); reportTitle.setTimeDesc(json.getString("timeDesc")); reportTitle.setUnit(json.getString("unit")); reportTitle.setCreateTime(json.getString("createTime")); reportTitle.setTimeDescLabel(json.getString("timeDescLabel")); reportTitle.setDepotNameLabel(json.getString("depotNameLabel")); } else { reportTitle.setTitle(titleStr); } reportTitle.setShowTitle(true); reportTitle.setStyle(createTitleStyle(titleInfos)); } if (titleInfos.get("showBorder") != null) { reportTitle.setShowBorder((Boolean) titleInfos.get("showBorder")); } if (titleInfos.get("showPageNumber") != null) { reportTitle.setShowPageNo((Boolean) titleInfos .get("showPageNumber")); } return reportTitle; } @SuppressWarnings("unchecked") public ReportForm generateReportFormModel(Map formInfo, String intercepterBean) throws Exception { Map map = (Map) formInfo .get("formStyle"); ReportForm reportFormModel = new ReportForm(); List listReportFormDatas = new ArrayList(); int columnCount = (Integer) formInfo.get("columnCount"); boolean showBorder = true; if (map.get("showBorder") != null) { showBorder = (Boolean) map.get("showBorder"); } reportFormModel.setColumnCount(columnCount); reportFormModel.setShowBorder(showBorder); List> datas = (List>) formInfo .get("datas"); this.fireAutoFormDataInterceptor(intercepterBean, datas); int labelAlign = 2; int dataAlign = 0; int dataStyle = 0; if (map.get("labelAlign") != null) { labelAlign = (Integer) map.get("labelAlign"); } if (map.get("dataStyle") != null) { dataStyle = (Integer) map.get("dataStyle"); } if (map.get("dataAlign") != null) { dataAlign = (Integer) map.get("dataAlign"); } ReportFormData reportFormDataModel = null; for (Map dataMap : datas) { reportFormDataModel = new ReportFormData(); Object data = dataMap.get("value"); int colSpan = (Integer) dataMap.get("colSpan"); int rowSpan = (Integer) dataMap.get("rowSpan"); String label = (String) dataMap.get("label"); reportFormDataModel.setLabel(label); reportFormDataModel.setLabelAlign(labelAlign); reportFormDataModel.setData(data); reportFormDataModel.setDataAlign(dataAlign); reportFormDataModel.setDataStyle(dataStyle); reportFormDataModel.setColSpan(colSpan); reportFormDataModel.setRowSpan(rowSpan); listReportFormDatas.add(reportFormDataModel); } reportFormModel.setListReportFormDataModel(listReportFormDatas); return reportFormModel; } @SuppressWarnings({"unchecked", "rawtypes"}) public List> getGridModelData(Map map, List> columnInfos, String intercepterBean) throws Exception { List> dataList = null; String dataScope = (String) map.get("dataScope"); String treeColumn = (String) map.get("treeColumn"); int maxSize = Integer.valueOf(map.get("maxSize").toString()); ViewManager viewManager = ViewManager.getInstance(); if (dataScope.equals("serverAll")) { Object dataProviderParameter = map.get("dataProviderParameter"); String dataProviderId = (String) map.get("dataProviderId"); int pageSize = Integer.valueOf(map.get("pageSize").toString()); if (map.get("sysParameter") != null) { ObjectMapper om = JsonUtils.getObjectMapper(); String sp = om.writeValueAsString(map.get("sysParameter")); ObjectNode rudeSysParameter = (ObjectNode) om.readTree(sp); JsonNode rudeCriteria = null; if (rudeSysParameter != null) { rudeCriteria = rudeSysParameter.remove("criteria"); } MetaData sysParameter = null; if (rudeSysParameter != null) { sysParameter = (MetaData) JsonUtils.toJavaObject( rudeSysParameter, null, null, false, null); if (rudeCriteria != null && rudeCriteria instanceof ObjectNode) { sysParameter.put("criteria", CriterionUtils .getCriteria((ObjectNode) rudeCriteria)); } if (sysParameter != null && !sysParameter.isEmpty()) { dataProviderParameter = new ParameterWrapper( dataProviderParameter, sysParameter); } } } DataType resultDataType = null; String resultDataTypeName = (String) map.get("resultDataType"); if (StringUtils.isNotEmpty(resultDataTypeName)) { resultDataType = viewManager.getDataType(resultDataTypeName); } DataProvider dataProvider = viewManager .getDataProvider(dataProviderId); Collection collection = null; if (pageSize > 0) { Page page = new Page(maxSize, 1); dataProvider.getPagingResult(dataProviderParameter, page, resultDataType); collection = page.getEntities(); } else { collection = (Collection) dataProvider.getResult( dataProviderParameter, resultDataType); } if (collection instanceof EntityList) { if (collection.size() <= 65536) { collection = ((EntityList) collection).getTarget(); } else { Field field = ((EntityCollection) collection).getClass() .getField("target"); field.setAccessible(true); collection = (Collection) field.get(collection); } } dataList = new ArrayList>(); for (Object obj : collection) { // if (ProxyBeanUtils.isProxy(obj)) { // obj = ProxyBeanUtils.getProxyTarget(obj); // } if (obj instanceof Map) { dataList.add((Map) obj); } else { if (collection.size() <= 65536) { Map targetMap = new HashMap(); EntityUtils.copyProperties(targetMap, obj); dataList.add(targetMap); } else { dataList.add(PropertyUtils.describe(obj)); } } } dataList = retrieveServerComplexPropertyData(dataList, columnInfos); } else if (dataScope.equals("currentPage")) { dataList = new ArrayList>(); List> clientData = (List>) map .get("data"); List subDataList; for (Map tempMap : clientData) { if (tempMap.get(treeColumn) != null) { map.put(treeColumn, tempMap.get(treeColumn)); } dataList.add(tempMap); if (tempMap.get("children") != null) { subDataList = (List) tempMap.get("children"); this.createChildData(dataList, subDataList, "", treeColumn); } } } for (Map dataMap : dataList) { Set nameSet = dataMap.keySet(); for (String name : nameSet) { replaceValueWithMapping(dataMap, columnInfos, name); } } fireGridDataInterceptor(intercepterBean, dataList); return dataList; } @SuppressWarnings("unchecked") public void replaceValueWithMapping(Map dataMap, List> columnInfos, String name) throws Exception { for (Map columnMap : columnInfos) { String displayName = name + "_$displayProperty"; if (columnMap.containsKey(displayName)) { String propertyName = (String) columnMap.get(displayName); if (StringUtils.isNotEmpty(propertyName)) { Object data = dataMap.get(name); if (data != null) { dataMap.put(name, BeanUtils.getBeanProperty(data, propertyName)); } } } Object obj = columnMap.get(name + "_$mapping"); if (obj != null) { List> mappingList = (List>) obj; for (Map mappingMap : mappingList) { Object mappingKey = mappingMap.get("key"); if (mappingKey != null) mappingKey = mappingKey.toString(); Object value = dataMap.get(name); if (value != null) value = value.toString(); if (mappingKey.equals(value)) { dataMap.put(name, mappingMap.get("value")); break; } } } else { Object children = columnMap.get("children"); if (children != null) { List> childrenList = (List>) children; replaceValueWithMapping(dataMap, childrenList, name); } } } } public List> retrieveServerComplexPropertyData( List> dataList, List> columnInfos) throws Exception { for (Map columnMap : columnInfos) { String columnName = (String) columnMap.get("columnName"); if (StringUtils.isNotEmpty(columnName)) { String[] columnNames = columnName.split("\\."); if (columnNames.length > 1) { int size = dataList.size(); Map map; for (int i = 0; i < size; i++) { map = dataList.get(i); Object data = calculateBeanPropertyData(columnNames, map); map.put(columnName, data); } } } } return dataList; } @SuppressWarnings({"rawtypes", "unchecked"}) private Object calculateBeanPropertyData(String[] columnNames, Map map) throws Exception { Object obj = map.get(columnNames[0]); if (obj == null) { return null; } if (columnNames.length == 1) { return obj; } else if (columnNames.length == 2) { return BeanUtils.getBeanProperty(obj, columnNames[1]); } else { Map subMap = PropertyUtils.describe(obj); List list = new ArrayList(); int i = 1; while (i < columnNames.length) { list.add(columnNames[i]); i++; } String[] array = list.toArray(new String[list.size()]); return calculateBeanPropertyData(array, subMap); } } @SuppressWarnings({"unchecked"}) public void createChildData(List> dataList, List sub, String header, String treeColumn) { for (Object obj : sub) { Map tempMap = null; if (obj instanceof Map) { tempMap = (Map) obj; } else { tempMap = new HashMap(); try { EntityUtils.copyProperties(tempMap, obj); } catch (Exception e) { e.printStackTrace(); } } String tmp = header + "\t"; if (tempMap.get(treeColumn) != null) { tempMap.put(treeColumn, tmp + tempMap.get(treeColumn)); } dataList.add(tempMap); if (tempMap.get("children") != null) { this.createChildData(dataList, (List) tempMap.get("children"), tmp, treeColumn); } } } public ReportTitleStyle createTitleStyle(Map map) { ReportTitleStyle titleStyle = new ReportTitleStyle(); int fontSize = (Integer) map.get("fontSize"); String fontColor = (String) map.get("fontColor"); String bgColor = (String) map.get("bgColor"); titleStyle.setBgColor(this.createRGB(bgColor)); titleStyle.setFontSize(fontSize); titleStyle.setFontColor(this.createRGB(fontColor)); return titleStyle; } public int[] createRGB(String s) { if (StringUtils.isNotEmpty(s)) { return ColorUtils.parse2RGB(s); } return null; } public void fireGridDataInterceptor(String beanName, List> list) throws Exception { fireControlDataInterceptor(beanName, SupportWidget.grid, list); } public void fireAutoFormDataInterceptor(String beanName, List> list) throws Exception { fireControlDataInterceptor(beanName, SupportWidget.form, list); } private void fireControlDataInterceptor(String beanName, SupportWidget supportWidget, List> list) throws Exception { if (StringUtils.isNotEmpty(beanName)) { Map map = DoradoContext.getCurrent() .getApplicationContext() .getBeansOfType(IDataInterceptor.class); for (Map.Entry entry : map.entrySet()) { if (entry.getValue().getName().equals(beanName)) { if (supportWidget.equals(SupportWidget.grid)) { entry.getValue().interceptGridData(list); } else if (supportWidget.equals(SupportWidget.form)) { entry.getValue().interceptAutoFormData(list); } } } } } }