3

I'm customizing a PLM Windchill Workflow, which provides a mechanism to execute java code snippets. Unfortunately, they are 'inserted' into prepared service's method, which means that there is no way to import classes, so I have to include full package names to use it. Don't try to understand the snippet below, just look how does it looks like:

wt.fc.QueryResult activities = wt.fc.PersistenceHelper.manager.find((wt.pds.StatementSpec) activitiesQuery);
    while (activities.hasMoreElements()) {
          wt.workflow.work.WfAssignedActivity activity = (wt.workflow.work.WfAssignedActivity) activities.nextElement();
          if(activity.getDisplayIdentifier().toString().equals("Analyze Image Request")){
              java.util.List<wt.workflow.work.WorkItem> workItems = wt.workflow.status.WfWorkflowStatusHelper.service.getWorkItems(activity);
              for (wt.workflow.work.WorkItem workItem : workItems){
                  String action = workItem.getActionPerformed();
                  if(action != null && action.equals("Accepted")){
                      wt.org.WTPrincipalReference approver = workItem.getOwnership().getOwner();
                      n_approver = approver.getFullName() + " ("+approver.getDisplayName()+")";
                      wt.fc.collections.WTHashSet approverSet = new wt.fc.collections.WTHashSet(java.util.Arrays.asList(approver));
                      wt.project.Role role = wt.project.Role.toRole("APPROVER");
                      com.ptc.windchill.pdmlink.change.server.impl.WorkflowProcessHelper.setChangeItemParticipants(report, role, approverSet);
                      break;
                  }
              }
              break;
          }
      }

enter image description here

And my question is - how to make this code any more readable? Of course there is no way to import classes inside the method, there is even no way to divide this snippet into separate methods (as it is 'pasted' into one) so I'm looking for other ideas.

DamianoPantani
  • 1,168
  • 2
  • 13
  • 23
  • 2
    Write the code in a custom class, and call it? – Andreas Sep 02 '16 at 07:43
  • @Andreas I could extract some pieces of code to separated classes and call it, but I have to leave a lot of logic in this snippet so yeah, it is a very good option but still doesn't solve my main problem. Thanks. – DamianoPantani Sep 02 '16 at 07:58

3 Answers3

2

One option to make the code more readable would be to separate chained method/property calls across multiple lines.

For example, this line:

wt.project.Role role = wt.project.Role.toRole("APPROVER");

could be rewritten as:

wt.project.Role role = wt
                       .project
                       .Role
                       .toRole("APPROVER");
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • @Damiano Actually this notation is already used for the functional side of Java, e.g. when calling nested methods in the Guava library. – Tim Biegeleisen Sep 02 '16 at 07:49
2

You can call this complete code from a Customized Java class.

You just have to call your class and take the final parameters required from the Java class to make it more readable.

If you need multiple outputs write multiple methods in Java class and call them in workflow expression.

jwpfox
  • 5,124
  • 11
  • 45
  • 42
Pranshu
  • 21
  • 1
1

You can't. Workflows expressions are methods bodies. A statement like

wt.fc.QueryResult activities = wt.fc.PersistenceHelper.manager.find((wt.pds.StatementSpec) activitiesQuery);

ends in a class under $WT_HOME/codebase/wt/workflow/expr/ with a method :

public static Object executemethod_1(Object[] var0, Object[] var1) throws Exception {
 wt.fc.QueryResult activities = wt.fc.PersistenceHelper.manager.find((wt.pds.StatementSpec) activitiesQuery);

// some generated code to handle variables...
}

So, you can't use import.

However : If you have a PDMLink version greater than 10, You can externalize workflow expression

http://support.ptc.com/cs/help/windchill_hc/wc100_hc/index.jspx?id=WFTemplateExtExpression&action=show

This create a java class under /codebase/ext/wt/workflow/externalize

Then you can do what you want, but you'll have to compile these classes, and do a stop/start in case of modifications.

Basically, it's nothing more than calling external code from the expression, so I don't use it a lot...

olikaf
  • 591
  • 3
  • 11