KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > administer > CommunityFiliator


1 /*
2  * CommunityFiliator.java
3  *
4  * Version: $Revision: 1.8 $
5  *
6  * Date: $Date: 2006/07/10 20:04:02 $
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 package org.dspace.administer;
41
42 import java.io.IOException JavaDoc;
43 import java.sql.SQLException JavaDoc;
44
45 import org.apache.commons.cli.CommandLine;
46 import org.apache.commons.cli.CommandLineParser;
47 import org.apache.commons.cli.HelpFormatter;
48 import org.apache.commons.cli.Options;
49 import org.apache.commons.cli.PosixParser;
50 import org.dspace.authorize.AuthorizeException;
51 import org.dspace.content.Community;
52 import org.dspace.core.Constants;
53 import org.dspace.core.Context;
54 import org.dspace.handle.HandleManager;
55 import org.dspace.storage.rdbms.DatabaseManager;
56
57 /**
58  * A command-line tool for setting/removing community/sub-community
59  * relationships. Takes community DB Id or handle arguments as inputs.
60  *
61  * @author rrodgers
62  * @version $Revision: 1.8 $
63  */

64
65 public class CommunityFiliator
66 {
67     public static void main(String JavaDoc[] argv) throws Exception JavaDoc
68     {
69         // create an options object and populate it
70
CommandLineParser parser = new PosixParser();
71
72         Options options = new Options();
73
74         options.addOption("s", "set", false, "set a parent/child relationship");
75         options.addOption("r", "remove", false,
76                 "remove a parent/child relationship");
77         options.addOption("p", "parent", true,
78                 "parent community (handle or database ID)");
79         options.addOption("c", "child", true,
80                 "child community (handle or databaseID)");
81         options.addOption("h", "help", false, "help");
82
83         CommandLine line = parser.parse(options, argv);
84
85         String JavaDoc command = null; // set or remove
86
String JavaDoc parentID = null;
87         String JavaDoc childID = null;
88
89         if (line.hasOption('h'))
90         {
91             HelpFormatter myhelp = new HelpFormatter();
92             myhelp.printHelp("CommunityFiliator\n", options);
93             System.out
94                     .println("\nestablish a relationship: CommunityFiliator -s -p parentID -c childID");
95             System.out
96                     .println("remove a relationship: CommunityFiliator -r -p parentID -c childID");
97
98             System.exit(0);
99         }
100
101         if (line.hasOption('s'))
102         {
103             command = "set";
104         }
105
106         if (line.hasOption('r'))
107         {
108             command = "remove";
109         }
110
111         if (line.hasOption('p')) // parent
112
{
113             parentID = line.getOptionValue('p');
114         }
115
116         if (line.hasOption('c')) // child
117
{
118             childID = line.getOptionValue('c');
119         }
120
121         // now validate
122
// must have a command set
123
if (command == null)
124         {
125             System.out
126                     .println("Error - must run with either set or remove (run with -h flag for details)");
127             System.exit(1);
128         }
129
130         if (command.equals("set") || command.equals("remove"))
131         {
132             if (parentID == null)
133             {
134                 System.out
135                         .println("Error - a parentID must be specified (run with -h flag for details)");
136                 System.exit(1);
137             }
138
139             if (childID == null)
140             {
141                 System.out
142                         .println("Error - a childID must be specified (run with -h flag for details)");
143                 System.exit(1);
144             }
145         }
146
147         CommunityFiliator filiator = new CommunityFiliator();
148         Context c = new Context();
149
150         // ve are superuser!
151
c.setIgnoreAuthorization(true);
152
153         try
154         {
155             // validate and resolve the parent and child IDs into commmunities
156
Community parent = filiator.resolveCommunity(c, parentID);
157             Community child = filiator.resolveCommunity(c, childID);
158
159             if (parent == null)
160             {
161                 System.out.println("Error, parent community cannot be found: "
162                         + parentID);
163                 System.exit(1);
164             }
165
166             if (child == null)
167             {
168                 System.out.println("Error, child community cannot be found: "
169                         + childID);
170                 System.exit(1);
171             }
172
173             if (command.equals("set"))
174             {
175                 filiator.filiate(c, parent, child);
176             }
177             else
178             {
179                 filiator.defiliate(c, parent, child);
180             }
181         }
182         catch (SQLException JavaDoc sqlE)
183         {
184             System.out.println("Error - SQL exception: " + sqlE.toString());
185         }
186         catch (AuthorizeException authE)
187         {
188             System.out.println("Error - Authorize exception: "
189                     + authE.toString());
190         }
191         catch (IOException JavaDoc ioE)
192         {
193             System.out.println("Error - IO exception: " + ioE.toString());
194         }
195     }
196
197     public void filiate(Context c, Community parent, Community child)
198             throws SQLException JavaDoc, AuthorizeException, IOException JavaDoc
199     {
200         // check that a valid filiation would be established
201
// first test - proposed child must currently be an orphan (i.e.
202
// top-level)
203
Community childDad = child.getParentCommunity();
204
205         if (childDad != null)
206         {
207             System.out.println("Error, child community: " + child.getID()
208                     + " already a child of: " + childDad.getID());
209             System.exit(1);
210         }
211
212         // second test - circularity: parent's parents can't include proposed
213
// child
214
Community[] parentDads = parent.getAllParents();
215
216         for (int i = 0; i < parentDads.length; i++)
217         {
218             if (parentDads[i].getID() == child.getID())
219             {
220                 System.out
221                         .println("Error, circular parentage - child is parent of parent");
222                 System.exit(1);
223             }
224         }
225
226         // everthing's OK
227
parent.addSubcommunity(child);
228
229         // complete the pending transaction
230
c.complete();
231         System.out.println("Filiation complete. Community: '" + parent.getID()
232                 + "' is parent of community: '" + child.getID() + "'");
233     }
234
235     public void defiliate(Context c, Community parent, Community child)
236             throws SQLException JavaDoc, AuthorizeException, IOException JavaDoc
237     {
238         // verify that child is indeed a child of parent
239
Community[] parentKids = parent.getSubcommunities();
240         boolean isChild = false;
241
242         for (int i = 0; i < parentKids.length; i++)
243         {
244             if (parentKids[i].getID() == child.getID())
245             {
246                 isChild = true;
247
248                 break;
249             }
250         }
251
252         if (!isChild)
253         {
254             System.out
255                     .println("Error, child community not a child of parent community");
256             System.exit(1);
257         }
258
259         // OK remove the mappings - but leave the community, which will become
260
// top-level
261
DatabaseManager.updateQuery(c,
262                 "DELETE FROM community2community WHERE parent_comm_id= ? "+
263                 "AND child_comm_id= ? ", parent.getID(), child.getID());
264
265         // complete the pending transaction
266
c.complete();
267         System.out.println("Defiliation complete. Community: '" + child.getID()
268                 + "' is no longer a child of community: '" + parent.getID()
269                 + "'");
270     }
271
272     private Community resolveCommunity(Context c, String JavaDoc communityID)
273             throws SQLException JavaDoc
274     {
275         Community community = null;
276
277         if (communityID.indexOf('/') != -1)
278         {
279             // has a / must be a handle
280
community = (Community) HandleManager.resolveToObject(c,
281                     communityID);
282
283             // ensure it's a community
284
if ((community == null)
285                     || (community.getType() != Constants.COMMUNITY))
286             {
287                 community = null;
288             }
289         }
290         else
291         {
292             community = Community.find(c, Integer.parseInt(communityID));
293         }
294
295         return community;
296     }
297 }
298
Popular Tags