cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

Java code to Download Content of WTDocument

vuchekar
9-Granite

Java code to Download Content of WTDocument

Hi All,

I am trying to download the content of WTDocument.

In Program i am getting

WARNING: The ManagerService is not initialized! This can be caused by:

1) Attempting to invoke a server only method from a remote client

2) Attempting to invoke a method on a service from the constructor or static initializer of another service

Exception in thread "main" java.lang.ExceptionInInitializerError

at ext.test.ExportWTDocumentNew.extractContent(ExportWTDocumentNew.java:90)

at ext.test.ExportWTDocumentNew.main(ExportWTDocumentNew.java:52)

Caused by: java.lang.NullPointerException

at wt.content.ContentServerHelper.<clinit>(ContentServerHelper.java:62)

... 2 more

.

My Program is as Below, Please help me out.

import wt.doc.WTDocument;

import wt.fc.PersistenceHelper;

import wt.fc.QueryResult;

import wt.httpgw.GatewayAuthenticator;

import wt.method.RemoteMethodServer;

import wt.query.QueryException;

import wt.query.QuerySpec;

import wt.util.WTException;

public class ExportWTDocumentNew {

/**

* @param args

* @throws WTException

*/

public static void main(String[] args) throws WTException {

// TODO Auto-generated method stub

System.out.println("Program to Extarct the Content of WTDocument");

System.out.println("Provide the Output folder path");

String output = "C:\\abc\\Log\\test";

System.out.println("Given Output Directory is :: "+output);

RemoteMethodServer rms = RemoteMethodServer.getDefault();

GatewayAuthenticator auth = new GatewayAuthenticator();

auth.setRemoteUser("wcadmin");

rms.setAuthenticator(auth);

System.out.println("Auth is done..");

WTDocument wtdoc= null;

QuerySpec qs = new QuerySpec(WTDocument.class);

QueryResult qr = PersistenceHelper.manager.find(qs);

while(qr.hasMoreElements()){

System.out.println("Inside the While loop");

wtdoc = (WTDocument)qr.nextElement();

System.out.println(wtdoc.getName());

System.out.println(wtdoc.getNumber());

}

extractContent(wtdoc, output);

}

private static void extractContent( WTDocument wtDoc, String outputDir )

{

try

{

String errorMessage = "";

wt.content.ContentHolder syHold = null;

syHold = (wt.doc.WTDocument)wt.content.ContentHelper.service.getContents(((wt.content.ContentHolder)wtDoc));

System.out.println("Value of syHold:: "+syHold);

java.util.Vector vectorOfAppData = wt.content.ContentHelper.getContentListAll( syHold );

if( vectorOfAppData != null)

{

java.util.Iterator appIter = vectorOfAppData.iterator();

while (appIter.hasNext())

{

wt.content.ApplicationData syAppData = (wt.content.ApplicationData)(appIter.next());

String docName = syAppData.getFileName();

long fileSize = syAppData.getFileSize();

String syContentFileName = outputDir + ((outputDir.endsWith("\\")) ? docName : "\\" + docName);

// Save the content file to disk on the Windchill server

wt.content.ContentServerHelper.service.writeContentStream(syAppData, syContentFileName);

}

}

else

{

errorMessage = "--> This primary business object has no primary content file. No file extracted.";

System.out.println("errorMessage");

}

}

catch (Exception e)

{

System.out.println("--> WTException");

e.printStackTrace();

}

}

}

Thanks,

Vivek

2 REPLIES 2

If you are within windchill 10.1 or higher the simplest way would be to put your code into a jsp and execute it - In pdmlink 10 the servletengine is in the same context as the methodserver

Another option is to invoke your method on the methodserver:

import java.lang.reflect.InvocationTargetException;

import java.rmi.RemoteException;

import org.apache.log4j.Logger;

import wt.method.RemoteAccess;

import wt.method.RemoteMethodServer;

import wt.util.WTException;

public class InvokeTest implements RemoteAccess {

public static Logger logger = Logger.getLogger(InvokeTest.class.getName());

public static void myMethod() throws WTException,RemoteException{

// your code goes here

}

public static void main(String[] args) throws WTException, RemoteException, InvocationTargetException {

RemoteMethodServer.getDefault().invoke("myMethod", "ext.tools.InvokeTest", null,new Class[]{}, new Object[]{} );

}

}

Top Tags