001: /*
002: * FlowMapperUtils.java
003: *
004: * Version: $Revision: 1.3 $
005: *
006: * Date: $Date: 2006/07/13 23:20:54 $
007: *
008: * Copyright (c) 2002, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */package org.dspace.app.xmlui.aspect.administrative;
040:
041: import java.io.IOException;
042: import java.sql.SQLException;
043:
044: import org.dspace.app.xmlui.utils.UIException;
045: import org.dspace.app.xmlui.wing.Message;
046: import org.dspace.authorize.AuthorizeException;
047: import org.dspace.authorize.AuthorizeManager;
048: import org.dspace.browse.BrowseException;
049: import org.dspace.browse.IndexBrowse;
050: import org.dspace.content.Collection;
051: import org.dspace.content.Item;
052: import org.dspace.core.Constants;
053: import org.dspace.core.Context;
054:
055: /**
056: *
057: * @author Scott Phillips
058: */
059: public class FlowMapperUtils {
060:
061: /** Language Strings */
062: private static final Message T_map_items = new Message("default",
063: "xmlui.administrative.FlowMapperUtils.map_items");
064: private static final Message T_unmaped_items = new Message(
065: "default",
066: "xmlui.administrative.FlowMapperUtils.unmaped_items");
067:
068: /**
069: * Map the given items into this collection
070: *
071: * @param context The current DSpace content
072: * @param collectionID The collection to map items into.
073: * @param itemIDs The items to map.
074: * @return Flow result
075: */
076: public static FlowResult processMapItems(Context context,
077: int collectionID, String[] itemIDs) throws SQLException,
078: AuthorizeException, UIException, IOException {
079: FlowResult result = new FlowResult();
080: result.setContinue(false);
081:
082: Collection toCollection = Collection
083: .find(context, collectionID);
084:
085: for (String itemID : itemIDs) {
086: Item item = Item.find(context, Integer.valueOf(itemID));
087:
088: if (AuthorizeManager.authorizeActionBoolean(context, item,
089: Constants.READ)) {
090: // make sure item doesn't belong to this collection
091: if (!item.isOwningCollection(toCollection)) {
092: toCollection.addItem(item);
093: // FIXME Exception handling
094: try {
095: IndexBrowse ib = new IndexBrowse(context);
096: ib.indexItem(item);
097: } catch (BrowseException bex) {
098: throw new UIException(
099: "Unable to process browse", bex);
100: }
101: }
102: }
103: }
104:
105: context.commit();
106:
107: result.setContinue(true);
108: result.setOutcome(true);
109: result.setMessage(T_map_items);
110:
111: return result;
112: }
113:
114: /**
115: * Unmap the given items from this collection
116: *
117: * @param context The DSpace context
118: * @param collectionID The collection to unmap these items from.
119: * @param itemIDs The items to be unmapped.
120: * @return A flow result
121: */
122: public static FlowResult processUnmapItems(Context context,
123: int collectionID, String[] itemIDs) throws SQLException,
124: AuthorizeException, UIException, IOException {
125: FlowResult result = new FlowResult();
126: result.setContinue(false);
127:
128: Collection toCollection = Collection
129: .find(context, collectionID);
130:
131: for (String itemID : itemIDs) {
132: Item item = Item.find(context, Integer.valueOf(itemID));
133:
134: if (AuthorizeManager.authorizeActionBoolean(context, item,
135: Constants.READ)) {
136: // make sure item doesn't belong to this collection
137: if (!item.isOwningCollection(toCollection)) {
138: toCollection.removeItem(item);
139: // FIXME Exception handling
140: try {
141: IndexBrowse ib = new IndexBrowse(context);
142: ib.indexItem(item);
143: } catch (BrowseException bex) {
144: throw new UIException(
145: "Unable to process browse", bex);
146: }
147: }
148: }
149: }
150:
151: context.commit();
152:
153: result.setContinue(true);
154: result.setOutcome(true);
155: result.setMessage(T_unmaped_items);
156:
157: return result;
158: }
159:
160: }
|