Advertisement
ossiejhmoore

Web Service Example - Add Document Add Version

Aug 13th, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.03 KB | None | 0 0
  1. import com.capspire.ecm.cs.ws.CSWSFactory;
  2. import com.opentext.ecm.api.OTAuthentication;
  3. import com.opentext.livelink.service.core.ContentService;
  4. import com.opentext.livelink.service.core.FileAtts;
  5. import com.opentext.livelink.service.docman.DocumentManagement;
  6. import com.opentext.livelink.service.docman.Metadata;
  7. import com.opentext.livelink.service.docman.Node;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.FileOutputStream;
  11. import java.io.InputStream;
  12. import java.io.OutputStream;
  13. import java.util.Date;
  14. import java.util.GregorianCalendar;
  15. import java.util.Properties;
  16. import javax.activation.DataHandler;
  17. import javax.activation.FileDataSource;
  18. import javax.xml.datatype.DatatypeFactory;
  19. import javax.xml.datatype.XMLGregorianCalendar;
  20.  
  21. public class WSExampleAddDocumentAddVersion {
  22.  
  23.     public static void main(String[] args) throws Exception {
  24.         try {
  25.             File file;
  26.             String aLineOfText;
  27.  
  28.             //init props
  29.             Properties props = new Properties();
  30.             {
  31.                 file = new File("configuration.properties");
  32.                 InputStream is = new FileInputStream(file);
  33.                 props.load(is);
  34.                 is.close();
  35.             }
  36.             //init wsdl urls and auth info
  37.             String wsdlAuth = props.getProperty("WSDL.CS.Authenctiation");
  38.             String wsdlDocMan = props.getProperty("WSDL.CS.DocumentManagement");
  39.             String wsdlContentService = props.getProperty("WSDL.CS.ContentService");
  40.             String csUsername = props.getProperty("WSDL.CS.Username");
  41.             String csPassword = props.getProperty("WSDL.CS.Password");
  42.  
  43.             //init auth & docman
  44.             OTAuthentication otauth = CSWSFactory.getOTAuthentication(wsdlAuth, csUsername, csPassword);
  45.             DocumentManagement dapi = CSWSFactory.getDocumentManagementClient(wsdlDocMan, otauth);
  46.  
  47.             //get my personal workspace
  48.             Node myPersonalWorkspace = dapi.getRootNode("PersonalWS");
  49.  
  50.             for (int i = 0; i < 5; i++) {
  51.  
  52.                 int dataidOfNewDocument = addDocument(dapi, myPersonalWorkspace, wsdlContentService, otauth);
  53.                 System.out.printf("Created document %s%n", dataidOfNewDocument);
  54.                 int newVersionNumber = addVersionToDocument(dapi, dataidOfNewDocument, wsdlContentService, otauth);
  55.                 System.out.printf("Created version %s for document %s%n", newVersionNumber, dataidOfNewDocument);
  56.  
  57.             }
  58.  
  59.         } catch (Exception e) {
  60.             throw new RuntimeException(e);
  61.         }
  62.     }
  63.  
  64.     private static int addVersionToDocument(
  65.             DocumentManagement dapi, int dataid,
  66.             String wsdlContentService, OTAuthentication otauth)
  67.             throws Exception {
  68.  
  69.         File dummyFile = getADummyFile();
  70.         Metadata meta = null;
  71.  
  72.         //get an addversion context id
  73.         String contextId = dapi.addVersionContext(dataid, meta);
  74.  
  75.         //create an xmlgregorian date for create & modify date
  76.         Date lastModifiedOfDummyFile = new Date(dummyFile.lastModified());
  77.         GregorianCalendar c = new GregorianCalendar();
  78.         c.setTime(lastModifiedOfDummyFile);
  79.         XMLGregorianCalendar xmlGregDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
  80.  
  81.         //create a FileAtts for this file
  82.         FileAtts fats = new FileAtts();
  83.         fats.setFileName(dummyFile.getName());
  84.         fats.setFileSize(dummyFile.length());
  85.         fats.setCreatedDate(xmlGregDate);
  86.         fats.setModifiedDate(xmlGregDate);
  87.  
  88.         //create a new content service for this context id
  89.         ContentService csapi = CSWSFactory.getContentServiceClient(
  90.                 wsdlContentService, otauth, contextId, fats);
  91.  
  92.         //now create a datahandler to wrap the raw file for api call
  93.         DataHandler dh = new DataHandler(new FileDataSource(dummyFile));
  94.  
  95.         //FINALLY.. use the content server to create new doc and version
  96.         String versionNumberAsString = csapi.uploadContent(dh);
  97.         int vernum = Integer.parseInt(versionNumberAsString);
  98.         return vernum;
  99.  
  100.     }
  101.  
  102.     private static int addDocument(
  103.             DocumentManagement dapi, Node myPersonalWorkspace,
  104.             String wsdlContentService, OTAuthentication otauth)
  105.             throws Exception {
  106.  
  107.         File dummyFile = getADummyFile();
  108.  
  109.         //add document using dummy file.
  110.         String comment = null; // optional
  111.         Metadata meta = null;  // this cound have bene populated
  112.         String contextId = dapi.createDocumentContext(
  113.                 myPersonalWorkspace.getID(),
  114.                 dummyFile.getName(),
  115.                 comment,
  116.                 false,
  117.                 meta);
  118.  
  119.         //create an xmlgregorian date for create & modify date
  120.         Date lastModifiedOfDummyFile = new Date(dummyFile.lastModified());
  121.         GregorianCalendar c = new GregorianCalendar();
  122.         c.setTime(lastModifiedOfDummyFile);
  123.         XMLGregorianCalendar xmlGregDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
  124.  
  125.         //create a FileAtts for this file
  126.         FileAtts fats = new FileAtts();
  127.         fats.setFileName(dummyFile.getName());
  128.         fats.setFileSize(dummyFile.length());
  129.         fats.setCreatedDate(xmlGregDate);
  130.         fats.setModifiedDate(xmlGregDate);
  131.  
  132.         //create a content Service
  133.         ContentService csapi = CSWSFactory.getContentServiceClient(
  134.                 wsdlContentService, otauth, contextId, fats
  135.         );
  136.         //now create a datahandler to wrap the raw file for api call
  137.         DataHandler dh = new DataHandler(new FileDataSource(dummyFile));
  138.  
  139.         //FINALLY.. use the content server to create new doc and version
  140.         String dataidAsString = csapi.uploadContent(dh);
  141.         int dataid = Integer.parseInt(dataidAsString);
  142.  
  143.         //delete dummy file
  144.         dummyFile.delete();
  145.  
  146.         return dataid;
  147.     }
  148.  
  149.     private static File getADummyFile() throws Exception {
  150.         File file;
  151.         StringBuilder aLineOfText;
  152.         //come up with a file(name) based on current system type in milliseconds
  153.         String name = String.format("%s.txt", System.currentTimeMillis());
  154.         file = new File(name);
  155.         //write some dummy text to the file
  156.         OutputStream os = new FileOutputStream(file);
  157.         aLineOfText = new StringBuilder(String.format("My fullpath is %s.%n", file.getAbsolutePath()));
  158.         os.write(aLineOfText.toString().getBytes());
  159.         //now write 5 mb of data to te file to make it kinda big.
  160.         long fiveMB = 1024 * 1024 * 5;
  161.         long lineNumber = 2;
  162.         while (file.length() < fiveMB) {
  163.             aLineOfText = new StringBuilder("x");
  164.             while (aLineOfText.length() < 65536) {
  165.                 aLineOfText.append(aLineOfText.toString());
  166.             }
  167.             os.write(String.format("[line %s] ", lineNumber).getBytes());
  168.             os.write(aLineOfText.toString().getBytes());
  169.             lineNumber++;
  170.         }
  171.         os.close();
  172.         return file;
  173.     }
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement