PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/com/cf/tkconnect/data/WriteExcelBook.java

https://github.com/zypacsinc/src
Java | 383 lines | 319 code | 45 blank | 19 comment | 25 complexity | 0256d5b9985793107258b24ca50edb91 MD5 | raw file
  1. package com.cf.tkconnect.data;
  2. import java.io.FileOutputStream;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Map;
  8. import javax.servlet.http.HttpServletResponse;
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  10. import org.apache.poi.ss.usermodel.Cell;
  11. import org.apache.poi.ss.usermodel.CreationHelper;
  12. import org.apache.poi.ss.usermodel.IndexedColors;
  13. import org.apache.poi.ss.usermodel.Workbook;
  14. import org.apache.poi.ss.util.CellReference;
  15. import org.apache.poi.xssf.usermodel.XSSFCell;
  16. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  17. import org.apache.poi.xssf.usermodel.XSSFColor;
  18. import org.apache.poi.xssf.usermodel.XSSFFont;
  19. import org.apache.poi.xssf.usermodel.XSSFRow;
  20. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  21. import org.apache.poi.xssf.usermodel.XSSFSheet;
  22. import com.cf.tkconnect.data.form.BPAttributeData;
  23. import com.cf.tkconnect.data.process.ProcessBPXMLTemplate;
  24. import com.cf.tkconnect.log.Log;
  25. import com.cf.tkconnect.log.LogSource;
  26. public class WriteExcelBook {
  27. /**
  28. * @param args
  29. */
  30. static Log logger = LogSource.getInstance(WriteExcelBook.class);
  31. ProcessBPXMLTemplate bptemp;
  32. Map<String,Map<String,Object>> demap = new HashMap<String,Map<String,Object>>();
  33. Map<String,Object> studiomap;
  34. List<String> bpdelist = new ArrayList<String>();
  35. List<String> bplidelist = new ArrayList<String>();
  36. XSSFWorkbook wb;
  37. List<String[]> upper_data_list;
  38. List<String[]> li_data_list;
  39. boolean saveData = false;
  40. CreationHelper createHelper;
  41. XSSFCellStyle cellHeaderStyle;
  42. XSSFCellStyle cellDataStyle;
  43. boolean setResponse = false;
  44. boolean batch = false;
  45. String[] batchResp;
  46. public WriteExcelBook(ProcessBPXMLTemplate bptemp, Map<String,Object> studiomap){
  47. this.bptemp = bptemp;
  48. this.studiomap = studiomap;
  49. }
  50. public void createWB(HttpServletResponse res) throws Exception{
  51. wb = new XSSFWorkbook(); //office 7,10
  52. setWB();
  53. wb.write(res.getOutputStream());
  54. res.flushBuffer();
  55. // writeWBToFile(wb);
  56. }
  57. public void saveWB(String filename,List<String[]> upper_data_list,List<String[]> li_data_list) throws Exception{
  58. this.saveData = true;
  59. setData(upper_data_list,li_data_list);
  60. setWorkBook();
  61. writeWBToFile(filename);
  62. }
  63. public void writeWB(HttpServletResponse res,List<String[]> upper_data_list,List<String[]> li_data_list) throws Exception{
  64. this.saveData = true;
  65. setData(upper_data_list,li_data_list);
  66. setWorkBook();
  67. wb.write(res.getOutputStream());
  68. res.flushBuffer();
  69. }
  70. public void setData(List<String[]> upper_data_list,List<String[]> li_data_list){
  71. this.upper_data_list = upper_data_list;
  72. this.li_data_list = li_data_list;
  73. }
  74. private void setWorkBook() throws Exception{
  75. wb = new XSSFWorkbook(); //office 7,10
  76. if(logger.isDebugEnabled())
  77. logger.debug("setWorkBook --data--li: "+li_data_list.size()+" upper :"+upper_data_list.size() );
  78. setWB();
  79. }
  80. private void setWB() throws Exception{
  81. XSSFSheet sheet1 = wb.createSheet("Upper Form");
  82. sheet1.setDisplayGridlines(true);
  83. XSSFSheet sheet2 = null;
  84. cellHeaderStyle = getCellHeaderStyle();
  85. cellDataStyle = getCellDataStyle();
  86. sheet1.setDefaultColumnWidth(30);
  87. // sheet1.setColumnWidth(1, 12);
  88. createHelper = wb.getCreationHelper();
  89. setDEMap();
  90. createSheetData( sheet1, this.bpdelist,"upper");
  91. if(bptemp.getBLIPMap().size() > 0){
  92. sheet2 = wb.createSheet("Line Items");
  93. sheet2.setDefaultColumnWidth(30);
  94. sheet2.setDisplayGridlines(true);
  95. // sheet2.setColumnWidth(1, 12);
  96. createSheetData( sheet2, this.bplidelist,"lineitem");
  97. }
  98. XSSFSheet sheet3 = wb.createSheet("Instructions");
  99. sheet3.setDefaultColumnWidth(120);
  100. // inst
  101. setInstructions(sheet3,createHelper, getInstructionCellStyle(wb));
  102. if(setResponse && batch){
  103. XSSFSheet sheet4 = wb.createSheet("Status");
  104. sheet4.setDefaultColumnWidth(50);
  105. }
  106. }
  107. private void setDEMap(){
  108. for(Map<String,Object> map : this.bptemp.getDEList())
  109. demap.put((String)map.get("Name"), map);
  110. Map<String,Object> m = new HashMap<String,Object>();
  111. m.put("Label", "Sr No.");
  112. demap.put("srno",m);
  113. bpdelist.add("srno");
  114. if(setResponse){
  115. bpdelist.add("statuscode");
  116. bpdelist.add("errordetails");
  117. m = new HashMap<String,Object>();
  118. m.put("Label", "Status Code");
  119. demap.put("statuscode",m);
  120. m = new HashMap<String,Object>();
  121. m.put("Label", "Error Details");
  122. demap.put("errordetails",m);
  123. }
  124. m = new HashMap<String,Object>();
  125. m.put("Label", "Project Number");
  126. demap.put("project_no",m);
  127. m = new HashMap<String,Object>();
  128. m.put("Label", "Web Service Method");
  129. demap.put("method",m);
  130. bpdelist.add("method");
  131. bpdelist.add("project_no");
  132. Iterator<String> it = bptemp.getBPMap().keySet().iterator();
  133. while(it.hasNext())
  134. bpdelist.add(it.next());
  135. bplidelist.add("srno");
  136. it = bptemp.getBLIPMap().keySet().iterator();
  137. while(it.hasNext())
  138. bplidelist.add(it.next());
  139. }
  140. private void createSheetData(XSSFSheet sheet, List<String> delist, String type) throws Exception {
  141. short rowindex = 0;
  142. XSSFRow row = sheet.createRow(rowindex);
  143. row.setHeightInPoints((short)45);
  144. // setColumns(columnIndex,rowindex,row,createHelper,cs,"srno");
  145. setColumnHeaders(row,delist);
  146. if(saveData && "upper".equals(type)){
  147. for(short i = 0; i < this.upper_data_list.size(); i++ ){
  148. row = sheet.createRow(i+1);
  149. row.setHeightInPoints((short)30);
  150. setColumnData(row,upper_data_list.get(i) );
  151. }
  152. }else if(saveData && "lineitem".equals(type)){
  153. if(logger.isDebugEnabled())
  154. logger.debug("createSheetData --data-- "+li_data_list.size() );
  155. for(short i = 0; i < this.li_data_list.size(); i++ ){
  156. row = sheet.createRow(i+1);
  157. row.setHeightInPoints((short)30);
  158. setColumnData(row,li_data_list.get(i) );
  159. }
  160. }
  161. }
  162. public void setSaveData(boolean savedata){
  163. this.saveData = savedata;
  164. }
  165. private void setColumnHeaders( XSSFRow row, List<String> delist){
  166. if(logger.isDebugEnabled())
  167. logger.debug("setColumns ---- "+delist.size() );
  168. for( int columnIndex = 0; columnIndex < delist.size(); columnIndex++){
  169. String dename = delist.get(columnIndex);
  170. // if(logger.isDebugEnabled())
  171. // logger.debug("column ----chk "+columnIndex +" de:"+dename);
  172. //String label = (String)demap.get(dename).get("Label");
  173. if(!demap.containsKey(dename)){
  174. Map<String,Object> m = new HashMap<String,Object>();
  175. m.put("Label", dename);
  176. demap.put(dename, m);
  177. }
  178. Map<String,Object> map = demap.get(dename);
  179. String value = (String) map.get("Label");
  180. if(logger.isDebugEnabled())
  181. logger.debug("column "+columnIndex +" de:"+dename+" :"+value);
  182. String mandatory = (String) map.get("Mandatory");
  183. if(mandatory != null && mandatory.equalsIgnoreCase("true"))
  184. mandatory ="*";
  185. else
  186. mandatory = "";
  187. String inputtype = (String) map.get("InputType");
  188. XSSFCell cell = row.createCell(columnIndex);
  189. value +="\n("+dename+") " +mandatory;
  190. if(inputtype != null)
  191. value +="\n["+inputtype+"]";
  192. cell.setCellStyle(cellHeaderStyle);
  193. cell.setCellType( Cell.CELL_TYPE_STRING);
  194. cell.setCellValue(createHelper.createRichTextString(value)) ;
  195. }
  196. }
  197. private void setColumnData( XSSFRow row, String[] data){
  198. // if(logger.isDebugEnabled())
  199. // logger.debug("setColumns --data-- "+data.length );
  200. for( int columnIndex = 0; columnIndex < data.length; columnIndex++){
  201. String value = data[columnIndex];
  202. // if(logger.isDebugEnabled())
  203. // logger.debug("column ----data "+columnIndex +" de:"+value);
  204. XSSFCell cell = row.createCell(columnIndex);
  205. cell.setCellStyle(cellDataStyle);
  206. cell.setCellType( Cell.CELL_TYPE_STRING);
  207. cell.setCellValue(createHelper.createRichTextString(value)) ;
  208. }
  209. }
  210. private void setInstructions(XSSFSheet sheet, CreationHelper createHelper, XSSFCellStyle cs){
  211. short rowindex = 0;
  212. String value = "Do not modify any column header titles or rearrange the columns";
  213. createIntructionRow(sheet,createHelper,cs,rowindex++,value);
  214. value = "Do not modify the name of this file";
  215. createIntructionRow(sheet,createHelper,cs,rowindex++,value);
  216. value = "";
  217. createIntructionRow(sheet,createHelper,cs,rowindex++,value);
  218. value = "BP Name : "+this.studiomap.get("studio_name");
  219. createIntructionRow(sheet,createHelper,cs,rowindex++,value);
  220. value = "";
  221. createIntructionRow(sheet,createHelper,cs,rowindex++,value);
  222. value = "Column with ex (record_no) , where record_no is the data element name, * indicates required ";
  223. createIntructionRow(sheet,createHelper,cs,rowindex++,value);
  224. value = "";
  225. createIntructionRow(sheet,createHelper,cs,rowindex++,value);
  226. value = "Column with ex [text] indicates the type of data , they can be of type timestamp/picker/float etc";
  227. createIntructionRow(sheet,createHelper,cs,rowindex++,value);
  228. value = "";
  229. createIntructionRow(sheet,createHelper,cs,rowindex++,value);
  230. value = "Timestamp or Date fields format is yyyy/mm/dd hh:mm:ss or yyyy/mm/dd ";
  231. createIntructionRow(sheet,createHelper,cs,rowindex++,value);
  232. value = "";
  233. createIntructionRow(sheet,createHelper,cs,rowindex++,value);
  234. value = "Sr No -- Serial Number on the upper form identifies a row in the upper form";
  235. createIntructionRow(sheet,createHelper,cs,rowindex++,value);
  236. value = "Sr No -- on the lineitems identifies this line item with the same serial number row in the upper form";
  237. createIntructionRow(sheet,createHelper,cs,rowindex++,value);
  238. value = "It is efficient to run in batch mode of 10 records, for that the service name & project number for project level";
  239. createIntructionRow(sheet,createHelper,cs,rowindex++,value);
  240. value = "should be same, when you select run service, select run as batch. ";
  241. createIntructionRow(sheet,createHelper,cs,rowindex++,value);
  242. value = "If you want to run as batch using directory service, rename this file with a _batch";
  243. createIntructionRow(sheet,createHelper,cs,rowindex++,value);
  244. createIntructionRow(sheet,createHelper,cs,rowindex++,value);
  245. value =" ex unifier_uo_bp.xslx change to unifier_uo_bp_batch.xslx ";
  246. if(logger.isDebugEnabled())
  247. logger.debug("setInstructions "+rowindex );
  248. }
  249. private void setBatchStatus(XSSFSheet sheet, CreationHelper createHelper, XSSFCellStyle cs){
  250. short rowindex = 0;
  251. String value = "Do not modify any column header titles or rearrange the columns";
  252. createIntructionRow(sheet,createHelper,cs,rowindex++,value);
  253. }
  254. private void createBatchStatusRow(XSSFSheet sheet, CreationHelper createHelper, XSSFCellStyle cs, short rowindex, String value){
  255. rowindex++;
  256. XSSFRow row = sheet.createRow(rowindex);
  257. row.setHeightInPoints((short)25);
  258. XSSFCell cell = row.createCell(0);
  259. cell.setCellStyle(cs);
  260. cell.setCellValue(createHelper.createRichTextString(value)) ;
  261. cell = row.createCell(1);
  262. cell.setCellStyle(cs);
  263. cell.setCellValue(createHelper.createRichTextString(value)) ;
  264. }
  265. private void createIntructionRow(XSSFSheet sheet, CreationHelper createHelper, XSSFCellStyle cs, short rowindex, String value){
  266. rowindex++;
  267. XSSFRow row = sheet.createRow(rowindex);
  268. row.setHeightInPoints((short)25);
  269. XSSFCell cell = row.createCell(0);
  270. cell.setCellStyle(cs);
  271. cell.setCellValue(createHelper.createRichTextString(value)) ;
  272. }
  273. private XSSFCellStyle getCellHeaderStyle(){
  274. XSSFCellStyle cs = this.wb.createCellStyle();
  275. cs.setWrapText(true);
  276. java.awt.Color c = new java.awt.Color(220,245,245);
  277. XSSFColor color = new XSSFColor(c);
  278. cs.setBorderBottom(XSSFCellStyle.BORDER_THIN);
  279. cs.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  280. cs.setBorderLeft(XSSFCellStyle.BORDER_THIN);
  281. cs.setLeftBorderColor(IndexedColors.BLACK.getIndex());
  282. cs.setFillForegroundColor(color);
  283. cs.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
  284. XSSFFont font = this.wb.createFont();
  285. font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
  286. cs.setFont(font);
  287. return cs;
  288. }
  289. private XSSFCellStyle getCellDataStyle(){
  290. XSSFCellStyle cs = this.wb.createCellStyle();
  291. cs.setWrapText(true);
  292. cs.setBorderBottom(XSSFCellStyle.BORDER_THIN);
  293. cs.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  294. cs.setBorderLeft(XSSFCellStyle.BORDER_THIN);
  295. cs.setLeftBorderColor(IndexedColors.BLACK.getIndex());
  296. // cs.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
  297. return cs;
  298. }
  299. private XSSFCellStyle getInstructionCellStyle(XSSFWorkbook wb){
  300. XSSFCellStyle cs = wb.createCellStyle();
  301. cs.setWrapText(true);
  302. java.awt.Color c = new java.awt.Color(245,233,162);
  303. XSSFColor color = new XSSFColor(c);
  304. cs.setBorderBottom(XSSFCellStyle.BORDER_THIN);
  305. cs.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  306. cs.setBorderLeft(XSSFCellStyle.BORDER_THIN);
  307. cs.setLeftBorderColor(IndexedColors.BLACK.getIndex());
  308. cs.setFillForegroundColor(color);
  309. cs.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
  310. XSSFFont font = wb.createFont();
  311. font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
  312. cs.setFont(font);
  313. return cs;
  314. }
  315. private void writeWBToFile(String filename) throws Exception{
  316. if(logger.isDebugEnabled())
  317. logger.debug("writeWBToFile file:"+filename);
  318. FileOutputStream fileOut = new FileOutputStream(filename);
  319. wb.write(fileOut);
  320. fileOut.close();
  321. }
  322. public void setIsResponse(boolean setResponse){
  323. this.setResponse = setResponse;
  324. }
  325. public void setBatch(boolean batch, String[] resp){
  326. this.batch = batch;
  327. this.batchResp = resp;
  328. }
  329. public static void main(String[] args) {
  330. // TODO Auto-generated method stub
  331. try{
  332. // WorkBook wb = new WorkBook();
  333. // wb.readWB();
  334. }catch(Exception e){
  335. e.printStackTrace();
  336. }
  337. }
  338. }