Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X
It posible to create WTDocument from Template from same Project?
Solved! Go to Solution.
Create Java Class ext.tpolis.WTDocumentHelper.java
package ext.tpolis;
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;
/**
* @author Ostapenko Vitaliy
* @version 1.0
*/
public class WTDocumentHelper {
/*
* getDocumentTemplate
* Return Template WTDocument of WTContainerRef
*/
public static WTDocument getDocumentTemplate(String paramDocTemplateName, WTContainerRef paramDocTemplateContainerRef){
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;
}
}
//Get latest revision of template
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;
}
/*
* createDocumentFromTemplate
* Return WTDocument create from Template
*/
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;
}
}
Compile Class.
Put WTDocumentHelper.class in to %WT_HOME%\codebase\ext\tpolis
In Template Workflow create Expression and add java code:
wt.doc.WTDocument document = (wt.doc.WTDocument)primaryBusinessObject;
wt.inf.container.WTContainer container = document.getContainer();
// Set Doc Name
String paramDocName = "TestDoc";
// Set Doc Template Name that is locate in Project
String paramDocTemplateName = "Test Template";
wt.doc.WTDocument docFromTemplate = null;
try{
docFromTemplate= ext.tpolis.WTDocumentHelper.createDocumentFromTemplate(paramDocName, paramDocTemplateName, container);
}catch (Exception e) {
e.printStackTrace();
}
Then in Project run Route Document after finish WfProcess in folder /Default of Project was created TestDoc from Document Template.
Hi,
Try below code to create WTDocument using template.
package ext.ptc;
import java.io.InputStream;
import com.ptc.windchill.enterprise.doc.server.DocumentUtility;
import com.ptc.windchill.enterprise.templates.TemplatesFactory;
import wt.admin.AdminDomainRef;
import wt.content.ApplicationData;
import wt.content.ContentServerHelper;
import wt.doc.DepartmentList;
import wt.doc.DocumentType;
import wt.doc.WTDocument;
import wt.enterprise.EnterpriseHelper;
import wt.enterprise.SequenceGenerator;
import wt.enterprise.TemplatesUtility;
import wt.inf.container.WTContainer;
import wt.inf.container.WTContainerHelper;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.util.WTProperties;
public class TemplateHelper {
/**
* return the WTDocument object basis the template name
* @param templateName
* @param orgName
* @throws Exception
*/
public static WTDocument getDocumentTemplate(String templateName, String orgName) throws Exception
{
QuerySpec qs = new QuerySpec(WTDocument.class);
SearchCondition templateNameSC = new SearchCondition(WTDocument.class,WTDocument.NAME,
SearchCondition.EQUAL,templateName);
qs.appendWhere(templateNameSC, new int[]{0,1});
WTContainer container = WTContainerHelper.service.getByPath("/wt.inf.container.OrgContainer="+orgName).getReferencedContainer();
System.out.println("Cont Name in getDocTemp ==>"+container.getName());
wt.fc.QueryResult qrTemplates = EnterpriseHelper.service.getTemplates(container,
qs, null, true, true, true);
if(qrTemplates.hasMoreElements())
return ((WTDocument)qrTemplates.nextElement());
else
return null;
}
}
2. Later we have to give these inputs pertaining to specific document subtype wherever it is getting created:
In my scenario, I was adhering document template of subtype ‘Commercial proposal document’ for organization ‘PTC’ where ‘Commercial Proposal Document’ Is the name of the Template.
WTDocument docTemplate = TemplateHelper.getDocumentTemplate("Commercial Proposal Template", "PTC");
System.out.println("Document Template Name : - "+docTemplate.getName());
doc=(WTDocument) VersionControlHelper.service.getLatestIteration(doc, true);
System.out.println("childDoc Name : - "+doc.getName());
PersistenceHelper.manager.save(cmpDOC);
wt.content.ContentHelper.service.copyContent((wt.content.ContentHolder)docTemplate, (wt.content.ContentHolder)cmpDOC);
Thanks,
Kaushik
If I Run Java class return:
(wt.pom.pomResource/4) wt.pom.ObjectNotPersistentException:
Object "wt.doc.WTDocument" is not constant.
at wt.method.RemoteMethodServer.invoke(RemoteMethodServer.java:795)
at wt.services.ServiceFactory$ClientInvocationHandler.invoke(ServiceFactory.java:349)
at com.sun.proxy.$Proxy11.copyContentItem(Unknown Source)
at ext.tpolis.Test.main(Test.java:147)
Try from Processor or WorkFlow.
Remotely it'll through error.
And change the template name and organization name as per your requirement.
Thanks,
Kaushik
Create Test WFProcess with my custom method and initialize.
WtDocument Create, bat without any primary content.
Create Java Class ext.tpolis.WTDocumentHelper.java
package ext.tpolis;
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;
/**
* @author Ostapenko Vitaliy
* @version 1.0
*/
public class WTDocumentHelper {
/*
* getDocumentTemplate
* Return Template WTDocument of WTContainerRef
*/
public static WTDocument getDocumentTemplate(String paramDocTemplateName, WTContainerRef paramDocTemplateContainerRef){
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;
}
}
//Get latest revision of template
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;
}
/*
* createDocumentFromTemplate
* Return WTDocument create from Template
*/
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;
}
}
Compile Class.
Put WTDocumentHelper.class in to %WT_HOME%\codebase\ext\tpolis
In Template Workflow create Expression and add java code:
wt.doc.WTDocument document = (wt.doc.WTDocument)primaryBusinessObject;
wt.inf.container.WTContainer container = document.getContainer();
// Set Doc Name
String paramDocName = "TestDoc";
// Set Doc Template Name that is locate in Project
String paramDocTemplateName = "Test Template";
wt.doc.WTDocument docFromTemplate = null;
try{
docFromTemplate= ext.tpolis.WTDocumentHelper.createDocumentFromTemplate(paramDocName, paramDocTemplateName, container);
}catch (Exception e) {
e.printStackTrace();
}
Then in Project run Route Document after finish WfProcess in folder /Default of Project was created TestDoc from Document Template.
Ran across this while trying to do the same thing. Ended up using a lot of what was said here with the addition of using EnterpriseHelper. With this I didn't need ContentHelper at all and it'll maintain the Save-As History accurately (which is what create from template does through the UI, it seems).
here's the code
public class DocumentHelper { /** * not sure what's going on here. got the code from... * https://community.ptc.com/t5/Windchill/How-java-code-to-create-WTDocument-from-template-in-Project/m-p/432582#M50927%3F&art_lang=en&posno=2&q=service.copyContent&source=search * */ public static WTDocument getDocumentTemplate (String paramDocTemplateName, WTContainerRef paramDocTemplateContainerRef) { WTDocument docTemplate = null; ReferenceFactory referenceFactory = new ReferenceFactory(); try { ArrayList templatesList = WTDocumentHelper.service.getTemplates(paramDocTemplateContainerRef); for (int i = 0; i < templatesList.size(); i++) { String[] templateProperties = (String[]) templatesList.get(i); if (templateProperties[1].equals(paramDocTemplateName)) { docTemplate = (WTDocument)referenceFactory.getReference(templateProperties[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 containerRef = WTContainerRef.newWTContainerRef(paramDocContainer); try { WTDocument docTemplate = getDocumentTemplate(paramDocTemplateName, containerRef); doc = (WTDocument)EnterpriseHelper.service.newCopy((RevisionControlled)docTemplate); doc.setName(paramDocName); doc.setContainerReference(containerRef); doc.setDomainRef(paramDocContainer.getDefaultDomainReference()); doc = (WTDocument)EnterpriseHelper.service.saveCopy((RevisionControlled)docTemplate, (RevisionControlled)doc); doc = (WTDocument)TemplatesUtility.changeFilename((WTDocument)doc); } catch (Exception e) { e.printStackTrace(); } return doc; } }
I don't necessarily like using code I don't fully understand... the getDocumentTemplate for example. Before I go to production I might end up changing that function to using the PersistanceHelper.find method of getting it.
Didn't like using some of the logic that I didn't understand, so I redid a bit of this code.
public class DocumentHelper { public static WTDocument getDocumentTemplate (String paramDocTemplateName) throws WTException { WTDocument docTemplate = null; QuerySpec wtDocTemplateSpec = new QuerySpec(WTDocument.class); wtDocTemplateSpec.appendWhere(new SearchCondition(WTDocument.class, WTDocument.NAME, SearchCondition.EQUAL, paramDocTemplateName), new int[] {0}); wtDocTemplateSpec.appendAnd(); wtDocTemplateSpec.appendWhere(new SearchCondition(WTDocument.class, "template.templated", SearchCondition.IS_TRUE), new int[] {0}); wtDocTemplateSpec.appendAnd(); wtDocTemplateSpec.appendWhere(new SearchCondition(WTDocument.class, "template.enabled", SearchCondition.IS_TRUE), new int[] {0}); QueryResult wtDocTemplateSpecResult = PersistenceHelper.manager.find((StatementSpec)wtDocTemplateSpec); if (wtDocTemplateSpecResult.hasMoreElements()) { docTemplate = (WTDocument)(wtDocTemplateSpecResult.nextElement()); docTemplate = (WTDocument)VersionControlHelper.service.allVersionsOf(docTemplate).nextElement(); } if (docTemplate == null) throw new WTException("Not found -> WTDocument Template: " + paramDocTemplateName); return docTemplate; } public static WTDocument createDocumentFromTemplate(String paramDocName, String paramDocTemplateName, WTContainer paramDocContainer) throws WTException, WTPropertyVetoException { WTDocument doc = null; WTContainerRef containerRef = WTContainerRef.newWTContainerRef(paramDocContainer); WTDocument docTemplate = getDocumentTemplate(paramDocTemplateName); doc = (WTDocument)EnterpriseHelper.service.newCopy((RevisionControlled)docTemplate); doc.setName(paramDocName); doc.setContainerReference(containerRef); doc.setDomainRef(paramDocContainer.getDefaultDomainReference()); doc = (WTDocument)EnterpriseHelper.service.saveCopy((RevisionControlled)docTemplate, (RevisionControlled)doc); doc = (WTDocument)TemplatesUtility.setFilename(doc); return doc; } }