|
| 1 | +/* |
| 2 | + * Copyright 2018, 2020 Mind And Go |
| 3 | + * |
| 4 | + * This file is part of OdooJavaAPI. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + */ |
| 19 | +package com.odoojava.api; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.io.FileOutputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.text.SimpleDateFormat; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Date; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Map.Entry; |
| 32 | +import java.util.TimeZone; |
| 33 | +import java.util.concurrent.ConcurrentHashMap; |
| 34 | +import java.util.stream.Collectors; |
| 35 | +import java.util.stream.Stream; |
| 36 | + |
| 37 | +import org.apache.xmlrpc.XmlRpcException; |
| 38 | + |
| 39 | +import com.odoojava.api.Field.FieldType; |
| 40 | +import com.odoojava.api.helpers.FilterHelper; |
| 41 | + |
| 42 | +/** |
| 43 | + * |
| 44 | + * Main class for managing reports with the server. |
| 45 | + * |
| 46 | + * @author Florent THOMAS |
| 47 | + * @param: reportListCache |
| 48 | + * . Consider Object part that will be set with name/model/type of |
| 49 | + * the Odoo report |
| 50 | + */ |
| 51 | +public class ReportAdapter { |
| 52 | + |
| 53 | + private Session session; |
| 54 | + private Version serverVersion; |
| 55 | + private Object[] report; |
| 56 | + private String reportName; |
| 57 | + |
| 58 | + /** |
| 59 | + * @ |
| 60 | + */ |
| 61 | + private static final HashMap<String, Object[]> reportListCache = new HashMap<String, Object[]>(); |
| 62 | + |
| 63 | + public ReportAdapter(Session session) throws XmlRpcException { |
| 64 | + super(); |
| 65 | + this.session = session; |
| 66 | + this.serverVersion = session.getServerVersion(); |
| 67 | + try { |
| 68 | + getReportList(); |
| 69 | + } catch (OdooApiException e) { |
| 70 | + // TODO Auto-generated catch block |
| 71 | + e.printStackTrace(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /* |
| 76 | + * Method listing the available report and their type Purpose is to use the |
| 77 | + * list later to check the existence of the report and its type. Appropriate |
| 78 | + * methods will be possible regarding the type |
| 79 | + */ |
| 80 | + private void getReportList() throws XmlRpcException, OdooApiException { |
| 81 | + reportListCache.clear(); |
| 82 | + ObjectAdapter objectAd = this.session.getObjectAdapter("ir.actions.report.xml"); |
| 83 | + FilterCollection filters = new FilterCollection(); |
| 84 | + String[] report_tuple = new String[] { "report_name", "model", "name", "report_type" }; |
| 85 | + RowCollection reports = objectAd.searchAndReadObject(filters, report_tuple); |
| 86 | + reports.forEach(report -> { |
| 87 | + Object[] repName = new Object[] { report.get("name"), report.get("model"), report.get("report_type") }; |
| 88 | + reportListCache.put(report.get("report_name").toString(), repName); |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @param reportName |
| 94 | + * @param ids |
| 95 | + * @return |
| 96 | + * @throws XmlRpcException |
| 97 | + * @throws OdooApiException |
| 98 | + */ |
| 99 | + public byte[] getReportAsByte(String reportName, Object[] ids) throws XmlRpcException, OdooApiException { |
| 100 | + checkReportName(reportName); |
| 101 | + byte[] reportDatas = session.executeReportService(reportName, ids); |
| 102 | + return reportDatas; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * |
| 107 | + * Method to prepare the report to be generated Make some usefull tests |
| 108 | + * regarding the |
| 109 | + * |
| 110 | + * @param reportName: |
| 111 | + * can be found in Technical > report > report |
| 112 | + * @throws OdooApiException |
| 113 | + * @throws XmlRpcException |
| 114 | + */ |
| 115 | + private void checkReportName(String reportName) throws OdooApiException, XmlRpcException { |
| 116 | + // TODO Auto-generated method stub |
| 117 | + // refresh |
| 118 | + getReportList(); |
| 119 | + if (reportName == null) { |
| 120 | + throw new OdooApiException("Report Name is mandatory. Please read the Odoo help."); |
| 121 | + } |
| 122 | + Object[] report = reportListCache.get(reportName); |
| 123 | + if (report == null) { |
| 124 | + throw new OdooApiException( |
| 125 | + "Your report don't seems to exist in the Odoo Database." + "Please check your configuration"); |
| 126 | + } |
| 127 | + if (!Arrays.asList("qweb-pdf", "qweb-html").contains(report[2])) { |
| 128 | + throw new OdooApiException( |
| 129 | + "Your report type is obsolete. Only QWEB report are allowed." + "Please check your configuration"); |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + public void setReport(String reportName) throws OdooApiException, XmlRpcException { |
| 135 | + checkReportName(reportName); |
| 136 | + Object[] report = reportListCache.get(reportName); |
| 137 | + this.report = report; |
| 138 | + this.reportName = reportName; |
| 139 | + } |
| 140 | + |
| 141 | + public String getReportType() { |
| 142 | + return this.report[2].toString(); |
| 143 | + } |
| 144 | + |
| 145 | + public String PrintReportToFileName(Object[] ids) throws IOException, XmlRpcException, OdooApiException { |
| 146 | + |
| 147 | + File tmp_file = File.createTempFile("odoo-" + report[1].toString() + "-", getReportType().replace("qweb-", "."), |
| 148 | + null); |
| 149 | + |
| 150 | + byte[] report_bytes = getReportAsByte(reportName, ids); |
| 151 | + FileOutputStream report_stream = new FileOutputStream(tmp_file); |
| 152 | + try { |
| 153 | + report_stream.write(report_bytes); |
| 154 | + } finally { |
| 155 | + report_stream.close(); |
| 156 | + } |
| 157 | + |
| 158 | + return tmp_file.getAbsolutePath().toString(); |
| 159 | + } |
| 160 | +} |
0 commit comments