PageRenderTime 39ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/converge-server/modules/converge-ejb/src/main/java/dk/i2m/converge/ejb/facades/ReportingFacadeBean.java

https://bitbucket.org/interactivemediamanagement/converge-1.x
Java | 345 lines | 273 code | 45 blank | 27 comment | 34 complexity | 636c34296d7765446efc9ce06366bf3f MD5 | raw file
  1. /*
  2. * Copyright (C) 2011 Interactive Media Management
  3. *
  4. * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  5. *
  6. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  7. *
  8. * You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
  9. */
  10. package dk.i2m.converge.ejb.facades;
  11. import dk.i2m.converge.core.content.NewsItem;
  12. import dk.i2m.converge.core.reporting.activity.ActivityReport;
  13. import dk.i2m.converge.core.reporting.activity.UserActivity;
  14. import dk.i2m.converge.core.reporting.activity.UserActivitySummary;
  15. import dk.i2m.converge.core.security.UserAccount;
  16. import dk.i2m.converge.core.security.UserRole;
  17. import dk.i2m.converge.ejb.services.DaoServiceLocal;
  18. import dk.i2m.converge.ejb.services.QueryBuilder;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import java.text.SimpleDateFormat;
  22. import java.util.Date;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import javax.ejb.EJB;
  28. import javax.ejb.Stateless;
  29. import org.apache.poi.hssf.usermodel.HSSFSheet;
  30. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  31. import org.apache.poi.hssf.usermodel.HeaderFooter;
  32. import org.apache.poi.ss.usermodel.*;
  33. import org.apache.poi.ss.util.WorkbookUtil;
  34. /**
  35. * Stateless Enterprise JavaBean implementing the reporting facade.
  36. *
  37. * @author Allan Lykke Christensen
  38. */
  39. @Stateless
  40. public class ReportingFacadeBean implements ReportingFacadeLocal {
  41. @EJB private DaoServiceLocal daoService;
  42. @EJB private SystemFacadeLocal systemFacade;
  43. private static final Logger LOG = Logger.getLogger(ReportingFacadeBean.class.getName());
  44. @Override
  45. public ActivityReport generateActivityReport(Date start, Date end, UserRole userRole, boolean submitter) {
  46. Long taskId = systemFacade.createBackgroundTask("Generating activity report");
  47. ActivityReport report = new ActivityReport(start, end, userRole);
  48. Map<String, Object> parameters = QueryBuilder.with("userRole", userRole).and("startDate", start).and("endDate", end).parameters();
  49. String query;
  50. if (submitter) {
  51. query = UserAccount.FIND_ACTIVE_USERS_BY_ROLE;
  52. } else {
  53. query = UserAccount.FIND_PASSIVE_USERS_BY_ROLE;
  54. }
  55. List<UserAccount> users = daoService.findWithNamedQuery(query, parameters);
  56. for (UserAccount user : users) {
  57. report.getUserActivity().add(generateUserActivityReport(start, end, user, submitter));
  58. }
  59. systemFacade.removeBackgroundTask(taskId);
  60. return report;
  61. }
  62. /**
  63. * Generates the activity report for a single user.
  64. *
  65. * @param start
  66. * Start of activities
  67. * @param end
  68. * End of activities
  69. * @param user
  70. * {@link UserAccount} for which to retrieve the report
  71. * @return {@link UserActivity} for the given period and user
  72. */
  73. @Override
  74. public UserActivity generateUserActivityReport(Date start, Date end, UserAccount user, boolean submitter) {
  75. if (user == null) {
  76. return null;
  77. }
  78. Long taskId = systemFacade.createBackgroundTask("Generating user activity report for " + user.getFullName());
  79. Map<String, Object> param = QueryBuilder.with("startDate", start).and("endDate", end).and("user", user).parameters();
  80. List<NewsItem> items;
  81. if (submitter) {
  82. items = daoService.findWithNamedQuery(NewsItem.FIND_SUBMITTED_BY_USER, param);
  83. } else {
  84. items = daoService.findWithNamedQuery(NewsItem.FIND_SUBMITTED_BY_PASSIVE_USER, param);
  85. }
  86. UserActivity userActivity = new UserActivity();
  87. userActivity.setUser(user);
  88. userActivity.setSubmitted(items);
  89. systemFacade.removeBackgroundTask(taskId);
  90. return userActivity;
  91. }
  92. @Override
  93. public UserActivitySummary generateUserActivitySummary(Date start, Date end, UserAccount user) {
  94. UserActivitySummary summary = new UserActivitySummary(generateUserActivityReport(start, end, user, true));
  95. return summary;
  96. }
  97. @Override
  98. public byte[] convertToExcel(ActivityReport report) {
  99. SimpleDateFormat format = new SimpleDateFormat("d MMMM yyyy");
  100. HSSFWorkbook wb = new HSSFWorkbook();
  101. String sheetName = WorkbookUtil.createSafeSheetName("Overview");
  102. int overviewSheetRow = 0;
  103. Font headerFont = wb.createFont();
  104. headerFont.setFontHeightInPoints((short) 14);
  105. headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
  106. Font userFont = wb.createFont();
  107. userFont.setFontHeightInPoints((short) 12);
  108. userFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
  109. Font storyFont = wb.createFont();
  110. storyFont.setFontHeightInPoints((short) 12);
  111. storyFont.setBoldweight(Font.BOLDWEIGHT_NORMAL);
  112. CellStyle headerStyle = createHeaderStyle(wb, headerFont);
  113. CellStyle headerVerticalStyle = createHeaderVerticalStyle(wb, headerFont);
  114. CellStyle userStyle = createUserStyle(wb, userFont);
  115. CellStyle storyCenterStyle = createStoryCenterStyle(wb);
  116. CellStyle storyStyle = createStoryStyle(wb, storyFont);
  117. CellStyle percentStyle = createPercentStyle(wb, userFont);
  118. HSSFSheet overviewSheet = wb.createSheet(sheetName);
  119. Header sheetHeader = overviewSheet.getHeader();
  120. sheetHeader.setLeft("CONVERGE " + report.getUserRole().getName() + " Activity Sheet");
  121. sheetHeader.setRight(format.format(report.getStart()) + " - " + format.format(report.getEnd()));
  122. overviewSheet.createFreezePane(0, 1, 0, 1);
  123. Row row = overviewSheet.createRow(0);
  124. row.createCell(0).setCellValue("ID");
  125. row.createCell(1).setCellValue("Name / Title");
  126. row.createCell(2).setCellValue("Word Count");
  127. row.createCell(3).setCellValue("Submitted");
  128. row.createCell(4).setCellValue("With Media");
  129. row.createCell(5).setCellValue("Used");
  130. row.createCell(6).setCellValue("With Media");
  131. row.createCell(7).setCellValue("Used %");
  132. row.createCell(8).setCellValue("With Media %");
  133. for (int i = 0; i <= 8; i++) {
  134. if (i < 2) {
  135. row.getCell(i).setCellStyle(headerStyle);
  136. } else {
  137. row.getCell(i).setCellStyle(headerVerticalStyle);
  138. }
  139. }
  140. overviewSheetRow++;
  141. for (UserActivity userActivity : report.getUserActivity()) {
  142. row = overviewSheet.createRow(overviewSheetRow);
  143. row.createCell(0).setCellValue(userActivity.getUser().getUsername());
  144. row.createCell(1).setCellValue(userActivity.getUser().getFullName());
  145. row.createCell(2).setCellValue(userActivity.getTotalWordCount());
  146. row.createCell(3).setCellValue(userActivity.getNumberOfNewsItemsSubmitted());
  147. row.createCell(4).setCellValue(userActivity.getNumberOfNewsItemsSubmittedWithMediaItems());
  148. row.createCell(5).setCellValue(userActivity.getNumberOfNewsItemsUsed());
  149. row.createCell(6).setCellValue(userActivity.getNumberOfNewsItemsUsedWithMedia());
  150. row.createCell(7).setCellValue(userActivity.getUsage());
  151. row.createCell(8).setCellValue(userActivity.getUsageWithMedia());
  152. for (int i = 0; i <= 8; i++) {
  153. if (i == 7 || i == 8) {
  154. row.getCell(i).setCellStyle(percentStyle);
  155. } else {
  156. row.getCell(i).setCellStyle(userStyle);
  157. }
  158. }
  159. overviewSheetRow++;
  160. int startRow = overviewSheetRow;
  161. for (NewsItem submitted : userActivity.getSubmitted()) {
  162. row = overviewSheet.createRow(overviewSheetRow);
  163. row.createCell(0).setCellValue(submitted.getId());
  164. row.createCell(1).setCellValue(submitted.getTitle());
  165. row.createCell(2).setCellValue(submitted.getWordCount());
  166. row.createCell(3).setCellValue("X");
  167. if (userActivity.getSubmittedWithMedia().contains(submitted)) {
  168. row.createCell(4).setCellValue("X");
  169. } else {
  170. row.createCell(4).setCellValue("");
  171. }
  172. if (userActivity.getUsed().contains(submitted)) {
  173. row.createCell(5).setCellValue("X");
  174. } else {
  175. row.createCell(5).setCellValue("");
  176. }
  177. if (userActivity.getUsedWithMedia().contains(submitted)) {
  178. row.createCell(6).setCellValue("X");
  179. } else {
  180. row.createCell(6).setCellValue("");
  181. }
  182. for (int i = 0; i <= 6; i++) {
  183. row.getCell(i).setCellStyle(storyStyle);
  184. if (i == 3 || i == 4 || i == 5 || i == 6) {
  185. row.getCell(i).setCellStyle(storyCenterStyle);
  186. }
  187. }
  188. overviewSheetRow++;
  189. }
  190. int endRow = overviewSheetRow - 1;
  191. overviewSheet.groupRow(startRow, endRow);
  192. overviewSheet.setRowGroupCollapsed(startRow, true);
  193. }
  194. // Auto-size
  195. for (int i = 0; i <= 8; i++) {
  196. overviewSheet.autoSizeColumn(i);
  197. }
  198. wb.setRepeatingRowsAndColumns(0, 0, 0, 0, 0);
  199. wb.setPrintArea(0, 0, 8, 0, overviewSheetRow);
  200. //overviewSheet.setFitToPage(true);
  201. overviewSheet.setAutobreaks(true);
  202. overviewSheet.getPrintSetup().setFitWidth((short) 1);
  203. overviewSheet.getPrintSetup().setFitHeight((short) 500);
  204. Footer footer = overviewSheet.getFooter();
  205. footer.setLeft("Page " + HeaderFooter.page() + " of " + HeaderFooter.numPages());
  206. footer.setRight("Generated on " + HeaderFooter.date() + " at " + HeaderFooter.time());
  207. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  208. try {
  209. wb.write(baos);
  210. } catch (IOException ex) {
  211. LOG.log(Level.SEVERE, null, ex);
  212. }
  213. return baos.toByteArray();
  214. }
  215. private CellStyle createUserStyle(HSSFWorkbook wb, Font userFont) {
  216. CellStyle style = wb.createCellStyle();
  217. style.setBorderBottom(CellStyle.BORDER_THIN);
  218. style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  219. style.setBorderTop(CellStyle.BORDER_THIN);
  220. style.setTopBorderColor(IndexedColors.BLACK.getIndex());
  221. style.setBorderLeft(CellStyle.BORDER_THIN);
  222. style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
  223. style.setBorderRight(CellStyle.BORDER_THIN);
  224. style.setRightBorderColor(IndexedColors.BLACK.getIndex());
  225. style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
  226. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  227. style.setFont(userFont);
  228. return style;
  229. }
  230. private CellStyle createStoryStyle(HSSFWorkbook wb, Font font) {
  231. CellStyle style = wb.createCellStyle();
  232. style.setBorderBottom(CellStyle.BORDER_THIN);
  233. style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  234. style.setBorderTop(CellStyle.BORDER_THIN);
  235. style.setTopBorderColor(IndexedColors.BLACK.getIndex());
  236. style.setBorderLeft(CellStyle.BORDER_THIN);
  237. style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
  238. style.setBorderRight(CellStyle.BORDER_THIN);
  239. style.setRightBorderColor(IndexedColors.BLACK.getIndex());
  240. style.setFont(font);
  241. return style;
  242. }
  243. private CellStyle createStoryCenterStyle(HSSFWorkbook wb) {
  244. CellStyle style = wb.createCellStyle();
  245. style.setBorderBottom(CellStyle.BORDER_THIN);
  246. style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  247. style.setBorderTop(CellStyle.BORDER_THIN);
  248. style.setTopBorderColor(IndexedColors.BLACK.getIndex());
  249. style.setBorderLeft(CellStyle.BORDER_THIN);
  250. style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
  251. style.setBorderRight(CellStyle.BORDER_THIN);
  252. style.setRightBorderColor(IndexedColors.BLACK.getIndex());
  253. style.setAlignment(CellStyle.ALIGN_CENTER);
  254. return style;
  255. }
  256. private CellStyle createHeaderStyle(HSSFWorkbook wb, Font headerFont) {
  257. CellStyle header = wb.createCellStyle();
  258. header.setBorderBottom(CellStyle.BORDER_THIN);
  259. header.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  260. header.setBorderTop(CellStyle.BORDER_THIN);
  261. header.setTopBorderColor(IndexedColors.BLACK.getIndex());
  262. header.setBorderLeft(CellStyle.BORDER_THIN);
  263. header.setLeftBorderColor(IndexedColors.BLACK.getIndex());
  264. header.setBorderRight(CellStyle.BORDER_THIN);
  265. header.setRightBorderColor(IndexedColors.BLACK.getIndex());
  266. header.setFont(headerFont);
  267. return header;
  268. }
  269. private CellStyle createHeaderVerticalStyle(HSSFWorkbook wb, Font headerFont) {
  270. CellStyle header = wb.createCellStyle();
  271. header.setBorderBottom(CellStyle.BORDER_THIN);
  272. header.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  273. header.setBorderTop(CellStyle.BORDER_THIN);
  274. header.setTopBorderColor(IndexedColors.BLACK.getIndex());
  275. header.setBorderLeft(CellStyle.BORDER_THIN);
  276. header.setLeftBorderColor(IndexedColors.BLACK.getIndex());
  277. header.setBorderRight(CellStyle.BORDER_THIN);
  278. header.setRightBorderColor(IndexedColors.BLACK.getIndex());
  279. header.setRotation((short) 90);
  280. header.setFont(headerFont);
  281. return header;
  282. }
  283. private CellStyle createPercentStyle(HSSFWorkbook wb, Font font) {
  284. CellStyle style = wb.createCellStyle();
  285. CreationHelper creationHelper = wb.getCreationHelper();
  286. style.setDataFormat(creationHelper.createDataFormat().getFormat("0%"));
  287. style.setBorderBottom(CellStyle.BORDER_THIN);
  288. style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  289. style.setBorderTop(CellStyle.BORDER_THIN);
  290. style.setTopBorderColor(IndexedColors.BLACK.getIndex());
  291. style.setBorderLeft(CellStyle.BORDER_THIN);
  292. style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
  293. style.setBorderRight(CellStyle.BORDER_THIN);
  294. style.setRightBorderColor(IndexedColors.BLACK.getIndex());
  295. style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
  296. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  297. style.setFont(font);
  298. return style;
  299. }
  300. }