use of org.compiere.model.MPInstance in project adempiere by adempiere.
the class DistributionRunOrders method executeDistribution.
/**
* Execute Distribution Run
* @return
* @throws Exception
*/
public boolean executeDistribution() throws Exception {
int M_DocType_ID = 0;
MDocType[] doc = MDocType.getOfDocBaseType(getCtx(), MDocType.DOCBASETYPE_DistributionOrder);
if (doc == null || doc.length == 0) {
log.severe("Not found default document type for docbasetype " + MDocType.DOCBASETYPE_DistributionOrder);
throw new Exception(Msg.getMsg(getCtx(), "SequenceDocNotFound"), CLogger.retrieveException());
} else
M_DocType_ID = doc[0].getC_DocType_ID();
String trxName = Trx.createTrxName("Run Distribution to DRP");
//trx needs to be committed too
Trx trx = Trx.get(trxName, true);
//Prepare Process
int AD_Process_ID = 271;
AD_Process_ID = MProcess.getProcess_ID("M_DistributionRun Create", get_TrxName());
MPInstance instance = new MPInstance(Env.getCtx(), AD_Process_ID, 0);
if (!instance.save()) {
throw new Exception(Msg.getMsg(getCtx(), "ProcessNoInstance"), CLogger.retrieveException());
}
//call process
ProcessInfo pi = new ProcessInfo("M_DistributionRun Orders", AD_Process_ID);
pi.setAD_PInstance_ID(instance.getAD_PInstance_ID());
pi.setRecord_ID(m_run.getM_DistributionRun_ID());
// Add Parameter - Selection=Y
MPInstancePara ip = new MPInstancePara(instance, 10);
ip.setParameter("C_DocType_ID", M_DocType_ID);
if (!ip.save()) {
// not translated
String msg = "No Parameter added";
throw new Exception(msg, CLogger.retrieveException());
}
// Add Parameter - DatePromised
ip = new MPInstancePara(instance, 20);
ip.setParameter("DatePromised", "");
ip.setP_Date(p_DatePromised);
//ip.setP_Date_To(p_DatePromised_To);
if (!ip.save()) {
// not translated
String msg = "No Parameter added";
throw new Exception(msg, CLogger.retrieveException());
}
// Add Parameter - M_Warehouse_ID
ip = new MPInstancePara(instance, 30);
ip.setParameter("M_Warehouse_ID", p_M_Warehouse_ID);
if (!ip.save()) {
// not translated
String msg = "No Parameter added";
throw new Exception(msg, CLogger.retrieveException());
}
// Add Parameter - CreateDO
ip = new MPInstancePara(instance, 40);
ip.setParameter("ConsolidateDocument", p_ConsolidateDocument);
if (!ip.save()) {
// not translated
String msg = "No Parameter added";
throw new Exception(msg, CLogger.retrieveException());
}
// Add Parameter - IsTest=Y
ip = new MPInstancePara(instance, 50);
ip.setParameter("IsTest", p_IsTest);
if (!ip.save()) {
// not translated
String msg = "No Parameter added";
throw new Exception(msg, CLogger.retrieveException());
}
//Distribution List
ip = new MPInstancePara(instance, 60);
ip.setParameter("M_DistributionList_ID", p_M_DistributionList_ID);
if (!ip.save()) {
// not translated
String msg = "No Parameter added";
throw new Exception(msg, CLogger.retrieveException());
}
//Based in DRP Demand
ip = new MPInstancePara(instance, 70);
ip.setParameter("IsRequiredDRP", p_BasedInDamnd);
if (!ip.save()) {
// not translated
String msg = "No Parameter added";
throw new Exception(msg, CLogger.retrieveException());
}
// Execute Process
MProcess worker = new MProcess(getCtx(), AD_Process_ID, get_TrxName());
worker.processIt(pi, Trx.get(get_TrxName(), true));
m_run.delete(true);
return true;
}
use of org.compiere.model.MPInstance in project adempiere by adempiere.
the class FinReportJasper method doIt.
/**************************************************************************
* Perform process.
* @return Message to be translated
* @throws Exception
*/
protected String doIt() throws Exception {
// Call the normal FinReport to fill the T_Report table
String finReportMsg = super.doIt();
// Now invoke the associated jasper report (must report on the T_Report table)
ArrayList<ProcessInfoParameter> list = new ArrayList<ProcessInfoParameter>();
// Copy the list of parameters from the financial report
ProcessInfoParameter[] oldpara = getParameter();
for (int i = 0; i < oldpara.length; i++) list.add(oldpara[i]);
// and add the T_Report_AD_PInstance_ID parameter
list.add(new ProcessInfoParameter("T_Report_AD_PInstance_ID", new Integer(getAD_PInstance_ID()), null, null, null));
ProcessInfoParameter[] pars = new ProcessInfoParameter[list.size()];
list.toArray(pars);
// Load Report Definition
m_report = new MReport(getCtx(), getRecord_ID(), get_TrxName());
MProcess proc = new MProcess(getCtx(), m_report.getJasperProcess_ID(), get_TrxName());
MPInstance instance = new MPInstance(proc, getRecord_ID());
instance.saveEx();
ProcessInfo poInfo = new ProcessInfo(proc.getName(), proc.getAD_Process_ID());
poInfo.setParameter(pars);
poInfo.setRecord_ID(getRecord_ID());
poInfo.setAD_Process_ID(proc.getAD_Process_ID());
poInfo.setAD_PInstance_ID(instance.getAD_PInstance_ID());
// need to commit in order to allow jasper to view the data
Trx trx = Trx.get(get_TrxName(), true);
trx.commit();
// CarlosRuiz - globalqss - allow procedure preprocess
if (proc.getProcedureName() != null && proc.getProcedureName().length() > 0) {
// execute on this thread/connection
String sql = "{call " + proc.getProcedureName() + "(?)}";
try {
// ro??
CallableStatement cstmt = DB.prepareCall(sql);
cstmt.setInt(1, getAD_PInstance_ID());
cstmt.executeUpdate();
cstmt.close();
} catch (Exception e) {
log.log(Level.SEVERE, sql, e);
poInfo.setSummary(Msg.getMsg(Env.getCtx(), "ProcessRunError") + " " + e.getLocalizedMessage());
}
}
// TODO - allow java class preprocess if the classname <> ProcessUtil.JASPER_STARTER_CLASS
ProcessUtil.startJavaProcess(getCtx(), poInfo, trx);
return finReportMsg;
}
use of org.compiere.model.MPInstance in project adempiere by adempiere.
the class ProcessBuilder method generateProcessInstance.
/**
* Define instance for process
*/
private void generateProcessInstance() {
if (processId == 0)
throw new AdempiereException("@AD_Process_ID@ @NotFound@");
instance = new MPInstance(context, processId, recordId);
instance.saveEx();
}
use of org.compiere.model.MPInstance in project adempiere by adempiere.
the class WProcess method createProcessPage.
// createParameterPage
//Modified by Rob klein 4/29/07
/**************************************************************************
* Create Parocess Page
* @param request request
* @param AD_Process_ID Process
* @return Page
*/
public void createProcessPage(HttpServletRequest request, HttpServletResponse response, int AD_Process_ID, int AD_Window_ID) {
MobileSessionCtx wsc = MobileSessionCtx.get(request);
MProcess process = MProcess.get(wsc.ctx, AD_Process_ID);
log.info("PI table id " + process.get_Table_ID());
log.info("PI table name id " + process.get_TableName());
log.info("PI table client id " + process.getAD_Client_ID());
log.info("PI table process id " + process.getAD_Process_ID());
log.info("PI process class name " + process.getClassname());
// need to check if Role can access
MobileDoc doc = null;
if (process == null) {
doc = MobileDoc.createWindow("Process Not Found");
} else {
doc = MobileDoc.createWindow(process.getName());
fieldset center = new fieldset();
doc.getBody().addElement(center);
if (process.getDescription() != null)
center.addElement(new p(new i(process.getDescription())));
if (process.getHelp() != null)
center.addElement(new p(process.getHelp(), AlignType.LEFT));
// Create Process Instance
MPInstance pInstance = fillParameter(request, process);
//
int AD_Table_ID = MobileUtil.getParameterAsInt(request, "AD_Table_ID");
int AD_Record_ID = MobileUtil.getParameterAsInt(request, "AD_Record_ID");
ProcessInfo pi = new ProcessInfo(process.getName(), process.getAD_Process_ID(), AD_Table_ID, AD_Record_ID);
pi.setAD_User_ID(Env.getAD_User_ID(wsc.ctx));
pi.setAD_Client_ID(Env.getAD_Client_ID(wsc.ctx));
pi.setClassName(process.getClassname());
log.info("PI client id " + pi.getAD_Client_ID());
pi.setAD_PInstance_ID(pInstance.getAD_PInstance_ID());
// Info
p p = new p();
p.addElement(Msg.translate(wsc.ctx, "AD_PInstance_ID") + ": " + pInstance.getAD_PInstance_ID());
center.addElement(p);
// Start
boolean processOK = false;
if (process.isWorkflow()) {
Trx trx = Trx.get(Trx.createTrxName("WebPrc"), true);
try {
WProcessCtl.process(this, AD_Window_ID, pi, trx, request);
//processOK = process.processIt(pi, trx);
trx.commit();
trx.close();
} catch (Throwable t) {
trx.rollback();
trx.close();
}
if (pi.isError()) {
center.addElement(new p("Error:" + pi.getSummary(), AlignType.LEFT).setClass("Cerror"));
processOK = false;
} else {
center.addElement(new p("OK: Workflow Started", AlignType.LEFT));
processOK = true;
}
center.addElement(new p().addElement(pi.getSummary()));
center.addElement(pi.getLogInfo(true));
}
String jasper = process.getJasperReport();
if (process.isJavaProcess()) {
if (jasper != null) {
pi.setPrintPreview(false);
pi.setIsBatch(true);
}
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()) {
center.addElement(new p("Error:" + pi.getSummary(), AlignType.LEFT).setClass("Cerror"));
processOK = false;
} else {
if (jasper != null) {
String error = MobileUtil.streamFile(response, pi.getPDFReport());
//String error = streamResult (request, response, pInstance.getAD_PInstance_ID(), file);
if (error == null)
return;
doc = MobileDoc.create(error);
wsc.ctx.put("AD_PInstance_ID=" + pInstance.getAD_PInstance_ID(), "ok");
} else {
center.addElement(new p().addElement(pi.getSummary()));
center.addElement(pi.getLogInfo(true));
}
}
}
// Report
if (process.isReport()) //if (processOK && process.isReport())
{
if (jasper == null) {
log.info(response.toString());
ReportEngine re = ReportEngine.get(wsc.ctx, pi);
if (re == null) {
center.addElement(new p("Could not start ReportEngine", AlignType.LEFT).setClass("Cerror"));
} else {
try {
File file = File.createTempFile("WProcess", ".pdf");
boolean ok = re.createPDF(file);
if (ok) {
String error = MobileUtil.streamFile(response, file);
//String error = streamResult (request, response, pInstance.getAD_PInstance_ID(), file);
if (error == null)
return;
doc = MobileDoc.create(error);
//Modified by Rob Klein 6/1/07
/**
String url = "WProcess?AD_PInstance_ID="
+ pInstance.getAD_PInstance_ID()
+ "&File="
+ URLEncoder.encode(file.getAbsolutePath(), WebEnv.ENCODING);
a link = new a (url, null, a.TARGET_BLANK, process.getName());
center
.addElement(new p()
.addElement("Report created: ")
.addElement(link));
// Marker that Process is OK
* */
wsc.ctx.put("AD_PInstance_ID=" + pInstance.getAD_PInstance_ID(), "ok");
} else
center.addElement(new p("Could not create Report", AlignType.LEFT).setClass("Cerror"));
} catch (Exception e) {
center.addElement(new p("Could not create Report:", AlignType.LEFT).setClass("Cerror"));
center.addElement(e.toString());
}
}
}
}
}
try {
MobileUtil.createResponse(request, response, this, null, doc, false);
} catch (IOException e) {
log.info(e.toString());
}
}
use of org.compiere.model.MPInstance in project adempiere by adempiere.
the class Scheduler method runProcess.
// doWork
/**
* Run Process or Report
* @param process process
* @return summary
* @throws Exception
*/
private String runProcess(MProcess process) throws Exception {
log.info(process.toString());
boolean isReport = (process.isReport() || process.getAD_ReportView_ID() > 0);
// Process (see also MWFActivity.performWork
int AD_Table_ID = m_model.getAD_Table_ID();
int Record_ID = m_model.getRecord_ID();
//
MPInstance pInstance = new MPInstance(process, Record_ID);
fillParameter(pInstance);
//
ProcessInfo pi = new ProcessInfo(process.getName(), process.getAD_Process_ID(), AD_Table_ID, Record_ID);
pi.setAD_User_ID(getAD_User_ID());
pi.setAD_Client_ID(m_model.getAD_Client_ID());
pi.setAD_PInstance_ID(pInstance.getAD_PInstance_ID());
MUser from = new MUser(getCtx(), pi.getAD_User_ID(), null);
if (// note, this call close the transaction, don't use m_trx below
!process.processIt(pi, m_trx)) {
// notify supervisor if error
int supervisor = m_model.getSupervisor_ID();
if (supervisor > 0) {
MUser user = new MUser(getCtx(), supervisor, null);
boolean email = user.isNotificationEMail();
boolean notice = user.isNotificationNote();
if (email || notice)
ProcessInfoUtil.setLogFromDB(pi);
if (email) {
MClient client = MClient.get(m_model.getCtx(), m_model.getAD_Client_ID());
client.sendEMail(from, user, process.getName(), pi.getSummary() + " " + pi.getLogInfo(), null);
}
if (notice) {
// HARDCODED ProcessRunError
int AD_Message_ID = 442;
MNote note = new MNote(getCtx(), AD_Message_ID, supervisor, null);
note.setClientOrg(m_model.getAD_Client_ID(), m_model.getAD_Org_ID());
note.setTextMsg(pi.getSummary());
//note.setDescription();
note.setRecord(MPInstance.Table_ID, pi.getAD_PInstance_ID());
note.saveEx();
}
}
} else {
// notify recipients on success
Integer[] userIDs = m_model.getRecipientAD_User_IDs();
if (userIDs.length > 0) {
ProcessInfoUtil.setLogFromDB(pi);
for (int i = 0; i < userIDs.length; i++) {
MUser user = new MUser(getCtx(), userIDs[i].intValue(), null);
boolean email = user.isNotificationEMail();
boolean notice = user.isNotificationNote();
File report = null;
if (isReport) {
// Report
ReportEngine re = ReportEngine.get(m_schedulerctx, pi);
if (re == null)
return "Cannot create Report AD_Process_ID=" + process.getAD_Process_ID() + " - " + process.getName();
report = re.getPDF();
}
if (notice) {
// ProcessOK
int AD_Message_ID = 441;
if (isReport)
// HARDCODED SchedulerResult
AD_Message_ID = 884;
MNote note = new MNote(getCtx(), AD_Message_ID, userIDs[i].intValue(), null);
note.setClientOrg(m_model.getAD_Client_ID(), m_model.getAD_Org_ID());
if (isReport) {
note.setTextMsg(m_model.getName());
note.setDescription(m_model.getDescription());
note.setRecord(AD_Table_ID, Record_ID);
} else {
note.setTextMsg(pi.getSummary());
// note.setDescription();
note.setRecord(MPInstance.Table_ID, pi.getAD_PInstance_ID());
}
if (note.save()) {
if (isReport) {
// Attachment
MAttachment attachment = new MAttachment(getCtx(), MNote.Table_ID, note.getAD_Note_ID(), null);
attachment.setClientOrg(m_model.getAD_Client_ID(), m_model.getAD_Org_ID());
attachment.addEntry(report);
attachment.setTextMsg(m_model.getName());
attachment.saveEx();
}
}
}
if (email) {
MClient client = MClient.get(m_model.getCtx(), m_model.getAD_Client_ID());
if (isReport) {
client.sendEMail(from, user, m_model.getName(), m_model.getDescription(), report);
} else {
client.sendEMail(from, user, process.getName(), pi.getSummary() + " " + pi.getLogInfo(), null);
}
}
}
}
}
return pi.getSummary();
}
Aggregations