package com.axxerion.connector; import com.axxerion.connector.objectservice.*; import java.io.*; import java.util.*; import java.util.zip.*; import javax.xml.rpc.Stub; import javax.activation.*; /** * Example of using the Axxerion Web service API for downloading documents with status 'published'. * It downloads the actual document in PDF format and associated meta information such as * folders, access permissions and modification date. */ public class DownloadPublished { private ObjectServiceIF objectService = null; private String url = null; private String userName = null; private String password = null; private String directory = null; public static void main(String[] args) { DownloadPublished downloadPublished = new DownloadPublished(); downloadPublished.init(args); } public void synchronize() throws Exception { // Connect to the web service objectService = createObjectService(); objectService.connect(userName, password); // Get a list of documents with status published int status = objectService.getEnumeration("DocumentStatus", "published"); System.out.println("Checking for documents with status " + status); Object[] objects = objectService.findByField("Document", "documentStatusCode", "" + status); System.out.println("Found " + objects.length + " documents"); for (int i = 0; i < objects.length; i++) { Document document = (Document) objects[i]; System.out.println("Document:" + document.getName()); // Get a list of folders where the document is stored Object[] objectFolders = objectService.findRelated("ObjectFolder", "Document", document.getId().intValue()); System.out.println("Found objectfolders:" + objectFolders.length); for (int j = 0; j < objectFolders.length; j++) { ObjectFolder objectFolder = (ObjectFolder) objectFolders[j]; Folder folder = (Folder) objectService.find("Folder", objectFolder.getFolderId()); if (folder == null) { System.out.println("Cannot find Folder:" + objectFolder.getId().intValue()); continue; } System.out.println("Folder:" + folder.getName()); } // Download the security settings for this document Object[] objectAccessList = objectService.findRelated("ObjectAccess", "Document", document.getId().intValue()); for (int j = 0; j < objectAccessList.length; j++) { ObjectAccess objectAccess = (ObjectAccess) objectAccessList[j]; SystemGroup systemGroup = (SystemGroup) objectService.find("SystemGroup", objectAccess.getSystemGroupId()); if (systemGroup == null) { System.out.println("Cannot find system group with Id:" + objectAccess.getId().intValue()); continue; } System.out.println("Access:" + systemGroup.getName() + " - " + objectService.getEnumerationValueName("AccessRight", objectAccess.getAccessRightCode())); } // Check if the document exists in the download directory, if not download the document. String fileName = document.getFileName().substring(0, document.getFileName().lastIndexOf('.')) + ".pdf"; System.out.println("Filename " + fileName); File file = new File(directory + "/" + fileName); if (!file.exists()) { downloadDocument(document, directory, fileName, "pdf"); } // Compare the timestamp of the most recent version of the document on the server, if newer download the document. DocumentVersion version = (DocumentVersion) objectService.find("DocumentVersion", document.getDocumentVersionId()); System.out.println("DocumentVersion " + version); long localTime = file.lastModified(); long documentTime = version.getCreateTime().getTime().getTime(); if (localTime < documentTime) { downloadDocument(document, directory, fileName, "pdf"); } } } /** * Download and save the document */ public void downloadDocument(Document document, String directory, String name, String format) throws Exception { String outFileName = directory + "/" + name; System.out.println("Downloading document " + outFileName); DataHandler attachment = objectService.view(document.getId().intValue(), format, true); ZipInputStream zip = new ZipInputStream(attachment.getInputStream()); BufferedInputStream in = new BufferedInputStream(zip); ZipEntry entry = zip.getNextEntry(); BufferedOutputStream bout = new BufferedOutputStream (new FileOutputStream (outFileName)); byte[] data = new byte[2048]; int bytesRead = 0; while ((bytesRead = in.read(data)) > 0) { bout.write(data, 0, bytesRead); } bout.close(); zip.closeEntry(); zip.close(); File file = new File(outFileName); DocumentVersion version = (DocumentVersion) objectService.find("DocumentVersion", document.getDocumentVersionId()); System.out.println("DocumentVersion " + version); file.setLastModified(version.getCreateTime().getTime().getTime()); System.out.println("Set time to " + version.getCreateTime().getTime().getTime()); } public void init(String[] args) { if (args == null || args.length == 0) { usage(); System.exit(0); } for (int i = 0; i < args.length; i += 2) { if (args[i].equals("-l")) { url = args[i+1]; } else if (args[i].equals("-u")) { userName = args[i+1]; } else if (args[i].equals("-p")) { password = args[i+1]; } else if (args[i].equals("-d")) { directory = args[i+1]; } else { usage(); System.exit(0); } } try { synchronize(); } catch (Exception e) { e.printStackTrace(); } } private void usage() { System.out.println("Downloads documents with status 'published' to local directory."); System.out.println("If the document does not exist or if it is older than the document on the server "); System.out.println("the document is downloaded to the local directory."); System.out.println("Usage: DownloadPublished [options]"); System.out.println("-l URL"); System.out.println("-u user name"); System.out.println("-p password"); System.out.println("-d directory"); } private ObjectServiceIF createObjectService() { try { Stub stub = (Stub) new ObjectService_Impl().getObjectServiceIFPort(); stub._setProperty(javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY, url); stub._setProperty(javax.xml.rpc.Stub.SESSION_MAINTAIN_PROPERTY,Boolean.valueOf("true")); return (ObjectServiceIF) stub; } catch (Exception e) { e.printStackTrace(); } return null; } }