6.4 Retrieve Document Sample

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

Example 6-1 Sample Document Retrieval

package devguidesamples;
 
import java.io.FileOutputStream;
 
import java.io.IOException;
import java.io.InputStream;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
 
import javax.activation.DataHandler;
 
import oracle.imaging.BasicUserToken;
import oracle.imaging.DocumentContentService;
import oracle.imaging.DocumentService;
import oracle.imaging.ImagingException;
import oracle.imaging.NameId;
import oracle.imaging.RenderOptions;
import oracle.imaging.RenderPage;
import oracle.imaging.RenderResult;
import oracle.imaging.Rendition;
import oracle.imaging.Search;
import oracle.imaging.SearchArgument;
import oracle.imaging.SearchService;
import oracle.imaging.SearchValue;
import oracle.imaging.ServicesFactory;
import oracle.imaging.UserToken;
 
public class RetrieveDocumentSample {
   public static void main(String[] args)
      throws IOException {
      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
            SearchService searchService = servicesFactory.getSearchService();
            DocumentContentService docContentService = 
                             servicesFactory.getDocumentContentService();
 
            // The find the document with invoice number 1234 using the Invoices search
            List<SearchArgument> searchArguments = new ArrayList<SearchArgument>();
            SearchValue searchValue = new SearchValue(SearchValue.Type.NUMBER, 1234);
            SearchArgument searchArgument = new SearchArgument("Invoice Number", searchValue);
            searchArgument.setOperatorValue(Search.Operator.EQUAL);
            searchArguments.add(searchArgument);
            Search.ResultSet resultSet =
               searchService.executeSavedSearch(new NameId("Invoices"), searchArguments);
            if (resultSet.getTotalResults() == 0) {
               System.out.println("Document not found");
            }
            String documentId = resultSet.getResults().get(0).getDocumentId();
            String documentName = resultSet.getResults().get(0).getDocument().getName();
 
            DataHandler fileData = null;
            FileOutputStream outputStream = null;
            
            // retrieve original native document content.
            fileData = docContentService.retrieve(documentId);
            outputStream = new FileOutputStream(documentName);
            fileData.writeTo(outputStream);
            outputStream.close();
            
            // Retrieve a document rendition with annotations
            Rendition rendition = docContentService.retrieveRendition(documentId, 
                                                     true, 
                                                     true, 
                                                     RenderOptions.RenditionOutput.ORIGINALORTIFF, 
                                                     null);
            
            fileData = rendition.getContent();
            outputStream = new FileOutputStream(documentName);
            fileData.writeTo(outputStream);
            outputStream.close();  
            
            //Render a specific page to JPEG format.
            RenderOptions renderOptions = new RenderOptions();
            renderOptions.setPageNumber(2);
            renderOptions.setFormat(RenderOptions.OutputFormat.JPEG);
            RenderResult result = docContentService.retrievePage(documentId, renderOptions); 
            RenderPage page = result.getPages()[0];
            
            fileData = page.getPageData();
            outputStream = new FileOutputStream(documentName);
            fileData.writeTo(outputStream);
            outputStream.close();  
         }
         finally {
            if (servicesFactory != null) {
               servicesFactory.logout();
            }
         }
      }
      catch (ImagingException e) {
         System.out.println(e.getMessage());
      }
   }
}