KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > checker > HandleDispatcher


1 /*
2  * Copyright (c) 2004-2005, Hewlett-Packard Company and Massachusetts
3  * Institute of Technology. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * - Neither the name of the Hewlett-Packard Company nor the name of the
17  * Massachusetts Institute of Technology nor the names of their
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32  * DAMAGE.
33  */

34 package org.dspace.checker;
35
36 import java.sql.SQLException JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.List JavaDoc;
39
40 import org.apache.log4j.Logger;
41 import org.dspace.content.DSpaceObject;
42 import org.dspace.core.Constants;
43 import org.dspace.core.Context;
44 import org.dspace.handle.HandleManager;
45
46 /**
47  * A BitstreamDispatcher that checks all the bitstreams contained within an
48  * item, collection or community referred to by Handle.
49  *
50  * @author Jim Downing
51  * @author Grace Carpenter
52  * @author Nathan Sarr
53  *
54  */

55 public class HandleDispatcher implements BitstreamDispatcher
56 {
57
58     /** Log 4j logger. */
59     private static final Logger LOG = Logger.getLogger(HandleDispatcher.class);
60
61     /** Handle to retrieve bitstreams from. */
62     String JavaDoc handle = null;
63
64     /** Has the type of object the handle refers to been determined. */
65     Boolean JavaDoc init = Boolean.FALSE;
66
67     /** the delegate to dispatch to. */
68     ListDispatcher delegate = null;
69
70     /**
71      * Database access for retrieving bitstreams
72      */

73     BitstreamInfoDAO bitstreamInfoDAO;
74
75     /**
76      * Blanked off, no-op constructor.
77      */

78     private HandleDispatcher()
79     {
80         ;
81     }
82
83     /**
84      * Main constructor.
85      *
86      * @param hdl
87      * the handle to get bitstreams from.
88      */

89     public HandleDispatcher(BitstreamInfoDAO bitInfoDAO, String JavaDoc hdl)
90     {
91         bitstreamInfoDAO = bitInfoDAO;
92         handle = hdl;
93     }
94
95     /**
96      * Private initialization routine.
97      *
98      * @throws SQLException
99      * if database access fails.
100      */

101     private void init()
102     {
103         Context context = null;
104         int dsoType = -1;
105
106         int id = -1;
107         try
108         {
109             context = new Context();
110             DSpaceObject dso = HandleManager.resolveToObject(context, handle);
111             id = dso.getID();
112             dsoType = dso.getType();
113             context.abort();
114
115         }
116         catch (SQLException JavaDoc e)
117         {
118             LOG.error("init error " + e.getMessage(), e);
119             throw new RuntimeException JavaDoc("init error" + e.getMessage(), e);
120
121         }
122         finally
123         {
124             // Abort the context if it's still valid
125
if ((context != null) && context.isValid())
126             {
127                 context.abort();
128             }
129         }
130
131         List JavaDoc ids = new ArrayList JavaDoc();
132
133         switch (dsoType)
134         {
135         case Constants.BITSTREAM:
136             ids.add(new Integer JavaDoc(id));
137             break;
138
139         case Constants.ITEM:
140             ids = bitstreamInfoDAO.getItemBitstreams(id);
141             break;
142
143         case Constants.COLLECTION:
144             ids = bitstreamInfoDAO.getCollectionBitstreams(id);
145             break;
146
147         case Constants.COMMUNITY:
148             ids = bitstreamInfoDAO.getCommunityBitstreams(id);
149             break;
150         }
151
152         delegate = new ListDispatcher(ids);
153         init = Boolean.TRUE;
154     }
155
156     /**
157      * Initializes this dispatcher on first execution.
158      *
159      * @see org.dspace.checker.BitstreamDispatcher#next()
160      */

161     public int next()
162     {
163         synchronized (init)
164         {
165             if (init == Boolean.FALSE)
166             {
167                 init();
168             }
169         }
170
171         return delegate.next();
172     }
173 }
174
Popular Tags