IBSheet7Factory.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. package com.udapsoft.modular.web.ibsheet7;
  2. import java.beans.PropertyEditor;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import javax.servlet.http.HttpServletRequest;
  7. import kr.co.hsnc.common.config.WAFConfig;
  8. import kr.co.hsnc.common.logger.Logger;
  9. import kr.co.hsnc.common.sql.Row;
  10. import kr.co.hsnc.common.sql.RowSet;
  11. import kr.co.hsnc.common.util.ValueObject;
  12. import org.jdom.CDATA;
  13. import org.jdom.Document;
  14. import org.jdom.Element;
  15. import org.jdom.output.Format;
  16. import org.jdom.output.XMLOutputter;
  17. import org.springframework.beans.BeanWrapper;
  18. import org.springframework.beans.PropertyAccessorFactory;
  19. import org.springframework.web.bind.ServletRequestUtils;
  20. import com.udapsoft.modular.core.exception.ModularException;
  21. import com.udapsoft.modular.core.util.StringUtils;
  22. import com.udapsoft.modular.web.ibsheet7.exception.IBSheet7MessageException;
  23. import com.udapsoft.modular.web.ibsheet7.exception.IBSheet7RenderingException;
  24. import com.udapsoft.modular.web.ibsheet7.util.IBSheet7Utils;
  25. public class IBSheet7Factory {
  26. private static final String TAG_MULTI_SHEET = "MULTISHEET";
  27. private static final String TAG_SHEET = "SHEET";
  28. private static final String TAG_ETCDATA = "ETC-DATA";
  29. private static final String TAG_ETC = "ETC";
  30. private static final String TAG_MULTI_RESULT = "MULTIDATA";
  31. private static final String TAG_MESSAGE = "MESSAGE";
  32. private static final String TAG_DATA = "DATA";
  33. private static final String ATTR_TOTAL = "TOTAL";
  34. private static final String ATTR_COLORDER = "COLORDER";
  35. private static final String ATTR_COLSEPARATOR = "COLSEPARATOR";
  36. private static final String ATTR_KEY = "KEY";
  37. private static final String TAG_TR = "TR";
  38. private static final String TAG_TD = "TD";
  39. private static final String TAG_RESULT = "RESULT";
  40. private static final String ATTR_CODE = "CODE";
  41. private static final String ATTR_MESSAGE = "MESSAGE";
  42. private static final String VAL_OK = "0";
  43. private String encoding = "utf-8";
  44. private String colSeparator;
  45. private Map<?, ?> customEditors;
  46. public void setEncoding(String encoding) {
  47. this.encoding = encoding;
  48. }
  49. public void setColSeparator(String colSeparator) {
  50. this.colSeparator = colSeparator;
  51. }
  52. public Map<?, ?> getCustomEditors() {
  53. return this.customEditors;
  54. }
  55. public void setCustomEditors(Map<?, ?> customEditors) {
  56. this.customEditors = customEditors;
  57. }
  58. public static Document createIBSheet7(ValueObject model, HttpServletRequest request) throws Exception {
  59. IBSheet7Factory factory = new IBSheet7Factory();
  60. return factory.renderMergedOutputModel(model, request);
  61. }
  62. protected Document renderMergedOutputModel(ValueObject model, HttpServletRequest request) throws Exception {
  63. Format format = Format.getRawFormat();
  64. format.setEncoding(this.encoding);
  65. XMLOutputter outputter = new XMLOutputter();
  66. outputter.setFormat(format);
  67. Document doc = new Document();
  68. String commandType = ServletRequestUtils.getStringParameter(request, "modular.web.ibsheet7.command", "search");
  69. String sheetId = ServletRequestUtils.getStringParameter(request, "modular.web.ibsheet7.id", "");
  70. String extProps = ServletRequestUtils.getStringParameter(request, "modular.web.ibsheet7.extend", "");
  71. if (commandType.equals("search")) {
  72. renderSearchResult(doc, model, request, sheetId, extProps);
  73. } else if (commandType.equals("multiSearch")) {
  74. String[] sheetIdArray = sheetId.split("\\^");
  75. renderSearchResult(doc, model, request, sheetIdArray, extProps, true);
  76. } else if (commandType.equals("save")) {
  77. renderSaveResult(doc, model, request, sheetId);
  78. } else if (commandType.equals("multiSave")) {
  79. String[] sheetIdArray = sheetId.split("\\^");
  80. renderSaveResult(doc, model, request, sheetIdArray, true);
  81. } else {
  82. throw new ModularException("can't handle a command type [" + commandType + "]");
  83. }
  84. if (kr.co.hsnc.common.util.tracer.Logger.isLogging) {
  85. IBSheet7Utils.printDebugXml(doc, outputter, this.encoding);
  86. }
  87. return doc;
  88. }
  89. protected void renderSearchResult(Document doc, ValueObject model, HttpServletRequest request, String sheetId, String extendProps) {
  90. String[] sheetIdArray = { sheetId };
  91. renderSearchResult(doc, model, request, sheetIdArray, extendProps, false);
  92. }
  93. protected void renderSearchResult(Document doc, ValueObject model, HttpServletRequest request, String[] sheetIdArray, String extendProps, boolean isMulti) {
  94. if ((isMulti) && (sheetIdArray.length <= 1)) {
  95. throw new IBSheet7RenderingException("MultiSearch 모드에는 sheetId가 2개 이상 필요합니다.");
  96. }
  97. Map<String, String> extendPropsMap = bindExtendProperties(extendProps);
  98. if ((model != null) && (model.size() > 0)) {
  99. Element root = null;
  100. if (isMulti) {
  101. root = new Element(TAG_MULTI_SHEET);
  102. doc.addContent(root);
  103. }
  104. String columns = "";
  105. for (int i = 0; i < sheetIdArray.length; i++) {
  106. Element sheet = new Element(TAG_SHEET);
  107. if (isMulti) {
  108. root.addContent(sheet);
  109. columns = ServletRequestUtils.getStringParameter(request, sheetIdArray[i] + "." + "modular.web.ibsheet7.columns", "");
  110. } else {
  111. doc.addContent(sheet);
  112. columns = ServletRequestUtils.getStringParameter(request, "modular.web.ibsheet7.columns", "");
  113. }
  114. if (i == 0) {
  115. Object messageValue = model.getObject("modular.web.ibsheet7.message");
  116. if ((messageValue != null) && ((messageValue instanceof IBSheet7MessageException))) {
  117. Element message = new Element(TAG_MESSAGE);
  118. sheet.addContent(message);
  119. IBSheet7MessageException mex = (IBSheet7MessageException) messageValue;
  120. message.addContent(mex.getMessage());
  121. }
  122. }
  123. Object etcValue = model.getObject(sheetIdArray[i]+ ".modular.web.ibsheet7.etcdata");
  124. if (etcValue != null) {
  125. Element etcData = new Element(TAG_ETCDATA);
  126. sheet.addContent(etcData);
  127. makeEtcDataElement(etcData, etcValue);
  128. }
  129. Element data = new Element(TAG_DATA);
  130. sheet.addContent(data);
  131. if (StringUtils.hasText(this.colSeparator)) {
  132. data.setAttribute(ATTR_COLSEPARATOR, this.colSeparator);
  133. }
  134. Object value = model.getObject(sheetIdArray[i]);
  135. if (value != null) {
  136. RowSet rs = (RowSet) value;
  137. data.setAttribute(ATTR_TOTAL, String.valueOf(rs.size()));
  138. for (int rsIdx = 0; rsIdx < rs.size(); rsIdx++) {
  139. makeDataElement(data, columns, rs.getRow(rsIdx), extendPropsMap);
  140. }
  141. } else {
  142. Logger.warn.print("can't find a sheetId[" + sheetIdArray[i] + "] form result object");
  143. }
  144. }
  145. }
  146. }
  147. protected void renderSaveResult(Document doc, ValueObject model, HttpServletRequest request, String sheetId) {
  148. String[] sheetIdArray = { sheetId };
  149. renderSaveResult(doc, model, request, sheetIdArray, false);
  150. }
  151. protected void renderSaveResult(Document doc, ValueObject model, HttpServletRequest request, String[] sheetIdArray, boolean isMulti) {
  152. Element root = null;
  153. if (isMulti) {
  154. root = new Element(TAG_MULTI_RESULT);
  155. doc.addContent(root);
  156. }
  157. for (int i = 0; i < sheetIdArray.length; i++) {
  158. Element sheet = new Element(TAG_SHEET);
  159. if (isMulti) {
  160. root.addContent(sheet);
  161. } else {
  162. doc.addContent(sheet);
  163. }
  164. Object etcValue = model.getObject(sheetIdArray[i]+ ".modular.web.ibsheet7.etcdata");
  165. if (etcValue != null) {
  166. Element etcData = new Element(TAG_ETCDATA);
  167. sheet.addContent(etcData);
  168. makeEtcDataElement(etcData, etcValue);
  169. }
  170. Element result = new Element(TAG_RESULT);
  171. sheet.addContent(result);
  172. Object messageValue = model.get("modular.web.ibsheet7.message");
  173. if ((messageValue != null) && ((messageValue instanceof IBSheet7MessageException))) {
  174. IBSheet7MessageException mex = (IBSheet7MessageException) messageValue;
  175. result.setAttribute(ATTR_CODE, Integer.toString(mex.getCode()));
  176. result.setAttribute(ATTR_MESSAGE, mex.getMessage());
  177. } else {
  178. result.setAttribute(ATTR_CODE, VAL_OK);
  179. result.setAttribute(ATTR_MESSAGE, "");
  180. }
  181. }
  182. }
  183. private void makeDataElement(Element data, String columns, Object obj, Map<String, String> extendPropsMap) {
  184. String[] columnsArray = columns.split(",");
  185. String colorder = columns.replaceAll(",", "|");
  186. data.setAttribute(ATTR_COLORDER, colorder);
  187. Element tr = new Element(TAG_TR);
  188. if (obj instanceof Row) {
  189. Row row = (Row) obj;
  190. makeExtendAttribute(tr, extendPropsMap, row);
  191. for (int i = 0; i < columnsArray.length; i++) {
  192. Element td = new Element(TAG_TD);
  193. String columnNm = columnsArray[i];
  194. if (("sStatus".equals(columnNm)) || (columnNm.startsWith(WAFConfig.get("modular.ibsheet7.column.attribute_prefix")))) {
  195. td.setText("");
  196. } else {
  197. Object columnVal = null;
  198. try {
  199. columnVal = row.get(columnNm);
  200. } catch (IllegalArgumentException e) {
  201. Logger.warn.print(e.getMessage());
  202. }
  203. if (columnVal != null) {
  204. Class<? extends Object> columnType = columnVal.getClass();
  205. bindColumnValue(td, columnNm, columnVal, columnType);
  206. }
  207. }
  208. tr.addContent(td);
  209. }
  210. }
  211. data.addContent(tr);
  212. }
  213. private void makeExtendAttribute(Element tr, Map<String, String> extendPropsMap, Object obj) {
  214. for (Iterator<?> iterator = extendPropsMap.entrySet().iterator(); iterator.hasNext();) {
  215. Map.Entry entry = (Map.Entry) iterator.next();
  216. String attributeName = (String) entry.getKey();
  217. String propertyName = (String) entry.getValue();
  218. PropertyEditor pe = null;
  219. Object val = null;
  220. if (!attributeName.startsWith("format.")) {
  221. try {
  222. Object columnVal = null;
  223. Class<? extends Object> columnType = null;
  224. if (obj instanceof Row) {
  225. Row row = (Row) obj;
  226. columnVal = row.get(propertyName);
  227. columnType = columnVal == null ? Object.class : columnVal.getClass();
  228. } else {
  229. BeanWrapper bean = PropertyAccessorFactory.forBeanPropertyAccess(obj);
  230. columnVal = bean.getPropertyValue(propertyName);
  231. columnType = bean.getPropertyType(propertyName);
  232. }
  233. if (this.customEditors != null) {
  234. pe = (PropertyEditor) this.customEditors.get(columnType);
  235. }
  236. val = columnVal;
  237. } catch (Exception e) {
  238. Logger.err.print("IBSheet7 View 결과 처리 중 오류가 발생하였습니다." + e.getMessage());
  239. }
  240. if (pe != null) {
  241. pe.setValue(val);
  242. tr.setAttribute(attributeName, val == null ? "" : pe.getAsText());
  243. } else {
  244. tr.setAttribute(attributeName, val == null ? "" : val.toString());
  245. }
  246. }
  247. }
  248. }
  249. private void bindColumnValue(Element td, String columnNm, Object columnVal, Class<? extends Object> columnType) {
  250. PropertyEditor pe = null;
  251. try {
  252. if (this.customEditors != null) {
  253. pe = (PropertyEditor) this.customEditors.get(columnType);
  254. }
  255. } catch (Exception e) {
  256. Logger.err.print("IBSheet7 View 결과 처리 [" + columnNm + "]중 오류가 발생하였습니다." + e.getMessage());
  257. }
  258. if (pe != null) {
  259. pe.setValue(columnVal);
  260. td.setText(columnVal == null ? "" : pe.getAsText());
  261. } else {
  262. String tdVal = columnVal == null ? "" : columnVal.toString();
  263. tdVal = tdVal.replaceAll("\r\n", "\n");
  264. td.setContent(new CDATA(tdVal));
  265. }
  266. }
  267. private Map<String, String> bindExtendProperties(String extendProps) {
  268. Map<String, String> extendPropsMap = new HashMap<String, String>();
  269. String[] values = StringUtils.commaDelimitedListToStringArray(extendProps);
  270. for (int i = 0; i < values.length; i++) {
  271. String value = values[i];
  272. if (StringUtils.hasText(value)) {
  273. String[] v = StringUtils.delimitedListToStringArray(value.trim(), "=");
  274. if ((v.length >= 2) && (StringUtils.hasText(v[0]))) {
  275. extendPropsMap.put(v[0].trim(), v[1]);
  276. }
  277. }
  278. }
  279. return extendPropsMap;
  280. }
  281. private void makeEtcDataElement(Element etcdata, Object obj) {
  282. if (obj instanceof Map) {
  283. Map<?, ?> map = (Map<?, ?>) obj;
  284. for (Iterator<?> it = map.entrySet().iterator(); it.hasNext();) {
  285. Element etc = new Element(TAG_ETC);
  286. Map.Entry entry = (Map.Entry) it.next();
  287. String attributeName = (String) entry.getKey();
  288. String propertyName = (String) entry.getValue();
  289. etc.setAttribute(ATTR_KEY, attributeName);
  290. etc.setText(propertyName);
  291. etcdata.addContent(etc);
  292. }
  293. }
  294. }
  295. private String getColumnValue(String columnNm, Object columnVal, Class<?> columnType) {
  296. PropertyEditor pe = null;
  297. try {
  298. if (this.customEditors != null) {
  299. pe = (PropertyEditor) this.customEditors.get(columnType);
  300. }
  301. } catch (Exception e) {
  302. Logger.err.print("IBSheet7 View 결과 처리 [" + columnNm + "]중 오류가 발생하였습니다." + e.getMessage());
  303. }
  304. String tdVal = "";
  305. if (pe != null) {
  306. pe.setValue(columnVal);
  307. tdVal = columnVal == null ? "" : pe.getAsText();
  308. } else {
  309. tdVal = columnVal == null ? "" : columnVal.toString();
  310. tdVal = tdVal.replaceAll("\r\n", "\n");
  311. }
  312. return tdVal;
  313. }
  314. }