package ca.carleton.gcrc.couch.onUpload.conversion;
import java.io.File;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import ca.carleton.gcrc.couch.client.CouchUserContext;
import ca.carleton.gcrc.couch.client.CouchUserDb;
import ca.carleton.gcrc.couch.onUpload.UploadConstants;
public class AttachmentDescriptor extends AbstractDescriptor {
final protected Logger logger = Logger.getLogger(this.getClass());
private FileConversionContext context;
private String attachmentName;
public AttachmentDescriptor(FileConversionContext context, String attachmentName){
this.context = context;
this.attachmentName = attachmentName;
}
protected JSONObject getJson(){
JSONObject doc = context.getDoc();
JSONObject attachments = doc.getJSONObject("nunaliit_attachments");
JSONObject files = attachments.getJSONObject("files");
JSONObject attachmentDescription = files.getJSONObject(attachmentName);
return attachmentDescription;
}
public String getStatus() {
return getStringAttribute(UploadConstants.UPLOAD_STATUS_KEY);
}
public void setStatus(String status) {
setStringAttribute(UploadConstants.UPLOAD_STATUS_KEY, status);
}
public String getAttachmentName() {
return getStringAttribute(UploadConstants.ATTACHMENT_NAME_KEY);
}
public void setAttachmentName(String attachmentName) {
setStringAttribute(UploadConstants.ATTACHMENT_NAME_KEY,attachmentName);
}
public String getFileClass() {
return getStringAttribute(UploadConstants.MIME_CLASS_KEY);
}
public void setFileClass(String fileClass) {
setStringAttribute(UploadConstants.MIME_CLASS_KEY,fileClass);
}
public String getContentType() {
return getStringAttribute(UploadConstants.MIME_KEY);
}
public void setContentType(String contentType) {
setStringAttribute(UploadConstants.MIME_KEY,contentType);
}
public String getEncodingType() {
return getStringAttribute(UploadConstants.ENCODING_KEY);
}
public void setEncodingType(String encodingType) {
setStringAttribute(UploadConstants.ENCODING_KEY,encodingType);
}
public long getSize() {
return getLongAttribute(UploadConstants.SIZE_KEY);
}
public void setSize(long size) {
setLongAttribute(UploadConstants.SIZE_KEY, size);
}
public int getHeight() {
return getIntAttribute(UploadConstants.HEIGHT_KEY);
}
public void setHeight(int height) {
setIntAttribute(UploadConstants.HEIGHT_KEY, height);
}
public int getWidth() {
return getIntAttribute(UploadConstants.WIDTH_KEY);
}
public void setWidth(int width) {
setIntAttribute(UploadConstants.WIDTH_KEY, width);
}
public String getOriginalName() {
return getStringAttribute(UploadConstants.ORIGINAL_NAME_KEY);
}
public void setOriginalName(String originalName) {
setStringAttribute(UploadConstants.ORIGINAL_NAME_KEY,originalName);
}
public String getSubmitterName() {
return getStringAttribute(UploadConstants.SUBMITTER_KEY);
}
public void setSubmitterName(String originalName) {
setStringAttribute(UploadConstants.SUBMITTER_KEY,originalName);
}
public CouchUserContext getSubmitter() {
CouchUserContext submitter = null;
String submitterName = getSubmitterName();
if( null != submitterName ) {
try {
CouchUserDb userDb = context.getClient().getUserDatabase();
submitter = userDb.getUserFromName(submitterName);
} catch(Exception e) {
logger.error("Unable to obtain submitter information",e);
// Ignore error
}
}
return submitter;
}
public String getMediaFileName() {
return getStringAttribute(UploadConstants.MEDIA_FILE_KEY);
}
public void setMediaFileName(String encodingType) {
setStringAttribute(UploadConstants.MEDIA_FILE_KEY,encodingType);
}
public File getMediaFile() {
File file = null;
String mediaFileName = getMediaFileName();
if( null != mediaFileName ) {
file = new File(context.getMediaDir(), mediaFileName);
}
return file;
}
public String getThumbnailReference() {
return getStringAttribute(UploadConstants.THUMBNAIL_KEY);
}
public void setThumbnailReference(String thumbnailRef) {
setStringAttribute(UploadConstants.THUMBNAIL_KEY, thumbnailRef);
}
public boolean isConversionPerformed() {
return getBooleanAttribute(UploadConstants.CONVERSION_PERFORMED_KEY);
}
public void setConversionPerformed(boolean flag) {
setBooleanAttribute(UploadConstants.CONVERSION_PERFORMED_KEY, flag);
}
public String getSource() {
return getStringAttribute(UploadConstants.SOURCE_KEY);
}
public void setSource(String source) {
setStringAttribute(UploadConstants.SOURCE_KEY, source);
}
public String getOriginalAttachment() {
return getStringAttribute(UploadConstants.ORIGINAL_ATTACHMENT_KEY);
}
public void setOriginalAttachment(String source) {
setStringAttribute(UploadConstants.ORIGINAL_ATTACHMENT_KEY, source);
}
public boolean isServerWorkDescriptionAvailable() {
JSONObject attachmentDescription = getJson();
JSONObject serverWorkDescription = attachmentDescription.optJSONObject(UploadConstants.SERVER_KEY);
if( null == serverWorkDescription ) {
return false;
}
return true;
}
public ServerWorkDescriptor getServerWorkDescription() {
ServerWorkDescriptor serverWorkDescription = null;
if( false == isServerWorkDescriptionAvailable() ){
JSONObject attachmentDescription = getJson();
JSONObject json = new JSONObject();
attachmentDescription.put(UploadConstants.SERVER_KEY, json);
}
serverWorkDescription = new ServerWorkDescriptor(this);
return serverWorkDescription;
}
public boolean isOriginalFileDescriptionAvailable() {
JSONObject attachmentDescription = getJson();
JSONObject json = attachmentDescription.optJSONObject(UploadConstants.ORIGINAL_FILE_KEY);
if( null == json ) {
return false;
}
return true;
}
public OriginalFileDescriptor getOriginalFileDescription() {
OriginalFileDescriptor serverWorkDescription = null;
if( false == isOriginalFileDescriptionAvailable() ){
JSONObject attachmentDescription = getJson();
JSONObject json = new JSONObject();
attachmentDescription.put(UploadConstants.ORIGINAL_FILE_KEY, json);
}
serverWorkDescription = new OriginalFileDescriptor(this);
return serverWorkDescription;
}
public boolean isUserDataDescriptionAvailable() {
JSONObject attachmentDescription = getJson();
JSONObject json = attachmentDescription.optJSONObject(UploadConstants.DATA_KEY);
if( null == json ) {
return false;
}
return true;
}
public UserDataDescriptor getUserDataDescription() {
UserDataDescriptor userDataDescription = null;
if( false == isUserDataDescriptionAvailable() ){
JSONObject attachmentDescription = getJson();
JSONObject json = new JSONObject();
attachmentDescription.put(UploadConstants.DATA_KEY, json);
}
userDataDescription = new UserDataDescriptor(this);
return userDataDescription;
}
public boolean isWorkDescriptionAvailable() {
JSONObject attachmentDescription = getJson();
JSONObject json = attachmentDescription.optJSONObject(UploadConstants.WORK_KEY);
if( null == json ) {
return false;
}
return true;
}
public WorkDescriptor getWorkDescription() {
if( false == isWorkDescriptionAvailable() ){
JSONObject attachmentDescription = getJson();
JSONObject json = new JSONObject();
attachmentDescription.put(UploadConstants.WORK_KEY, json);
}
return new WorkDescriptor(this);
}
public boolean isFilePresent(){
return context.isFilePresent(attachmentName);
}
public void removeFile(){
context.removeFile(attachmentName);
}
public JSONObject uploadFile(File uploadedFile, String mimeType) throws Exception {
return context.uploadFile(attachmentName, uploadedFile, mimeType);
}
public File getMediaDir(){
return context.getMediaDir();
}
public boolean isSavingRequired(){
return context.isSavingRequired();
}
public void setSavingRequired(boolean flag){
context.setSavingRequired(flag);
}
}