use of org.compiere.util.Trx in project adempiere by adempiere.
the class MWFActivity method setUserChoice.
// setVariable
/**
* Set User Choice
* @param AD_User_ID user
* @param value new Value
* @param displayType display type
* @param textMsg optional Message
* @return true if set
* @throws Exception if error
*/
public boolean setUserChoice(int AD_User_ID, String value, int displayType, String textMsg) throws Exception {
// Check if user approves own document when a role is reponsible
/*
* 2007-06-08, matthiasO.
* The following sequence makes sure that only users in roles which
* have the 'Approve own document flag' set can set the user choice
* of 'Y' (approve) or 'N' (reject).
* IMHO this is against the meaning of 'Approve own document': Why
* should a user who is faced with the task of approving documents
* generally be required to have the ability to approve his OWN
* documents? If the document to approve really IS his own document
* this will be respected when trying to find an approval user in
* the call to getApprovalUser(...) below.
*/
/*
if (getNode().isUserApproval() && getPO() instanceof DocAction)
{
DocAction doc = (DocAction)m_po;
MUser user = new MUser (getCtx(), AD_User_ID, null);
MRole[] roles = user.getRoles(m_po.getAD_Org_ID());
boolean canApproveOwnDoc = false;
for (int r = 0; r < roles.length; r++)
{
if (roles[r].isCanApproveOwnDoc())
{
canApproveOwnDoc = true;
break;
} // found a role which allows to approve own document
}
if (!canApproveOwnDoc)
{
String info = user.getName() + " cannot approve own document " + doc;
addTextMsg(info);
log.fine(info);
return false; // ignore
}
}*/
setWFState(StateEngine.STATE_Running);
setAD_User_ID(AD_User_ID);
Trx trx = (get_TrxName() != null) ? Trx.get(get_TrxName(), false) : null;
boolean ok = setVariable(value, displayType, textMsg, trx);
if (!ok)
return false;
String newState = StateEngine.STATE_Completed;
// Approval
if (getNode().isUserApproval() && getPO(trx) instanceof DocAction) {
DocAction doc = (DocAction) m_po;
try {
// Not approved
if (!"Y".equals(value)) {
newState = StateEngine.STATE_Aborted;
if (!(doc.processIt(DocAction.ACTION_Reject)))
setTextMsg("Cannot Reject - Document Status: " + doc.getDocStatus());
} else {
if (isInvoker()) {
int startAD_User_ID = Env.getAD_User_ID(getCtx());
if (startAD_User_ID == 0)
startAD_User_ID = doc.getDoc_User_ID();
int nextAD_User_ID = getApprovalUser(startAD_User_ID, doc.getC_Currency_ID(), doc.getApprovalAmt(), doc.getAD_Org_ID(), // own doc
startAD_User_ID == doc.getDoc_User_ID());
// No Approver
if (nextAD_User_ID <= 0) {
newState = StateEngine.STATE_Aborted;
setTextMsg("Cannot Approve - No Approver");
doc.processIt(DocAction.ACTION_Reject);
} else if (startAD_User_ID != nextAD_User_ID) {
forwardTo(nextAD_User_ID, "Next Approver");
newState = StateEngine.STATE_Suspended;
} else // Approve
{
if (!(doc.processIt(DocAction.ACTION_Approve))) {
newState = StateEngine.STATE_Aborted;
setTextMsg("Cannot Approve - Document Status: " + doc.getDocStatus());
}
}
} else // No Invoker - Approve
if (!(doc.processIt(DocAction.ACTION_Approve))) {
newState = StateEngine.STATE_Aborted;
setTextMsg("Cannot Approve - Document Status: " + doc.getDocStatus());
}
}
doc.saveEx();
} catch (Exception e) {
newState = StateEngine.STATE_Terminated;
setTextMsg("User Choice: " + e.toString());
addTextMsg(e);
log.log(Level.WARNING, "", e);
}
// Send Approval Notification
if (newState.equals(StateEngine.STATE_Aborted)) {
MUser to = new MUser(getCtx(), doc.getDoc_User_ID(), null);
// send email
if (to.isNotificationEMail()) {
MClient client = MClient.get(getCtx(), doc.getAD_Client_ID());
client.sendEMail(doc.getDoc_User_ID(), Msg.getMsg(getCtx(), "NotApproved") + ": " + doc.getDocumentNo(), (doc.getSummary() != null ? doc.getSummary() + "\n" : "") + (doc.getProcessMsg() != null ? doc.getProcessMsg() + "\n" : "") + (getTextMsg() != null ? getTextMsg() : ""), null);
}
// Send Note
if (to.isNotificationNote()) {
MNote note = new MNote(getCtx(), "NotApproved", doc.getDoc_User_ID(), null);
note.setTextMsg((doc.getSummary() != null ? doc.getSummary() + "\n" : "") + (doc.getProcessMsg() != null ? doc.getProcessMsg() + "\n" : "") + (getTextMsg() != null ? getTextMsg() : ""));
// 2007-06-08, matthiasO.
// Add record information to the note, so that the user receiving the
// note can jump to the doc easily
note.setRecord(m_po.get_Table_ID(), m_po.get_ID());
note.saveEx();
}
}
}
setWFState(newState);
return ok;
}
use of org.compiere.util.Trx in project adempiere by adempiere.
the class VInOutInvoiceGen method generateInvoices.
// generateShipments_complete
/**************************************************************************
* Generate Invoices
*/
private void generateInvoices() {
String trxName = Trx.createTrxName("IVG");
//trx needs to be committed too
Trx trx = Trx.get(trxName, true);
//String trxName = null;
//Trx trx = null;
// prevents from being called twice
m_selectionActive = false;
statusBar.setStatusLine(Msg.getMsg(Env.getCtx(), "InvGenerateGen"));
statusBar.setStatusDB(String.valueOf(selection.size()));
// Prepare Process
int AD_Process_ID = 0;
KeyNamePair docTypeKNPair = (KeyNamePair) cmbDocType.getSelectedItem();
if (docTypeKNPair.getKey() == MRMA.Table_ID) {
// C_Invoice_GenerateRMA - org.adempiere.process.InvoiceGenerateRMA
AD_Process_ID = 52002;
} else {
// HARDCODED C_InvoiceCreate
AD_Process_ID = 134;
}
MPInstance instance = new MPInstance(Env.getCtx(), AD_Process_ID, 0);
if (!instance.save()) {
info.setText(Msg.getMsg(Env.getCtx(), "ProcessNoInstance"));
return;
}
//insert selection
/*Selection exist from shipment*/
StringBuffer insert = new StringBuffer();
insert.append("INSERT INTO T_SELECTION(AD_PINSTANCE_ID, T_SELECTION_ID) ");
int counter = 0;
for (Integer selectedId : selection) {
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 Shipments";
log.config(msg);
info.setText(msg);
trx.rollback();
return;
}
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 Shipments";
log.config(msg);
info.setText(msg);
trx.rollback();
return;
}
}
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.setText(msg);
log.log(Level.SEVERE, msg);
return;
}
para = new MPInstancePara(instance, 20);
para.setParameter("DocAction", "CO");
if (!para.save()) {
// not translated
String msg = "No DocAction Parameter added";
info.setText(msg);
log.log(Level.SEVERE, msg);
return;
}
// Execute Process
ProcessCtl worker = new ProcessCtl(this, Env.getWindowNo(this), pi, trx);
// complete tasks in unlockUI / generateInvoice_complete
worker.start();
}
use of org.compiere.util.Trx in project adempiere by adempiere.
the class VInOutInvoiceGen method generateShipments.
// saveSelection
/**************************************************************************
* Generate Shipments
*/
private void generateShipments() {
log.info("M_Warehouse_ID=" + m_M_Warehouse_ID);
String trxName = Trx.createTrxName("IOG");
//trx needs to be committed too
Trx trx = Trx.get(trxName, true);
//String trxName = null;
//Trx trx = null;
// prevents from being called twice
m_selectionActive = false;
statusBar.setStatusLine(Msg.getMsg(Env.getCtx(), "InOutGenerateGen"));
statusBar.setStatusDB(String.valueOf(selection.size()));
// Prepare Process
int AD_Process_ID = 0;
KeyNamePair docTypeKNPair = (KeyNamePair) cmbDocType.getSelectedItem();
if (docTypeKNPair.getKey() == MRMA.Table_ID) {
// M_InOut_GenerateRMA - org.adempiere.process.InOutGenerateRMA
AD_Process_ID = 52001;
} else {
// M_InOut_Generate - org.compiere.process.InOutGenerate
AD_Process_ID = 199;
}
MPInstance instance = new MPInstance(Env.getCtx(), AD_Process_ID, 0);
if (!instance.save()) {
info.setText(Msg.getMsg(Env.getCtx(), "ProcessNoInstance"));
return;
}
//insert selection
StringBuffer insert = new StringBuffer();
insert.append("INSERT INTO T_SELECTION(AD_PINSTANCE_ID, T_SELECTION_ID) ");
int counter = 0;
for (Integer selectedId : selection) {
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 Shipments";
log.config(msg);
info.setText(msg);
trx.rollback();
return;
}
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 Shipments";
log.config(msg);
info.setText(msg);
trx.rollback();
return;
}
}
//call process
ProcessInfo pi = new ProcessInfo("VInOutGen", AD_Process_ID);
pi.setAD_PInstance_ID(instance.getAD_PInstance_ID());
// Add Parameter - Selection=Y
MPInstancePara ip = new MPInstancePara(instance, 10);
ip.setParameter("Selection", "Y");
if (!ip.save()) {
// not translated
String msg = "No Parameter added";
info.setText(msg);
log.log(Level.SEVERE, msg);
return;
}
// Add Parameter - M_Warehouse_ID=x
ip = new MPInstancePara(instance, 20);
ip.setParameter("M_Warehouse_ID", Integer.parseInt(m_M_Warehouse_ID.toString()));
if (!ip.save()) {
// not translated
String msg = "No Parameter added";
info.setText(msg);
log.log(Level.SEVERE, msg);
return;
}
// Execute Process
ProcessCtl worker = new ProcessCtl(this, Env.getWindowNo(this), pi, trx);
// complete tasks in unlockUI / generateShipments_complete
worker.start();
//
}
use of org.compiere.util.Trx 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.util.Trx in project adempiere by adempiere.
the class ModelADServiceImpl method updateData.
public StandardResponseDocument updateData(ModelCRUDRequestDocument req) throws XFireFault {
StandardResponseDocument ret = StandardResponseDocument.Factory.newInstance();
StandardResponse resp = ret.addNewStandardResponse();
ModelCRUD modelCRUD = req.getModelCRUDRequest().getModelCRUD();
String serviceType = modelCRUD.getServiceType();
ADLoginRequest reqlogin = req.getModelCRUDRequest().getADLoginRequest();
String err = modelLogin(reqlogin, webServiceName, "updateData", serviceType);
if (err != null && err.length() > 0) {
resp.setError(err);
resp.setIsError(true);
return ret;
}
// Validate parameters vs service type
validateCRUD(modelCRUD);
String tableName = modelCRUD.getTableName();
int recordID = modelCRUD.getRecordID();
resp.setRecordID(recordID);
Properties ctx = m_cs.getM_ctx();
// start a trx
String trxName = Trx.createTrxName("ws_modelUpdateData");
Trx trx = Trx.get(trxName, false);
// get the PO for the tablename and record ID
MTable table = MTable.get(ctx, tableName);
if (table == null)
return rollbackAndSetError(trx, resp, ret, true, "No table " + tableName);
PO po = table.getPO(recordID, trxName);
if (po == null)
return rollbackAndSetError(trx, resp, ret, true, "No Record " + recordID + " in " + tableName);
POInfo poinfo = POInfo.getPOInfo(ctx, table.getAD_Table_ID());
DataRow dr = modelCRUD.getDataRow();
for (DataField field : dr.getFieldList()) {
// TODO: Implement lookup
if (m_webservicetype.isInputColumnNameAllowed(field.getColumn())) {
int idxcol = po.get_ColumnIndex(field.getColumn());
if (idxcol < 0) {
// The column doesn't exist - it must exist as it's defined in security
return rollbackAndSetError(trx, resp, ret, true, "Web service type " + m_webservicetype.getValue() + ": input column " + field.getColumn() + " does not exist");
} else {
try {
setValueAccordingToClass(po, poinfo, field, idxcol);
} catch (XFireFault e) {
log.log(Level.WARNING, "Error setting value", e);
return rollbackAndSetError(trx, resp, ret, true, "Web service type " + m_webservicetype.getValue() + ": input column " + field.getColumn() + " value could not be set: " + e.getLocalizedMessage());
}
}
} else {
return rollbackAndSetError(trx, resp, ret, true, "Web service type " + m_webservicetype.getValue() + ": input column " + field.getColumn() + " not allowed");
}
}
if (!po.save())
return rollbackAndSetError(trx, resp, ret, true, "Cannot save record in " + tableName + ": " + CLogger.retrieveErrorString("no log message"));
// close the trx
if (!trx.commit())
return rollbackAndSetError(trx, resp, ret, true, "Cannot commit transaction after delete record " + recordID + " in " + tableName);
trx.close();
return ret;
}
Aggregations