001: /*
002: * Copyright (c) 2004-2005, Hewlett-Packard Company and Massachusetts
003: * Institute of Technology. All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are
007: * met:
008: *
009: * - Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * - Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * - Neither the name of the Hewlett-Packard Company nor the name of the
017: * Massachusetts Institute of Technology nor the names of their
018: * contributors may be used to endorse or promote products derived from
019: * this software without specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
024: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
025: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
026: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
027: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
028: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
029: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
030: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
031: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
032: * DAMAGE.
033: */
034: package org.dspace.checker;
035:
036: import java.sql.SQLException;
037: import java.util.ArrayList;
038: import java.util.List;
039:
040: import org.apache.log4j.Logger;
041: import org.dspace.content.DSpaceObject;
042: import org.dspace.core.Constants;
043: import org.dspace.core.Context;
044: import org.dspace.handle.HandleManager;
045:
046: /**
047: * A BitstreamDispatcher that checks all the bitstreams contained within an
048: * item, collection or community referred to by Handle.
049: *
050: * @author Jim Downing
051: * @author Grace Carpenter
052: * @author Nathan Sarr
053: *
054: */
055: public class HandleDispatcher implements BitstreamDispatcher {
056:
057: /** Log 4j logger. */
058: private static final Logger LOG = Logger
059: .getLogger(HandleDispatcher.class);
060:
061: /** Handle to retrieve bitstreams from. */
062: String handle = null;
063:
064: /** Has the type of object the handle refers to been determined. */
065: Boolean init = Boolean.FALSE;
066:
067: /** the delegate to dispatch to. */
068: ListDispatcher delegate = null;
069:
070: /**
071: * Database access for retrieving bitstreams
072: */
073: BitstreamInfoDAO bitstreamInfoDAO;
074:
075: /**
076: * Blanked off, no-op constructor.
077: */
078: private HandleDispatcher() {
079: ;
080: }
081:
082: /**
083: * Main constructor.
084: *
085: * @param hdl
086: * the handle to get bitstreams from.
087: */
088: public HandleDispatcher(BitstreamInfoDAO bitInfoDAO, String hdl) {
089: bitstreamInfoDAO = bitInfoDAO;
090: handle = hdl;
091: }
092:
093: /**
094: * Private initialization routine.
095: *
096: * @throws SQLException
097: * if database access fails.
098: */
099: private void init() {
100: Context context = null;
101: int dsoType = -1;
102:
103: int id = -1;
104: try {
105: context = new Context();
106: DSpaceObject dso = HandleManager.resolveToObject(context,
107: handle);
108: id = dso.getID();
109: dsoType = dso.getType();
110: context.abort();
111:
112: } catch (SQLException e) {
113: LOG.error("init error " + e.getMessage(), e);
114: throw new RuntimeException("init error" + e.getMessage(), e);
115:
116: } finally {
117: // Abort the context if it's still valid
118: if ((context != null) && context.isValid()) {
119: context.abort();
120: }
121: }
122:
123: List ids = new ArrayList();
124:
125: switch (dsoType) {
126: case Constants.BITSTREAM:
127: ids.add(new Integer(id));
128: break;
129:
130: case Constants.ITEM:
131: ids = bitstreamInfoDAO.getItemBitstreams(id);
132: break;
133:
134: case Constants.COLLECTION:
135: ids = bitstreamInfoDAO.getCollectionBitstreams(id);
136: break;
137:
138: case Constants.COMMUNITY:
139: ids = bitstreamInfoDAO.getCommunityBitstreams(id);
140: break;
141: }
142:
143: delegate = new ListDispatcher(ids);
144: init = Boolean.TRUE;
145: }
146:
147: /**
148: * Initializes this dispatcher on first execution.
149: *
150: * @see org.dspace.checker.BitstreamDispatcher#next()
151: */
152: public int next() {
153: synchronized (init) {
154: if (init == Boolean.FALSE) {
155: init();
156: }
157: }
158:
159: return delegate.next();
160: }
161: }
|