package nz.ac.lconz.irr.curate.task;

import org.apache.log4j.Logger;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Bitstream;
import org.dspace.content.Bundle;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.curate.AbstractCurationTask;
import org.dspace.curate.Curator;

import java.io.IOException;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * User: schweer
 * Date: 23/04/12
 * Time: 11:44 AM
 * To change this template use File | Settings | File Templates.
 */
public class MakeThesisPrimaryBitstream extends AbstractCurationTask {
	private final static Logger log = Logger.getLogger(MakeThesisPrimaryBitstream.class);


	@Override
	public int perform(DSpaceObject dso) throws IOException {
		if (dso.getType() != Constants.ITEM) {
			return Curator.CURATE_SKIP;
		}
		Item item = (Item) dso;

		Bitstream thesisBitstream = null;
		Bundle thesisBundle = null;

		try {
			Bundle[] contentBundles = item.getBundles(Constants.CONTENT_BUNDLE_NAME);
			for (Bundle bundle : contentBundles) {
				if (bundle.getPrimaryBitstreamID() > 0) {
					String message = "Item id=" + item.getID() + " already has a primary bitstream";
					log.info(message);
					report(message);
					setResult(message);
					return Curator.CURATE_SKIP;
				}

				Bitstream thesisCandidate = bundle.getBitstreamByName("thesis.pdf");
				if (thesisCandidate != null) {
					thesisBitstream = thesisCandidate;
					thesisBundle = bundle;
					break;
				}
			}
		} catch (SQLException e) {
			String message = "Problem getting bundles for item id=" + item.getID();
			log.warn(message, e);
			report(message);
			setResult(e.getMessage());
			return Curator.CURATE_ERROR;
		}

		if (thesisBitstream != null && thesisBundle != null) {
			try {
				thesisBundle.setPrimaryBitstreamID(thesisBitstream.getID());
				thesisBundle.update();
			} catch (SQLException e) {
				String message = "Problem setting primary bitstream id=" + thesisBitstream.getID() + " for item id=" + item.getID();
				log.warn(message, e);
				report(message);
				setResult(e.getMessage());
				return Curator.CURATE_ERROR;
			} catch (AuthorizeException e) {
				String message = "Not authorised to set primary bitstream id=" + thesisBitstream.getID() + " for item id=" + item.getID();
				log.warn(message, e);
				report(message);
				setResult(e.getMessage());
				return Curator.CURATE_ERROR;
			}
			String message = "Set primary bitstream id=" + thesisBitstream.getID() + " for item id=" + item.getID();
			setResult(message);
			report(message);
			log.info(message);
			return Curator.CURATE_SUCCESS;
		} else {
			String message = "No thesis.pdf file found for item " + item.getID();
			setResult(message);
			report(message);
			log.info(message);
			return Curator.CURATE_FAIL;
		}
	}
}

@@ -464,160 +464,357 @@
 on;%0A
-%0A/**%0A * Created with IntelliJ IDEA.%0A * User: schweer%0A * Date: 23/04/12%0A * Time: 11:44 AM%0A * To change this template use File %7C Settings %7C File Templates
+import java.util.ArrayList;%0A%0A/**%0A * @author Andrea Schweer for the LCoNZ Institutional Research Repositories%0A *%0A * Curation task to make a bitstream with the name thesis.pdf the primary bitstream in the content bundle.%0A * It will also re-order the bitstreams in the content bundle to ensure that the primary bitstream is first in the bitstream order
 .%0A *
@@ -2157,16 +2157,89 @@
 pdate();
+%0A%0A%09%09%09%09makePrimaryBitstreamFirst(thesisBundle);%0A%09%09%09%09thesisBundle.update();
 %0A%09%09%09%7D ca
@@ -3165,11 +3165,771 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%0A%09private void makePrimaryBitstreamFirst(Bundle bundle) throws AuthorizeException, SQLException %7B%0A%09%09int primaryBitstreamID = bundle.getPrimaryBitstreamID();%0A%09%09if (primaryBitstreamID %3C 0) %7B%0A%09%09%09return; // no primary bitstream -%3E nothing to be done%0A%09%09%7D%0A%0A%09%09Bitstream%5B%5D bitstreams = bundle.getBitstreams();%0A%09%09ArrayList%3CInteger%3E ids = new ArrayList%3CInteger%3E(bitstreams.length);%0A%0A%09%09for (Bitstream bitstream : bitstreams) %7B%0A%09%09%09int bitstreamID = bitstream.getID();%0A%09%09%09if (bitstreamID == primaryBitstreamID) %7B%0A%09%09%09%09ids.add(0, bitstreamID); // insert as first%0A%09%09%09%7D else %7B%0A%09%09%09%09ids.add(bitstreamID); // insert in order%0A%09%09%09%7D%0A%09%09%7D%0A%09%09int%5B%5D newOrder = new int%5Bids.size()%5D;%0A%09%09for (int i = 0; i %3C ids.size(); i++) %7B%0A%09%09%09newOrder%5Bi%5D = ids.get(i);%0A%09%09%7D%0A%09%09bundle.setOrder(newOrder);%0A%09%7D
 %0A%7D%0A

Make thesis.pdf first bitstream as well as primary (Redmine #1711)