001: /*
002: * Upgrade11To12.java
003: *
004: * Version: $Revision: 1303 $
005: *
006: * Date: $Date: 2005-08-25 12:20:29 -0500 (Thu, 25 Aug 2005) $
007: *
008: * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040: package org.dspace.administer;
041:
042: import org.dspace.content.Bitstream;
043: import org.dspace.content.BitstreamFormat;
044: import org.dspace.content.Bundle;
045: import org.dspace.content.Collection;
046: import org.dspace.content.Item;
047: import org.dspace.content.ItemIterator;
048: import org.dspace.core.Context;
049:
050: /**
051: * Command-line tool for making changes to DSpace database when updating from
052: * version 1.1/1.1.1 to 1.2.
053: * <P>
054: * The changes are:
055: * <ul>
056: * <li>Setting owning collection field for items
057: * <li>Reorganising content bitstreams into one bundle named ORIGINAL, license
058: * bitstreams into a bundle named LICENSE
059: * <li>Setting the sequence_id numbers in the bitstream table. This happens as
060: * item.update() is called on every item.
061: * <li>If a (newly-reorganised) 'ORIGINAL' bundle contains a text/html
062: * bitstream, that bitstream is set to the primary bitstream for HTML support.
063: * </ul>
064: */
065: public class Upgrade11To12 {
066: public static void main(String[] argv) throws Exception {
067: Context c = new Context();
068:
069: // ve are superuser!
070: c.setIgnoreAuthorization(true);
071:
072: ItemIterator ii = null;
073:
074: // first set owning Collections
075: Collection[] collections = Collection.findAll(c);
076:
077: System.out
078: .println("Setting item owningCollection fields in database");
079:
080: for (int q = 0; q < collections.length; q++) {
081: ii = collections[q].getItems();
082:
083: while (ii.hasNext()) {
084: Item myItem = ii.next();
085:
086: // set it if it's not already set
087: if (myItem.getOwningCollection() == null) {
088: myItem.setOwningCollection(collections[q]);
089: myItem.update();
090: System.out.println("Set owner of item "
091: + myItem.getID() + " to collection "
092: + collections[q].getID());
093: }
094: }
095: }
096:
097: // commit pending transactions before continuing
098: c.commit();
099:
100: // now combine some bundles
101: ii = Item.findAll(c);
102:
103: while (ii.hasNext()) {
104: boolean skipItem = false;
105: Item myItem = ii.next();
106:
107: int licenseBundleIndex = -1; // array index of license bundle (we'll
108: // skip this one often)
109: int primaryBundleIndex = -1; // array index of our primary bundle
110: // (all bitstreams assemble here)
111:
112: System.out.println("Processing item #: " + myItem.getID());
113:
114: Bundle[] myBundles = myItem.getBundles();
115:
116: // look for bundles with multiple bitstreams
117: // (if any found, we'll skip this item)
118: for (int i = 0; i < myBundles.length; i++) {
119: // skip if bundle is already named
120: if (myBundles[i].getName() != null) {
121: System.out
122: .println("Skipping this item - named bundles already found");
123: skipItem = true;
124:
125: break;
126: }
127:
128: Bitstream[] bitstreams = myBundles[i].getBitstreams();
129:
130: // skip this item if we already have bundles combined in this
131: // item
132: if (bitstreams.length > 1) {
133: System.out
134: .println("Skipping this item - compound bundles already found");
135: skipItem = true;
136:
137: break;
138: }
139:
140: // is this the license? check the format
141: BitstreamFormat bf = bitstreams[0].getFormat();
142:
143: if (bf.getShortDescription().equals("License")) {
144: System.out.println("Found license!");
145:
146: if (licenseBundleIndex == -1) {
147: licenseBundleIndex = i;
148: System.out.println("License bundle set to: "
149: + i);
150: } else {
151: System.out
152: .println("ERROR - multiple license bundles in item - skipping");
153: skipItem = true;
154:
155: break;
156: }
157: } else {
158: // not a license, if primary isn't set yet, set it
159: if (primaryBundleIndex == -1) {
160: primaryBundleIndex = i;
161: System.out.println("Primary bundle set to: "
162: + i);
163: }
164: }
165: }
166:
167: if (!skipItem) {
168: // name the primary and license bundles
169: if (primaryBundleIndex != -1) {
170: myBundles[primaryBundleIndex].setName("ORIGINAL");
171: myBundles[primaryBundleIndex].update();
172: }
173:
174: if (licenseBundleIndex != -1) {
175: myBundles[licenseBundleIndex].setName("LICENSE");
176: myBundles[licenseBundleIndex].update();
177: }
178:
179: for (int i = 0; i < myBundles.length; i++) {
180: Bitstream[] bitstreams = myBundles[i]
181: .getBitstreams();
182:
183: // now we can safely assume no bundles with multiple
184: // bitstreams
185: if (bitstreams.length > 0) {
186: if ((i != primaryBundleIndex)
187: && (i != licenseBundleIndex)) {
188: // only option left is a bitstream to be combined
189: // with primary bundle
190: // and remove now-redundant bundle
191: myBundles[primaryBundleIndex]
192: .addBitstream(bitstreams[0]); // add to
193: // primary
194: myItem.removeBundle(myBundles[i]); // remove this
195: // bundle
196:
197: System.out.println("Bitstream from bundle "
198: + i + " moved to primary bundle");
199:
200: // flag if HTML bitstream
201: if (bitstreams[0].getFormat().getMIMEType()
202: .equals("text/html")) {
203: System.out
204: .println("Set primary bitstream to HTML file in item #"
205: + myItem.getID()
206: + " for HTML support.");
207: }
208: }
209: }
210: }
211: }
212: }
213:
214: c.complete();
215: }
216: }
|