Package ca.carleton.gcrc.couch.onUpload.conversion

Examples of ca.carleton.gcrc.couch.onUpload.conversion.FileConversionContext


  }

  private void performSubmittedWork(Work work) throws Exception {
    String attachmentName = work.getAttachmentName();
   
    FileConversionContext conversionContext =
      new FileConversionContextImpl(work,documentDbDesign,mediaDir);
   
    DocumentDescriptor docDescriptor = conversionContext.getDocument();
   
    AttachmentDescriptor attDescription = null;
    if( docDescriptor.isAttachmentDescriptionAvailable(attachmentName) ){
      attDescription = docDescriptor.getAttachmentDescription(attachmentName);
    }
   
    if( null == attDescription ) {
      logger.info("Submission can not be found");

    } else if( false == UploadConstants.UPLOAD_STATUS_SUBMITTED.equals( attDescription.getStatus() ) ) {
      logger.info("File not in submit state");
     
    } else {
      OriginalFileDescriptor originalObj = attDescription.getOriginalFileDescription();
     
      if( null == originalObj) {
        logger.error("Submission does not contain original");
        throw new Exception("Submission does not contain original");
     

      File file = originalObj.getMediaFile();
      if( null == file ){
        logger.error("No media file reported");
        throw new Exception("No media file reported");
      }
      if( false == file.exists() || false == file.isFile() ){
        logger.error(""+file.getAbsolutePath()+" is not a file.");
        throw new Exception(""+file.getAbsolutePath()+" is not a file.");
      };

      // Set file size
      long fileSize = file.length();
      originalObj.setSize(fileSize);

      // Mime type, encoding type and file class
      boolean pluginFound = false;
      String mimeType = null;
      String mimeEncoding = null;
      String fileClass = null;
      for(FileConversionPlugin fcp : this.fileConverters) {
        FileConversionMetaData md = fcp.getFileMetaData(file);
        if( md.isFileConvertable() ) {
          mimeType = md.getMimeType();
          fileClass = md.getFileClass();
          mimeEncoding = md.getMimeEncoding();
 
          pluginFound = true;
          break;
        }
      }
      if( false == pluginFound ) {
        logger.info("No plugin found for uploaded file");
       
        SystemFile sf = SystemFile.getSystemFile(file);
        mimeType = sf.getMimeType();
        mimeEncoding = sf.getMimeEncoding();
      }
      if( null != mimeType ) {
        originalObj.setContentType(mimeType);
      }
      if( null != mimeEncoding ) {
        originalObj.setEncodingType(mimeEncoding);
      }
      if( null != fileClass ){
        attDescription.setFileClass(fileClass);
      }

      // Update status
      attDescription.setStatus(UploadConstants.UPLOAD_STATUS_ANALYZED);
      conversionContext.saveDocument();
    }
  }
View Full Code Here


   * @throws Exception
   */
  private void performSubmittedInlineWork(Work work) throws Exception {
    String attachmentName = work.getAttachmentName();
   
    FileConversionContext conversionContext =
      new FileConversionContextImpl(work,documentDbDesign,mediaDir);

    DocumentDescriptor docDescriptor = conversionContext.getDocument();

    AttachmentDescriptor attDescription = null;
    if( docDescriptor.isAttachmentDescriptionAvailable(attachmentName) ){
      attDescription = docDescriptor.getAttachmentDescription(attachmentName);
    }
   
    if( null == attDescription ) {
      logger.info("Submission can not be found");

    } else if( false == UploadConstants.UPLOAD_STATUS_SUBMITTED_INLINE.equals( attDescription.getStatus() ) ) {
      logger.info("File not in submit inline state");
     
    } else {

      // Verify that a file is attached to the document
      if( false == attDescription.isFilePresent() ) {
        // Invalid state
        throw new Exception("Invalid state. A file must be present for submitted_inline");
      }
     
      // Download file
      File outputFile = File.createTempFile("inline", "", mediaDir);
      conversionContext.downloadFile(attachmentName, outputFile);
     
      // Create an original structure to point to the file in the
      // media dir. This way, when we go to "submitted" state, we will
      // be ready.
      OriginalFileDescriptor originalDescription = attDescription.getOriginalFileDescription();
      originalDescription.setMediaFileName(outputFile.getName());

      // Delete current attachment
      attDescription.removeFile();

      // Update status
      attDescription.setStatus(UploadConstants.UPLOAD_STATUS_SUBMITTED);
      conversionContext.saveDocument();
    }
  }
View Full Code Here

  }

  private void performAnalyzedWork(Work work) throws Exception {
    String attachmentName = work.getAttachmentName();

    FileConversionContext conversionContext =
      new FileConversionContextImpl(work,documentDbDesign,mediaDir);

    DocumentDescriptor docDescriptor = conversionContext.getDocument();

    AttachmentDescriptor attDescription = null;
    if( docDescriptor.isAttachmentDescriptionAvailable(attachmentName) ){
      attDescription = docDescriptor.getAttachmentDescription(attachmentName);
    }
   
    if( null == attDescription ) {
      logger.info("Analysis object not found");

    } else if( false == UploadConstants.UPLOAD_STATUS_ANALYZED.equals( attDescription.getStatus() ) ) {
      logger.info("File not in analyzed state");
     
    } else {
      if( false == attDescription.isOriginalFileDescriptionAvailable() ) {
        logger.error("Analysis required but original object is not present");
        throw new Exception("Analysis required but original object is not present");
      }
      OriginalFileDescriptor originalObj = attDescription.getOriginalFileDescription();
     
      // Verify if a submitter is specified
      CouchAuthenticationContext submitter = attDescription.getSubmitter();

      boolean pluginFound = false;
      String fileClass = attDescription.getFileClass();
      for(FileConversionPlugin fcp : this.fileConverters) {
        if( fcp.handlesFileClass(fileClass, FileConversionPlugin.WORK_ANALYZE) ) {
          fcp.performWork(FileConversionPlugin.WORK_ANALYZE, attDescription);
          pluginFound = true;
          break;
        }
      }
      if( false == pluginFound ) {
        logger.info("No plugin found to analyze file class: "+fileClass);
       
        // By default, original file is used
        attDescription.setMediaFileName(originalObj.getMediaFileName());
        attDescription.setContentType(originalObj.getContentType());
        attDescription.setEncodingType(originalObj.getEncodingType());
        attDescription.setSize(originalObj.getSize());
      }
     
      // Update document
      boolean shouldSendNotification = false;
      if( CouchNunaliitUtils.hasVetterRole(submitter, settings.getAtlasName()) ) {
        // If submitter has vetter role, then no need to wait for approval
        attDescription.setStatus(UploadConstants.UPLOAD_STATUS_APPROVED);
      } else {
        attDescription.setStatus(UploadConstants.UPLOAD_STATUS_WAITING_FOR_APPROVAL);
        shouldSendNotification = true;
      }
      conversionContext.saveDocument();
     
      // Notify that upload is available
      if( shouldSendNotification ) {
        sendVettingNotification(work.getDocId(), work.getDocument(), attachmentName);
      }
View Full Code Here

  }

  private void performApprovedWork(Work work) throws Exception {
    String attachmentName = work.getAttachmentName();
   
    FileConversionContext conversionContext =
      new FileConversionContextImpl(work,documentDbDesign,mediaDir);

    DocumentDescriptor docDescriptor = conversionContext.getDocument();

    AttachmentDescriptor attDescription = null;
    if( docDescriptor.isAttachmentDescriptionAvailable(attachmentName) ){
      attDescription = docDescriptor.getAttachmentDescription(attachmentName);
    }

    if( null == attDescription ) {
      logger.info("Approved object not found");

    } else if( false == UploadConstants.UPLOAD_STATUS_APPROVED.equals( attDescription.getStatus() ) ) {
      logger.info("File not in approved state");
     
    } else {
      boolean pluginFound = false;
      String fileClass = attDescription.getFileClass();
      for(FileConversionPlugin fcp : this.fileConverters) {
        if( fcp.handlesFileClass(fileClass, FileConversionPlugin.WORK_APPROVE) ) {
          fcp.performWork(FileConversionPlugin.WORK_APPROVE, attDescription);
          pluginFound = true;
          break;
        }
      }
      if( false == pluginFound ) {
        logger.info("No plugin found for uploaded file class: "+fileClass);
       
        String mimeType = attDescription.getContentType();
        if( null == mimeType ) {
          mimeType = "application/octet-stream";
        }
       
        // By default, upload original file
        conversionContext.uploadFile(
          attDescription.getAttachmentName()
          ,attDescription.getMediaFile()
          ,mimeType
          );
      }

      // Update status
      attDescription.setStatus(UploadConstants.UPLOAD_STATUS_ATTACHED);
      conversionContext.saveDocument();
    }
  }
View Full Code Here

  }
 
  private void performOrientationWork(Work work) throws Exception {
    String attachmentName = work.getAttachmentName();
   
    FileConversionContext conversionContext =
      new FileConversionContextImpl(work,documentDbDesign,mediaDir);

    DocumentDescriptor docDescriptor = conversionContext.getDocument();

    AttachmentDescriptor attDescription = null;
    if( docDescriptor.isAttachmentDescriptionAvailable(attachmentName) ){
      attDescription = docDescriptor.getAttachmentDescription(attachmentName);
    }

    if( null == attDescription ) {
      logger.info("Approved object not found");

    } else if( false == UploadConstants.UPLOAD_STATUS_ATTACHED.equals( attDescription.getStatus() ) ) {
      logger.info("File not in attached state");

    } else {
      ServerWorkDescriptor serverDescription = attDescription.getServerWorkDescription();
      int orientationLevel = serverDescription.getOrientationLevel();
     
      if( orientationLevel >= UploadConstants.SERVER_ORIENTATION_VALUE ) {
        logger.info("Orientation work already done");
      } else {
        boolean pluginFound = false;
        String fileClass = attDescription.getFileClass();
        for(FileConversionPlugin fcp : this.fileConverters) {
          if( fcp.handlesFileClass(fileClass, FileConversionPlugin.WORK_ORIENT) ) {
            fcp.performWork(FileConversionPlugin.WORK_ORIENT, attDescription);
            pluginFound = true;
            break;
          }
        }
        if( false == pluginFound ) {
          logger.info("No plugin found for uploaded file class: "+fileClass);
        }
 
        // Update status
        conversionContext.saveDocument();
      }
    }
  }
View Full Code Here

  }
 
  private void performThumbnailWork(Work work) throws Exception {
    String attachmentName = work.getAttachmentName();
   
    FileConversionContext conversionContext =
      new FileConversionContextImpl(work,documentDbDesign,mediaDir);

    DocumentDescriptor docDescriptor = conversionContext.getDocument();

    AttachmentDescriptor attDescription = null;
    if( docDescriptor.isAttachmentDescriptionAvailable(attachmentName) ){
      attDescription = docDescriptor.getAttachmentDescription(attachmentName);
    }

    if( null == attDescription ) {
      logger.info("Approved object not found");

    } else if( false == UploadConstants.UPLOAD_STATUS_ATTACHED.equals( attDescription.getStatus() ) ) {
      logger.info("File not in attached state");

    } else {
      ServerWorkDescriptor serverDescription = attDescription.getServerWorkDescription();
      String thumbnailReference = attDescription.getThumbnailReference();
      int thumbnailLevel = serverDescription.getThumbnailLevel();
     
      if( thumbnailLevel >= UploadConstants.SERVER_THUMBNAIL_VALUE ) {
        logger.info("Orientation work already done");
 
      } else if( null != thumbnailReference ) {
        // This is an instance where the thumbnail was already
        // created but the server work was not noted. This happens
        // with legacy documents.
        // In this case, update server work and save document.
        serverDescription.setThumbnailLevel(UploadConstants.SERVER_THUMBNAIL_VALUE);
       
        // Update status
        conversionContext.saveDocument();
       
        logger.info("Updated server thumbnail status");

      } else {

        boolean pluginFound = false;
        String fileClass = attDescription.getFileClass();
        for(FileConversionPlugin fcp : this.fileConverters) {
          if( fcp.handlesFileClass(fileClass, FileConversionPlugin.WORK_THUMBNAIL) ) {
            fcp.performWork(FileConversionPlugin.WORK_THUMBNAIL, attDescription);
            pluginFound = true;
           
            logger.info("Created thumbnail");
            break;
          }
        }
        if( false == pluginFound ) {
          logger.info("No plugin found for thumbnail creation, file class: "+fileClass);
        }

        // Update status
        conversionContext.saveDocument();
      }
    }
  }
View Full Code Here

  }
 
  private void performUploadOriginalImageWork(Work work) throws Exception {
    String attachmentName = work.getAttachmentName();
   
    FileConversionContext conversionContext =
      new FileConversionContextImpl(work,documentDbDesign,mediaDir);

    DocumentDescriptor docDescriptor = conversionContext.getDocument();

    AttachmentDescriptor attDescription = null;
    if( docDescriptor.isAttachmentDescriptionAvailable(attachmentName) ){
      attDescription = docDescriptor.getAttachmentDescription(attachmentName);
    }

    if( null == attDescription ) {
      throw new Exception("Approved object not found");

    } else if( false == UploadConstants.UPLOAD_STATUS_ATTACHED.equals( attDescription.getStatus() ) ) {
      throw new Exception("File not in attached state");

    } else {
      WorkDescriptor workDescription = attDescription.getWorkDescription();

      boolean pluginFound = false;
      String fileClass = attDescription.getFileClass();
      for(FileConversionPlugin fcp : this.fileConverters) {
        if( fcp.handlesFileClass(fileClass, FileConversionPlugin.WORK_UPLOAD_ORIGINAL) ) {
          fcp.performWork(FileConversionPlugin.WORK_UPLOAD_ORIGINAL, attDescription);
          pluginFound = true;
         
          logger.info("Original file uploaded");
          break;
        }
      }
      if( false == pluginFound ) {
        workDescription.setStringAttribute(UploadConstants.UPLOAD_WORK_UPLOAD_ORIGINAL_IMAGE, "No plugin found for thumbnail creation, file class: "+fileClass);
      }

      // Update status
      conversionContext.saveDocument();
    }
  }
View Full Code Here

  }
 
  private void performRotateWork(String workType, Work work) throws Exception {
    String attachmentName = work.getAttachmentName();
   
    FileConversionContext conversionContext =
      new FileConversionContextImpl(work,documentDbDesign,mediaDir);

    DocumentDescriptor docDescriptor = conversionContext.getDocument();

    AttachmentDescriptor attDescription = null;
    if( docDescriptor.isAttachmentDescriptionAvailable(attachmentName) ){
      attDescription = docDescriptor.getAttachmentDescription(attachmentName);
    }

    if( null == attDescription ) {
      throw new Exception("Approved object not found");

    } else if( false == UploadConstants.UPLOAD_STATUS_ATTACHED.equals( attDescription.getStatus() ) ) {
      throw new Exception("File not in attached state");

    } else {
      WorkDescriptor workDescription = attDescription.getWorkDescription();

      boolean pluginFound = false;
      String fileClass = attDescription.getFileClass();
      for(FileConversionPlugin fcp : this.fileConverters) {
        if( fcp.handlesFileClass(fileClass, workType) ) {
          fcp.performWork(workType, attDescription);
          pluginFound = true;
         
          logger.info("Rotation work complete: "+workType);
          break;
        }
      }
      if( false == pluginFound ) {
        workDescription.setStringAttribute(UploadConstants.UPLOAD_WORK_UPLOAD_ORIGINAL_IMAGE, "No plugin found for thumbnail creation, file class: "+fileClass);
      }

      // Update status
      conversionContext.saveDocument();
    }
  }
View Full Code Here

  }

  public void approveFile(AttachmentDescriptor attDescription) throws Exception {

    DocumentDescriptor docDescriptor = attDescription.getDocumentDescriptor();
    FileConversionContext approvedContext = docDescriptor.getContext();
   
    String fullFileName = null;
    FileInputStream fis = null;
    try {
      File file = attDescription.getMediaFile();
      fullFileName = file.getAbsolutePath();
     
      fis = new FileInputStream(file);
      InputStreamReader reader = new InputStreamReader(fis,"UTF-8");
     
      GeoJsonParser parser = new GeoJsonParser();
      List<GeoJsonFeature> features = parser.parse(reader);

      fis.close();
      fis = null;
     
      logger.debug("Number of uploaded features: "+features.size());

      int count = 0;
      try {
        for(GeoJsonFeature feature : features){
          ++count;
          logger.debug("Creating geojson feature: "+count);

          uploadFeature(feature, approvedContext);
        }
      } catch(Exception e) {
        throw new Exception("Error while uploading GeoJSON features",e);
      }

      logger.debug("Done creating geojson features");
     
      JSONObject doc = approvedContext.getDoc();

      logger.debug("Obtained document");
     
      // Create layer definition
      {
        JSONObject layerDef = new JSONObject();
       
        layerDef.put("nunaliit_type", "layerDefinition");
        layerDef.put("id", docDescriptor.getDocId());

        String originalFileName = attDescription.getOriginalName();
       
        if( null != originalFileName ) {
          layerDef.put("name", "GeoJSON - "+originalFileName);

        } else {
          layerDef.put("name", "GeoJSON");
        }
       

        logger.debug("Computing BBOX");

        // BBOX
        {
          boolean include = false;
          BoundingBox boundingBox = new BoundingBox();
          for(GeoJsonFeature feature : features){
            Geometry geometry = feature.getGeometry();
            if( null != geometry ){
              geometry.extendBoundingBox(boundingBox);
              include = true;
            }
          }
          if( include ){
            JSONArray bbox = new JSONArray();
            bbox.put( boundingBox.getMinX() );
            bbox.put( boundingBox.getMinY() );
            bbox.put( boundingBox.getMaxX() );
            bbox.put( boundingBox.getMaxY() );

            layerDef.put("bbox", bbox);
          }
        }
       
        doc.put("nunaliit_layer_definition", layerDef);
      }
     
      logger.debug("Updating layer definition document: "+doc.getString("_id"));
     
      // Save changes to document
      approvedContext.saveDocument();
     
      logger.debug("Uploading file: "+attDescription.getAttachmentName());
     
      // Upload original file
      approvedContext.uploadFile(
        attDescription.getAttachmentName()
        ,attDescription.getMediaFile()
        ,"application/xml"
        );
     
View Full Code Here

    }
  }

  public void approveFile(AttachmentDescriptor attDescription) throws Exception {
    // Upload file
    FileConversionContext conversionContext = attDescription.getContext();
    String attachmentName = attDescription.getAttachmentName();
    File file = attDescription.getMediaFile();
    String mimeType = attDescription.getContentType();
    conversionContext.uploadFile(attachmentName, file, mimeType);
  }
View Full Code Here

TOP

Related Classes of ca.carleton.gcrc.couch.onUpload.conversion.FileConversionContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.