KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > content > InstallItem


1 /*
2  * InstallItem.java
3  *
4  * Version: $Revision: 1.26 $
5  *
6  * Date: $Date: 2006/11/27 16:16:50 $
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.content;
41
42 import java.io.IOException JavaDoc;
43 import java.sql.SQLException JavaDoc;
44
45 import org.dspace.authorize.AuthorizeException;
46 import org.dspace.core.ConfigurationManager;
47 import org.dspace.core.Context;
48 import org.dspace.handle.HandleManager;
49 import org.dspace.search.DSIndexer;
50
51 /**
52  * Support to install item in the archive
53  *
54  * @author dstuve
55  * @version $Revision: 1.26 $
56  */

57 public class InstallItem
58 {
59     /**
60      * Take an InProgressSubmission and turn it into a fully-archived Item,
61      * creating a new Handle
62      *
63      * @param c
64      * DSpace Context
65      * @param is
66      * submission to install
67      *
68      * @return the fully archived Item
69      */

70     public static Item installItem(Context c, InProgressSubmission is)
71             throws SQLException JavaDoc, IOException JavaDoc, AuthorizeException
72     {
73         return installItem(c, is, null);
74     }
75
76     /**
77      * Take an InProgressSubmission and turn it into a fully-archived Item.
78      *
79      * @param c current context
80      * @param is
81      * submission to install
82      * @param suppliedHandle
83      * the existing Handle to give the installed item
84      *
85      * @return the fully archived Item
86      */

87     public static Item installItem(Context c, InProgressSubmission is,
88             String JavaDoc suppliedHandle) throws SQLException JavaDoc,
89             IOException JavaDoc, AuthorizeException
90     {
91         Item item = is.getItem();
92         String JavaDoc handle;
93
94         // create accession date
95
DCDate now = DCDate.getCurrent();
96         item.addDC("date", "accessioned", null, now.toString());
97         item.addDC("date", "available", null, now.toString());
98
99         // create issue date if not present
100
DCValue[] currentDateIssued = item.getDC("date", "issued", Item.ANY);
101
102         if (currentDateIssued.length == 0)
103         {
104             item.addDC("date", "issued", null, now.toString());
105         }
106
107         // if no previous handle supplied, create one
108
if (suppliedHandle == null)
109         {
110             // create handle
111
handle = HandleManager.createHandle(c, item);
112         }
113         else
114         {
115             handle = HandleManager.createHandle(c, item, suppliedHandle);
116         }
117
118         String JavaDoc handleref = HandleManager.getCanonicalForm(handle);
119
120         // Add handle as identifier.uri DC value
121
item.addDC("identifier", "uri", null, handleref);
122
123         String JavaDoc provDescription = "Made available in DSpace on " + now
124                 + " (GMT). " + getBitstreamProvenanceMessage(item);
125
126         if (currentDateIssued.length != 0)
127         {
128             DCDate d = new DCDate(currentDateIssued[0].value);
129             provDescription = provDescription + " Previous issue date: "
130                     + d.toString();
131         }
132
133         // Add provenance description
134
item.addDC("description", "provenance", "en", provDescription);
135
136         // create collection2item mapping
137
is.getCollection().addItem(item);
138
139         // set owning collection
140
item.setOwningCollection(is.getCollection());
141
142         // set in_archive=true
143
item.setArchived(true);
144
145         // save changes ;-)
146
item.update();
147
148         // add item to search and browse indices
149
DSIndexer.indexContent(c, item);
150
151         // remove in-progress submission
152
is.deleteWrapper();
153
154         // remove the item's policies and replace them with
155
// the defaults from the collection
156
item.inheritCollectionDefaultPolicies(is.getCollection());
157
158         return item;
159     }
160
161
162     /**
163      * Generate provenance-worthy description of the bitstreams contained in an
164      * item.
165      *
166      * @param myitem the item generate description for
167      *
168      * @return provenance description
169      */

170     public static String JavaDoc getBitstreamProvenanceMessage(Item myitem)
171                             throws SQLException JavaDoc
172     {
173         // Get non-internal format bitstreams
174
Bitstream[] bitstreams = myitem.getNonInternalBitstreams();
175
176         // Create provenance description
177
String JavaDoc mymessage = "No. of bitstreams: " + bitstreams.length + "\n";
178
179         // Add sizes and checksums of bitstreams
180
for (int j = 0; j < bitstreams.length; j++)
181         {
182             mymessage = mymessage + bitstreams[j].getName() + ": "
183                     + bitstreams[j].getSize() + " bytes, checksum: "
184                     + bitstreams[j].getChecksum() + " ("
185                     + bitstreams[j].getChecksumAlgorithm() + ")\n";
186         }
187
188         return mymessage;
189     }
190 }
191
Popular Tags