KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > administer > Upgrade11To12


1 /*
2  * Upgrade11To12.java
3  *
4  * Version: $Revision: 1.7 $
5  *
6  * Date: $Date: 2005/08/25 17:20:29 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.administer;
41
42 import org.dspace.content.Bitstream;
43 import org.dspace.content.BitstreamFormat;
44 import org.dspace.content.Bundle;
45 import org.dspace.content.Collection;
46 import org.dspace.content.Item;
47 import org.dspace.content.ItemIterator;
48 import org.dspace.core.Context;
49
50 /**
51  * Command-line tool for making changes to DSpace database when updating from
52  * version 1.1/1.1.1 to 1.2.
53  * <P>
54  * The changes are:
55  * <ul>
56  * <li>Setting owning collection field for items
57  * <li>Reorganising content bitstreams into one bundle named ORIGINAL, license
58  * bitstreams into a bundle named LICENSE
59  * <li>Setting the sequence_id numbers in the bitstream table. This happens as
60  * item.update() is called on every item.
61  * <li>If a (newly-reorganised) 'ORIGINAL' bundle contains a text/html
62  * bitstream, that bitstream is set to the primary bitstream for HTML support.
63  * </ul>
64  */

65 public class Upgrade11To12
66 {
67     public static void main(String JavaDoc[] argv) throws Exception JavaDoc
68     {
69         Context c = new Context();
70
71         // ve are superuser!
72
c.setIgnoreAuthorization(true);
73
74         ItemIterator ii = null;
75
76         // first set owning Collections
77
Collection[] collections = Collection.findAll(c);
78
79         System.out.println("Setting item owningCollection fields in database");
80
81         for (int q = 0; q < collections.length; q++)
82         {
83             ii = collections[q].getItems();
84
85             while (ii.hasNext())
86             {
87                 Item myItem = ii.next();
88
89                 // set it if it's not already set
90
if (myItem.getOwningCollection() == null)
91                 {
92                     myItem.setOwningCollection(collections[q]);
93                     myItem.update();
94                     System.out.println("Set owner of item " + myItem.getID()
95                             + " to collection " + collections[q].getID());
96                 }
97             }
98         }
99
100         // commit pending transactions before continuing
101
c.commit();
102
103         // now combine some bundles
104
ii = Item.findAll(c);
105
106         while (ii.hasNext())
107         {
108             boolean skipItem = false;
109             Item myItem = ii.next();
110
111             int licenseBundleIndex = -1; // array index of license bundle (we'll
112
// skip this one often)
113
int primaryBundleIndex = -1; // array index of our primary bundle
114
// (all bitstreams assemble here)
115

116             System.out.println("Processing item #: " + myItem.getID());
117
118             Bundle[] myBundles = myItem.getBundles();
119
120             // look for bundles with multiple bitstreams
121
// (if any found, we'll skip this item)
122
for (int i = 0; i < myBundles.length; i++)
123             {
124                 // skip if bundle is already named
125
if (myBundles[i].getName() != null)
126                 {
127                     System.out
128                             .println("Skipping this item - named bundles already found");
129                     skipItem = true;
130
131                     break;
132                 }
133
134                 Bitstream[] bitstreams = myBundles[i].getBitstreams();
135
136                 // skip this item if we already have bundles combined in this
137
// item
138
if (bitstreams.length > 1)
139                 {
140                     System.out
141                             .println("Skipping this item - compound bundles already found");
142                     skipItem = true;
143
144                     break;
145                 }
146
147                 // is this the license? check the format
148
BitstreamFormat bf = bitstreams[0].getFormat();
149
150                 if (bf.getShortDescription().equals("License"))
151                 {
152                     System.out.println("Found license!");
153
154                     if (licenseBundleIndex == -1)
155                     {
156                         licenseBundleIndex = i;
157                         System.out.println("License bundle set to: " + i);
158                     }
159                     else
160                     {
161                         System.out
162                                 .println("ERROR - multiple license bundles in item - skipping");
163                         skipItem = true;
164
165                         break;
166                     }
167                 }
168                 else
169                 {
170                     // not a license, if primary isn't set yet, set it
171
if (primaryBundleIndex == -1)
172                     {
173                         primaryBundleIndex = i;
174                         System.out.println("Primary bundle set to: " + i);
175                     }
176                 }
177             }
178
179             if (!skipItem)
180             {
181                 // name the primary and license bundles
182
if (primaryBundleIndex != -1)
183                 {
184                     myBundles[primaryBundleIndex].setName("ORIGINAL");
185                     myBundles[primaryBundleIndex].update();
186                 }
187
188                 if (licenseBundleIndex != -1)
189                 {
190                     myBundles[licenseBundleIndex].setName("LICENSE");
191                     myBundles[licenseBundleIndex].update();
192                 }
193
194                 for (int i = 0; i < myBundles.length; i++)
195                 {
196                     Bitstream[] bitstreams = myBundles[i].getBitstreams();
197
198                     // now we can safely assume no bundles with multiple
199
// bitstreams
200
if (bitstreams.length > 0)
201                     {
202                         if ((i != primaryBundleIndex)
203                                 && (i != licenseBundleIndex))
204                         {
205                             // only option left is a bitstream to be combined
206
// with primary bundle
207
// and remove now-redundant bundle
208
myBundles[primaryBundleIndex]
209                                     .addBitstream(bitstreams[0]); // add to
210
// primary
211
myItem.removeBundle(myBundles[i]); // remove this
212
// bundle
213

214                             System.out.println("Bitstream from bundle " + i
215                                     + " moved to primary bundle");
216
217                             // flag if HTML bitstream
218
if (bitstreams[0].getFormat().getMIMEType().equals(
219                                     "text/html"))
220                             {
221                                 System.out
222                                         .println("Set primary bitstream to HTML file in item #"
223                                                 + myItem.getID()
224                                                 + " for HTML support.");
225                             }
226                         }
227                     }
228                 }
229             }
230         }
231
232         c.complete();
233     }
234 }
235
Popular Tags