Advertisement
Guest User

Untitled

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