📄 mwfactivity.java
字号:
{
newState = StateEngine.STATE_Aborted;
if (!(doc.processIt (DocAction.ACTION_Reject)))
setTextMsg ("Cannot Reject - Document Status: " + doc.getDocStatus());
}
else
{
if (isInvoker())
{
int startAD_User_ID = getAD_User_ID();
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(),
startAD_User_ID == doc.getDoc_User_ID()); // own doc
// 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());
}
}
}
// No Invoker - Approve
else if (!(doc.processIt (DocAction.ACTION_Approve)))
{
newState = StateEngine.STATE_Aborted;
setTextMsg ("Cannot Approve - Document Status: " + doc.getDocStatus());
}
}
doc.save();
}
catch (Exception e)
{
newState = StateEngine.STATE_Terminated;
setTextMsg ("User Choice: " + e.toString());
log.log(Level.WARNING, "", e);
}
// Send Approval Notification
if (newState.equals(StateEngine.STATE_Aborted))
{
MClient client = MClient.get(getCtx(), doc.getAD_Client_ID());
client.sendEMail(doc.getDoc_User_ID(),
doc.getDocumentInfo() + ": " + Msg.getMsg(getCtx(), "NotApproved"),
doc.getSummary()
+ "\n" + doc.getProcessMsg()
+ "\n" + getTextMsg(),
doc.createPDF());
}
}
setWFState (newState);
return ok;
} // setUserChoice
/**
* Forward To
* @param AD_User_ID user
* @param textMsg text message
* @return true if forwarded
*/
public boolean forwardTo (int AD_User_ID, String textMsg)
{
if (AD_User_ID == getAD_User_ID())
{
log.log(Level.WARNING, "Same User - AD_User_ID=" + AD_User_ID);
return false;
}
//
MUser oldUser = MUser.get(getCtx(), getAD_User_ID());
MUser user = MUser.get(getCtx(), AD_User_ID);
if (user == null || user.get_ID() == 0)
{
log.log(Level.WARNING, "Does not exist - AD_User_ID=" + AD_User_ID);
return false;
}
// Update
setAD_User_ID (user.getAD_User_ID());
setTextMsg(textMsg);
save();
// Close up Old Event
getEventAudit();
m_audit.setAD_User_ID(oldUser.getAD_User_ID());
m_audit.setTextMsg(getTextMsg());
m_audit.setAttributeName("AD_User_ID");
m_audit.setOldValue(oldUser.getName()+ "("+oldUser.getAD_User_ID()+")");
m_audit.setNewValue(user.getName()+ "("+user.getAD_User_ID()+")");
//
m_audit.setWFState(getWFState());
m_audit.setEventType(MWFEventAudit.EVENTTYPE_StateChanged);
long ms = System.currentTimeMillis() - m_audit.getCreated().getTime();
m_audit.setElapsedTimeMS(new BigDecimal(ms));
m_audit.save();
// Create new one
m_audit = new MWFEventAudit(this);
m_audit.save();
return true;
} // forwardTo
/**
* Set User Confirmation
* @param textMsg optional message
*/
public void setUserConfirmation (int AD_User_ID, String textMsg)
{
log.fine(textMsg);
setWFState (StateEngine.STATE_Running);
setAD_User_ID(AD_User_ID);
if (textMsg != null)
setTextMsg (textMsg);
setWFState (StateEngine.STATE_Completed);
} // setUserConfirmation
/**
* Fill Parameter
* @param pInstance process instance
*/
private void fillParameter(MPInstance pInstance, Trx trx)
{
getPO(trx);
//
MWFNodePara[] nParams = m_node.getParameters();
MPInstancePara[] iParams = pInstance.getParameters();
for (int pi = 0; pi < iParams.length; pi++)
{
MPInstancePara iPara = iParams[pi];
for (int np = 0; np < nParams.length; np++)
{
MWFNodePara nPara = nParams[np];
if (iPara.getParameterName().equals(nPara.getAttributeName()))
{
String variableName = nPara.getAttributeValue();
log.fine(nPara.getAttributeName()
+ " = " + variableName);
// Value - Constant/Variable
Object value = variableName;
if (variableName != null && variableName.length() == 0)
value = null;
else if (variableName.indexOf("@") != -1 && m_po != null) // we have a variable
{
// Strip
int index = variableName.indexOf("@");
String columnName = variableName.substring(index+1);
index = columnName.indexOf("@");
if (index == -1)
{
log.warning(nPara.getAttributeName()
+ " - cannot evaluate=" + variableName);
break;
}
columnName = columnName.substring(0, index);
index = m_po.get_ColumnIndex(columnName);
if (index != -1)
{
value = m_po.get_Value(index);
}
else // not a column
{
// try Env
String env = Env.getContext(getCtx(), columnName);
if (env.length() == 0)
{
log.warning(nPara.getAttributeName()
+ " - not column nor environment =" + columnName
+ "(" + variableName + ")");
break;
}
else
value = env;
}
} // @variable@
// No Value
if (value == null)
{
if (nPara.isMandatory())
log.warning(nPara.getAttributeName()
+ " - empty - mandatory!");
else
log.fine(nPara.getAttributeName()
+ " - empty");
break;
}
// Convert to Type
try
{
if (DisplayType.isNumeric(nPara.getDisplayType())
|| DisplayType.isID(nPara.getDisplayType()))
{
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(nPara.getAttributeName()
+ " = " + variableName + " (=" + bd + "=)");
}
else if (DisplayType.isDate(nPara.getDisplayType()))
{
Timestamp ts = null;
if (value instanceof Timestamp)
ts = (Timestamp)value;
else
ts = Timestamp.valueOf(value.toString());
iPara.setP_Date(ts);
log.fine(nPara.getAttributeName()
+ " = " + variableName + " (=" + ts + "=)");
}
else
{
iPara.setP_String(value.toString());
log.fine(nPara.getAttributeName()
+ " = " + variableName
+ " (=" + value + "=) " + value.getClass().getName());
}
if (!iPara.save())
log.warning("Not Saved - " + nPara.getAttributeName());
}
catch (Exception e)
{
log.warning(nPara.getAttributeName()
+ " = " + variableName + " (" + value
+ ") " + value.getClass().getName()
+ " - " + e.getLocalizedMessage());
}
break;
}
} // node parameter loop
} // instance parameter loop
} // fillParameter
/**
* Post Immediate
*/
private void postImmediate()
{
if (CConnection.get().isAppsServerOK(false))
{
try
{
Server server = CConnection.get().getServer();
if (server != null)
{
String error = server.postImmediate(Env.getCtx(),
m_postImmediate.getAD_Client_ID(),
m_postImmediate.get_Table_ID(), m_postImmediate.get_ID(),
true, null);
m_postImmediate.get_Logger().config("Server: " + error == null ? "OK" : error);
return;
}
else
m_postImmediate.get_Logger().config("NoAppsServer");
}
catch (RemoteException e)
{
m_postImmediate.get_Logger().config("(RE) " + e.getMessage());
}
catch (Exception e)
{
m_postImmediate.get_Logger().config("(ex) " + e.getMessage());
}
}
} // PostImmediate
/*********************************
* Send EMail
*/
private void sendEMail()
{
DocAction doc = (DocAction)m_po;
MMailText text = new MMailText (getCtx(), m_node.getR_MailText_ID(), null);
text.setPO(m_po, true);
//
String subject = doc.getDocumentInfo()
+ ": " + text.getMailHeader();
String message = text.getMailText(true)
+ "\n-----\n" + doc.getDocumentInfo()
+ "\n" + doc.getSummary();
File pdf = doc.createPDF();
//
MClient client = MClient.get(doc.getCtx(), doc.getAD_Client_ID());
// Explicit EMail
sendEMail(client, 0, m_node.getEMail(), subject, message, pdf);
// Recipient Type
String recipient = m_node.getEMailRecipient();
// email to document user
if (recipient == null || recipient.length() == 0)
sendEMail(client, doc.getDoc_User_ID(), null, subject, message, pdf);
else if (recipient.equals(MWFNode.EMAILRECIPIENT_DocumentBusinessPartner))
{
int index = m_po.get_ColumnIndex("AD_User_ID");
if (index > 0)
{
Object oo = m_po.get_Value(index);
if (oo instanceof Integer)
{
int AD_User_ID = ((Integer)oo).intValue();
if (AD_User_ID != 0)
sendEMail(client, AD_User_ID, null, subject, message, pdf);
else
log.fine("No User in Document");
}
else
log.fine("Empty User in Document");
}
else
log.fine("No User Field in Document");
}
else if (recipient.equals(MWFNode.EMAILRECIPIENT_DocumentOwner))
sendEMail(client, doc.getDoc_User_ID(), null, subject, message, pdf);
else if (recipient.equals(MWFNode.EMAILRECIPIENT_WFResponsible))
{
MWFResponsible resp = getResponsible();
if (resp.isInvoker())
sendEMail(client, doc.getDoc_User_ID(), null, subject, message, pdf);
else if (resp.isHuman())
sendEMail(client, resp.getAD_User_ID(), null, subject, message, pdf);
else if (resp.isRole())
{
MRole role = resp.getRole();
if (role != null)
{
MUser[] users = MUser.getWithRole(role);
for (int i = 0; i < users.length; i++)
sendEMail(client, users[i].getAD_User_ID(), null, subject, message, pdf);
}
}
else if (resp.isOrganization())
{
MOrgInfo org = MOrgInfo.get(getCtx(), m_po.getAD_Org_ID());
if (org.getSupervisor_ID() == 0)
log.fine("No Supervisor for AD_Org_ID=" + m_po.getAD_Org_ID());
else
sendEMail(client, org.getSupervisor_ID(), null, subject, message, pdf);
}
}
} // sendEMail
/**
* Send actual EMail
* @param client client
* @param AD_User_ID user
* @param email email string
* @param subject subject
* @param message message
* @param pdf attachment
*/
private void sendEMail (MClient client, int AD_User_ID, String email,
String subject, String message, File pdf)
{
if (AD_User_ID != 0)
{
MUser user = MUser.get(getCtx(), AD_User_ID);
email = user.getEMail();
if (email != null && email.length() > 0)
{
email = email.trim();
if (!m_emails.contains(email))
{
client.sendEMail(null, user, subject, message, pdf);
m_emails.add(email);
}
}
else
log.info("No EMail for User " + user.getName());
}
else if (email != null && email.length() > 0)
{
// Just one
if (email.indexOf(";") == -1)
{
email = email.trim();
if (!m_emails.contains(email))
{
client.sendEMail(email, subject, message, pdf);
m_emails.add(email);
}
return;
}
// Multiple EMail
StringTokenizer st = new StringTokenizer(email, ";");
while (st.hasMoreTokens())
{
String email1 = st.nextToken().trim();
if (email1.length() == 0)
continue;
if (!m_emails.contains(email1))
{
client.sendEMail(email1, subject, message, pdf);
m_emails.add(email1);
}
}
}
} // sendEMail
/**************************************************************************
* Get Process Activity (Event) History
* @return history
*/
public String getHistoryHTML()
{
SimpleDateFormat format = DisplayType.getDateFormat(DisplayType.DateTime);
StringBuffer sb = new StringBuffer();
MWFEventAudit[] events = MWFEventAudit.get(getCtx(), getAD_WF_Process_ID());
for (int i = 0; i < events.length; i++)
{
MWFEventAudit audit = events[i];
// sb.append("<p style=\"width:400\">");
sb.append("<p>");
sb.append(format.format(audit.getCreated()))
.append(" ")
.append(getHTMLpart("b", audit.getNodeName()))
.append(": ")
.append(getHTMLpart(null, audit.getDescription()))
.append(getHTMLpart("i", audit.getTextMsg()));
sb.append("</p>");
}
return sb.toString();
} // getHistory
/**
* Get HTML part
* @param tag HTML tag
* @param content content
* @return <tag>content</tag>
*/
private StringBuffer getHTMLpart (String tag, String content)
{
StringBuffer sb = new StringBuffer();
if (content == null || content.length() == 0)
return sb;
if (tag != null && tag.length() > 0)
sb.append("<").append(tag).append(">");
sb.append(content);
if (tag != null && tag.length() > 0)
sb.append("</").append(tag).append(">");
return sb;
} // getHTMLpart
/**************************************************************************
* Does the underlying PO (!) object have a PDF Attachment
* @return true if there is a pdf attachment
*/
public boolean isPdfAttachment()
{
if (getPO() == null)
return false;
return m_po.isPdfAttachment();
} // isPDFAttachment
/**
* Get PDF Attachment of underlying PO (!) object
* @return pdf data or null
*/
public byte[] getPdfAttachment()
{
if (getPO() == null)
return null;
return m_po.getPdfAttachment();
} // getPdfAttachment
/**
* String Representation
* @return info
*/
public String toString ()
{
StringBuffer sb = new StringBuffer ("MWFActivity[");
sb.append(get_ID()).append(",Node=");
if (m_node == null)
sb.append(getAD_WF_Node_ID());
else
sb.append(m_node.getName());
sb.append(",State=").append(getWFState())
.append(",AD_User_ID=").append(getAD_User_ID())
.append(",").append(getCreated())
.append ("]");
return sb.toString ();
} // toString
/**
* User String Representation.
* Suspended: Approve it (Joe)
* @return info
*/
public String toStringX ()
{
StringBuffer sb = new StringBuffer();
sb.append(getWFStateText())
.append(": ").append(getNode().getName());
if (getAD_User_ID() > 0)
{
MUser user = MUser.get(getCtx(), getAD_User_ID());
sb.append(" (").append(user.getName()).append(")");
}
return sb.toString();
} // toStringX
} // MWFActivity
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -