package ca.carleton.gcrc.couch.onUpload;
import java.io.File;
import ca.carleton.gcrc.couch.client.CouchDb;
import ca.carleton.gcrc.couch.client.CouchDesignDocument;
import net.sf.json.JSONObject;
public class FileConversionContext {
private JSONObject doc;
private CouchDesignDocument dd;
private String attachmentName;
private File mediaDir;
public FileConversionContext(
JSONObject doc
,CouchDesignDocument dd
,String attachmentName
,File mediaDir
) {
this.doc = doc;
this.dd = dd;
this.attachmentName = attachmentName;
this.mediaDir = mediaDir;
}
public String getId() {
return doc.optString("_id");
}
public JSONObject getDoc() {
return doc;
}
public CouchDesignDocument getDesignDocument() {
return dd;
}
public CouchDb getDatabase() {
return dd.getDatabase();
}
public String getAttachmentName() {
return attachmentName;
}
public File getMediaDir() {
return mediaDir;
}
public String getContentType() {
String mimeType = null;
JSONObject descriptionObj = getAttachmentDescription();
if( null != descriptionObj ) {
mimeType = descriptionObj.getString(UploadConstants.MIME_KEY);
}
return mimeType;
}
public File getFile() {
File file = null;
JSONObject descriptionObj = getAttachmentDescription();
if( null != descriptionObj ) {
String mediaFileName = descriptionObj.optString(UploadConstants.MEDIA_FILE_KEY);
if( null != mediaFileName ) {
file = new File(mediaDir, mediaFileName);
}
}
return file;
}
public File getOriginalFile() {
File file = null;
JSONObject descriptionObj = getAttachmentDescription();
if( null != descriptionObj ) {
JSONObject originalObj = descriptionObj.optJSONObject("original");
if( null != originalObj ) {
String mediaFileName = originalObj.optString(UploadConstants.MEDIA_FILE_KEY);
if( null != mediaFileName ) {
file = new File(mediaDir, mediaFileName);
}
}
}
return file;
}
public String getFileClass() {
String fileClass = null;
JSONObject descriptionObj = getAttachmentDescription();
if( null != descriptionObj ) {
fileClass = descriptionObj.optString(UploadConstants.MIME_CLASS_KEY);
}
return fileClass;
}
public String getStatus() {
String status = null;
JSONObject descriptionObj = getAttachmentDescription();
if( null != descriptionObj ) {
status = descriptionObj.optString(UploadConstants.UPLOAD_STATUS_KEY);
}
return status;
}
public void setStatus(String status) {
JSONObject descriptionObj = getAttachmentDescription();
descriptionObj.put(UploadConstants.UPLOAD_STATUS_KEY, status);
}
public JSONObject getAttachmentDescription() {
JSONObject attachmentDescription = null;
{
JSONObject attachments = doc.optJSONObject("nunaliit_attachments");
if( null != attachments ) {
JSONObject files = attachments.optJSONObject("files");
if( null != files ) {
attachmentDescription = files.optJSONObject(attachmentName);
}
}
}
return attachmentDescription;
}
public JSONObject createAttachmentDescription(String newAttachmentName) {
JSONObject attachmentDescription = null;
{
JSONObject attachments = doc.optJSONObject("nunaliit_attachments");
if( null != attachments ) {
JSONObject files = attachments.optJSONObject("files");
if( null != files ) {
attachmentDescription = new JSONObject();
attachmentDescription.put("attachmentName", newAttachmentName);
files.put(newAttachmentName, attachmentDescription);
}
}
}
return attachmentDescription;
}
public JSONObject getCreatedObject() {
return doc.optJSONObject("nunaliit_created");
}
public JSONObject getLastUpdatedObject() {
return doc.optJSONObject("nunaliit_last_updated");
}
public JSONObject saveDocument() throws Exception {
String docId = getId();
dd.getDatabase().updateDocument(doc);
doc = dd.getDatabase().getDocument(docId);
return doc;
}
public JSONObject uploadFile(String attachmentName, File uploadedFile, String mimeType) throws Exception {
String docId = getId();
dd.getDatabase().uploadAttachment(
doc
,attachmentName
,uploadedFile
,mimeType
);
// Refresh doc
doc = dd.getDatabase().getDocument(docId);
return doc;
}
}