KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * DSpaceMETSIngester
3  *
4  * Version: $Revision: 1.1 $
5  *
6  * Date: $Date: 2006/03/17 00:04:38 $
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.IOException JavaDoc;
44 import java.sql.SQLException JavaDoc;
45 import java.util.Set JavaDoc;
46
47 import org.apache.log4j.Logger;
48 import org.dspace.authorize.AuthorizeException;
49 import org.dspace.content.Bitstream;
50 import org.dspace.content.Bundle;
51 import org.dspace.content.Collection;
52 import org.dspace.content.Item;
53 import org.dspace.content.crosswalk.CrosswalkException;
54 import org.dspace.content.crosswalk.MetadataValidationException;
55 import org.dspace.core.Context;
56 import org.dspace.license.CreativeCommons;
57 import org.jdom.Element;
58
59 /**
60  * Packager plugin to ingest a
61  * METS (Metadata Encoding & Transmission Standard) package
62  * that conforms to the DSpace METS SIP (Submission Information Package) Profile.
63  * See <a HREF="http://www.loc.gov/standards/mets/">http://www.loc.gov/standards/mets/</a>
64  * for more information on METS, and
65  * <a HREF="http://www.dspace.org/standards/METS/SIP/profilev0p9p1/metssipv0p9p1.pdf">
66  * http://www.dspace.org/standards/METS/SIP/profilev0p9p1/metssipv0p9p1.pdf</a>
67  * (or a similar file in the /standards/METS/SIP resource hierarchy)
68  * for more information about the DSpace METS SIP profile.
69  *
70  * @author Larry Stone
71  * @version $Revision: 1.1 $
72  * @see org.dspace.content.packager.METSManifest
73  */

74 public class DSpaceMETSIngester
75        extends AbstractMETSIngester
76 {
77     /** log4j category */
78     private static Logger log = Logger.getLogger(DSpaceMETSIngester.class);
79
80     // first part of required mets@PROFILE value
81
private final static String JavaDoc PROFILE_START = "DSpace METS SIP Profile";
82
83     // just check the profile name.
84
void checkManifest(METSManifest manifest)
85         throws MetadataValidationException
86     {
87         String JavaDoc profile = manifest.getProfile();
88         if (profile == null)
89             throw new MetadataValidationException("Cannot accept METS with no PROFILE attribute!");
90         else if (!profile.startsWith(PROFILE_START))
91             throw new MetadataValidationException("METS has unacceptable PROFILE value, profile="+profile);
92     }
93
94     // nothing needed.
95
public void checkPackageFiles(Set JavaDoc packageFiles, Set JavaDoc missingFiles,
96                                   METSManifest manifest)
97         throws PackageValidationException, CrosswalkException
98     {
99         // This is where a subclass would arrange to use or ignore
100
// any "extra" files added to its type of package.
101
}
102
103
104     /**
105      * Choose DMD section(s) to crosswalk.
106      * <p>
107      * The algorithm is:<br>
108      * 1. Find MODS (preferably) or DC as primary DMD.<br>
109      * 2. If (1) succeeds, crosswalk it and ignore all other DMDs with
110      * same GROUPID<br>
111      * 3. Crosswalk remaining DMDs not eliminated already.
112      */

113     public void chooseItemDmd(Context context, Item item,
114                               METSManifest manifest,
115                               AbstractMETSIngester.MdrefManager callback,
116                               Element dmds[])
117         throws CrosswalkException,
118                AuthorizeException, SQLException JavaDoc, IOException JavaDoc
119     {
120         int found = -1;
121
122         // MODS is preferred
123
for (int i = 0; i < dmds.length; ++i)
124             if ("MODS".equals(manifest.getMdType(dmds[i])))
125                 found = i;
126
127         // DC acceptable if no MODS
128
if (found == -1)
129         {
130             for (int i = 0; i < dmds.length; ++i)
131                 if ("DC".equals(manifest.getMdType(dmds[i])))
132                     found = i;
133         }
134
135         String JavaDoc groupID = null;
136         if (found >= 0)
137         {
138             manifest.crosswalkItem(context, item, dmds[found], callback);
139             groupID = dmds[found].getAttributeValue("GROUPID");
140
141             if (groupID != null)
142             {
143                 for (int i = 0; i < dmds.length; ++i)
144                 {
145                     String JavaDoc g = dmds[i].getAttributeValue("GROUPID");
146                     if (g != null && !g.equals(groupID))
147                         manifest.crosswalkItem(context, item, dmds[i], callback);
148                 }
149             }
150         }
151
152         // otherwise take the first. Don't xwalk more than one because
153
// each xwalk _adds_ metadata, and could add duplicate fields.
154
else
155         {
156             if (dmds.length > 0)
157                 manifest.crosswalkItem(context, item, dmds[0], callback);
158         }
159     }
160
161
162     /**
163      * Policy: For DSpace deposit license, take deposit license
164      * supplied by explicit argument first, else use collection's
165      * default deposit license.
166      * For Creative Commons, look for a rightsMd containing a CC license.
167      */

168     public void addLicense(Context context, Collection collection,
169                            Item item, METSManifest manifest,
170                            AbstractMETSIngester.MdrefManager callback,
171                            String JavaDoc license)
172         throws PackageValidationException, CrosswalkException,
173                AuthorizeException, SQLException JavaDoc, IOException JavaDoc
174     {
175         PackageUtils.addDepositLicense(context, license, item, collection);
176
177         // If package includes a Creative Commons license, add that:
178
Element rmds[] = manifest.getItemRightsMD();
179         for (int i = 0; i < rmds.length; ++i)
180         {
181             String JavaDoc type = manifest.getMdType(rmds[i]);
182             if (type != null && type.equals("Creative Commons"))
183             {
184                 log.debug("Got Creative Commons license in rightsMD");
185                 CreativeCommons.setLicense(context, item,
186                             manifest.getMdContentAsStream(rmds[i], callback),
187                             manifest.getMdContentMimeType(rmds[i]));
188
189                 // if there was a bitstream, get rid of it, since
190
// it's just an artifact now that the CC license is installed.
191
Element mdRef = rmds[i].getChild("mdRef", METSManifest.metsNS);
192                 if (mdRef != null)
193                 {
194                     Bitstream bs = callback.getBitstreamForMdRef(mdRef);
195                     if (bs != null)
196                     {
197                         Bundle parent[] = bs.getBundles();
198                         if (parent.length > 0)
199                         {
200                             parent[0].removeBitstream(bs);
201                             parent[0].update();
202                         }
203                     }
204                 }
205             }
206         }
207     }
208
209     // last change to fix up Item.
210
public void finishItem(Context context, Item item)
211         throws PackageValidationException, CrosswalkException,
212          AuthorizeException, SQLException JavaDoc, IOException JavaDoc
213     {
214         // nothing to do.
215
}
216 }
217
Popular Tags