1 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 65 public class Upgrade11To12 66 { 67 public static void main(String [] argv) throws Exception 68 { 69 Context c = new Context(); 70 71 c.setIgnoreAuthorization(true); 73 74 ItemIterator ii = null; 75 76 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 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 c.commit(); 102 103 ii = Item.findAll(c); 105 106 while (ii.hasNext()) 107 { 108 boolean skipItem = false; 109 Item myItem = ii.next(); 110 111 int licenseBundleIndex = -1; int primaryBundleIndex = -1; 116 System.out.println("Processing item #: " + myItem.getID()); 117 118 Bundle[] myBundles = myItem.getBundles(); 119 120 for (int i = 0; i < myBundles.length; i++) 123 { 124 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 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 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 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 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 if (bitstreams.length > 0) 201 { 202 if ((i != primaryBundleIndex) 203 && (i != licenseBundleIndex)) 204 { 205 myBundles[primaryBundleIndex] 209 .addBitstream(bitstreams[0]); myItem.removeBundle(myBundles[i]); 214 System.out.println("Bitstream from bundle " + i 215 + " moved to primary bundle"); 216 217 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 |