KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > content > packager > PackageUtils


1 /*
2  * PackageUtils.java
3  *
4  * Version: $Revision: 1.2 $
5  *
6  * Date: $Date: 2006/03/15 21:52:02 $
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
41 package org.dspace.content.packager;
42
43 import java.io.ByteArrayInputStream JavaDoc;
44 import java.io.FilterInputStream JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.InputStream JavaDoc;
47 import java.sql.SQLException JavaDoc;
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 /**
62  * Container class for code that is useful to many packagers.
63  *
64  * @author Larry Stone
65  * @version $Revision: 1.2 $
66  */

67
68 public class PackageUtils
69 {
70     /**
71      * Test that item has adequate metadata.
72      * Check item for the minimal DC metadata required to ingest a
73      * new item, and throw a PackageValidationException if test fails.
74      * Used by all SIP processors as a common sanity test.
75      *
76      * @param item - item to test.
77      */

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     /**
87      * Add DSpace Deposit License to an Item.
88      * Utility function to add the a user-supplied deposit license or
89      * a default one if none was given; creates new bitstream in the
90      * "LICENSE" bundle and gives it the special license bitstream format.
91      *
92      * @param context - dspace context
93      * @param license - license string to add, may be null to invoke default.
94      * @param item - the item.
95      * @param collection - get the default license from here.
96      */

97     public static void addDepositLicense(Context context, String JavaDoc license,
98                                        Item item, Collection collection)
99         throws SQLException JavaDoc, IOException JavaDoc, AuthorizeException
100     {
101         if (license == null)
102             license = collection.getLicense();
103         InputStream JavaDoc lis = new ByteArrayInputStream JavaDoc(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     /**
117      * Find bitstream by its Name, looking in all bundles.
118      *
119      * @param item Item whose bitstreams to search.
120      * @param name Bitstream's name to match.
121      * @return first bitstream found or null.
122      */

123     public static Bitstream getBitstreamByName(Item item, String JavaDoc name)
124         throws SQLException JavaDoc
125     {
126         return getBitstreamByName(item, name, null);
127     }
128
129     /**
130      * Find bitstream by its Name, looking in specific named bundle.
131      *
132      * @param item - dspace item whose bundles to search.
133      * @param bsName - name of bitstream to match.
134      * @param bnName - bundle name to match, or null for all.
135      * @return the format found or null if none found.
136      */

137     public static Bitstream getBitstreamByName(Item item, String JavaDoc bsName, String JavaDoc bnName)
138         throws SQLException JavaDoc
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     /**
159      * Find bitstream by its format, looking in a specific bundle.
160      * Used to look for particularly-typed Package Manifest bitstreams.
161      *
162      * @param item - dspace item whose bundles to search.
163      * @param bsf - BitstreamFormat object to match.
164      * @param bnName - bundle name to match, or null for all.
165      * @return the format found or null if none found.
166      */

167     public static Bitstream getBitstreamByFormat(Item item,
168             BitstreamFormat bsf, String JavaDoc bnName)
169         throws SQLException JavaDoc
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     /**
191      * Predicate, does this bundle container meta-information. I.e.
192      * does this bundle contain descriptive metadata or other metadata
193      * such as license bitstreams? If so we probablly don't want to put
194      * it into the "content" section of a package; hence this predicate.
195      *
196      * @param bn -- the bundle
197      * @return true if this bundle name indicates it is a meta-info bundle.
198      */

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     /**
207      * Stream wrapper that does not allow its wrapped stream to be
208      * closed. This is needed to work around problem when loading
209      * bitstreams from ZipInputStream. The Bitstream constructor
210      * invokes close() on the input stream, which would prematurely end
211      * the ZipInputStream.
212      * Example:
213      * <pre>
214      * ZipEntry ze = zip.getNextEntry();
215      * Bitstream bs = bundle.createBitstream(new PackageUtils.UnclosableInputStream(zipInput));
216      * </pre>
217      */

218     public static class UnclosableInputStream extends FilterInputStream JavaDoc
219     {
220         public UnclosableInputStream(InputStream JavaDoc in)
221         {
222             super(in);
223         }
224
225         /**
226          * Do nothing, to prevent wrapped stream from being closed prematurely.
227          */

228         public void close()
229         {
230         }
231     }
232
233     /**
234      * Find or create a bitstream format to match the given short
235      * description.
236      * Used by packager ingesters to obtain a special bitstream
237      * format for the manifest (and/or metadata) file.
238      * <p>
239      * NOTE: When creating a new format, do NOT set any extensions, since
240      * we don't want any file with the same extension, which may be something
241      * generic like ".xml", to accidentally get set to this format.
242      * @param context - the context.
243      * @param shortDesc - short descriptive name, used to locate existing format.
244      * @param MIMEtype - mime content-type
245      * @param desc - long description
246      * @return BitstreamFormat object that was found or created. Never null.
247      */

248      public static BitstreamFormat findOrCreateBitstreamFormat(Context context,
249             String JavaDoc shortDesc, String JavaDoc MIMEType, String JavaDoc desc)
250         throws SQLException JavaDoc, AuthorizeException
251      {
252         BitstreamFormat bsf = BitstreamFormat.findByShortDescription(context,
253                                 shortDesc);
254         // not found, try to create one
255
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