import com.capspire.ecm.cs.ws.CSWSFactory;
import com.opentext.ecm.api.OTAuthentication;
import com.opentext.livelink.service.core.ContentService;
import com.opentext.livelink.service.core.FileAtts;
import com.opentext.livelink.service.docman.DocumentManagement;
import com.opentext.livelink.service.docman.Metadata;
import com.opentext.livelink.service.docman.Node;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class WSExampleAddDocumentAddVersion {
public static void main(String[] args) throws Exception {
try {
File file;
String aLineOfText;
//init props
Properties props = new Properties();
{
file = new File("configuration.properties");
InputStream is = new FileInputStream(file);
props.load(is);
is.close();
}
//init wsdl urls and auth info
String wsdlAuth = props.getProperty("WSDL.CS.Authenctiation");
String wsdlDocMan = props.getProperty("WSDL.CS.DocumentManagement");
String wsdlContentService = props.getProperty("WSDL.CS.ContentService");
String csUsername = props.getProperty("WSDL.CS.Username");
String csPassword = props.getProperty("WSDL.CS.Password");
//init auth & docman
OTAuthentication otauth = CSWSFactory.getOTAuthentication(wsdlAuth, csUsername, csPassword);
DocumentManagement dapi = CSWSFactory.getDocumentManagementClient(wsdlDocMan, otauth);
//get my personal workspace
Node myPersonalWorkspace = dapi.getRootNode("PersonalWS");
for (int i = 0; i < 5; i++) {
int dataidOfNewDocument = addDocument(dapi, myPersonalWorkspace, wsdlContentService, otauth);
System.out.printf("Created document %s%n", dataidOfNewDocument);
int newVersionNumber = addVersionToDocument(dapi, dataidOfNewDocument, wsdlContentService, otauth);
System.out.printf("Created version %s for document %s%n", newVersionNumber, dataidOfNewDocument);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static int addVersionToDocument(
DocumentManagement dapi, int dataid,
String wsdlContentService, OTAuthentication otauth)
throws Exception {
File dummyFile = getADummyFile();
Metadata meta = null;
//get an addversion context id
String contextId = dapi.addVersionContext(dataid, meta);
//create an xmlgregorian date for create & modify date
Date lastModifiedOfDummyFile = new Date(dummyFile.lastModified());
GregorianCalendar c = new GregorianCalendar();
c.setTime(lastModifiedOfDummyFile);
XMLGregorianCalendar xmlGregDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
//create a FileAtts for this file
FileAtts fats = new FileAtts();
fats.setFileName(dummyFile.getName());
fats.setFileSize(dummyFile.length());
fats.setCreatedDate(xmlGregDate);
fats.setModifiedDate(xmlGregDate);
//create a new content service for this context id
ContentService csapi = CSWSFactory.getContentServiceClient(
wsdlContentService, otauth, contextId, fats);
//now create a datahandler to wrap the raw file for api call
DataHandler dh = new DataHandler(new FileDataSource(dummyFile));
//FINALLY.. use the content server to create new doc and version
String versionNumberAsString = csapi.uploadContent(dh);
int vernum = Integer.parseInt(versionNumberAsString);
return vernum;
}
private static int addDocument(
DocumentManagement dapi, Node myPersonalWorkspace,
String wsdlContentService, OTAuthentication otauth)
throws Exception {
File dummyFile = getADummyFile();
//add document using dummy file.
String comment = null; // optional
Metadata meta = null; // this cound have bene populated
String contextId = dapi.createDocumentContext(
myPersonalWorkspace.getID(),
dummyFile.getName(),
comment,
false,
meta);
//create an xmlgregorian date for create & modify date
Date lastModifiedOfDummyFile = new Date(dummyFile.lastModified());
GregorianCalendar c = new GregorianCalendar();
c.setTime(lastModifiedOfDummyFile);
XMLGregorianCalendar xmlGregDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
//create a FileAtts for this file
FileAtts fats = new FileAtts();
fats.setFileName(dummyFile.getName());
fats.setFileSize(dummyFile.length());
fats.setCreatedDate(xmlGregDate);
fats.setModifiedDate(xmlGregDate);
//create a content Service
ContentService csapi = CSWSFactory.getContentServiceClient(
wsdlContentService, otauth, contextId, fats
);
//now create a datahandler to wrap the raw file for api call
DataHandler dh = new DataHandler(new FileDataSource(dummyFile));
//FINALLY.. use the content server to create new doc and version
String dataidAsString = csapi.uploadContent(dh);
int dataid = Integer.parseInt(dataidAsString);
//delete dummy file
dummyFile.delete();
return dataid;
}
private static File getADummyFile() throws Exception {
File file;
StringBuilder aLineOfText;
//come up with a file(name) based on current system type in milliseconds
String name = String.format("%s.txt", System.currentTimeMillis());
file = new File(name);
//write some dummy text to the file
OutputStream os = new FileOutputStream(file);
aLineOfText = new StringBuilder(String.format("My fullpath is %s.%n", file.getAbsolutePath()));
os.write(aLineOfText.toString().getBytes());
//now write 5 mb of data to te file to make it kinda big.
long fiveMB = 1024 * 1024 * 5;
long lineNumber = 2;
while (file.length() < fiveMB) {
aLineOfText = new StringBuilder("x");
while (aLineOfText.length() < 65536) {
aLineOfText.append(aLineOfText.toString());
}
os.write(String.format("[line %s] ", lineNumber).getBytes());
os.write(aLineOfText.toString().getBytes());
lineNumber++;
}
os.close();
return file;
}
}
Comments