Approve Engineering Change Request is a multiple user task, there are three vote options: Approve, Research_Required and Reject.
Requirements: If user votes on “Research_Required” and “Reject”, then comment is mandatory, currently we don’t have check for this.
Technical solution Options
- Use the code in Complete transition, but his only works for Single user tasks.
- Use java script check thru UI
- Create a class extends StandardNmWorkItemService service class
Below is how I achieve this using #3.
First, add this service to wt.properties
wt.services.service.10041=com.ptc.netmarkets.work.NmWorkItemService/MyNmWorkitemService
Below is the code of the service.
package ext.hhi.services;
import java.util.HashMap;
import java.util.Iterator;
import wt.fc.ObjectReference;
import wt.fc.Persistable;
import wt.method.RemoteMethodServer;
import wt.util.WTException;
import wt.workflow.engine.WfActivity;
import wt.workflow.work.WorkItem;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.netmarkets.work.StandardNmWorkItemService;
import ext.hhi.utils.MiscHelper;
import ext.hhi.utils.StringUtil;
public class MyNmWorkitemService extends StandardNmWorkItemService {
/**
*
*/
private static final long serialVersionUID = 7130504123627625857L;
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public static MyNmWorkitemService newMyNmWorkitemService() throws WTException {
MyNmWorkitemService service = new MyNmWorkitemService();
service.initialize();
return service;
}
@Override
public void complete(NmCommandBean commandBean, HashMap map) throws WTException {
if (!RemoteMethodServer.ServerFlag) {
String method = "complete";
Class<?>[] argTypes = {NmCommandBean.class,HashMap.class};
Object[] argValues = {commandBean, map};
try {
RemoteMethodServer.getDefault().invoke(method, null, this, argTypes, argValues);
}
catch (Exception e) {
if (e instanceof WTException) {
throw (WTException) e;
} else {
throw new WTException(e);
}
}
}
validateComplete(commandBean, map);
super.complete(commandBean, map);
}
private void validateComplete(NmCommandBean commandBean, HashMap<?, ?> map) throws WTException {
for(Object mapKey:map.keySet()) {
System.out.println("params key is:" + mapKey.toString());
}
for(Object mapValue:map.values()) {
System.out.println("params value is:" + mapValue.toString());
}
com.ptc.netmarkets.model.NmOid nmoid = commandBean.getPageOid();
WorkItem workitem = getWorkItem(nmoid);
ObjectReference sourceReference = workitem.getSource();
if(sourceReference != null) {
Persistable sourceObject = sourceReference.getObject();
if(sourceObject != null && sourceObject instanceof WfActivity) {
WfActivity activity = (WfActivity)sourceObject;
String activityName = activity.getName();
if("Your activity".equals(activityName)) {
String vote = MiscHelper.fixValue(getUserRoute(map));
String comments = MiscHelper.fixValue(getUserComments(map));
System.out.println("vote:" + route);
System.out.println("comments:" + comments);
if(vote.equalsIgnoreCase("Research_Required")||vote.equalsIgnoreCase("Reject")) {
if(StringUtil.isEmpty(comments)) {
throw new WTException("Research_Required or Reject Comments are required.");
}
}
}
}
}
}
private static String getUserComments(HashMap<?, ?> params) {
String comments = "";
try {
Iterator<?> it = params.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
if(key.trim().endsWith("comments___textarea"))
comments = (String) params.get(key);
}
} catch (Exception e) {
System.out.println("getUserComment Error");
e.printStackTrace();
}
System.out.println("Inisde get user comments:" + comments);
return comments;
}
private static String getUserRoute(HashMap<?,?> params) {
String route = "";
try {
Iterator<?> it = params.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
if(key.endsWith("WfUserEvent0___radio"))
route = (String) params.get(key);
}
} catch (Exception e) {
System.out.println("getUserRoute error");
e.printStackTrace();
}
System.out.println("Vote option is:" + route);
return route;
}
}