Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static Paragraph getParagraph(Context context, int size, int style, @StringRes int text) {
- return getParagraph(size, style, context.getString(text));
- }
- private static Paragraph getParagraph(int size, int style, String text) {
- Font font = new Font(Font.FontFamily.HELVETICA, size, style);
- return new Paragraph(text, font);
- }
- private static PdfPCell getCell(Context context, int size, int style, @StringRes int text) {
- return getCell(size, style, context.getString(text));
- }
- private static PdfPCell getCell(int size, int style, String text) {
- Font font = new Font(Font.FontFamily.HELVETICA, size, style);
- Phrase phrase = new Phrase(new Chunk(text, font));
- return new PdfPCell(phrase);
- }
- private static PdfPTable getTable(int columns) {
- PdfPTable table = new PdfPTable(columns);
- table.setWidthPercentage(100);
- table.setSpacingBefore(16);
- return table;
- }
- public static void generate(final Context context, final long systemId, final String fileName, final EnvoyReport envoyReport) {
- Document doc = PdfHelper.createPdfDocument(Constants.FilePath.REPORTS, fileName);
- try {
- doc.open();
- int textSize = 14;
- int titleSize = 16;
- doc.add(getParagraph(context, titleSize + 8, Font.NORMAL, R.string.envoy_report_title));
- doc.add(getParagraph(textSize, Font.NORMAL, DateUtils.formatDateTime(envoyReport.getCreatedAt())));
- PdfHelper.addEmptyLine(doc, 1);
- //ENVOY
- doc.add(getParagraph(context, titleSize, Font.BOLD, R.string.envoy));
- PdfPTable table = getTable(3);
- //row 1
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.serial_number));
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.software_version));
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.type));
- //row 2
- table.addCell(envoyReport.getEnvoy().getDevice().getSerialNumber());
- table.addCell(envoyReport.getEnvoy().getSoftwareVersionFormatted());
- String envoyType = EnvoyHelper.getEnvoyType(context, envoyReport.getEnvoy().getDevice().getPartNumber());
- if (envoyType != null && envoyReport.getEnvoy().getDevice().isMetered()) {
- envoyType += (" " + context.getString(R.string.metered));
- }
- table.addCell(envoyType != null ? envoyType.replace("ENVOY", context.getString(R.string.envoy)) : "-");
- doc.add(table);
- table = getTable(3);
- //row 1
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.production_meter));
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.consumption_meter));
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.rate_structure));
- //row 2
- EnvoyReport.EnvoyReportMeters meters = envoyReport.getMeters();
- boolean hasProductionDetails = meters != null && meters.getProduction() != null;
- table.addCell(hasProductionDetails ? getMeterCell(context, meters.getProduction(), meters.getReadings()) : context.getString(R.string.disabled));
- boolean hasConsumptionDetails = meters != null && meters.getConsumption() != null;
- table.addCell(hasConsumptionDetails ? getMeterCell(context, meters.getConsumption(), meters.getReadings()) : context.getString(R.string.disabled));
- table.addCell(envoyReport.getTariff().getText(context));
- doc.add(table);
- table = getTable(2);
- //row 1
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.connection_to_enlighten));
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.last_report_to_enlighten));
- //row 2
- table.addCell(envoyReport.getConnectionType().getText(context));
- table.addCell(envoyReport.getLastReportDate() != null ? DateUtils.formatDateTime(envoyReport.getLastReportDate()) : "-");
- doc.add(table);
- Realm realm = Realm.getDefaultInstance();
- final System system = QueryUtils.findSystem(realm, systemId);
- if (system != null) {
- doc.add(getParagraph(context, titleSize, Font.BOLD, R.string.system));
- table = getTable(3);
- //row 1
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.name));
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.installer_reference));
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.owner));
- //row 2
- table.addCell(system.getName());
- table.addCell(system.getReference() != null ? system.getReference() : "-");
- table.addCell(system.getOwner() != null ? system.getOwner().getEmail() : "-");
- doc.add(table);
- Address address = system.getAddress();
- if (address != null) {
- table = getTable(2);
- //row 1
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.street1));
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.street2));
- //row 2
- table.addCell(address.getStreet1());
- table.addCell(TextUtils.isEmpty(address.getStreet2()) ? "-" : address.getStreet2());
- //row 3
- if (!TextUtils.isEmpty(address.getCity()) || !TextUtils.isEmpty(address.getState())) {
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.city));
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.state));
- //row 4
- table.addCell(TextUtils.isEmpty(address.getCity()) ? "-" : address.getCity());
- table.addCell(TextUtils.isEmpty(address.getState()) ? "-" : address.getState());
- }
- //row 5
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.postal_code));
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.country));
- //row 6
- table.addCell(address.getPostalCode());
- table.addCell(new Locale("", address.getCountry()).getDisplayCountry());
- doc.add(table);
- }
- }
- PdfHelper.addEmptyLine(doc, 2);
- int microinvertersCount = envoyReport.getMicroinverters().size();
- String microinverters = context.getResources().getQuantityString(R.plurals.n_microinverters, microinvertersCount, microinvertersCount);
- int batteriesCount = envoyReport.getBatteries().size();
- String batteries = context.getResources().getQuantityString(R.plurals.n_batteries, batteriesCount, batteriesCount);
- int qRelaysCount = envoyReport.getQRelays().size();
- String qRelays = context.getResources().getQuantityString(R.plurals.n_q_relays, qRelaysCount, qRelaysCount);
- doc.add(getParagraph(titleSize, Font.BOLD, context.getString(R.string.x_y_and_z, microinverters, batteries, qRelays)));
- if (envoyReport.getAgfProfileResponse() != null && envoyReport.getAgfProfileResponse().getName() != null) {
- doc.add(getParagraph(titleSize, Font.NORMAL, envoyReport.getAgfProfileResponse().getName()));
- }
- HashMap<DeviceType, ArrayList<InventoryResponse.InventoryDevice>> invertersByType = new HashMap<>();
- HashMap<String, ArrayList<InventoryResponse.InventoryDevice>> invertersByShortPartNumber = new HashMap<>();
- for (InventoryResponse.InventoryDevice inverter : envoyReport.getMicroinverters()) {
- if (inverter.getAgf() != null) {
- DeviceType type = getDeviceType(inverter.getAgf().getDeviceRecordType());
- if (!invertersByType.containsKey(type)) {
- invertersByType.put(type, new ArrayList<InventoryResponse.InventoryDevice>());
- }
- invertersByType.get(type).add(inverter);
- }
- String pn = DeviceUtils.getShortPartNumber(inverter.getPartNumber());
- if (!invertersByShortPartNumber.containsKey(pn)) {
- invertersByShortPartNumber.put(pn, new ArrayList<InventoryResponse.InventoryDevice>());
- }
- invertersByShortPartNumber.get(pn).add(inverter);
- }
- for (String pn : invertersByShortPartNumber.keySet()) {
- PdfHelper.addEmptyLine(doc, 1);
- String type = Constants.microinvertersTypes.containsKey(pn) ? Constants.microinvertersTypes.get(pn) : context.getString(R.string.microinverters);
- int count = invertersByShortPartNumber.get(pn).size();
- doc.add(getParagraph(titleSize, Font.NORMAL, context.getResources().getQuantityString(R.plurals.n_microinverters, count, count) + " " + type));
- table = getTable(3);
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.serial_number));
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.last_report));
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.power));
- for (InventoryResponse.InventoryDevice report : invertersByShortPartNumber.get(pn)) {
- table.addCell(getCell(textSize, Font.NORMAL, report.getSerialNumber()));
- table.addCell(getCell(textSize, Font.NORMAL, DateUtils.formatDateTime(report.getLastReportDate())));
- table.addCell(getCell(textSize, Font.NORMAL, report.getProduction() != null ? Utils.formatMeasurement(context, report.getProduction().getLastReportWatts(), context.getString(R.string.w_unit)) : "-"));
- }
- doc.add(table);
- }
- if (!envoyReport.getBatteries().isEmpty()) {
- PdfHelper.addEmptyLine(doc, 1);
- doc.add(getParagraph(titleSize, Font.NORMAL, batteries));
- table = getTable(3);
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.serial_number));
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.last_report));
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.state_of_charge));
- for (InventoryResponse.InventoryDevice report : envoyReport.getBatteries()) {
- table.addCell(getCell(textSize, Font.NORMAL, report.getSerialNumber()));
- table.addCell(getCell(textSize, Font.NORMAL, DateUtils.formatDateTime(report.getLastReportDate())));
- table.addCell(getCell(textSize, Font.NORMAL, report.getPercentFull() != null ? report.getPercentFull() + "%" : "-"));
- }
- doc.add(table);
- }
- if (!envoyReport.getQRelays().isEmpty()) {
- PdfHelper.addEmptyLine(doc, 1);
- doc.add(getParagraph(titleSize, Font.NORMAL, qRelays));
- table = getTable(2);
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.serial_number));
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.last_report));
- for (InventoryResponse.InventoryDevice report : envoyReport.getQRelays()) {
- table.addCell(getCell(textSize, Font.NORMAL, report.getSerialNumber()));
- table.addCell(getCell(textSize, Font.NORMAL, DateUtils.formatDateTime(report.getLastReportDate())));
- }
- doc.add(table);
- }
- if (envoyReport.getAgfProfileResponse() != null) {
- HashMap<String, AgfProfileResponse.AgfProfileReportGroup> groups = envoyReport.getAgfProfileResponse().getGroups();
- if (groups != null && !groups.isEmpty()) {
- for (DeviceType type : invertersByType.keySet()) {
- boolean addTitle = true;
- for (AgfProfileResponse.AgfProfileReportGroup group : groups.values()) {
- table = getTable(2);
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.parameter));
- table.addCell(getCell(context, textSize, Font.BOLD, R.string.value));
- boolean addTable = false;
- for (AgfProfileResponse.AgfProfileReportParameter parameter : group.getParameters().values()) {
- if (getDeviceType(parameter.getType()) != type) {
- continue;
- }
- addTable = true;
- table.addCell(getCell(textSize, Font.NORMAL, parameter.getLabel()));
- String value = parameter.getDisplayValue();
- if (!TextUtils.isEmpty(parameter.getUnits())) {
- value += " " + parameter.getUnits();
- }
- table.addCell(getCell(textSize, Font.NORMAL, value));
- }
- if (addTable) {
- if (addTitle) {
- addTitle = false;
- PdfHelper.addEmptyLine(doc, 1);
- String text = context.getString(type.getText());
- if (type == DeviceType.S_SERIES) {
- text = context.getString(R.string.x_and_y, text, context.getString(DeviceType.IQ_SERIES.getText()));
- }
- doc.add(getParagraph(titleSize, Font.BOLD, text + " - " + context.getString(R.string.settings)));
- }
- PdfHelper.addEmptyLine(doc, 1);
- doc.add(getParagraph(titleSize, Font.NORMAL, group.getLabel()));
- doc.add(table);
- }
- }
- }
- }
- }
- PdfHelper.addEmptyLine(doc, 3);
- Paragraph p = getParagraph(textSize - 2, Font.NORMAL, context.getString(R.string.generated_by, context.getString(R.string.app_name)));
- p.setAlignment(Element.ALIGN_RIGHT);
- doc.add(p);
- p = getParagraph(textSize - 2, Font.NORMAL, context.getString(R.string.all_rights_reserved, Calendar.getInstance().get(Calendar.YEAR)));
- p.setAlignment(Element.ALIGN_RIGHT);
- doc.add(p);
- } catch (Exception e) {
- Log.e("EnvoyReportHelper", e.getLocalizedMessage());
- } finally {
- doc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement