PageRenderTime 168ms CodeModel.GetById 55ms RepoModel.GetById 12ms app.codeStats 1ms

/org.openscada.ae.ui.views.export.excel/src/org/openscada/ae/ui/views/export/excel/impl/ExportEventsImpl.java

https://bitbucket.org/ctron/org.openscada.orilla
Java | 333 lines | 260 code | 55 blank | 18 comment | 21 complexity | e281103aee607d2e4060b4cc78a4ed02 MD5 | raw file
Possible License(s): LGPL-3.0
  1. /*
  2. * This file is part of the OpenSCADA project
  3. * Copyright (C) 2006-2010 TH4 SYSTEMS GmbH (http://th4-systems.com)
  4. *
  5. * OpenSCADA is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License version 3
  7. * only, as published by the Free Software Foundation.
  8. *
  9. * OpenSCADA is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License version 3 for more details
  13. * (a copy is included in the LICENSE file that accompanied this code).
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * version 3 along with OpenSCADA. If not, see
  17. * <http://opensource.org/licenses/lgpl-3.0.html> for a copy of the LGPLv3 License.
  18. */
  19. package org.openscada.ae.ui.views.export.excel.impl;
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.HashSet;
  26. import java.util.Iterator;
  27. import java.util.List;
  28. import java.util.Set;
  29. import org.apache.poi.hpsf.CustomProperties;
  30. import org.apache.poi.hpsf.DocumentSummaryInformation;
  31. import org.apache.poi.hssf.usermodel.HSSFCell;
  32. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  33. import org.apache.poi.hssf.usermodel.HSSFDataFormat;
  34. import org.apache.poi.hssf.usermodel.HSSFFooter;
  35. import org.apache.poi.hssf.usermodel.HSSFHeader;
  36. import org.apache.poi.hssf.usermodel.HSSFPatternFormatting;
  37. import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
  38. import org.apache.poi.hssf.usermodel.HSSFRow;
  39. import org.apache.poi.hssf.usermodel.HSSFSheet;
  40. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  41. import org.apache.poi.hssf.usermodel.HeaderFooter;
  42. import org.apache.poi.hssf.util.HSSFColor;
  43. import org.apache.poi.ss.usermodel.CellStyle;
  44. import org.apache.poi.ss.usermodel.Font;
  45. import org.apache.poi.ss.usermodel.PrintSetup;
  46. import org.apache.poi.ss.usermodel.Sheet;
  47. import org.apache.poi.ss.util.CellRangeAddress;
  48. import org.eclipse.core.runtime.IProgressMonitor;
  49. import org.eclipse.core.runtime.IStatus;
  50. import org.eclipse.core.runtime.Status;
  51. import org.eclipse.jface.viewers.IStructuredSelection;
  52. import org.openscada.ae.Event;
  53. import org.openscada.ae.ui.views.export.excel.Activator;
  54. import org.openscada.ae.ui.views.export.excel.Cell;
  55. import org.openscada.ae.ui.views.export.excel.FileTarget;
  56. import org.openscada.ae.ui.views.export.excel.config.DynamicField;
  57. import org.openscada.ae.ui.views.export.excel.config.Field;
  58. import org.openscada.ae.ui.views.export.excel.config.StaticField;
  59. import org.openscada.ui.databinding.AdapterHelper;
  60. import org.slf4j.Logger;
  61. import org.slf4j.LoggerFactory;
  62. public class ExportEventsImpl implements FileTarget
  63. {
  64. private final static Logger logger = LoggerFactory.getLogger ( ExportEventsImpl.class );
  65. private IStructuredSelection selection;
  66. private File file;
  67. public ExportEventsImpl ()
  68. {
  69. }
  70. public void setSelection ( final IStructuredSelection selection )
  71. {
  72. this.selection = selection;
  73. }
  74. @Override
  75. public void setFile ( final File file )
  76. {
  77. this.file = file;
  78. }
  79. public void check ()
  80. {
  81. if ( this.file == null )
  82. {
  83. throw new IllegalStateException ( Messages.ExportImpl_ErrorMessage_NoFileSelected );
  84. }
  85. if ( this.selection == null )
  86. {
  87. throw new IllegalStateException ( Messages.ExportImpl_ErrorMessage_NoEventsSelected );
  88. }
  89. }
  90. public IStatus write ( final IProgressMonitor monitor )
  91. {
  92. if ( this.selection == null )
  93. {
  94. return Status.OK_STATUS;
  95. }
  96. if ( this.file.exists () )
  97. {
  98. if ( !this.file.delete () )
  99. {
  100. return new Status ( IStatus.ERROR, Activator.PLUGIN_ID, String.format ( Messages.ExportImpl_ErrorMessage_FailedToDeleteFile, this.file ) );
  101. }
  102. }
  103. final List<Event> events = new ArrayList<Event> ();
  104. final Iterator<?> i = this.selection.iterator ();
  105. while ( i.hasNext () )
  106. {
  107. final Event e = AdapterHelper.adapt ( i.next (), Event.class );
  108. if ( e != null )
  109. {
  110. events.add ( e );
  111. }
  112. }
  113. try
  114. {
  115. return storeExcel ( this.file, events, getFields ( events ), monitor );
  116. }
  117. catch ( final Exception e )
  118. {
  119. logger.warn ( Messages.ExportImpl_ErrorMessage_FailedToExport, e );
  120. return new Status ( IStatus.ERROR, Activator.PLUGIN_ID, Messages.ExportImpl_ErrorMessage_FailedToExport, e );
  121. }
  122. }
  123. private List<Field> getFields ( final Collection<Event> events )
  124. {
  125. final Set<Field> fields = new HashSet<Field> ();
  126. fields.add ( new StaticField ( "id" ) { //$NON-NLS-1$
  127. @Override
  128. public void render ( final Event event, final Cell cell )
  129. {
  130. cell.setDataAsText ( event.getId ().toString () );
  131. }
  132. } );
  133. fields.add ( new StaticField ( "sourceTimestamp" ) { //$NON-NLS-1$
  134. @Override
  135. public void render ( final Event event, final Cell cell )
  136. {
  137. cell.setDataAsDate ( event.getSourceTimestamp () );
  138. }
  139. } );
  140. fields.add ( new StaticField ( "entryTimestamp" ) { //$NON-NLS-1$
  141. @Override
  142. public void render ( final Event event, final Cell cell )
  143. {
  144. cell.setDataAsDate ( event.getEntryTimestamp () );
  145. }
  146. } );
  147. for ( final Event event : events )
  148. {
  149. for ( final String key : event.getAttributes ().keySet () )
  150. {
  151. fields.add ( new DynamicField ( key ) );
  152. }
  153. }
  154. return new ArrayList<Field> ( fields );
  155. }
  156. private IStatus storeExcel ( final File file, final List<Event> events, final List<Field> columns, final IProgressMonitor monitor ) throws IOException
  157. {
  158. final HSSFWorkbook workbook = new HSSFWorkbook ();
  159. final HSSFDataFormat dateFormat = workbook.createDataFormat ();
  160. final HSSFCellStyle dateCellStyle = workbook.createCellStyle ();
  161. dateCellStyle.setDataFormat ( dateFormat.getFormat ( "YYYY-MM-DD hh:mm:ss.000" ) );
  162. try
  163. {
  164. monitor.beginTask ( Messages.ExportImpl_Progress_ExportingEvents, events.size () + 3 + columns.size () );
  165. try
  166. {
  167. monitor.subTask ( Messages.ExportImpl_Progress_CreateWorkbook );
  168. monitor.worked ( 1 );
  169. final HSSFSheet sheet = createSheet ( events, workbook, columns );
  170. monitor.worked ( 1 );
  171. monitor.setTaskName ( Messages.ExportImpl_Progress_ExportEvents );
  172. for ( int i = 0; i < events.size (); i++ )
  173. {
  174. final HSSFRow row = sheet.createRow ( i + 1 );
  175. final Event e = events.get ( i );
  176. for ( int j = 0; j < columns.size (); j++ )
  177. {
  178. final Field field = columns.get ( j );
  179. final ExcelCell cell = new ExcelCell ( row, j, dateCellStyle );
  180. field.render ( e, cell );
  181. }
  182. monitor.worked ( 1 );
  183. if ( monitor.isCanceled () )
  184. {
  185. return Status.CANCEL_STATUS;
  186. }
  187. }
  188. workbook.setRepeatingRowsAndColumns ( 0, -1, -1, 0, 1 );
  189. monitor.setTaskName ( "Auto sizing" );
  190. for ( int i = 0; i < columns.size (); i++ )
  191. {
  192. monitor.subTask ( String.format ( "Auto sizing column: %s", columns.get ( i ).getHeader () ) );
  193. sheet.autoSizeColumn ( i );
  194. monitor.worked ( 1 );
  195. if ( monitor.isCanceled () )
  196. {
  197. return Status.CANCEL_STATUS;
  198. }
  199. }
  200. }
  201. finally
  202. {
  203. monitor.subTask ( Messages.ExportImpl_Progress_CloseFile );
  204. if ( workbook != null )
  205. {
  206. makeDocInfo ( workbook );
  207. final FileOutputStream stream = new FileOutputStream ( file );
  208. workbook.write ( stream );
  209. stream.close ();
  210. }
  211. monitor.worked ( 1 );
  212. }
  213. }
  214. finally
  215. {
  216. monitor.done ();
  217. }
  218. return Status.OK_STATUS;
  219. }
  220. private void makeDocInfo ( final HSSFWorkbook workbook )
  221. {
  222. workbook.createInformationProperties ();
  223. final DocumentSummaryInformation dsi = workbook.getDocumentSummaryInformation ();
  224. dsi.setCompany ( "TH4 SYSTEMS GmbH" );
  225. final CustomProperties cp = new CustomProperties ();
  226. cp.put ( "openSCADA Export Version", Activator.getDefault ().getBundle ().getVersion ().toString () );
  227. dsi.setCustomProperties ( cp );
  228. }
  229. private HSSFSheet createSheet ( final List<Event> events, final HSSFWorkbook workbook, final List<Field> columns )
  230. {
  231. final HSSFSheet sheet = workbook.createSheet ( Messages.ExportImpl_ExcelSheet_Name );
  232. final HSSFHeader header = sheet.getHeader ();
  233. header.setLeft ( Messages.ExportImpl_ExcelSheet_Header );
  234. header.setRight ( HeaderFooter.date () + " " + HeaderFooter.time () );//$NON-NLS-1$
  235. final HSSFFooter footer = sheet.getFooter ();
  236. footer.setLeft ( String.format ( Messages.ExportImpl_ExcelSheet_Footer_1, events.size () ) );
  237. footer.setRight ( Messages.ExportImpl_ExcelSheet_Footer_2 + HeaderFooter.page () + Messages.ExportImpl_ExcelSheet_Footer_3 + HeaderFooter.numPages () );
  238. makeHeader ( columns, sheet );
  239. final HSSFPrintSetup printSetup = sheet.getPrintSetup ();
  240. printSetup.setLandscape ( true );
  241. printSetup.setFitWidth ( (short)1 );
  242. printSetup.setFitHeight ( (short)0 );
  243. printSetup.setPaperSize ( PrintSetup.A4_PAPERSIZE );
  244. sheet.setAutoFilter ( new CellRangeAddress ( 0, 0, 0, columns.size () - 1 ) );
  245. sheet.createFreezePane ( 0, 1 );
  246. sheet.setFitToPage ( true );
  247. sheet.setAutobreaks ( true );
  248. printSetup.setFooterMargin ( 0.25 );
  249. sheet.setMargin ( Sheet.LeftMargin, 0.25 );
  250. sheet.setMargin ( Sheet.RightMargin, 0.25 );
  251. sheet.setMargin ( Sheet.TopMargin, 0.25 );
  252. sheet.setMargin ( Sheet.BottomMargin, 0.5 );
  253. return sheet;
  254. }
  255. private void makeHeader ( final List<Field> columns, final HSSFSheet sheet )
  256. {
  257. final Font font = sheet.getWorkbook ().createFont ();
  258. font.setFontName ( "Arial" );
  259. font.setBoldweight ( Font.BOLDWEIGHT_BOLD );
  260. font.setColor ( HSSFColor.WHITE.index );
  261. final CellStyle style = sheet.getWorkbook ().createCellStyle ();
  262. style.setFont ( font );
  263. style.setFillForegroundColor ( HSSFColor.BLACK.index );
  264. style.setFillPattern ( HSSFPatternFormatting.SOLID_FOREGROUND );
  265. final HSSFRow row = sheet.createRow ( 0 );
  266. for ( int i = 0; i < columns.size (); i++ )
  267. {
  268. final Field field = columns.get ( i );
  269. final HSSFCell cell = row.createCell ( i );
  270. cell.setCellValue ( field.getHeader () );
  271. cell.setCellStyle ( style );
  272. }
  273. }
  274. public File getFile ()
  275. {
  276. return this.file;
  277. }
  278. }