use of org.compiere.model.MPInstance in project adempiere by adempiere.
the class Process method runProcess.
/**************************************************************************
* Create Process Page
* @param AD_Process_ID Process
* @return Page
*/
public static RunProcessResponseDocument runProcess(CompiereService m_cs, RunProcessDocument req) {
RunProcessResponseDocument res = RunProcessResponseDocument.Factory.newInstance();
RunProcessResponse r = res.addNewRunProcessResponse();
RunProcess rp = req.getRunProcess();
int AD_Process_ID = rp.getADProcessID();
int m_record_id = rp.getADRecordID();
//WebSessionCtx wsc = WebSessionCtx.get (request);
MProcess process = MProcess.get(m_cs.getM_ctx(), AD_Process_ID);
// need to check if Role can access
if (process == null) {
// WebDoc doc = WebDoc.createWindow("Process not found");
r.setError("Process not found");
r.setIsError(true);
return res;
}
//process.getDescription()
//process.getHelp()
// Evaluate DocAction, if call have DocAction parameter, then try to set DocAction before calling workflow process
String docAction = rp.getDocAction();
if (docAction != null && docAction.length() > 0) {
// - the process must be a workflow document
if (process.getAD_Workflow_ID() > 0) {
MWorkflow wf = MWorkflow.get(m_cs.getM_ctx(), process.getAD_Workflow_ID());
if (wf.getWorkflowType().equals(MWorkflow.WORKFLOWTYPE_DocumentProcess)) {
// - get the table associated with the workflow document
// - set DocAction in such table
// get the PO for the tablename and record ID
MTable table = MTable.get(m_cs.getM_ctx(), wf.getAD_Table_ID());
if (table != null) {
PO po = table.getPO(m_record_id, null);
if (po != null) {
po.set_ValueOfColumn("DocAction", docAction);
po.save();
}
}
}
}
}
// Create Process Instance
MPInstance pInstance = null;
try {
pInstance = fillParameter(m_cs, rp.getParamValues(), process);
} catch (Exception ex) {
//center.addElement(new p("B��d: " + ex.getMessage(), AlignType.LEFT).setClass("ProcesResultError"));
r.setError(ex.getMessage());
r.setIsError(true);
return res;
}
if (m_record_id > 0) {
pInstance.setRecord_ID(m_record_id);
pInstance.save();
}
//
ProcessInfo pi = new ProcessInfo(process.getName(), process.getAD_Process_ID());
pi.setAD_User_ID(Env.getAD_User_ID(m_cs.getM_ctx()));
pi.setAD_Client_ID(Env.getAD_Client_ID(m_cs.getM_ctx()));
pi.setAD_PInstance_ID(pInstance.getAD_PInstance_ID());
if (m_record_id > 0)
pi.setRecord_ID(m_record_id);
// Info
//p p = new p();
//p.addElement(Msg.translate(wsc.ctx, "AD_PInstance_ID") + ": " + pInstance.getAD_PInstance_ID());
//center.addElement(p);
boolean processOK = false;
boolean jasperreport = (process != null && process.getClassname() != null && process.getClassname().indexOf("net.sf.compilo.report.ReportStarter") >= 0);
if (jasperreport) {
//this.jasperReport( request, response, wsc.ctx, pi);
processOK = true;
}
// Start
if (process.isWorkflow()) {
try {
int AD_Workflow_ID = process.getAD_Workflow_ID();
MWorkflow wf = MWorkflow.get(Env.getCtx(), AD_Workflow_ID);
// may return null
MWFProcess wfProcess = wf.startWait(pi);
if (wfProcess != null) {
//wynik
r.setSummary(pi.getSummary());
r.setLogInfo(pi.getLogInfo(true));
r.setIsError(false);
return res;
}
} catch (Exception ex) {
r.setError(ex.getMessage());
r.setLogInfo(pi.getLogInfo(true));
r.setIsError(true);
return res;
//Wyj�tek: pi.getLogInfo(true) pi.getLogInfo(true)
}
//started = wfProcess != null;
}
if (process.isJavaProcess() && !jasperreport) {
Trx trx = Trx.get(Trx.createTrxName("WebPrc"), true);
try {
processOK = process.processIt(pi, trx);
trx.commit();
trx.close();
} catch (Throwable t) {
trx.rollback();
trx.close();
}
if (!processOK || pi.isError()) {
// b��d: pi.getSummary()
r.setSummary(pi.getSummary());
r.setLogInfo(pi.getLogInfo(true));
r.setIsError(true);
processOK = false;
} else {
r.setSummary(pi.getSummary());
r.setLogInfo(pi.getLogInfo(true));
r.setIsError(false);
//return res;
// wynik - String summary = pi.getSummary();
}
}
// Report
if (//&& !m_jasperreport)
(process.isReport() || jasperreport)) /*processOK &&*/
{
r.setIsReport(true);
//if (m_jasperreport)
//{
// center.addElement(new p("JASPER REPORT", AlignType.LEFT).setClass("Cerror"));
//}
//else
{
ReportEngine re = null;
if (!jasperreport)
re = start(pi);
//ReportEngine.get(m_cs.getM_ctx(), pi);
if (re == null && !jasperreport) {
//b��d: "Nie uda�o si� uruchomi� silnika raport�w (ReportEngine)",
} else {
try {
boolean ok = false;
File file;
String file_type = "pdf";
if (!jasperreport) {
//file = File.createTempFile("WProcess", ".pdf");
//ok = re.createPDF(file);
MPrintFormat pf = re.getPrintFormat();
if (pf.isTableBased()) {
CharArrayWriter wr = new CharArrayWriter();
//file = File.createTempFile("WProcess", ".xls");
ok = ReportEngineEx.createEXCEL_HTML_wr(re, m_cs.getM_ctx(), wr, false, re.getPrintFormat().getLanguage());
//ok = re.createXML( file );
file_type = "xls";
String data = wr.toString();
if (data != null)
r.setData(data.getBytes());
r.setReportFormat(file_type);
} else {
byte[] dat = re.createPDFData();
file_type = "pdf";
r.setData(dat);
r.setReportFormat(file_type);
}
//r.setReportFormat("xls");
ok = true;
} else {
JasperPrint jp = getJasperReportPrint(m_cs.getM_ctx(), pi);
//file = File.createTempFile("WProcess", ".pdf");
ByteArrayOutputStream wr = new ByteArrayOutputStream();
net.sf.jasperreports.engine.JasperExportManager.exportReportToPdfStream(jp, wr);
//exportReportToPdfFile( jp, file.getAbsolutePath() );
file_type = "pdf";
r.setData(wr.toByteArray());
r.setReportFormat(file_type);
ok = true;
}
if (ok) {
//pInstance.getAD_PInstance_ID()
//file.getAbsolutePath()
// Marker that Process is OK
m_cs.getM_ctx().put("AD_PInstance_ID=" + pInstance.getAD_PInstance_ID(), "ok");
} else {
r.setError("Cannot create report");
r.setLogInfo(pi.getLogInfo(true));
r.setIsError(true);
return res;
//"Cannot create report:",
}
} catch (Exception e) {
r.setError("Cannot create report:" + e.getMessage());
r.setLogInfo(pi.getLogInfo(true));
r.setIsError(true);
return res;
// ,
}
}
}
}
return res;
}
use of org.compiere.model.MPInstance in project adempiere by adempiere.
the class Process method fillParameter.
// createProcessPage
private static MPInstance fillParameter(CompiereService m_cs, DataRow dr, MProcess process) throws Exception {
MPInstance pInstance = new MPInstance(process, 0);
DataField[] f = dr.getFieldArray();
HashMap fmap = new HashMap();
for (int i = 0; i < f.length; i++) fmap.put(f[i].getColumn(), f[i].getVal());
//
MPInstancePara[] iParams = pInstance.getParameters();
for (int pi = 0; pi < iParams.length; pi++) {
MPInstancePara iPara = iParams[pi];
String key = iPara.getParameterName();
MProcessPara pPara = process.getParameter(key);
if (pPara == null) {
log.log(Level.SEVERE, "Parameter not found: " + key);
continue;
}
int displayType = pPara.getAD_Reference_ID();
String valueString = null;
Object ob = fmap.get(key);
if (ob != null)
valueString = ob.toString();
String valueString2 = null;
if (pPara.isRange()) {
ob = fmap.get(key + "_2");
if (ob != null)
valueString2 = ob.toString();
}
log.fine("fillParameter - " + key + " = " + valueString);
Object value = valueString;
if (valueString != null && valueString.length() == 0)
value = null;
if (value != null && (DisplayType.List == displayType || DisplayType.TableDir == displayType || DisplayType.Table == displayType) && value.equals("-1"))
value = null;
// No Value
if (value == null && DisplayType.YesNo != pPara.getAD_Reference_ID()) {
if (pPara.isMandatory())
throw new Exception(" Parameter " + pPara.getName() + " is required.");
} else {
// Convert to Type
try {
if (DisplayType.isNumeric(displayType) || DisplayType.isID(displayType)) {
BigDecimal bd = null;
if (value instanceof BigDecimal)
bd = (BigDecimal) value;
else if (value instanceof Integer)
bd = new BigDecimal(((Integer) value).intValue());
else
bd = new BigDecimal(value.toString());
iPara.setP_Number(bd);
log.fine("fillParameter - " + key + " = " + valueString + " (=" + bd + "=)");
if (pPara.isRange()) {
bd = null;
bd = new BigDecimal(valueString2.toString());
iPara.setP_Number_To(bd);
}
} else if (DisplayType.isDate(displayType)) {
java.util.Date d;
if (displayType == DisplayType.DateTime)
d = m_cs.dateFormat.parse(value.toString());
else
// TODO: datetime
d = m_cs.dateFormat.parse(value.toString());
//d = m_cs.dateTimeFormat.parse(value.toString());
Timestamp ts = null;
ts = new Timestamp(d.getTime());
iPara.setP_Date(ts);
if (pPara.isRange()) {
if (displayType == DisplayType.DateTime)
d = m_cs.dateFormat.parse(valueString2);
else //d = m_cs.dateTimeFormat.parse(valueString2);
{
if (valueString2 == null || valueString2.length() == 0)
d = new java.util.Date();
else
//TODO: datetime
d = m_cs.dateFormat.parse(valueString2);
}
ts = new Timestamp(d.getTime());
iPara.setP_Date_To(ts);
}
log.fine("fillParameter - " + key + " = " + valueString + " (=" + ts + "=)");
} else if (DisplayType.YesNo == pPara.getAD_Reference_ID()) {
String bv = "N";
if (value == null)
bv = "N";
else //if (value.toString().toLowerCase().charAt(0)=='t')
{
if ("true".equalsIgnoreCase(valueString) || "y".equalsIgnoreCase(valueString))
bv = "Y";
else
bv = "N";
}
//bv ="Y";
iPara.setP_String(bv);
} else {
iPara.setP_String(value.toString());
}
if (// kolec - ustawia wartosc dla parametru Lookup
pPara.isLookup()) {
Lookup lok = pPara.getLookup();
if (lok != null) {
NamePair np = lok.getDirect(value, false, false);
if (np != null) {
iPara.setInfo(np.getName());
}
}
} else {
if (value != null)
iPara.setInfo(value.toString());
if (valueString2 != null)
iPara.setInfo_To(valueString2);
}
//
iPara.save();
} catch (Exception e) {
log.warning("fillParameter - " + key + " = " + valueString + " (" + value + ") " + value.getClass().getName() + " - " + e.getLocalizedMessage());
}
}
// not null
}
// instance parameter loop
// kolec - tego chyba brakowalo
pInstance.save();
return pInstance;
}
use of org.compiere.model.MPInstance in project adempiere by adempiere.
the class ReportProcessor method addProcessParameters.
// runReport
private void addProcessParameters(int AD_PInstance_ID, Map params) {
log.finest("ReportStarter.addProcessParameters");
MPInstance pinstance = new MPInstance(m_ctx, AD_PInstance_ID, null);
MPInstancePara[] pinstancePara = pinstance.getParameters();
m_Record_ID = pinstance.getRecord_ID();
params.put("AD_Client_ID", new Integer(pinstance.getAD_Client_ID()));
params.put("AD_Org_ID", new Integer(pinstance.getAD_Org_ID()));
for (int i = 0; i < pinstancePara.length; i++) {
MPInstancePara para = pinstancePara[i];
if (para.getInfo() != null) {
if (para.getInfo_To() != null) {
params.put(para.getParameterName() + "_Info1", para.getInfo());
params.put(para.getParameterName() + "_info2", para.getInfo_To());
} else
params.put(para.getParameterName() + "_Info", para.getInfo());
} else {
params.put(para.getParameterName() + "_Info", "");
params.put(para.getParameterName() + "_Info1", "");
params.put(para.getParameterName() + "_info2", "");
}
if (para.getP_String() != null) {
if (para.getP_String_To() != null) {
params.put(para.getParameterName() + "1", para.getP_String());
params.put(para.getParameterName() + "2", para.getP_String_To());
} else
params.put(para.getParameterName(), para.getP_String());
} else if (para.getP_Date() != null) {
if (para.getP_Date() != null) {
params.put(para.getParameterName() + "1", para.getP_Date());
params.put(para.getParameterName() + "2", para.getP_Date_To());
} else
params.put(para.getParameterName(), para.getP_Date());
} else if (para.getP_Number() != null) {
if (para.getP_Number_To() != null && !para.getP_Number_To().equals("")) {
try {
Integer pIntNum = Integer.valueOf(para.getP_Number().toString());
Integer pIntNumTo = Integer.valueOf(para.getP_Number_To().toString());
params.put(para.getParameterName() + "1", pIntNum);
params.put(para.getParameterName() + "2", pIntNumTo);
} catch (NumberFormatException nfe) {
try {
Double pDoubleNum = Double.valueOf(para.getP_Number().toString());
Double pDoubleNumTo = Double.valueOf(para.getP_Number_To().toString());
params.put(para.getParameterName() + "1", pDoubleNum);
params.put(para.getParameterName() + "2", pDoubleNumTo);
} catch (NumberFormatException nfe2) {
params.put(para.getParameterName() + "1", para.getP_Number());
params.put(para.getParameterName() + "2", para.getP_Number_To());
}
}
} else {
try {
Integer pIntNum = Integer.valueOf(para.getP_Number().toString());
params.put(para.getParameterName(), pIntNum);
} catch (NumberFormatException nfe) {
try {
Double pDoubleNum = Double.valueOf(para.getP_Number().toString());
params.put(para.getParameterName(), pDoubleNum);
} catch (NumberFormatException nfe2) {
params.put(para.getParameterName(), para.getP_Number());
}
}
}
}
}
}
use of org.compiere.model.MPInstance in project adempiere by adempiere.
the class InvoiceGenFromShipment method generate.
/**************************************************************************
* Generate Invoices
*/
public String generate(IStatusBar statusBar, KeyNamePair docTypeKNPair, String docActionSelected) {
String info = "";
String trxName = Trx.createTrxName("IVG");
//trx needs to be committed too
Trx trx = Trx.get(trxName, true);
// prevents from being called twice
setSelectionActive(false);
statusBar.setStatusLine(Msg.getMsg(Env.getCtx(), "InvGenerateGen"));
statusBar.setStatusDB(String.valueOf(getSelection().size()));
// Prepare Process
int AD_Process_ID = 0;
if (docTypeKNPair.getKey() == MInOut.Table_ID) {
// HARDCODED- AD_Process.Value=C_Invoice_Generate_from_Shipment; class=org.adempiere.process.InvoiceGenerateFromShipment
AD_Process_ID = 53345;
} else {
// AD_Process_ID = 52002; // AD_Process.Value=C_Invoice_GenerateRMA (Manual); class=org.adempiere.process.InvoiceGenerateRMA
}
MPInstance instance = new MPInstance(Env.getCtx(), AD_Process_ID, 0);
if (!instance.save()) {
info = Msg.getMsg(Env.getCtx(), "ProcessNoInstance");
return info;
}
//insert selection
StringBuffer insert = new StringBuffer();
insert.append("INSERT INTO T_SELECTION(AD_PINSTANCE_ID, T_SELECTION_ID) ");
int counter = 0;
for (Integer selectedId : getSelection()) {
counter++;
if (counter > 1)
insert.append(" UNION ");
insert.append("SELECT ");
insert.append(instance.getAD_PInstance_ID());
insert.append(", ");
insert.append(selectedId);
insert.append(" FROM DUAL ");
if (counter == 1000) {
if (DB.executeUpdate(insert.toString(), trxName) < 0) {
// not translated!
String msg = "No Invoices";
info = msg;
log.config(msg);
trx.rollback();
return info;
}
insert = new StringBuffer();
insert.append("INSERT INTO T_SELECTION(AD_PINSTANCE_ID, T_SELECTION_ID) ");
counter = 0;
}
}
if (counter > 0) {
if (DB.executeUpdate(insert.toString(), trxName) < 0) {
// not translated!
String msg = "No Invoices";
info = msg;
log.config(msg);
trx.rollback();
return info;
}
}
ProcessInfo pi = new ProcessInfo("", AD_Process_ID);
pi.setAD_PInstance_ID(instance.getAD_PInstance_ID());
// Add Parameters
MPInstancePara para = new MPInstancePara(instance, 10);
para.setParameter("Selection", "Y");
if (!para.save()) {
// not translated
String msg = "No Selection Parameter added";
info = msg;
log.log(Level.SEVERE, msg);
return info;
}
para = new MPInstancePara(instance, 20);
para.setParameter("DocAction", docActionSelected);
if (!para.save()) {
// not translated
String msg = "No DocAction Parameter added";
info = msg;
log.log(Level.SEVERE, msg);
return info;
}
setTrx(trx);
setProcessInfo(pi);
return info;
}
use of org.compiere.model.MPInstance in project adempiere by adempiere.
the class SvrProcess method unlock.
// lock
/**
* Unlock Process Instance.
* Update Process Instance DB and write option return message
*/
private void unlock() {
try {
MPInstance instance = new MPInstance(getCtx(), processInfo.getAD_PInstance_ID(), null);
if (instance.get_ID() == 0) {
log.log(Level.SEVERE, "Did not find PInstance " + processInfo.getAD_PInstance_ID());
return;
}
instance.setIsProcessing(false);
instance.setResult(!processInfo.isError());
instance.setErrorMsg(processInfo.getSummary());
instance.saveEx();
log.fine(instance.toString());
ProcessInfoUtil.saveLogToDB(processInfo);
} catch (Exception e) {
log.severe("unlock() - " + e.getLocalizedMessage());
}
}
Aggregations