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() > 0 ) {
      // 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();
         
          JSONObject nunaliitAttachments = doc.optJSONObject(UploadConstants.ATTACHMENTS_KEY);
          if( null == nunaliitAttachments ) {
            nunaliitAttachments = new JSONObject();
            nunaliitAttachments.put("nunaliit_type", "attachment_descriptions");
            nunaliitAttachments.put("files", new JSONObject());
            doc.put(UploadConstants.ATTACHMENTS_KEY, nunaliitAttachments);
          }
          JSONObject fileDic = nunaliitAttachments.optJSONObject("files");
          if( null == fileDic ) {
            fileDic = new JSONObject();
            nunaliitAttachments.put("files",fileDic);
          }

          // Compute name for attachment
          String attachmentName = null;
          {
            File tempFile = new File(originalName);
            attachmentName = tempFile.getName();
            if( fileDic.containsKey(attachmentName) ) {
              // Select a different file name
              String prefix = "";
              String suffix = "";
              int pos = attachmentName.indexOf('.', 1);
              if( pos < 0 ) {
                prefix = attachmentName;
              } else {
                prefix = attachmentName.substring(0, pos-1);
                suffix = attachmentName.substring(pos);
              }
              int counter = 0;
              while( fileDic.containsKey(attachmentName) ) {
                attachmentName = prefix + counter + suffix;
                ++counter;
              }
            }
          }
         
          JSONObject fileDescription = new JSONObject();
          fileDic.put(attachmentName, fileDescription);
         
          fileDescription.put("attachmentName", attachmentName);
          fileDescription.put(UploadConstants.UPLOAD_STATUS_KEY, UploadConstants.UPLOAD_STATUS_SUBMITTED);
          fileDescription.put("originalName", originalName);
         
          JSONObject originalJson = new JSONObject();
          originalJson.put("mediaFile", actualFile.getName());
          fileDescription.put("original", originalJson);

          // Add user data
          JSONObject userData = new JSONObject();
          fileDescription.put(UploadConstants.DATA_KEY, userData);
          for(String key : parameters.keySet()) {
            if( "id".equals(key)
             || "rev".equals(key)
             ) {
              // Drop already processed parameters
            } else {
              List<String> values = parameters.get(key);
              if( values.size() > 0 ) {
                userData.put(key, values.get(0));
              }
            }
          }
         
          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);
          }
        }
      }
    }
   
    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.