KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * SupervisedItem.java
3  *
4  * Version: $Revision: 1.5 $
5  *
6  * Date: $Date: 2006/05/26 14:17:15 $
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;
42
43 import java.io.IOException JavaDoc;
44 import java.sql.SQLException JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.List JavaDoc;
47
48 import org.apache.log4j.Logger;
49
50 import org.dspace.authorize.AuthorizeException;
51 import org.dspace.content.Item;
52 import org.dspace.content.Collection;
53 import org.dspace.content.WorkspaceItem;
54 import org.dspace.core.Context;
55 import org.dspace.eperson.EPerson;
56 import org.dspace.eperson.Group;
57 import org.dspace.storage.rdbms.DatabaseManager;
58 import org.dspace.storage.rdbms.TableRowIterator;
59 import org.dspace.storage.rdbms.TableRow;
60
61 /**
62  * Class to handle WorkspaceItems which are being supervised. It extends the
63  * WorkspaceItem class and adds the methods required to be a Supervised Item.
64  *
65  * @author Richard Jones
66  * @version $Revision: 1.5 $
67  */

68 public class SupervisedItem extends WorkspaceItem
69 {
70     
71     /** log4j logger */
72     private static Logger log = Logger.getLogger(SupervisedItem.class);
73     
74     /** The item this workspace object pertains to */
75     private Item item;
76     
77     /** Our context */
78     private Context ourContext;
79
80     /** The table row corresponding to this workspace item */
81     private TableRow siRow;
82
83     /** The collection the item is being submitted to */
84     private Collection collection;
85     
86     /**
87      * Construct a supervised item out of the given row
88      *
89      * @param context the context this object exists in
90      * @param row the database row
91      */

92     SupervisedItem(Context context, TableRow row)
93         throws SQLException JavaDoc
94     {
95         // construct a new workspace item
96
super(context, row);
97     }
98
99     /**
100      * Get all workspace items which are being supervised
101      *
102      * @param context the context this object exists in
103      *
104      * @return array of SupervisedItems
105      */

106     public static SupervisedItem[] getAll(Context context)
107         throws SQLException JavaDoc
108     {
109         List JavaDoc sItems = new ArrayList JavaDoc();
110         
111         // The following query pulls out distinct workspace items which have
112
// entries in the supervisory linking database. We use DISTINCT to
113
// prevent multiple instances of the item in the case that it is
114
// supervised by more than one group
115
String JavaDoc query = "SELECT DISTINCT workspaceitem.* " +
116                        "FROM workspaceitem, epersongroup2workspaceitem " +
117                        "WHERE workspaceitem.workspace_item_id = " +
118                        "epersongroup2workspaceitem.workspace_item_id " +
119                        "ORDER BY workspaceitem.workspace_item_id";
120         
121         TableRowIterator tri = DatabaseManager.queryTable(context,
122                                     "workspaceitem",
123                                     query);
124         
125         while (tri.hasNext())
126         {
127             TableRow row = tri.next();
128             SupervisedItem si = new SupervisedItem(context, row);
129             
130             sItems.add(si);
131         }
132         
133         tri.close();
134         
135         SupervisedItem[] siArray = new SupervisedItem[sItems.size()];
136         siArray = (SupervisedItem[]) sItems.toArray(siArray);
137
138         return siArray;
139     }
140     
141     /**
142      * Gets all the groups that are supervising a particular workspace item
143      *
144      * @param c the context this object exists in
145      * @param wi the ID of the workspace item
146      *
147      * @return the supervising groups in an array
148      */

149     public Group[] getSupervisorGroups(Context c, int wi)
150         throws SQLException JavaDoc
151     {
152         List JavaDoc groupList = new ArrayList JavaDoc();
153         String JavaDoc query = "SELECT epersongroup.* " +
154                        "FROM epersongroup, epersongroup2workspaceitem " +
155                        "WHERE epersongroup2workspaceitem.workspace_item_id" +
156                        " = ? " +
157                        " AND epersongroup2workspaceitem.eperson_group_id =" +
158                        " epersongroup.eperson_group_id " +
159                        "ORDER BY epersongroup.name";
160         
161         TableRowIterator tri = DatabaseManager.queryTable(c,"epersongroup",query, wi);
162         
163         while (tri.hasNext())
164         {
165             TableRow row = tri.next();
166             Group group = Group.find(c,row.getIntColumn("eperson_group_id"));
167             
168             groupList.add(group);
169         }
170         
171         tri.close();
172         
173         Group[] groupArray = new Group[groupList.size()];
174         groupArray = (Group[]) groupList.toArray(groupArray);
175
176         return groupArray;
177     }
178     
179     /**
180      * Gets all the groups that are supervising a this workspace item
181      *
182      *
183      * @return the supervising groups in an array
184      */

185     // FIXME: We should arrange this code to use the above getSupervisorGroups
186
// method by building the relevant info before passing the request.
187
public Group[] getSupervisorGroups()
188         throws SQLException JavaDoc
189     {
190         Context ourContext = new Context();
191         
192         List JavaDoc groupList = new ArrayList JavaDoc();
193         String JavaDoc query = "SELECT epersongroup.* " +
194                        "FROM epersongroup, epersongroup2workspaceitem " +
195                        "WHERE epersongroup2workspaceitem.workspace_item_id" +
196                        " = ? " +
197                        " AND epersongroup2workspaceitem.eperson_group_id =" +
198                        " epersongroup.eperson_group_id " +
199                        "ORDER BY epersongroup.name";
200         
201         TableRowIterator tri = DatabaseManager.queryTable(ourContext,
202                                     "epersongroup",
203                                     query, this.getID());
204         
205         while (tri.hasNext())
206         {
207             TableRow row = tri.next();
208             Group group = Group.find(ourContext,
209                                 row.getIntColumn("eperson_group_id"));
210             
211             groupList.add(group);
212         }
213         
214         tri.close();
215         
216         Group[] groupArray = new Group[groupList.size()];
217         groupArray = (Group[]) groupList.toArray(groupArray);
218
219         return groupArray;
220     }
221     
222     /**
223      * Get items being supervised by given EPerson
224      *
225      * @param ep the eperson who's items to supervise we want
226      * @param context the dspace context
227      *
228      * @return the items eperson is supervising in an array
229      */

230     public static SupervisedItem[] findbyEPerson(Context context, EPerson ep)
231         throws SQLException JavaDoc
232     {
233         List JavaDoc sItems = new ArrayList JavaDoc();
234         String JavaDoc query = "SELECT DISTINCT workspaceitem.* " +
235                         "FROM workspaceitem, epersongroup2workspaceitem, " +
236                         "epersongroup2eperson " +
237                         "WHERE workspaceitem.workspace_item_id = " +
238                         "epersongroup2workspaceitem.workspace_item_id " +
239                         "AND epersongroup2workspaceitem.eperson_group_id =" +
240                         " epersongroup2eperson.eperson_group_id " +
241                         "AND epersongroup2eperson.eperson_id= ? " +
242                         " ORDER BY workspaceitem.workspace_item_id";
243
244         TableRowIterator tri = DatabaseManager.queryTable(context,
245                                     "workspaceitem",
246                                     query,ep.getID());
247         
248         while (tri.hasNext())
249         {
250             TableRow row = tri.next();
251             SupervisedItem si = new SupervisedItem(context, row);
252             sItems.add(si);
253         }
254         
255         tri.close();
256         
257         SupervisedItem[] siArray = new SupervisedItem[sItems.size()];
258         siArray = (SupervisedItem[]) sItems.toArray(siArray);
259
260         return siArray;
261         
262     }
263     
264 }
265
Popular Tags