Showing posts with label Windchill Code Snippets. Show all posts
Showing posts with label Windchill Code Snippets. Show all posts

Soft Type – How to get type definition reference id

When we want to find the list of soft type objects and with the help of QuerySpec in Windchill, we need to use soft type’s type definition reference id in the QuerySpec condition.
Below code snippet will be useful in writing this QuerySpec

TypeDefinitionReference typeDefRef = TypedUtility.getTypeDefinitionReference("com.maheshmhetre.epmdoc.TestEPMDocument");
if (typeDefRef != null) {
qs.appendWhere(new SearchCondition(EPMDocument.class,
"typeDefinitionReference.key.id",
SearchCondition.EQUAL,
                           typeDefRef.getKey().getId()));
}


Here ‘com.maheshmhetre.epmdoc.TestEPMDocument’ is the Internal Name of the soft type. This value can be retrieved from soft type details from Type and Attribute Manager.

Windchill Document Usage Link


The WTDocumentUsageLink is an IteratedUsageLink between WTDocuments and WTDocumentMasters and used to create uses relationships between documents or document structure.
For example:
Several documents can contain the same glossary or one document can be made up of several sub-documents.
Documents should use this if a document is made up of subdocuments and the sub-documents can be reused by other documents, or need to be controlled separately.


The main document is linked to the other document’s master objects. Here main document is linked to the WTDocumentMaster objects of document_1, document_2 and document_3.
This means main document is always pointing to the latest copy of all linked document even they get new version or iteration.
Model Diagram:


JavaDoc:

wt.doc Class WTDocumentUsageLink
java.lang.Object
 wt.fc.WTObject
  wt.fc.ObjectToObjectLink
      wt.vc.struct.IteratedUsageLink
          wt.doc.WTDocumentUsageLink
public class WTDocumentUsageLink
extends IteratedUsageLink
implements Externalizable
UI Navigations:
To see the usage documents, navigate to main document’s details page -> Structure -> Document Structure table
To add the child documents, use ‘Add Documents’ row level action on parent document.
API Navigations:
To get the usage documents, we need to navigate through this link table. The service APIs are available in WTDocumentService class.
The relevant methods are (Please refer JavaDoc for more details)
Enumeration
getOrderedUsesWTDocumentMasters(WTDocument document)
         Navigates the WTDocumentUsageLink along the uses role, returning a Enumeration of WTDocumentMaster.
Enumeration
getOrderedUsesWTDocuments(WTDocument document, ConfigSpec configSpec)
         Applies the configSpec to the result of navigating the WTDocumentUsageLink along the uses role; returns a Enumeration of Persistable[], in which the WTDocumentUsageLinks are at the 0th position and the corresponding iterations (or master if there is no corresponding iteration) in the 1th position.
QueryResult
getUsedByWTDocuments(WTDocumentMaster documentMaster)
         Navigates the WTDocumentUsageLink along the usedBy role, returning a QueryResult of WTDocuments.
QueryResult
getUsesWTDocumentMasters(WTDocument document)
         Navigates the WTDocumentUsageLink along the uses role, returning a QueryResult of WTDocumentMaster.
QueryResult
getUsesWTDocuments(WTDocument document, WTDocumentConfigSpec configSpec)
         Applies the configSpec to the result of navigating the WTDocumentUsageLink along the uses role; returns a QueryResult of Persistable[], in which the WTDocumentUsageLinks are at the 0th position and the corresponding iterations (or master if there is no corresponding iteration) in the 1th position.
QueryResult
getUsesWTDocumentUsageLinks(WTDocument document)
         Returns the WTDocumentUsageLinks resulting from navigating the WTDocumentUsageLink along the uses role.

Windchill Command Line Utility Skeleton

In the last topic Windchill Remote Method Server Access API we saw how we can use RemoteMethodServer to invoke method server code remotely. Even though there are plenty of utilities available in Windchill OOTB, we came across various scenarios where we need to write our own custom utilities.
In this post I am trying to share the Windchill command line utility skeleton which uses RemoteMethodServer.
package com.blogspot.maheshmhetre.windchill.utilities;

import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;

import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;
import wt.session.SessionHelper;
import wt.util.WTException;

public class WindchillCustomUtilitySkeleton {

 //START of inner class - Server
 public static class Server implements RemoteAccess {
  
  public static void methodOne(String param1, String param2) {
   try {
    //Get login credentials
    SessionHelper.manager.getPrincipal();
    
    //Call core processing method
    WindchillCustomUtilitySkeleton utility = new WindchillCustomUtilitySkeleton(); 
    utility.doOperation(param1, param2);
    
   } catch (Exception e) {
    e.printStackTrace();
   }
   
   return;
  }
 } //END of inner class - Server

 private void doOperation(String param1, String param2) throws WTException{
  //TODO execute your utility logic
  System.out.println("This sysout will be available in method server log.");
 }
 
 public static void main(String[] args) {
  //TODO get utility input parameters
  //TODO basic validation

  System.out.println("This sysout will be available on console from where the utility is running.");
  //prepare input params
  //data types will depends on your method parameters and can be any valid object type
  Class argTypes[] = {String.class, String.class};//add all remote method parameter data types
  Object argValues[] = {"param_1_value", "param_2_value"};//respective data value
  
  //call remote method - pass inner class name, method name, arg types and arg values
  try {
   RemoteMethodServer.getDefault().invoke("methodOne", 
     "com.blogspot.maheshmhetre.windchill.utilities.WindchillCustomUtilitySkeleton$Server",
     null, argTypes, argValues);
  } catch (RemoteException e) {
   e.printStackTrace();
  } catch (InvocationTargetException e) {
   e.printStackTrace();
  }

  Runtime.getRuntime().exit(0);
 }
}

Please share your thoughts to improve this skeleton.

Windchill Remote Method Server Access API


To invoke Windchill server side code from outside method server, we need to use RMI calls. Windchill provides mechanism to do this by using RemoteMethodServer class.

The method signature
RemoteMethodServer.getDefault().invoke(String methodName, String className, Object obj, Class[] paramClassTypeArray, Objet[] paramValuesArray);

The core code to invoke remote method is
RemoteMethodServer remotemethodserver = RemoteMethodServer.getDefault();
remotemethodserver.setUserName(userName);
remotemethodserver.setPassword(Password);

Class argTypes[] = {String.class, String.class};//add all remote method parameter data types
Object argValues[] = {"param_1_value", "param_2_value"};//respective data value

Object returnObj = remotemethodserver.invoke(methodName, className, null, argTypes, argValues);

If you want the user who is executing the code should provide username and password explicitly using basic authentication handled by framework, skip the username and password set to RemoteMethodServer and user SessionHelper.manager.getPrincipal(); Adding this will give popup for username and password if not available.
Keep in mind that there may be an issue if you are executing the remote access code from some external terminal like putty session. The popup will not work by default and need some additional settings.

In the next post we will see a command line utility skeleton which uses RemoteMethodServer API.