KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Community.java
3  *
4  * Version: $Revision: 1.37 $
5  *
6  * Date: $Date: 2006/09/05 10:33:39 $
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.content;
41
42 import java.io.IOException JavaDoc;
43 import java.io.InputStream JavaDoc;
44 import java.sql.SQLException JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.List JavaDoc;
47 import java.util.MissingResourceException JavaDoc;
48
49 import org.apache.log4j.Logger;
50 import org.dspace.authorize.AuthorizeException;
51 import org.dspace.authorize.AuthorizeManager;
52 import org.dspace.authorize.ResourcePolicy;
53 import org.dspace.core.Constants;
54 import org.dspace.core.Context;
55 import org.dspace.core.I18N;
56 import org.dspace.core.LogManager;
57 import org.dspace.eperson.Group;
58 import org.dspace.handle.HandleManager;
59 import org.dspace.history.HistoryManager;
60 import org.dspace.search.DSIndexer;
61 import org.dspace.storage.rdbms.DatabaseManager;
62 import org.dspace.storage.rdbms.TableRow;
63 import org.dspace.storage.rdbms.TableRowIterator;
64
65 /**
66  * Class representing a community
67  * <P>
68  * The community's metadata (name, introductory text etc.) is loaded into'
69  * memory. Changes to this metadata are only reflected in the database after
70  * <code>update</code> is called.
71  *
72  * @author Robert Tansley
73  * @version $Revision: 1.37 $
74  */

75 public class Community extends DSpaceObject
76 {
77     /** log4j category */
78     private static Logger log = Logger.getLogger(Community.class);
79
80     /** Our context */
81     private Context ourContext;
82
83     /** The table row corresponding to this item */
84     private TableRow communityRow;
85
86     /** The logo bitstream */
87     private Bitstream logo;
88
89     /** Handle, if any */
90     private String JavaDoc handle;
91
92     /**
93      * Construct a community object from a database row.
94      *
95      * @param context
96      * the context this object exists in
97      * @param row
98      * the corresponding row in the table
99      */

100     Community(Context context, TableRow row) throws SQLException JavaDoc
101     {
102         ourContext = context;
103         communityRow = row;
104
105         // Get the logo bitstream
106
if (communityRow.isColumnNull("logo_bitstream_id"))
107         {
108             logo = null;
109         }
110         else
111         {
112             logo = Bitstream.find(ourContext, communityRow
113                     .getIntColumn("logo_bitstream_id"));
114         }
115
116         // Get our Handle if any
117
handle = HandleManager.findHandle(context, this);
118
119         // Cache ourselves
120
context.cache(this, row.getIntColumn("community_id"));
121     }
122
123     /**
124      * Get a community from the database. Loads in the metadata
125      *
126      * @param context
127      * DSpace context object
128      * @param id
129      * ID of the community
130      *
131      * @return the community, or null if the ID is invalid.
132      */

133     public static Community find(Context context, int id) throws SQLException JavaDoc
134     {
135         // First check the cache
136
Community fromCache = (Community) context
137                 .fromCache(Community.class, id);
138
139         if (fromCache != null)
140         {
141             return fromCache;
142         }
143
144         TableRow row = DatabaseManager.find(context, "community", id);
145
146         if (row == null)
147         {
148             if (log.isDebugEnabled())
149             {
150                 log.debug(LogManager.getHeader(context, "find_community",
151                         "not_found,community_id=" + id));
152             }
153
154             return null;
155         }
156         else
157         {
158             if (log.isDebugEnabled())
159             {
160                 log.debug(LogManager.getHeader(context, "find_community",
161                         "community_id=" + id));
162             }
163
164             return new Community(context, row);
165         }
166     }
167
168     /**
169      * Create a new community, with a new ID.
170      *
171      * @param context
172      * DSpace context object
173      *
174      * @return the newly created community
175      */

176     public static Community create(Community parent, Context context)
177             throws SQLException JavaDoc, AuthorizeException
178     {
179         // Only administrators and adders can create communities
180
if (!(AuthorizeManager.isAdmin(context) || AuthorizeManager
181                 .authorizeActionBoolean(context, parent, Constants.ADD)))
182         {
183             throw new AuthorizeException(
184                     "Only administrators can create communities");
185         }
186
187         TableRow row = DatabaseManager.create(context, "community");
188         Community c = new Community(context, row);
189         c.handle = HandleManager.createHandle(context, c);
190
191         // create the default authorization policy for communities
192
// of 'anonymous' READ
193
Group anonymousGroup = Group.find(context, 0);
194
195         ResourcePolicy myPolicy = ResourcePolicy.create(context);
196         myPolicy.setResource(c);
197         myPolicy.setAction(Constants.READ);
198         myPolicy.setGroup(anonymousGroup);
199         myPolicy.update();
200
201         HistoryManager.saveHistory(context, c, HistoryManager.CREATE, context
202                 .getCurrentUser(), context.getExtraLogInfo());
203
204         log.info(LogManager.getHeader(context, "create_community",
205                 "community_id=" + row.getIntColumn("community_id"))
206                 + ",handle=" + c.handle);
207
208         return c;
209     }
210
211     /**
212      * Get a list of all communities in the system. These are alphabetically
213      * sorted by community name.
214      *
215      * @param context
216      * DSpace context object
217      *
218      * @return the communities in the system
219      */

220     public static Community[] findAll(Context context) throws SQLException JavaDoc
221     {
222         TableRowIterator tri = DatabaseManager.queryTable(context, "community",
223                 "SELECT * FROM community ORDER BY name");
224
225         List JavaDoc communities = new ArrayList JavaDoc();
226
227         while (tri.hasNext())
228         {
229             TableRow row = tri.next();
230
231             // First check the cache
232
Community fromCache = (Community) context.fromCache(
233                     Community.class, row.getIntColumn("community_id"));
234
235             if (fromCache != null)
236             {
237                 communities.add(fromCache);
238             }
239             else
240             {
241                 communities.add(new Community(context, row));
242             }
243         }
244         // close the TableRowIterator to free up resources
245
tri.close();
246
247         Community[] communityArray = new Community[communities.size()];
248         communityArray = (Community[]) communities.toArray(communityArray);
249
250         return communityArray;
251     }
252
253     /**
254      * Get a list of all top-level communities in the system. These are
255      * alphabetically sorted by community name. A top-level community is one
256      * without a parent community.
257      *
258      * @param context
259      * DSpace context object
260      *
261      * @return the top-level communities in the system
262      */

263     public static Community[] findAllTop(Context context) throws SQLException JavaDoc
264     {
265         // get all communities that are not children
266
TableRowIterator tri = DatabaseManager.queryTable(context, "community",
267                 "SELECT * FROM community WHERE NOT community_id IN "
268                         + "(SELECT child_comm_id FROM community2community) "
269                         + "ORDER BY name");
270
271         List JavaDoc topCommunities = new ArrayList JavaDoc();
272
273         while (tri.hasNext())
274         {
275             TableRow row = tri.next();
276
277             // First check the cache
278
Community fromCache = (Community) context.fromCache(
279                     Community.class, row.getIntColumn("community_id"));
280
281             if (fromCache != null)
282             {
283                 topCommunities.add(fromCache);
284             }
285             else
286             {
287                 topCommunities.add(new Community(context, row));
288             }
289         }
290         // close the TableRowIterator to free up resources
291
tri.close();
292
293         Community[] communityArray = new Community[topCommunities.size()];
294         communityArray = (Community[]) topCommunities.toArray(communityArray);
295
296         return communityArray;
297     }
298
299     /**
300      * Get the internal ID of this collection
301      *
302      * @return the internal identifier
303      */

304     public int getID()
305     {
306         return communityRow.getIntColumn("community_id");
307     }
308
309     public String JavaDoc getHandle()
310     {
311         return handle;
312     }
313
314     /**
315      * Get the value of a metadata field
316      *
317      * @param field
318      * the name of the metadata field to get
319      *
320      * @return the value of the metadata field
321      *
322      * @exception IllegalArgumentException
323      * if the requested metadata field doesn't exist
324      */

325     public String JavaDoc getMetadata(String JavaDoc field)
326     {
327         return communityRow.getStringColumn(field);
328     }
329
330     /**
331      * Set a metadata value
332      *
333      * @param field
334      * the name of the metadata field to get
335      * @param value
336      * value to set the field to
337      *
338      * @exception IllegalArgumentException
339      * if the requested metadata field doesn't exist
340      * @exception MissingResourceException
341      */

342     public void setMetadata(String JavaDoc field, String JavaDoc value)throws MissingResourceException JavaDoc
343     {
344         if ((field.trim()).equals("name") && (value.trim()).equals(""))
345         {
346             try
347             {
348                 value = I18N.message("untitled", Community.class);
349             }
350             catch (MissingResourceException JavaDoc e)
351             {
352                 value = "Untitled";
353             }
354         }
355         communityRow.setColumn(field, value);
356     }
357
358     /**
359      * Get the logo for the community. <code>null</code> is return if the
360      * community does not have a logo.
361      *
362      * @return the logo of the community, or <code>null</code>
363      */

364     public Bitstream getLogo()
365     {
366         return logo;
367     }
368
369     /**
370      * Give the community a logo. Passing in <code>null</code> removes any
371      * existing logo. You will need to set the format of the new logo bitstream
372      * before it will work, for example to "JPEG". Note that
373      * <code>update(/code> will need to be called for the change to take
374      * effect. Setting a logo and not calling <code>update</code> later may
375      * result in a previous logo lying around as an "orphaned" bitstream.
376      *
377      * @param is the stream to use as the new logo
378      *
379      * @return the new logo bitstream, or <code>null</code> if there is no
380      * logo (<code>null</code> was passed in)
381      */

382     public Bitstream setLogo(InputStream JavaDoc is) throws AuthorizeException,
383             IOException JavaDoc, SQLException JavaDoc
384     {
385         // Check authorisation
386
// authorized to remove the logo when DELETE rights
387
// authorized when canEdit
388
if (!((is == null) && AuthorizeManager.authorizeActionBoolean(
389                 ourContext, this, Constants.DELETE)))
390         {
391             canEdit();
392         }
393
394         // First, delete any existing logo
395
if (logo != null)
396         {
397             log.info(LogManager.getHeader(ourContext, "remove_logo",
398                     "community_id=" + getID()));
399             communityRow.setColumnNull("logo_bitstream_id");
400             logo.delete();
401             logo = null;
402         }
403
404         if (is != null)
405         {
406             Bitstream newLogo = Bitstream.create(ourContext, is);
407             communityRow.setColumn("logo_bitstream_id", newLogo.getID());
408             logo = newLogo;
409
410             // now create policy for logo bitstream
411
// to match our READ policy
412
List JavaDoc policies = AuthorizeManager.getPoliciesActionFilter(
413                     ourContext, this, Constants.READ);
414             AuthorizeManager.addPolicies(ourContext, policies, newLogo);
415
416             log.info(LogManager.getHeader(ourContext, "set_logo",
417                     "community_id=" + getID() + "logo_bitstream_id="
418                             + newLogo.getID()));
419         }
420
421         return logo;
422     }
423
424     /**
425      * Update the community metadata (including logo) to the database.
426      */

427     public void update() throws SQLException JavaDoc, IOException JavaDoc, AuthorizeException
428     {
429         // Check authorisation
430
canEdit();
431
432         HistoryManager.saveHistory(ourContext, this, HistoryManager.MODIFY,
433                 ourContext.getCurrentUser(), ourContext.getExtraLogInfo());
434
435         log.info(LogManager.getHeader(ourContext, "update_community",
436                 "community_id=" + getID()));
437
438         DatabaseManager.update(ourContext, communityRow);
439
440         // now re-index this Community
441
DSIndexer.reIndexContent(ourContext, this);
442     }
443
444     /**
445      * Get the collections in this community. Throws an SQLException because
446      * creating a community object won't load in all collections.
447      *
448      * @return array of Collection objects
449      */

450     public Collection[] getCollections() throws SQLException JavaDoc
451     {
452         List JavaDoc collections = new ArrayList JavaDoc();
453
454         // Get the table rows
455
TableRowIterator tri = DatabaseManager.queryTable(
456             ourContext,"collection",
457             "SELECT collection.* FROM collection, community2collection WHERE " +
458             "community2collection.collection_id=collection.collection_id " +
459             "AND community2collection.community_id= ? ORDER BY collection.name",
460             getID());
461
462         // Make Collection objects
463
while (tri.hasNext())
464         {
465             TableRow row = tri.next();
466
467             // First check the cache
468
Collection fromCache = (Collection) ourContext.fromCache(
469                     Collection.class, row.getIntColumn("collection_id"));
470
471             if (fromCache != null)
472             {
473                 collections.add(fromCache);
474             }
475             else
476             {
477                 collections.add(new Collection(ourContext, row));
478             }
479         }
480         // close the TableRowIterator to free up resources
481
tri.close();
482
483         // Put them in an array
484
Collection[] collectionArray = new Collection[collections.size()];
485         collectionArray = (Collection[]) collections.toArray(collectionArray);
486
487         return collectionArray;
488     }
489
490     /**
491      * Get the immediate sub-communities of this community. Throws an
492      * SQLException because creating a community object won't load in all
493      * collections.
494      *
495      * @return array of Community objects
496      */

497     public Community[] getSubcommunities() throws SQLException JavaDoc
498     {
499         List JavaDoc subcommunities = new ArrayList JavaDoc();
500
501         // Get the table rows
502
TableRowIterator tri = DatabaseManager.queryTable(
503                 ourContext,"community",
504                 "SELECT community.* FROM community, community2community WHERE " +
505                 "community2community.child_comm_id=community.community_id " +
506                 "AND community2community.parent_comm_id= ? ORDER BY community.name",
507                 getID());
508         
509
510         // Make Community objects
511
while (tri.hasNext())
512         {
513             TableRow row = tri.next();
514
515             // First check the cache
516
Community fromCache = (Community) ourContext.fromCache(
517                     Community.class, row.getIntColumn("community_id"));
518
519             if (fromCache != null)
520             {
521                 subcommunities.add(fromCache);
522             }
523             else
524             {
525                 subcommunities.add(new Community(ourContext, row));
526             }
527         }
528         // close the TableRowIterator to free up resources
529
tri.close();
530
531         // Put them in an array
532
Community[] communityArray = new Community[subcommunities.size()];
533         communityArray = (Community[]) subcommunities.toArray(communityArray);
534
535         return communityArray;
536     }
537
538     /**
539      * Return the parent community of this community, or null if the community
540      * is top-level
541      *
542      * @return the immediate parent community, or null if top-level
543      */

544     public Community getParentCommunity() throws SQLException JavaDoc
545     {
546         Community parentCommunity = null;
547
548         // Get the table rows
549
TableRowIterator tri = DatabaseManager.queryTable(
550                 ourContext,"community",
551                 "SELECT community.* FROM community, community2community WHERE " +
552                 "community2community.parent_comm_id=community.community_id " +
553                 "AND community2community.child_comm_id= ? ",
554                 getID());
555         
556         // Make Community object
557
if (tri.hasNext())
558         {
559             TableRow row = tri.next();
560
561             // First check the cache
562
Community fromCache = (Community) ourContext.fromCache(
563                     Community.class, row.getIntColumn("community_id"));
564
565             if (fromCache != null)
566             {
567                 parentCommunity = fromCache;
568             }
569             else
570             {
571                 parentCommunity = new Community(ourContext, row);
572             }
573         }
574         // close the TableRowIterator to free up resources
575
tri.close();
576
577         return parentCommunity;
578     }
579
580     /**
581      * Return an array of parent communities of this community, in ascending
582      * order. If community is top-level, return an empty array.
583      *
584      * @return an array of parent communities, empty if top-level
585      */

586     public Community[] getAllParents() throws SQLException JavaDoc
587     {
588         List JavaDoc parentList = new ArrayList JavaDoc();
589         Community parent = getParentCommunity();
590
591         while (parent != null)
592         {
593             parentList.add(parent);
594             parent = parent.getParentCommunity();
595         }
596
597         // Put them in an array
598
Community[] communityArray = new Community[parentList.size()];
599         communityArray = (Community[]) parentList.toArray(communityArray);
600
601         return communityArray;
602     }
603
604     /**
605      * Create a new collection within this community. The collection is created
606      * without any workflow groups or default submitter group.
607      *
608      * @return the new collection
609      */

610     public Collection createCollection() throws SQLException JavaDoc,
611             AuthorizeException
612     {
613         // Check authorisation
614
AuthorizeManager.authorizeAction(ourContext, this, Constants.ADD);
615
616         Collection c = Collection.create(ourContext);
617         addCollection(c);
618
619         return c;
620     }
621
622     /**
623      * Add an exisiting collection to the community
624      *
625      * @param c
626      * collection to add
627      */

628     public void addCollection(Collection c) throws SQLException JavaDoc,
629             AuthorizeException
630     {
631         // Check authorisation
632
AuthorizeManager.authorizeAction(ourContext, this, Constants.ADD);
633
634         log.info(LogManager.getHeader(ourContext, "add_collection",
635                 "community_id=" + getID() + ",collection_id=" + c.getID()));
636
637         // Find out if mapping exists
638
TableRowIterator tri = DatabaseManager.queryTable(ourContext,
639                 "community2collection",
640                 "SELECT * FROM community2collection WHERE " +
641                 "community_id= ? AND collection_id= ? ",getID(),c.getID());
642         
643         if (!tri.hasNext())
644         {
645             // No existing mapping, so add one
646
TableRow mappingRow = DatabaseManager.create(ourContext,
647                     "community2collection");
648
649             mappingRow.setColumn("community_id", getID());
650             mappingRow.setColumn("collection_id", c.getID());
651
652             DatabaseManager.update(ourContext, mappingRow);
653         }
654         // close the TableRowIterator to free up resources
655
tri.close();
656     }
657
658     /**
659      * Create a new sub-community within this community.
660      *
661      * @return the new community
662      */

663     public Community createSubcommunity() throws SQLException JavaDoc,
664             AuthorizeException
665     {
666         // Check authorisation
667
AuthorizeManager.authorizeAction(ourContext, this, Constants.ADD);
668
669         Community c = create(this, ourContext);
670         addSubcommunity(c);
671
672         return c;
673     }
674
675     /**
676      * Add an exisiting community as a subcommunity to the community
677      *
678      * @param c
679      * subcommunity to add
680      */

681     public void addSubcommunity(Community c) throws SQLException JavaDoc,
682             AuthorizeException
683     {
684         // Check authorisation
685
AuthorizeManager.authorizeAction(ourContext, this, Constants.ADD);
686
687         log.info(LogManager.getHeader(ourContext, "add_subcommunity",
688                 "parent_comm_id=" + getID() + ",child_comm_id=" + c.getID()));
689
690         // Find out if mapping exists
691
TableRowIterator tri = DatabaseManager.queryTable(ourContext,
692                 "community2community",
693                 "SELECT * FROM community2community WHERE parent_comm_id= ? "+
694                 "AND child_comm_id= ? ",getID(), c.getID());
695         
696         if (!tri.hasNext())
697         {
698             // No existing mapping, so add one
699
TableRow mappingRow = DatabaseManager.create(ourContext,
700                     "community2community");
701
702             mappingRow.setColumn("parent_comm_id", getID());
703             mappingRow.setColumn("child_comm_id", c.getID());
704
705             DatabaseManager.update(ourContext, mappingRow);
706         }
707         // close the TableRowIterator to free up resources
708
tri.close();
709     }
710
711     /**
712      * Remove a collection. Any items then orphaned are deleted.
713      *
714      * @param c
715      * collection to remove
716      */

717     public void removeCollection(Collection c) throws SQLException JavaDoc,
718             AuthorizeException, IOException JavaDoc
719     {
720         // Check authorisation
721
AuthorizeManager.authorizeAction(ourContext, this, Constants.REMOVE);
722
723         log.info(LogManager.getHeader(ourContext, "remove_collection",
724                 "community_id=" + getID() + ",collection_id=" + c.getID()));
725
726         // Remove any mappings
727
DatabaseManager.updateQuery(ourContext,
728                 "DELETE FROM community2collection WHERE community_id= ? "+
729                 "AND collection_id= ? ", getID(), c.getID());
730
731         // Is the community an orphan?
732
TableRowIterator tri = DatabaseManager.query(ourContext,
733                 "SELECT * FROM community2collection WHERE collection_id= ? ",
734                 c.getID());
735
736         if (!tri.hasNext())
737         {
738             //make the right to remove the collection explicit because the
739
// implicit relation
740
//has been removed. This only has to concern the currentUser
741
// because
742
//he started the removal process and he will end it too.
743
//also add right to remove from the collection to remove it's
744
// items.
745
AuthorizeManager.addPolicy(ourContext, c, Constants.DELETE,
746                     ourContext.getCurrentUser());
747             AuthorizeManager.addPolicy(ourContext, c, Constants.REMOVE,
748                     ourContext.getCurrentUser());
749
750             // Orphan; delete it
751
c.delete();
752         }
753         // close the TableRowIterator to free up resources
754
tri.close();
755     }
756
757     /**
758      * Remove a subcommunity. Any substructure then orphaned is deleted.
759      *
760      * @param c
761      * subcommunity to remove
762      */

763     public void removeSubcommunity(Community c) throws SQLException JavaDoc,
764             AuthorizeException, IOException JavaDoc
765     {
766         // Check authorisation
767
AuthorizeManager.authorizeAction(ourContext, this, Constants.REMOVE);
768
769         log.info(LogManager.getHeader(ourContext, "remove_subcommunity",
770                 "parent_comm_id=" + getID() + ",child_comm_id=" + c.getID()));
771
772         // Remove any mappings
773
DatabaseManager.updateQuery(ourContext,
774                 "DELETE FROM community2community WHERE parent_comm_id= ? " +
775                 " AND child_comm_id= ? ", getID(),c.getID());
776
777         // Is the subcommunity an orphan?
778
TableRowIterator tri = DatabaseManager.query(ourContext,
779                 "SELECT * FROM community2community WHERE child_comm_id= ? ",
780                 c.getID());
781
782         if (!tri.hasNext())
783         {
784             //make the right to remove the sub explicit because the implicit
785
// relation
786
//has been removed. This only has to concern the currentUser
787
// because
788
//he started the removal process and he will end it too.
789
//also add right to remove from the subcommunity to remove it's
790
// children.
791
AuthorizeManager.addPolicy(ourContext, c, Constants.DELETE,
792                     ourContext.getCurrentUser());
793             AuthorizeManager.addPolicy(ourContext, c, Constants.REMOVE,
794                     ourContext.getCurrentUser());
795
796             // Orphan; delete it
797
c.delete();
798         }
799         // close the TableRowIterator to free up resources
800
tri.close();
801     }
802
803     /**
804      * Delete the community, including the metadata and logo. Collections and
805      * subcommunities that are then orphans are deleted.
806      */

807     public void delete() throws SQLException JavaDoc, AuthorizeException, IOException JavaDoc
808     {
809         // Check authorisation
810
// FIXME: If this was a subcommunity, it is first removed from it's
811
// parent.
812
// This means the parentCommunity == null
813
// But since this is also the case for top-level communities, we would
814
// give everyone rights to remove the top-level communities.
815
// The same problem occurs in removing the logo
816
if (!AuthorizeManager.authorizeActionBoolean(ourContext,
817                 getParentCommunity(), Constants.REMOVE))
818         {
819             AuthorizeManager
820                     .authorizeAction(ourContext, this, Constants.DELETE);
821         }
822
823         // If not a top-level community, have parent remove me; this
824
// will call delete() after removing the linkage
825
Community parent = getParentCommunity();
826
827         if (parent != null)
828         {
829             parent.removeSubcommunity(this);
830
831             return;
832         }
833
834         log.info(LogManager.getHeader(ourContext, "delete_community",
835                 "community_id=" + getID()));
836
837         // remove from the search index
838
DSIndexer.unIndexContent(ourContext, this);
839
840         HistoryManager.saveHistory(ourContext, this, HistoryManager.REMOVE,
841                 ourContext.getCurrentUser(), ourContext.getExtraLogInfo());
842
843         // Remove from cache
844
ourContext.removeCached(this, getID());
845
846         // Remove collections
847
Collection[] cols = getCollections();
848
849         for (int i = 0; i < cols.length; i++)
850         {
851             removeCollection(cols[i]);
852         }
853
854         // Remove subcommunities
855
Community[] comms = getSubcommunities();
856
857         for (int j = 0; j < comms.length; j++)
858         {
859             removeSubcommunity(comms[j]);
860         }
861
862         // Remove the logo
863
setLogo(null);
864
865         // Remove all authorization policies
866
AuthorizeManager.removeAllPolicies(ourContext, this);
867
868         // Delete community row
869
DatabaseManager.delete(ourContext, communityRow);
870     }
871
872     /**
873      * Return <code>true</code> if <code>other</code> is the same Community
874      * as this object, <code>false</code> otherwise
875      *
876      * @param other
877      * object to compare to
878      *
879      * @return <code>true</code> if object passed in represents the same
880      * community as this object
881      */

882     public boolean equals(Object JavaDoc other)
883     {
884         if (!(other instanceof Community))
885         {
886             return false;
887         }
888
889         return (getID() == ((Community) other).getID());
890     }
891
892     /**
893      * return type found in Constants
894      */

895     public int getType()
896     {
897         return Constants.COMMUNITY;
898     }
899
900     /**
901      * return TRUE if context's user can edit community, false otherwise
902      *
903      * @return boolean true = current user can edit community
904      */

905     public boolean canEditBoolean() throws java.sql.SQLException JavaDoc
906     {
907         try
908         {
909             canEdit();
910
911             return true;
912         }
913         catch (AuthorizeException e)
914         {
915             return false;
916         }
917     }
918
919     public void canEdit() throws AuthorizeException, SQLException JavaDoc
920     {
921         Community[] parents = getAllParents();
922
923         for (int i = 0; i < parents.length; i++)
924         {
925             if (AuthorizeManager.authorizeActionBoolean(ourContext, parents[i],
926                     Constants.WRITE))
927             {
928                 return;
929             }
930
931             if (AuthorizeManager.authorizeActionBoolean(ourContext, parents[i],
932                     Constants.ADD))
933             {
934                 return;
935             }
936         }
937
938         AuthorizeManager.authorizeAction(ourContext, this, Constants.WRITE);
939     }
940
941     /**
942      * counts items in this community
943      *
944      * @return total items
945      */

946     public int countItems() throws SQLException JavaDoc
947     {
948         int total = 0;
949         // add collection counts
950
Collection[] cols = getCollections();
951         for ( int i = 0; i < cols.length; i++)
952         {
953             total += cols[i].countItems();
954         }
955         // add sub-community counts
956
Community[] comms = getSubcommunities();
957         for ( int j = 0; j < comms.length; j++ )
958         {
959             total += comms[j].countItems();
960         }
961         return total;
962     }
963 }
964
Popular Tags