1 40 package org.dspace.content; 41 42 import java.io.IOException ; 43 import java.io.InputStream ; 44 import java.sql.SQLException ; 45 import java.util.ArrayList ; 46 import java.util.List ; 47 import java.util.MissingResourceException ; 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 75 public class Community extends DSpaceObject 76 { 77 78 private static Logger log = Logger.getLogger(Community.class); 79 80 81 private Context ourContext; 82 83 84 private TableRow communityRow; 85 86 87 private Bitstream logo; 88 89 90 private String handle; 91 92 100 Community(Context context, TableRow row) throws SQLException 101 { 102 ourContext = context; 103 communityRow = row; 104 105 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 handle = HandleManager.findHandle(context, this); 118 119 context.cache(this, row.getIntColumn("community_id")); 121 } 122 123 133 public static Community find(Context context, int id) throws SQLException 134 { 135 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 176 public static Community create(Community parent, Context context) 177 throws SQLException , AuthorizeException 178 { 179 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 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 220 public static Community[] findAll(Context context) throws SQLException 221 { 222 TableRowIterator tri = DatabaseManager.queryTable(context, "community", 223 "SELECT * FROM community ORDER BY name"); 224 225 List communities = new ArrayList (); 226 227 while (tri.hasNext()) 228 { 229 TableRow row = tri.next(); 230 231 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 tri.close(); 246 247 Community[] communityArray = new Community[communities.size()]; 248 communityArray = (Community[]) communities.toArray(communityArray); 249 250 return communityArray; 251 } 252 253 263 public static Community[] findAllTop(Context context) throws SQLException 264 { 265 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 topCommunities = new ArrayList (); 272 273 while (tri.hasNext()) 274 { 275 TableRow row = tri.next(); 276 277 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 tri.close(); 292 293 Community[] communityArray = new Community[topCommunities.size()]; 294 communityArray = (Community[]) topCommunities.toArray(communityArray); 295 296 return communityArray; 297 } 298 299 304 public int getID() 305 { 306 return communityRow.getIntColumn("community_id"); 307 } 308 309 public String getHandle() 310 { 311 return handle; 312 } 313 314 325 public String getMetadata(String field) 326 { 327 return communityRow.getStringColumn(field); 328 } 329 330 342 public void setMetadata(String field, String value)throws MissingResourceException 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 e) 351 { 352 value = "Untitled"; 353 } 354 } 355 communityRow.setColumn(field, value); 356 } 357 358 364 public Bitstream getLogo() 365 { 366 return logo; 367 } 368 369 382 public Bitstream setLogo(InputStream is) throws AuthorizeException, 383 IOException , SQLException 384 { 385 if (!((is == null) && AuthorizeManager.authorizeActionBoolean( 389 ourContext, this, Constants.DELETE))) 390 { 391 canEdit(); 392 } 393 394 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 List 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 427 public void update() throws SQLException , IOException , AuthorizeException 428 { 429 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 DSIndexer.reIndexContent(ourContext, this); 442 } 443 444 450 public Collection[] getCollections() throws SQLException 451 { 452 List collections = new ArrayList (); 453 454 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 while (tri.hasNext()) 464 { 465 TableRow row = tri.next(); 466 467 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 tri.close(); 482 483 Collection[] collectionArray = new Collection[collections.size()]; 485 collectionArray = (Collection[]) collections.toArray(collectionArray); 486 487 return collectionArray; 488 } 489 490 497 public Community[] getSubcommunities() throws SQLException 498 { 499 List subcommunities = new ArrayList (); 500 501 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 while (tri.hasNext()) 512 { 513 TableRow row = tri.next(); 514 515 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 tri.close(); 530 531 Community[] communityArray = new Community[subcommunities.size()]; 533 communityArray = (Community[]) subcommunities.toArray(communityArray); 534 535 return communityArray; 536 } 537 538 544 public Community getParentCommunity() throws SQLException 545 { 546 Community parentCommunity = null; 547 548 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 if (tri.hasNext()) 558 { 559 TableRow row = tri.next(); 560 561 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 tri.close(); 576 577 return parentCommunity; 578 } 579 580 586 public Community[] getAllParents() throws SQLException 587 { 588 List parentList = new ArrayList (); 589 Community parent = getParentCommunity(); 590 591 while (parent != null) 592 { 593 parentList.add(parent); 594 parent = parent.getParentCommunity(); 595 } 596 597 Community[] communityArray = new Community[parentList.size()]; 599 communityArray = (Community[]) parentList.toArray(communityArray); 600 601 return communityArray; 602 } 603 604 610 public Collection createCollection() throws SQLException , 611 AuthorizeException 612 { 613 AuthorizeManager.authorizeAction(ourContext, this, Constants.ADD); 615 616 Collection c = Collection.create(ourContext); 617 addCollection(c); 618 619 return c; 620 } 621 622 628 public void addCollection(Collection c) throws SQLException , 629 AuthorizeException 630 { 631 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 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 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 tri.close(); 656 } 657 658 663 public Community createSubcommunity() throws SQLException , 664 AuthorizeException 665 { 666 AuthorizeManager.authorizeAction(ourContext, this, Constants.ADD); 668 669 Community c = create(this, ourContext); 670 addSubcommunity(c); 671 672 return c; 673 } 674 675 681 public void addSubcommunity(Community c) throws SQLException , 682 AuthorizeException 683 { 684 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 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 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 tri.close(); 709 } 710 711 717 public void removeCollection(Collection c) throws SQLException , 718 AuthorizeException, IOException 719 { 720 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 DatabaseManager.updateQuery(ourContext, 728 "DELETE FROM community2collection WHERE community_id= ? "+ 729 "AND collection_id= ? ", getID(), c.getID()); 730 731 TableRowIterator tri = DatabaseManager.query(ourContext, 733 "SELECT * FROM community2collection WHERE collection_id= ? ", 734 c.getID()); 735 736 if (!tri.hasNext()) 737 { 738 AuthorizeManager.addPolicy(ourContext, c, Constants.DELETE, 746 ourContext.getCurrentUser()); 747 AuthorizeManager.addPolicy(ourContext, c, Constants.REMOVE, 748 ourContext.getCurrentUser()); 749 750 c.delete(); 752 } 753 tri.close(); 755 } 756 757 763 public void removeSubcommunity(Community c) throws SQLException , 764 AuthorizeException, IOException 765 { 766 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 DatabaseManager.updateQuery(ourContext, 774 "DELETE FROM community2community WHERE parent_comm_id= ? " + 775 " AND child_comm_id= ? ", getID(),c.getID()); 776 777 TableRowIterator tri = DatabaseManager.query(ourContext, 779 "SELECT * FROM community2community WHERE child_comm_id= ? ", 780 c.getID()); 781 782 if (!tri.hasNext()) 783 { 784 AuthorizeManager.addPolicy(ourContext, c, Constants.DELETE, 792 ourContext.getCurrentUser()); 793 AuthorizeManager.addPolicy(ourContext, c, Constants.REMOVE, 794 ourContext.getCurrentUser()); 795 796 c.delete(); 798 } 799 tri.close(); 801 } 802 803 807 public void delete() throws SQLException , AuthorizeException, IOException 808 { 809 if (!AuthorizeManager.authorizeActionBoolean(ourContext, 817 getParentCommunity(), Constants.REMOVE)) 818 { 819 AuthorizeManager 820 .authorizeAction(ourContext, this, Constants.DELETE); 821 } 822 823 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 DSIndexer.unIndexContent(ourContext, this); 839 840 HistoryManager.saveHistory(ourContext, this, HistoryManager.REMOVE, 841 ourContext.getCurrentUser(), ourContext.getExtraLogInfo()); 842 843 ourContext.removeCached(this, getID()); 845 846 Collection[] cols = getCollections(); 848 849 for (int i = 0; i < cols.length; i++) 850 { 851 removeCollection(cols[i]); 852 } 853 854 Community[] comms = getSubcommunities(); 856 857 for (int j = 0; j < comms.length; j++) 858 { 859 removeSubcommunity(comms[j]); 860 } 861 862 setLogo(null); 864 865 AuthorizeManager.removeAllPolicies(ourContext, this); 867 868 DatabaseManager.delete(ourContext, communityRow); 870 } 871 872 882 public boolean equals(Object other) 883 { 884 if (!(other instanceof Community)) 885 { 886 return false; 887 } 888 889 return (getID() == ((Community) other).getID()); 890 } 891 892 895 public int getType() 896 { 897 return Constants.COMMUNITY; 898 } 899 900 905 public boolean canEditBoolean() throws java.sql.SQLException 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 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 946 public int countItems() throws SQLException 947 { 948 int total = 0; 949 Collection[] cols = getCollections(); 951 for ( int i = 0; i < cols.length; i++) 952 { 953 total += cols[i].countItems(); 954 } 955 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 |