Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X
how to get documents template from library using api code through a workflow , i have a code which convert template to document automatically but only one input , i need to give the ibrary container as input to convert all templates into document
Solved! Go to Solution.
package ext.test;
import java.util.ArrayList;
import wt.content.ContentHelper;
import wt.content.ContentHolder;
import wt.doc.WTDocument;
import wt.enterprise.TemplatesUtility;
import wt.fc.PersistenceHelper;
import wt.fc.ReferenceFactory;
import wt.folder.FolderHelper;
import wt.folder.FolderingInfo;
import wt.inf.container.WTContainer;
import wt.inf.container.WTContainerRef;
import wt.util.WTException;
import wt.vc.VersionControlHelper;
public class WTDocumentHelper {
public static WTDocument getDocumentTemplate(String
paramDocTemplateContainerRef)
paramDocTemplateName,
WTContainerRef
{
WTDocument docTemplate = null;
ReferenceFactory refFact = new wt.fc.ReferenceFactory();
try {
ArrayList asd = wt.doc.WTDocumentHelper.service.getTemplates(paramDocTemplateContainerRef);
for(int i = 0; i < asd.size(); i++){
String[] tpl = (String[]) asd.get(i);
if (tpl[1].equals(paramDocTemplateName)){
docTemplate = (WTDocument) refFact.getReference(tpl[4]).getObject();
break;
}
}
docTemplate=(WTDocument) VersionControlHelper.service.getLatestIteration(docTemplate, true);
if (docTemplate==null)
throw new WTException("Not found WTDocument Template :" + paramDocTemplateName);
} catch (WTException e) {
e.printStackTrace();
}
return docTemplate;
}
public static WTDocument createDocumentFromTemplate(String paramDocName,String
paramDocTemplateName, WTContainer paramDocContainer) throws WTException {
WTDocument doc = null;
WTContainerRef contRef = WTContainerRef.newWTContainerRef(paramDocContainer);
try {
WTDocument docTemplate = getDocumentTemplate(paramDocTemplateName,contRef);
doc = WTDocument.newWTDocument();
doc.setName(paramDocName);
doc.setTypeDefinitionReference(docTemplate.getTypeDefinitionReference());
doc.setContainerReference(contRef);
doc.setDomainRef(paramDocContainer.getDefaultDomainReference());
doc.setFormat(docTemplate.getFormat());
PersistenceHelper.manager.save(doc);
//Copy primary content file from Template
ContentHelper.service.copyContent((ContentHolder)docTemplate,
(ContentHolder) doc);
//Rename primary content file
doc = (WTDocument)TemplatesUtility.setFilename((WTDocument)doc);
} catch (Exception e) {
e.printStackTrace();
}
return doc;
}
}
*while coping and pasting ,check for the alignment.
create a new work flow after that
Refer Sample expression code for reference.
System.out.println("*******START************");
wt.part.WTPart part = (wt.part.WTPart)primaryBusinessObject;
com.ptc.core.lwc.server.PersistableAdapter obj = new com.ptc.core.lwc.server.PersistableAdapter(part, null, null, null);
obj.load("Choose_library");
String attrValue = (String) obj.get("Choose_library");
System.out.println("The selected library is "+attrValue);
String a = attrValue;
System.out.println("The selected library is "+a);
if(a.equals("Library4")) { ref =
wt.inf.container.WTContainerHelper.service.getByPath("wt.inf.container.OrgContainer=WeaponX/wt.inf.library.WTLibrary=Lib
rary4"); }
else if(a.equals("Library3")) { ref =
wt.inf.container.WTContainerHelper.service.getByPath("wt.inf.container.OrgContainer=WeaponX/wt.inf.library.WTLibrary=Lib
rary3"); }
else if(a.equals("Library2")) { ref =
wt.inf.container.WTContainerHelper.service.getByPath("wt.inf.container.OrgContainer=WeaponX/wt.inf.library.WTLibrary=Lib
rary2"); }
else if(a.equals("Library1")) { ref =
wt.inf.container.WTContainerHelper.service.getByPath("wt.inf.container.OrgContainer=WeaponX/wt.inf.library.WTLibrary=Lib
rary1"); }
else { System.out.println("PLEASE CONTACT ADMIN"); }
java.util.ArrayList asd = wt.doc.WTDocumentHelper.service.getTemplates(ref);
for(int i = 0; i < asd.size(); i++)
{
String[] template = (String[])asd.get(i);
System.out.println("The Selected template is " +template[1]);
wt.part.WTPartMaster master = (wt.part.WTPartMaster) part.getMaster();
System.out.println("***** Name ->"+ master.getName());
wt.part.WTPart latest = (wt.part.WTPart)wt.vc.VersionControlHelper.service.allVersionsOf(master).nextElement();
String versionLatest = wt.vc.VersionControlHelper.getVersionIdentifier(latest).getValue();
System.out.println("******** Identity -> "+ latest.getIdentity());
System.out.println("******** Latest Version -> "+ versionLatest);
System.out.println("******** Latest Iteration -> "+ latest.getIterationDisplayIdentifier());
System.out.println("******** State of latest version of latest iteration -> "+latest.getState());
if (versionLatest.equals("A") == true){
System.out.println("********Automatic Document Creation Workflow has Started************** ");
wt.inf.container.WTContainer container = ref.getContainer();
String paramDocTemplateName = template[1];
String paramDocName = paramDocTemplateName;
wt.doc.WTDocument docFromTemplate = null;
try{
docFromTemplate= ext.test.WTDocumentHelper.createDocumentFromTemplate(paramDocName, paramDocTemplateName,
container);
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("************Automatic Document Creation Workflow has Ended********** ");
System.out.println("********DESCRIBE LINK ATTACHING**********");
wt.part.WTPartDescribeLink wl=wt.part.WTPartDescribeLink.newWTPartDescribeLink(part, docFromTemplate);
wt.fc.PersistenceServerHelper.manager.insert(wl);
System.out.println("**********DESCRIBE LINK ATTACHED******");
}
else
{
System.out.println("************THIS IS A REVISED VERSION**************");
}
}
System.out.println("***********END******************* ");
*while coping and pasting ,check for the alignment.
looks like method
wt.enterprise.EnterpriseHelper.service.getTemplates
does the trick. It is however not part of the JavaDoc,and therefore perhaps a little bit unsupported ;).
BR,
Joachim
Thanks Joachim , but i written a different code , it helped me.
package ext.test;
import java.util.ArrayList;
import wt.content.ContentHelper;
import wt.content.ContentHolder;
import wt.doc.WTDocument;
import wt.enterprise.TemplatesUtility;
import wt.fc.PersistenceHelper;
import wt.fc.ReferenceFactory;
import wt.folder.FolderHelper;
import wt.folder.FolderingInfo;
import wt.inf.container.WTContainer;
import wt.inf.container.WTContainerRef;
import wt.util.WTException;
import wt.vc.VersionControlHelper;
public class WTDocumentHelper {
public static WTDocument getDocumentTemplate(String
paramDocTemplateContainerRef)
paramDocTemplateName,
WTContainerRef
{
WTDocument docTemplate = null;
ReferenceFactory refFact = new wt.fc.ReferenceFactory();
try {
ArrayList asd = wt.doc.WTDocumentHelper.service.getTemplates(paramDocTemplateContainerRef);
for(int i = 0; i < asd.size(); i++){
String[] tpl = (String[]) asd.get(i);
if (tpl[1].equals(paramDocTemplateName)){
docTemplate = (WTDocument) refFact.getReference(tpl[4]).getObject();
break;
}
}
docTemplate=(WTDocument) VersionControlHelper.service.getLatestIteration(docTemplate, true);
if (docTemplate==null)
throw new WTException("Not found WTDocument Template :" + paramDocTemplateName);
} catch (WTException e) {
e.printStackTrace();
}
return docTemplate;
}
public static WTDocument createDocumentFromTemplate(String paramDocName,String
paramDocTemplateName, WTContainer paramDocContainer) throws WTException {
WTDocument doc = null;
WTContainerRef contRef = WTContainerRef.newWTContainerRef(paramDocContainer);
try {
WTDocument docTemplate = getDocumentTemplate(paramDocTemplateName,contRef);
doc = WTDocument.newWTDocument();
doc.setName(paramDocName);
doc.setTypeDefinitionReference(docTemplate.getTypeDefinitionReference());
doc.setContainerReference(contRef);
doc.setDomainRef(paramDocContainer.getDefaultDomainReference());
doc.setFormat(docTemplate.getFormat());
PersistenceHelper.manager.save(doc);
//Copy primary content file from Template
ContentHelper.service.copyContent((ContentHolder)docTemplate,
(ContentHolder) doc);
//Rename primary content file
doc = (WTDocument)TemplatesUtility.setFilename((WTDocument)doc);
} catch (Exception e) {
e.printStackTrace();
}
return doc;
}
}
*while coping and pasting ,check for the alignment.
create a new work flow after that
Refer Sample expression code for reference.
System.out.println("*******START************");
wt.part.WTPart part = (wt.part.WTPart)primaryBusinessObject;
com.ptc.core.lwc.server.PersistableAdapter obj = new com.ptc.core.lwc.server.PersistableAdapter(part, null, null, null);
obj.load("Choose_library");
String attrValue = (String) obj.get("Choose_library");
System.out.println("The selected library is "+attrValue);
String a = attrValue;
System.out.println("The selected library is "+a);
if(a.equals("Library4")) { ref =
wt.inf.container.WTContainerHelper.service.getByPath("wt.inf.container.OrgContainer=WeaponX/wt.inf.library.WTLibrary=Lib
rary4"); }
else if(a.equals("Library3")) { ref =
wt.inf.container.WTContainerHelper.service.getByPath("wt.inf.container.OrgContainer=WeaponX/wt.inf.library.WTLibrary=Lib
rary3"); }
else if(a.equals("Library2")) { ref =
wt.inf.container.WTContainerHelper.service.getByPath("wt.inf.container.OrgContainer=WeaponX/wt.inf.library.WTLibrary=Lib
rary2"); }
else if(a.equals("Library1")) { ref =
wt.inf.container.WTContainerHelper.service.getByPath("wt.inf.container.OrgContainer=WeaponX/wt.inf.library.WTLibrary=Lib
rary1"); }
else { System.out.println("PLEASE CONTACT ADMIN"); }
java.util.ArrayList asd = wt.doc.WTDocumentHelper.service.getTemplates(ref);
for(int i = 0; i < asd.size(); i++)
{
String[] template = (String[])asd.get(i);
System.out.println("The Selected template is " +template[1]);
wt.part.WTPartMaster master = (wt.part.WTPartMaster) part.getMaster();
System.out.println("***** Name ->"+ master.getName());
wt.part.WTPart latest = (wt.part.WTPart)wt.vc.VersionControlHelper.service.allVersionsOf(master).nextElement();
String versionLatest = wt.vc.VersionControlHelper.getVersionIdentifier(latest).getValue();
System.out.println("******** Identity -> "+ latest.getIdentity());
System.out.println("******** Latest Version -> "+ versionLatest);
System.out.println("******** Latest Iteration -> "+ latest.getIterationDisplayIdentifier());
System.out.println("******** State of latest version of latest iteration -> "+latest.getState());
if (versionLatest.equals("A") == true){
System.out.println("********Automatic Document Creation Workflow has Started************** ");
wt.inf.container.WTContainer container = ref.getContainer();
String paramDocTemplateName = template[1];
String paramDocName = paramDocTemplateName;
wt.doc.WTDocument docFromTemplate = null;
try{
docFromTemplate= ext.test.WTDocumentHelper.createDocumentFromTemplate(paramDocName, paramDocTemplateName,
container);
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("************Automatic Document Creation Workflow has Ended********** ");
System.out.println("********DESCRIBE LINK ATTACHING**********");
wt.part.WTPartDescribeLink wl=wt.part.WTPartDescribeLink.newWTPartDescribeLink(part, docFromTemplate);
wt.fc.PersistenceServerHelper.manager.insert(wl);
System.out.println("**********DESCRIBE LINK ATTACHED******");
}
else
{
System.out.println("************THIS IS A REVISED VERSION**************");
}
}
System.out.println("***********END******************* ");
*while coping and pasting ,check for the alignment.