Package ca.carleton.gcrc.couch.onUpload

Source Code of ca.carleton.gcrc.couch.onUpload.UploadListener

package ca.carleton.gcrc.couch.onUpload;

import java.io.File;
import java.security.Principal;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import net.sf.json.JSONObject;
import ca.carleton.gcrc.couch.client.CouchDb;
import ca.carleton.gcrc.upload.LoadedFile;
import ca.carleton.gcrc.upload.OnUploadedListener;

public class UploadListener implements OnUploadedListener {

  final protected Logger logger = Logger.getLogger(this.getClass());
 
  private CouchDb couchDb = null;
 
  public UploadListener() {
   
  }

  public CouchDb getCouchDb() {
    return couchDb;
  }

  public void setCouchDb(CouchDb couchDb) {
    this.couchDb = couchDb;
  }
 
  @Override
  public JSONObject onLoad(
    String progressId
    ,List<LoadedFile> uploadedFiles
    ,Map<String, List<String>> parameters
    ,Principal userPrincipal
    ) throws Exception {
   
    // Check if this is uploading to an existing document
    // or uploading to a new document
    String docId = null;
    String revision = null;
    if( parameters.containsKey("id") ) {
      if( parameters.get("id").size() > 0 ) {
        docId = parameters.get("id").get(0);
      }
    }
    if( parameters.containsKey("rev") ) {
      if( parameters.get("rev").size() > 0 ) {
        revision = parameters.get("rev").get(0);
      }
    }
   
    if( null != docId
     && null != revision
     && uploadedFiles.size() == 1 ) {
      // This is uploading to an existing document
      JSONObject doc = null;
      try {
        doc = couchDb.getDocument(docId);
       
        logger.info("onLoad fetched: "+doc.optString("_id")+" -> "+doc.optString("_rev"));
      } catch(Exception e) {
        logger.error("Unable to load document for id: "+docId,e);
      }

      if( null != doc ) {
        for(LoadedFile uploadedFile : uploadedFiles) {
          File actualFile = uploadedFile.getFile();
          String originalName = uploadedFile.getOriginalFileName();
   
          doc.put(UploadConstants.UPLOAD_KEY, UploadConstants.UPLOAD_VALUE_SUBMITTED);
          JSONObject originalObj = new JSONObject();
          originalObj.put("media", actualFile.getName());
          originalObj.put("name", originalName);
          doc.put("original", originalObj);
         
          try {
            couchDb.updateDocument(doc);
           
            logger.info("onLoad update: "+doc.optString("_id")+" -> "+actualFile);
          } catch(Exception e) {
            logger.error("Unable to save information about file: "+actualFile.getName(),e);
          }
        }
      }
     
    } else {
      for(LoadedFile uploadedFile : uploadedFiles) {
        File actualFile = uploadedFile.getFile();
        String originalName = uploadedFile.getOriginalFileName();
 
        JSONObject doc = new JSONObject();
        doc.put(UploadConstants.UPLOAD_KEY, UploadConstants.UPLOAD_VALUE_SUBMITTED);
        JSONObject originalObj = new JSONObject();
        originalObj.put("media", actualFile.getName());
        originalObj.put("name", originalName);
        doc.put("original", originalObj);
       
        try {
          couchDb.createDocument(doc);
         
          logger.info("onLoad: "+doc.optString("_id")+" -> "+actualFile);
        } catch(Exception e) {
          logger.error("Unable to save information about file: "+actualFile.getName(),e);
        }
      }
    }
   
    return null;
  }

}
TOP

Related Classes of ca.carleton.gcrc.couch.onUpload.UploadListener

TOP
Copyright © 2018 www.massapi.com. 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.