Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.12 KB | None | 0 0
  1. private static Paragraph getParagraph(Context context, int size, int style, @StringRes int text) {
  2. return getParagraph(size, style, context.getString(text));
  3. }
  4.  
  5. private static Paragraph getParagraph(int size, int style, String text) {
  6. Font font = new Font(Font.FontFamily.HELVETICA, size, style);
  7. return new Paragraph(text, font);
  8. }
  9.  
  10. private static PdfPCell getCell(Context context, int size, int style, @StringRes int text) {
  11. return getCell(size, style, context.getString(text));
  12. }
  13.  
  14. private static PdfPCell getCell(int size, int style, String text) {
  15. Font font = new Font(Font.FontFamily.HELVETICA, size, style);
  16. Phrase phrase = new Phrase(new Chunk(text, font));
  17. return new PdfPCell(phrase);
  18. }
  19.  
  20. private static PdfPTable getTable(int columns) {
  21. PdfPTable table = new PdfPTable(columns);
  22. table.setWidthPercentage(100);
  23. table.setSpacingBefore(16);
  24. return table;
  25. }
  26.  
  27. public static void generate(final Context context, final long systemId, final String fileName, final EnvoyReport envoyReport) {
  28. Document doc = PdfHelper.createPdfDocument(Constants.FilePath.REPORTS, fileName);
  29. try {
  30. doc.open();
  31.  
  32. int textSize = 14;
  33. int titleSize = 16;
  34.  
  35. doc.add(getParagraph(context, titleSize + 8, Font.NORMAL, R.string.envoy_report_title));
  36. doc.add(getParagraph(textSize, Font.NORMAL, DateUtils.formatDateTime(envoyReport.getCreatedAt())));
  37. PdfHelper.addEmptyLine(doc, 1);
  38.  
  39. //ENVOY
  40. doc.add(getParagraph(context, titleSize, Font.BOLD, R.string.envoy));
  41.  
  42. PdfPTable table = getTable(3);
  43.  
  44. //row 1
  45. table.addCell(getCell(context, textSize, Font.BOLD, R.string.serial_number));
  46. table.addCell(getCell(context, textSize, Font.BOLD, R.string.software_version));
  47. table.addCell(getCell(context, textSize, Font.BOLD, R.string.type));
  48. //row 2
  49. table.addCell(envoyReport.getEnvoy().getDevice().getSerialNumber());
  50. table.addCell(envoyReport.getEnvoy().getSoftwareVersionFormatted());
  51. String envoyType = EnvoyHelper.getEnvoyType(context, envoyReport.getEnvoy().getDevice().getPartNumber());
  52. if (envoyType != null && envoyReport.getEnvoy().getDevice().isMetered()) {
  53. envoyType += (" " + context.getString(R.string.metered));
  54. }
  55. table.addCell(envoyType != null ? envoyType.replace("ENVOY", context.getString(R.string.envoy)) : "-");
  56. doc.add(table);
  57.  
  58. table = getTable(3);
  59. //row 1
  60. table.addCell(getCell(context, textSize, Font.BOLD, R.string.production_meter));
  61. table.addCell(getCell(context, textSize, Font.BOLD, R.string.consumption_meter));
  62. table.addCell(getCell(context, textSize, Font.BOLD, R.string.rate_structure));
  63. //row 2
  64. EnvoyReport.EnvoyReportMeters meters = envoyReport.getMeters();
  65. boolean hasProductionDetails = meters != null && meters.getProduction() != null;
  66. table.addCell(hasProductionDetails ? getMeterCell(context, meters.getProduction(), meters.getReadings()) : context.getString(R.string.disabled));
  67. boolean hasConsumptionDetails = meters != null && meters.getConsumption() != null;
  68. table.addCell(hasConsumptionDetails ? getMeterCell(context, meters.getConsumption(), meters.getReadings()) : context.getString(R.string.disabled));
  69. table.addCell(envoyReport.getTariff().getText(context));
  70. doc.add(table);
  71.  
  72. table = getTable(2);
  73. //row 1
  74. table.addCell(getCell(context, textSize, Font.BOLD, R.string.connection_to_enlighten));
  75. table.addCell(getCell(context, textSize, Font.BOLD, R.string.last_report_to_enlighten));
  76. //row 2
  77. table.addCell(envoyReport.getConnectionType().getText(context));
  78. table.addCell(envoyReport.getLastReportDate() != null ? DateUtils.formatDateTime(envoyReport.getLastReportDate()) : "-");
  79. doc.add(table);
  80.  
  81. Realm realm = Realm.getDefaultInstance();
  82. final System system = QueryUtils.findSystem(realm, systemId);
  83. if (system != null) {
  84. doc.add(getParagraph(context, titleSize, Font.BOLD, R.string.system));
  85.  
  86. table = getTable(3);
  87. //row 1
  88. table.addCell(getCell(context, textSize, Font.BOLD, R.string.name));
  89. table.addCell(getCell(context, textSize, Font.BOLD, R.string.installer_reference));
  90. table.addCell(getCell(context, textSize, Font.BOLD, R.string.owner));
  91. //row 2
  92. table.addCell(system.getName());
  93. table.addCell(system.getReference() != null ? system.getReference() : "-");
  94. table.addCell(system.getOwner() != null ? system.getOwner().getEmail() : "-");
  95. doc.add(table);
  96.  
  97. Address address = system.getAddress();
  98. if (address != null) {
  99. table = getTable(2);
  100. //row 1
  101. table.addCell(getCell(context, textSize, Font.BOLD, R.string.street1));
  102. table.addCell(getCell(context, textSize, Font.BOLD, R.string.street2));
  103. //row 2
  104. table.addCell(address.getStreet1());
  105. table.addCell(TextUtils.isEmpty(address.getStreet2()) ? "-" : address.getStreet2());
  106. //row 3
  107. if (!TextUtils.isEmpty(address.getCity()) || !TextUtils.isEmpty(address.getState())) {
  108. table.addCell(getCell(context, textSize, Font.BOLD, R.string.city));
  109. table.addCell(getCell(context, textSize, Font.BOLD, R.string.state));
  110. //row 4
  111. table.addCell(TextUtils.isEmpty(address.getCity()) ? "-" : address.getCity());
  112. table.addCell(TextUtils.isEmpty(address.getState()) ? "-" : address.getState());
  113. }
  114. //row 5
  115. table.addCell(getCell(context, textSize, Font.BOLD, R.string.postal_code));
  116. table.addCell(getCell(context, textSize, Font.BOLD, R.string.country));
  117. //row 6
  118. table.addCell(address.getPostalCode());
  119. table.addCell(new Locale("", address.getCountry()).getDisplayCountry());
  120. doc.add(table);
  121. }
  122. }
  123.  
  124. PdfHelper.addEmptyLine(doc, 2);
  125. int microinvertersCount = envoyReport.getMicroinverters().size();
  126. String microinverters = context.getResources().getQuantityString(R.plurals.n_microinverters, microinvertersCount, microinvertersCount);
  127. int batteriesCount = envoyReport.getBatteries().size();
  128. String batteries = context.getResources().getQuantityString(R.plurals.n_batteries, batteriesCount, batteriesCount);
  129. int qRelaysCount = envoyReport.getQRelays().size();
  130. String qRelays = context.getResources().getQuantityString(R.plurals.n_q_relays, qRelaysCount, qRelaysCount);
  131. doc.add(getParagraph(titleSize, Font.BOLD, context.getString(R.string.x_y_and_z, microinverters, batteries, qRelays)));
  132.  
  133. if (envoyReport.getAgfProfileResponse() != null && envoyReport.getAgfProfileResponse().getName() != null) {
  134. doc.add(getParagraph(titleSize, Font.NORMAL, envoyReport.getAgfProfileResponse().getName()));
  135. }
  136.  
  137. HashMap<DeviceType, ArrayList<InventoryResponse.InventoryDevice>> invertersByType = new HashMap<>();
  138. HashMap<String, ArrayList<InventoryResponse.InventoryDevice>> invertersByShortPartNumber = new HashMap<>();
  139. for (InventoryResponse.InventoryDevice inverter : envoyReport.getMicroinverters()) {
  140. if (inverter.getAgf() != null) {
  141. DeviceType type = getDeviceType(inverter.getAgf().getDeviceRecordType());
  142. if (!invertersByType.containsKey(type)) {
  143. invertersByType.put(type, new ArrayList<InventoryResponse.InventoryDevice>());
  144. }
  145. invertersByType.get(type).add(inverter);
  146. }
  147.  
  148. String pn = DeviceUtils.getShortPartNumber(inverter.getPartNumber());
  149. if (!invertersByShortPartNumber.containsKey(pn)) {
  150. invertersByShortPartNumber.put(pn, new ArrayList<InventoryResponse.InventoryDevice>());
  151. }
  152. invertersByShortPartNumber.get(pn).add(inverter);
  153. }
  154. for (String pn : invertersByShortPartNumber.keySet()) {
  155. PdfHelper.addEmptyLine(doc, 1);
  156. String type = Constants.microinvertersTypes.containsKey(pn) ? Constants.microinvertersTypes.get(pn) : context.getString(R.string.microinverters);
  157. int count = invertersByShortPartNumber.get(pn).size();
  158. doc.add(getParagraph(titleSize, Font.NORMAL, context.getResources().getQuantityString(R.plurals.n_microinverters, count, count) + " " + type));
  159.  
  160. table = getTable(3);
  161. table.addCell(getCell(context, textSize, Font.BOLD, R.string.serial_number));
  162. table.addCell(getCell(context, textSize, Font.BOLD, R.string.last_report));
  163. table.addCell(getCell(context, textSize, Font.BOLD, R.string.power));
  164. for (InventoryResponse.InventoryDevice report : invertersByShortPartNumber.get(pn)) {
  165. table.addCell(getCell(textSize, Font.NORMAL, report.getSerialNumber()));
  166. table.addCell(getCell(textSize, Font.NORMAL, DateUtils.formatDateTime(report.getLastReportDate())));
  167. table.addCell(getCell(textSize, Font.NORMAL, report.getProduction() != null ? Utils.formatMeasurement(context, report.getProduction().getLastReportWatts(), context.getString(R.string.w_unit)) : "-"));
  168. }
  169. doc.add(table);
  170. }
  171. if (!envoyReport.getBatteries().isEmpty()) {
  172. PdfHelper.addEmptyLine(doc, 1);
  173. doc.add(getParagraph(titleSize, Font.NORMAL, batteries));
  174.  
  175. table = getTable(3);
  176. table.addCell(getCell(context, textSize, Font.BOLD, R.string.serial_number));
  177. table.addCell(getCell(context, textSize, Font.BOLD, R.string.last_report));
  178. table.addCell(getCell(context, textSize, Font.BOLD, R.string.state_of_charge));
  179. for (InventoryResponse.InventoryDevice report : envoyReport.getBatteries()) {
  180. table.addCell(getCell(textSize, Font.NORMAL, report.getSerialNumber()));
  181. table.addCell(getCell(textSize, Font.NORMAL, DateUtils.formatDateTime(report.getLastReportDate())));
  182. table.addCell(getCell(textSize, Font.NORMAL, report.getPercentFull() != null ? report.getPercentFull() + "%" : "-"));
  183. }
  184. doc.add(table);
  185. }
  186. if (!envoyReport.getQRelays().isEmpty()) {
  187. PdfHelper.addEmptyLine(doc, 1);
  188. doc.add(getParagraph(titleSize, Font.NORMAL, qRelays));
  189.  
  190. table = getTable(2);
  191. table.addCell(getCell(context, textSize, Font.BOLD, R.string.serial_number));
  192. table.addCell(getCell(context, textSize, Font.BOLD, R.string.last_report));
  193. for (InventoryResponse.InventoryDevice report : envoyReport.getQRelays()) {
  194. table.addCell(getCell(textSize, Font.NORMAL, report.getSerialNumber()));
  195. table.addCell(getCell(textSize, Font.NORMAL, DateUtils.formatDateTime(report.getLastReportDate())));
  196. }
  197. doc.add(table);
  198. }
  199.  
  200. if (envoyReport.getAgfProfileResponse() != null) {
  201. HashMap<String, AgfProfileResponse.AgfProfileReportGroup> groups = envoyReport.getAgfProfileResponse().getGroups();
  202. if (groups != null && !groups.isEmpty()) {
  203. for (DeviceType type : invertersByType.keySet()) {
  204. boolean addTitle = true;
  205. for (AgfProfileResponse.AgfProfileReportGroup group : groups.values()) {
  206. table = getTable(2);
  207. table.addCell(getCell(context, textSize, Font.BOLD, R.string.parameter));
  208. table.addCell(getCell(context, textSize, Font.BOLD, R.string.value));
  209. boolean addTable = false;
  210. for (AgfProfileResponse.AgfProfileReportParameter parameter : group.getParameters().values()) {
  211. if (getDeviceType(parameter.getType()) != type) {
  212. continue;
  213. }
  214. addTable = true;
  215. table.addCell(getCell(textSize, Font.NORMAL, parameter.getLabel()));
  216. String value = parameter.getDisplayValue();
  217. if (!TextUtils.isEmpty(parameter.getUnits())) {
  218. value += " " + parameter.getUnits();
  219. }
  220. table.addCell(getCell(textSize, Font.NORMAL, value));
  221. }
  222. if (addTable) {
  223. if (addTitle) {
  224. addTitle = false;
  225. PdfHelper.addEmptyLine(doc, 1);
  226. String text = context.getString(type.getText());
  227. if (type == DeviceType.S_SERIES) {
  228. text = context.getString(R.string.x_and_y, text, context.getString(DeviceType.IQ_SERIES.getText()));
  229. }
  230. doc.add(getParagraph(titleSize, Font.BOLD, text + " - " + context.getString(R.string.settings)));
  231. }
  232. PdfHelper.addEmptyLine(doc, 1);
  233. doc.add(getParagraph(titleSize, Font.NORMAL, group.getLabel()));
  234. doc.add(table);
  235. }
  236. }
  237. }
  238. }
  239. }
  240.  
  241. PdfHelper.addEmptyLine(doc, 3);
  242. Paragraph p = getParagraph(textSize - 2, Font.NORMAL, context.getString(R.string.generated_by, context.getString(R.string.app_name)));
  243. p.setAlignment(Element.ALIGN_RIGHT);
  244. doc.add(p);
  245. p = getParagraph(textSize - 2, Font.NORMAL, context.getString(R.string.all_rights_reserved, Calendar.getInstance().get(Calendar.YEAR)));
  246. p.setAlignment(Element.ALIGN_RIGHT);
  247. doc.add(p);
  248. } catch (Exception e) {
  249. Log.e("EnvoyReportHelper", e.getLocalizedMessage());
  250. } finally {
  251. doc.close();
  252. }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement