4.5 Create Document Sample

The following sample demonstrates the basic concepts discussed in this section.

Example 4-1 text

package devguidesamples;

import java.math.BigDecimal;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import oracle.imaging.Application;
import oracle.imaging.ApplicationService;
import oracle.imaging.BasicUserToken;
import oracle.imaging.Document;
import oracle.imaging.DocumentContentService;
import oracle.imaging.DocumentService;
import oracle.imaging.ImagingException;
import oracle.imaging.NameId;
import oracle.imaging.ServicesFactory;
import oracle.imaging.UserToken;

public class CreateDocumentSample {
   public static void main(String[] args) {

      try { // try-catch
         UserToken credentials = new BasicUserToken("ipmuser", "ipmuserpwd");
         ServicesFactory servicesFactory =          
            ServicesFactory.login(credentials, Locale.US,
              "http://ipmhost:16000/imaging/ws");
         try { // try-finally to ensure logout
            DocumentService docService = servicesFactory.getDocumentService();
            DocumentContentService docContentService =                
               servicesFactory.getDocumentContentService();
            NameId invoicesAppNameId = null;

            // List the viewable applications to confirm that "Invoices" exists
            List<NameId> appsList =
             docService.listTargetApplications(Document.Ability.CREATEDOCUMENT);
            for (NameId nameId: appsList) {
               if (nameId.getName().equals("Invoices")) {
                  invoicesAppNameId = nameId;
               }
            }
            if (invoicesAppNameId == null) {
               System.out.println("Invoices application not found.");
               return;
            }
            
            // Upload document content
            String fileName = "C:/PathToImages/invoice1234.tif";
            DataHandler fileData = new DataHandler(new FileDataSource(fileName));
            String uploadToken = docContentService.uploadDocument(fileData, 
             "invoice1234.tif");

            // Index the document
            List<Document.FieldValue> fieldValues = new
               ArrayList<Document.FieldValue>();
            fieldValues.add(new Document.FieldValue("Invoice Number", 1234));
            fieldValues.add(new Document.FieldValue("Purchase Order", 4321));
            fieldValues.add(new Document.FieldValue("Vendor", "Acme Supply"));
            fieldValues.add(new Document.FieldValue("Receive Date", new
              BigDecimal("99.95")));
            fieldValues.add(new Document.FieldValue("Amount", new Date())); //now
            docService.createDocument(uploadToken, invoicesAppNameId,
               fieldValues);
         }
         finally {
            if (servicesFactory != null) {
               servicesFactory.logout();
            }
         }
      }
      catch (ImagingException e) {
         System.out.println(e.getMessage());
      }
   }
}