1 40 41 package org.dspace.content.packager; 42 43 import java.io.ByteArrayInputStream ; 44 import java.io.FilterInputStream ; 45 import java.io.IOException ; 46 import java.io.InputStream ; 47 import java.sql.SQLException ; 48 49 import org.dspace.authorize.AuthorizeException; 50 import org.dspace.content.Bitstream; 51 import org.dspace.content.BitstreamFormat; 52 import org.dspace.content.Bundle; 53 import org.dspace.content.Collection; 54 import org.dspace.content.DCValue; 55 import org.dspace.content.FormatIdentifier; 56 import org.dspace.content.Item; 57 import org.dspace.core.Constants; 58 import org.dspace.core.Context; 59 import org.dspace.license.CreativeCommons; 60 61 67 68 public class PackageUtils 69 { 70 78 public static void checkMetadata(Item item) 79 throws PackageValidationException 80 { 81 DCValue t[] = item.getDC( "title", null, Item.ANY); 82 if (t == null || t.length == 0) 83 throw new PackageValidationException("Item cannot be created without the required \"title\" DC metadata."); 84 } 85 86 97 public static void addDepositLicense(Context context, String license, 98 Item item, Collection collection) 99 throws SQLException , IOException , AuthorizeException 100 { 101 if (license == null) 102 license = collection.getLicense(); 103 InputStream lis = new ByteArrayInputStream (license.getBytes()); 104 Bundle lb = item.createBundle(Constants.LICENSE_BUNDLE_NAME); 105 Bitstream lbs = lb.createBitstream(lis); 106 lis.close(); 107 BitstreamFormat bf = BitstreamFormat.findByShortDescription(context, "License"); 108 if (bf == null) 109 bf = FormatIdentifier.guessFormat(context, lbs); 110 lbs.setFormat(bf); 111 lbs.setName(Constants.LICENSE_BITSTREAM_NAME); 112 lbs.setSource(Constants.LICENSE_BITSTREAM_NAME); 113 lbs.update(); 114 } 115 116 123 public static Bitstream getBitstreamByName(Item item, String name) 124 throws SQLException 125 { 126 return getBitstreamByName(item, name, null); 127 } 128 129 137 public static Bitstream getBitstreamByName(Item item, String bsName, String bnName) 138 throws SQLException 139 { 140 Bundle[] bundles; 141 if (bnName == null) 142 bundles = item.getBundles(); 143 else 144 bundles = item.getBundles(bnName); 145 for (int i = 0; i < bundles.length; i++) 146 { 147 Bitstream[] bitstreams = bundles[i].getBitstreams(); 148 149 for (int k = 0; k < bitstreams.length; k++) 150 { 151 if (bsName.equals(bitstreams[k].getName())) 152 return bitstreams[k]; 153 } 154 } 155 return null; 156 } 157 158 167 public static Bitstream getBitstreamByFormat(Item item, 168 BitstreamFormat bsf, String bnName) 169 throws SQLException 170 { 171 int fid = bsf.getID(); 172 Bundle[] bundles; 173 if (bnName == null) 174 bundles = item.getBundles(); 175 else 176 bundles = item.getBundles(bnName); 177 for (int i = 0; i < bundles.length; i++) 178 { 179 Bitstream[] bitstreams = bundles[i].getBitstreams(); 180 181 for (int k = 0; k < bitstreams.length; k++) 182 { 183 if (bitstreams[k].getFormat().getID() == fid) 184 return bitstreams[k]; 185 } 186 } 187 return null; 188 } 189 190 199 public static boolean isMetaInfoBundle(Bundle bn) 200 { 201 return (bn.getName().equals(Constants.LICENSE_BUNDLE_NAME) || 202 bn.getName().equals(CreativeCommons.CC_BUNDLE_NAME) || 203 bn.getName().equals(Constants.METADATA_BUNDLE_NAME)); 204 } 205 206 218 public static class UnclosableInputStream extends FilterInputStream 219 { 220 public UnclosableInputStream(InputStream in) 221 { 222 super(in); 223 } 224 225 228 public void close() 229 { 230 } 231 } 232 233 248 public static BitstreamFormat findOrCreateBitstreamFormat(Context context, 249 String shortDesc, String MIMEType, String desc) 250 throws SQLException , AuthorizeException 251 { 252 BitstreamFormat bsf = BitstreamFormat.findByShortDescription(context, 253 shortDesc); 254 if (bsf == null) 256 { 257 bsf = BitstreamFormat.create(context); 258 bsf.setShortDescription(shortDesc); 259 bsf.setMIMEType(MIMEType); 260 bsf.setDescription(desc); 261 bsf.setSupportLevel(BitstreamFormat.KNOWN); 262 bsf.update(); 263 } 264 return bsf; 265 } 266 } 267 | Popular Tags |