/*
 */
package org.datadryad.api;

import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.apache.log4j.Logger;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Collection;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.WorkspaceItem;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.handle.HandleManager;
import org.dspace.identifier.DOIIdentifierProvider;

/**
 * Wraps a DSpace Item and provides Dryad-specific behaviors
 * @author Dan Leehr <dan.leehr@nescent.org>
 */
public abstract class DryadObject {
    private static final String PUBLICATION_NAME_SCHEMA = "prism";
    private static final String PUBLICATION_NAME_ELEMENT = "publicationName";
    private static final String PUBLICATION_NAME_QUALIFIER = null;
    private static Logger log = Logger.getLogger(DryadObject.class);
    protected static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    protected Item item;
    protected DryadObject(Item item) {
        if(item == null) {
            throw new RuntimeException("Cannot instantiate a DryadObject with null item");
        }
        this.item = item;
    }
    protected static Collection collectionFromHandle(Context context, String handle) throws SQLException {
        DSpaceObject object = HandleManager.resolveToObject(context, handle);
        if(object.getType() == Constants.COLLECTION) {
            return (Collection)object;
        } else {
            return null;
        }
    }

    public Item getItem() {
        return item;
    }

    public String getIdentifier() {
        return DOIIdentifierProvider.getDoiValue(getItem());
    }

    public void setPublicationName(String publicationName) throws SQLException {
        getItem().clearMetadata(PUBLICATION_NAME_SCHEMA, PUBLICATION_NAME_ELEMENT, PUBLICATION_NAME_QUALIFIER, null);
        getItem().addMetadata(PUBLICATION_NAME_SCHEMA, PUBLICATION_NAME_ELEMENT, PUBLICATION_NAME_QUALIFIER, null, publicationName);
        try {
            getItem().update();
        } catch (AuthorizeException ex) {
            log.error("Authorize exception setting publication name", ex);
        }
    }

    protected final void addToCollectionAndArchive(Collection collection) throws SQLException {
        getItem().setOwningCollection(collection);
        try {
            collection.addItem(getItem());
            getItem().setArchived(true);
            getItem().update();
            collection.update();
        } catch (AuthorizeException ex) {
            log.error("Authorize exception adding to collection", ex);
        } catch (IOException ex) {
            log.error("IO exception adding to collection", ex);
        }
    }
}

@@ -132,32 +132,88 @@
 va.text.
-SimpleDateFormat
+ParseException;%0Aimport java.text.SimpleDateFormat;%0Aimport java.util.Date
 ;%0Aimport
@@ -320,24 +320,59 @@
 Collection;%0A
+import org.dspace.content.DCValue;%0A
 import org.d
@@ -989,24 +989,233 @@
 IER = null;%0A
+%0A    private static final String DATE_ACCESSIONED_SCHEMA = %22dc%22;%0A    private static final String DATE_ACCESSIONED_ELEMENT = %22date%22;%0A    private static final String DATE_ACCESSIONED_QUALIFIER = %22accessioned%22;%0A%0A
     private 
@@ -3131,11 +3131,562 @@
  %7D%0A    %7D
+%0A%0A    public Date getDateAccessioned() %7B%0A        Date dateAccessioned = null;%0A        DCValue%5B%5D metadata = item.getMetadata(DATE_ACCESSIONED_SCHEMA, DATE_ACCESSIONED_ELEMENT, DATE_ACCESSIONED_QUALIFIER, Item.ANY);%0A        if(metadata.length %3E 0) %7B%0A            DCValue firstDate = metadata%5B0%5D;%0A            try %7B%0A                dateAccessioned = DATE_FORMAT.parse(firstDate.value);%0A            %7D catch (ParseException ex) %7B%0A                log.error(%22Error parsing %22 + firstDate.value, ex);%0A            %7D%0A        %7D%0A        return dateAccessioned;%0A    %7D
 %0A%7D%0A

Add method to getDateAccessioned