Skip to content

Commit

Permalink
Merge pull request #10 from Martin-Schoenbeck-Beratungen-GmbH/ts-modi…
Browse files Browse the repository at this point in the history
…fication

ReportCtl
  • Loading branch information
lHeidbreder authored Feb 2, 2024
2 parents ca1454a + 4fc5e12 commit f4318b1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 35 deletions.
Binary file modified META-INF/2Pack.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: beluga.serverprint
Bundle-SymbolicName: beluga.serverprint
Bundle-Version: 5.1.2.qualifier
Bundle-Version: 5.2.0.qualifier
Service-Component: OSGI-INF/*.xml
Bundle-RequiredExecutionEnvironment: JavaSE-11
Require-Bundle: org.adempiere.base;bundle-version="5.1.0",
Expand Down
72 changes: 40 additions & 32 deletions src/de/schoenbeck/serverprint/work/Copy.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.activation.FileDataSource;

import org.adempiere.exceptions.AdempiereException;
import org.adempiere.report.jasper.ReportStarter;
import org.adempiere.webui.apps.AEnv;
import org.compiere.model.MArchive;
import org.compiere.model.MAttachmentEntry;
import org.compiere.model.MClient;
import org.compiere.model.MMailText;
import org.compiere.model.MPInstance;
import org.compiere.model.MProcess;
import org.compiere.model.MTable;
import org.compiere.model.MUser;
import org.compiere.model.MUserMail;
import org.compiere.model.PO;
import org.compiere.model.PrintInfo;
import org.compiere.print.ServerReportCtl;
import org.compiere.process.ProcessInfo;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.util.CLogger;
Expand Down Expand Up @@ -170,38 +172,34 @@ public void execute (Map<ServerPrintCopyParam, File> files) throws Exception {

public static File prepareReport (ServerPrintCopyParam p) {

ProcessInfoParameter pi1 = new ProcessInfoParameter("C_Doctype_ID", p.c_doctype_id, null,"","");
ProcessInfoParameter pi2 = new ProcessInfoParameter("ReportVariant", p.reportVariant, null,"","");

// Create a process info instance. This is a composite class containing the parameters.
ProcessInfo pi = new ProcessInfo("Document",0,0,0);
pi.setParameter(p.c_doctype_id == 0 ? new ProcessInfoParameter[] {pi2} : new ProcessInfoParameter[] {pi1, pi2});
pi.setRecord_ID(p.record_id);

// Lookup process in the AD, in this case by value
MProcess pr = new MProcess(Env.getCtx(), p.ad_process_id, null);

// Create an instance of the actual process class.
ReportStarter process = new ReportStarter();

// Create process instance (mainly for logging/sync purpose)
ProcessInfo reportPI = new ProcessInfo("Document", p.ad_process_id, p.ad_table_id, p.record_id);
reportPI.setParameter(new ProcessInfoParameter[] {
new ProcessInfoParameter("C_Doctype_ID", p.c_doctype_id, null,"",""),
new ProcessInfoParameter("ReportVariant", p.reportVariant, null,"","")
});
MPInstance mpi = new MPInstance(Env.getCtx(), 0, null);
mpi.setAD_Process_ID(pr.get_ID());
mpi.setAD_Process_ID(p.ad_process_id);
mpi.setRecord_ID(p.record_id);
mpi.save();

// Connect the process to the process instance.
pi.setAD_PInstance_ID(mpi.get_ID());
pi.setAD_Process_ID(p.ad_process_id);
pi.setExport(true);
if (p.exportFileExtension != null)
pi.setExportFileExtension(p.exportFileExtension);
pi.setTable_ID(p.ad_table_id);

CLogger.get().info("Starting process " + pr.getName());
process.startProcess(Env.getCtx(), pi, null);
File export = pi.getExportFile();
reportPI.setAD_PInstance_ID(mpi.getAD_PInstance_ID());
reportPI.setExport(true);
reportPI.setIsBatch(true);
reportPI.setPrintPreview(true);
reportPI.setReportingProcess(true);
if (p.exportFileExtension != null) {
reportPI.setExportFileExtension(p.exportFileExtension);
reportPI.setReportType(p.exportFileExtension.toUpperCase());
}

boolean success = ServerReportCtl.start(reportPI);
if (!success)
throw new AdempiereException("Report Failure");

File export = reportPI.getExportFile();
if (export == null)
export = reportPI.getPDFReport();
if (export == null)
throw new AdempiereException("No Report returned");

String title = createReportTitle(p);
if (title == null)
Expand All @@ -222,9 +220,19 @@ private static String createReportTitle(ServerPrintCopyParam p) {
if (p.exportFilenamePattern == null || p.exportFilenamePattern.equals(""))
return null;

PO record = MTable.get(p.ad_table_id).getPO(p.record_id, null);

String rtn = p.exportFilenamePattern;
if (p.windowno > 0)
rtn = Env.parseContext(Env.getCtx(), p.windowno, p.exportFilenamePattern, false, true);

final Matcher replacements = Pattern.compile("@\\$([^@ \\n]+)@").matcher(rtn);
while (replacements.find()) {
int index = record.get_ColumnIndex(replacements.group(1));
if (index >= 0)
rtn = rtn.replaceFirst(Matcher.quoteReplacement(replacements.group(0)), record.get_Value(index).toString());
}

rtn = Env.parseContext(Env.getCtx(), p.windowno, rtn, false, true);

return rtn;
}

Expand Down
8 changes: 6 additions & 2 deletions src/de/schoenbeck/serverprint/work/ServerPrintWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void start () throws Exception {
.build());

if (params.length > 1)
copyparams.addAll(getSubParams(Arrays.copyOfRange(params, 1, params.length)));
copyparams.addAll(getSubParams(Arrays.copyOfRange(params, 1, params.length), params[0].sbsp_printoption_id));

copies.add(new Copy(copyparams.toArray(new ServerPrintCopyParam[copyparams.size()])));
}
Expand Down Expand Up @@ -222,7 +222,7 @@ private int choosePrintOptionId (HashSet<Integer> options) throws Exception {
return rtn == null ? 0 : (Integer) rtn;
}

private List<ServerPrintCopyParam> getSubParams (ServerPrintWorkerParam[] subParams) throws SQLException {
private List<ServerPrintCopyParam> getSubParams (ServerPrintWorkerParam[] subParams, int printoption_id) throws SQLException {
LinkedList<ServerPrintCopyParam> rtn = new LinkedList<>();

ResultSet rs = null;
Expand All @@ -239,6 +239,10 @@ private List<ServerPrintCopyParam> getSubParams (ServerPrintWorkerParam[] subPar

rs = ps.executeQuery();
while (rs.next()) {
if (rs.getInt("sbsp_printoption_id") != printoption_id
&& rs.getInt("sbsp_printoption_id") != 0)
continue;

rtn.add(new ServerPrintCopyParamBuilder(trxName)
.setFromResultSet(rs)
.setAd_user_id_bpartner(param.ad_user_id_bpartner)
Expand Down

0 comments on commit f4318b1

Please sign in to comment.