1 40 package org.dspace.eperson; 41 42 import java.sql.SQLException ; 43 import java.util.ArrayList ; 44 import java.util.List ; 45 import java.util.Vector ; 46 47 import org.apache.log4j.Logger; 48 import org.dspace.authorize.AuthorizeException; 49 import org.dspace.authorize.AuthorizeManager; 50 import org.dspace.content.DSpaceObject; 51 import org.dspace.core.ConfigurationManager; 52 import org.dspace.core.Constants; 53 import org.dspace.core.Context; 54 import org.dspace.core.LogManager; 55 import org.dspace.core.Utils; 56 import org.dspace.history.HistoryManager; 57 import org.dspace.storage.rdbms.DatabaseManager; 58 import org.dspace.storage.rdbms.TableRow; 59 import org.dspace.storage.rdbms.TableRowIterator; 60 61 67 public class EPerson extends DSpaceObject 68 { 69 70 public static final int EMAIL = 1; 71 72 73 public static final int LASTNAME = 2; 74 75 76 public static final int ID = 3; 77 78 79 public static final int NETID = 4; 80 81 82 private static Logger log = Logger.getLogger(EPerson.class); 83 84 85 private Context myContext; 86 87 88 private TableRow myRow; 89 90 98 EPerson(Context context, TableRow row) 99 { 100 myContext = context; 101 myRow = row; 102 103 context.cache(this, row.getIntColumn("eperson_id")); 105 } 106 107 117 public static EPerson find(Context context, int id) throws SQLException 118 { 119 EPerson fromCache = (EPerson) context.fromCache(EPerson.class, id); 121 122 if (fromCache != null) 123 { 124 return fromCache; 125 } 126 127 TableRow row = DatabaseManager.find(context, "eperson", id); 128 129 if (row == null) 130 { 131 return null; 132 } 133 else 134 { 135 return new EPerson(context, row); 136 } 137 } 138 139 144 public static EPerson findByEmail(Context context, String email) 145 throws SQLException , AuthorizeException 146 { 147 TableRow row = DatabaseManager.findByUnique(context, "eperson", 148 "email", email); 149 150 if (row == null) 151 { 152 return null; 153 } 154 else 155 { 156 EPerson fromCache = (EPerson) context.fromCache(EPerson.class, row 158 .getIntColumn("eperson_id")); 159 160 if (fromCache != null) 161 { 162 return fromCache; 163 } 164 else 165 { 166 return new EPerson(context, row); 167 } 168 } 169 } 170 171 181 public static EPerson findByNetid(Context context, String netid) 182 throws SQLException 183 { 184 if (netid == null) 185 return null; 186 187 TableRow row = DatabaseManager.findByUnique(context, "eperson", 188 "netid", netid); 189 190 if (row == null) 191 { 192 return null; 193 } 194 else 195 { 196 EPerson fromCache = (EPerson) context.fromCache(EPerson.class, row 198 .getIntColumn("eperson_id")); 199 200 if (fromCache != null) 201 { 202 return fromCache; 203 } 204 else 205 { 206 return new EPerson(context, row); 207 } 208 } 209 } 210 211 221 public static EPerson[] search(Context context, String query) 222 throws SQLException 223 { 224 return search(context, query, -1, -1); 225 } 226 227 242 public static EPerson[] search(Context context, String query, int offset, int limit) 243 throws SQLException 244 { 245 String params = "%"+query.toLowerCase()+"%"; 246 String dbquery = "SELECT * FROM eperson WHERE eperson_id = ? OR " + 247 "firstname ILIKE ? OR lastname ILIKE ? OR email ILIKE ? ORDER BY lastname, firstname ASC "; 248 249 if (offset >= 0 && limit >0) { 250 dbquery += "LIMIT " + limit + " OFFSET " + offset; 251 } 252 253 Integer int_param; 255 try { 256 int_param = Integer.valueOf(query); 257 } 258 catch (NumberFormatException e) { 259 int_param = new Integer (-1); 260 } 261 262 TableRowIterator rows = DatabaseManager.query(context, dbquery, new Object [] {int_param,params,params,params}); 264 265 List epeopleRows = rows.toList(); 266 EPerson[] epeople = new EPerson[epeopleRows.size()]; 267 268 for (int i = 0; i < epeopleRows.size(); i++) 269 { 270 TableRow row = (TableRow) epeopleRows.get(i); 271 272 EPerson fromCache = (EPerson) context.fromCache(EPerson.class, row 274 .getIntColumn("eperson_id")); 275 276 if (fromCache != null) 277 { 278 epeople[i] = fromCache; 279 } 280 else 281 { 282 epeople[i] = new EPerson(context, row); 283 } 284 } 285 286 return epeople; 287 } 288 289 300 public static int searchResultCount(Context context, String query) 301 throws SQLException 302 { 303 String dbquery = "%"+query.toLowerCase()+"%"; 304 Long count; 305 306 Integer int_param; 308 try { 309 int_param = Integer.valueOf(query); 310 } 311 catch (NumberFormatException e) { 312 int_param = new Integer (-1); 313 } 314 315 TableRow row = DatabaseManager.querySingle(context, 317 "SELECT count(*) as count FROM eperson WHERE eperson_id = ? OR " + 318 "firstname ILIKE ? OR lastname ILIKE ? OR email ILIKE ?", 319 new Object [] {int_param,dbquery,dbquery,dbquery}); 320 321 if ("oracle".equals(ConfigurationManager.getProperty("db.name"))) 323 { 324 count = new Long (row.getIntColumn("count")); 325 } 326 else { 328 count = new Long (row.getLongColumn("count")); 329 } 330 331 return count.intValue(); 332 } 333 334 335 336 347 public static EPerson[] findAll(Context context, int sortField) 348 throws SQLException 349 { 350 String s; 351 352 switch (sortField) 353 { 354 case ID: 355 s = "eperson_id"; 356 break; 357 358 case EMAIL: 359 s = "email"; 360 break; 361 362 case NETID: 363 s = "netid"; 364 break; 365 366 default: 367 s = "lastname"; 368 } 369 370 TableRowIterator rows = DatabaseManager.query(context, 373 "SELECT * FROM eperson ORDER BY "+s); 374 375 List epeopleRows = rows.toList(); 376 377 EPerson[] epeople = new EPerson[epeopleRows.size()]; 378 379 for (int i = 0; i < epeopleRows.size(); i++) 380 { 381 TableRow row = (TableRow) epeopleRows.get(i); 382 383 EPerson fromCache = (EPerson) context.fromCache(EPerson.class, row 385 .getIntColumn("eperson_id")); 386 387 if (fromCache != null) 388 { 389 epeople[i] = fromCache; 390 } 391 else 392 { 393 epeople[i] = new EPerson(context, row); 394 } 395 } 396 397 return epeople; 398 } 399 400 406 public static EPerson create(Context context) throws SQLException , 407 AuthorizeException 408 { 409 if (!AuthorizeManager.isAdmin(context)) 411 { 412 throw new AuthorizeException( 413 "You must be an admin to create an EPerson"); 414 } 415 416 TableRow row = DatabaseManager.create(context, "eperson"); 418 419 EPerson e = new EPerson(context, row); 420 421 log.info(LogManager.getHeader(context, "create_eperson", "eperson_id=" 422 + e.getID())); 423 424 HistoryManager.saveHistory(context, e, HistoryManager.REMOVE, context 425 .getCurrentUser(), context.getExtraLogInfo()); 426 427 return e; 428 } 429 430 434 public void delete() throws SQLException , AuthorizeException, 435 EPersonDeletionException 436 { 437 if (!AuthorizeManager.isAdmin(myContext)) 439 { 440 throw new AuthorizeException( 441 "You must be an admin to delete an EPerson"); 442 } 443 444 HistoryManager.saveHistory(myContext, this, HistoryManager.REMOVE, 445 myContext.getCurrentUser(), myContext.getExtraLogInfo()); 446 447 Vector constraintList = getDeleteConstraints(); 450 451 if (constraintList.size() > 0) 454 { 455 throw new EPersonDeletionException(constraintList); 456 } 457 458 myContext.removeCached(this, getID()); 460 461 DatabaseManager.updateQuery(myContext, 463 "DELETE FROM EPersonGroup2EPerson WHERE eperson_id= ? ", 464 getID()); 465 466 DatabaseManager.updateQuery(myContext, 468 "DELETE FROM subscription WHERE eperson_id= ? ", 469 getID()); 470 471 DatabaseManager.delete(myContext, myRow); 473 474 log.info(LogManager.getHeader(myContext, "delete_eperson", 475 "eperson_id=" + getID())); 476 } 477 478 483 public int getID() 484 { 485 return myRow.getIntColumn("eperson_id"); 486 } 487 488 public String getHandle() 489 { 490 return null; 492 } 493 494 499 public String getEmail() 500 { 501 return myRow.getStringColumn("email"); 502 } 503 504 510 public void setEmail(String s) 511 { 512 if (s != null) 513 { 514 s = s.toLowerCase(); 515 } 516 517 myRow.setColumn("email", s); 518 } 519 520 525 public String getNetid() 526 { 527 return myRow.getStringColumn("netid"); 528 } 529 530 536 public void setNetid(String s) 537 { 538 if (s != null) 539 { 540 s = s.toLowerCase(); 541 } 542 543 myRow.setColumn("netid", s); 544 } 545 546 552 public String getFullName() 553 { 554 String f = myRow.getStringColumn("firstname"); 555 String l = myRow.getStringColumn("lastname"); 556 557 if ((l == null) && (f == null)) 558 { 559 return getEmail(); 560 } 561 else if (f == null) 562 { 563 return l; 564 } 565 else 566 { 567 return (f + " " + l); 568 } 569 } 570 571 576 public String getFirstName() 577 { 578 return myRow.getStringColumn("firstname"); 579 } 580 581 587 public void setFirstName(String firstname) 588 { 589 myRow.setColumn("firstname", firstname); 590 } 591 592 597 public String getLastName() 598 { 599 return myRow.getStringColumn("lastname"); 600 } 601 602 608 public void setLastName(String lastname) 609 { 610 myRow.setColumn("lastname", lastname); 611 } 612 613 619 public void setCanLogIn(boolean login) 620 { 621 myRow.setColumn("can_log_in", login); 622 } 623 624 629 public boolean canLogIn() 630 { 631 return myRow.getBooleanColumn("can_log_in"); 632 } 633 634 640 public void setRequireCertificate(boolean isrequired) 641 { 642 myRow.setColumn("require_certificate", isrequired); 643 } 644 645 650 public boolean getRequireCertificate() 651 { 652 return myRow.getBooleanColumn("require_certificate"); 653 } 654 655 661 public void setSelfRegistered(boolean sr) 662 { 663 myRow.setColumn("self_registered", sr); 664 } 665 666 671 public boolean getSelfRegistered() 672 { 673 return myRow.getBooleanColumn("self_registered"); 674 } 675 676 687 public String getMetadata(String field) 688 { 689 return myRow.getStringColumn(field); 690 } 691 692 703 public void setMetadata(String field, String value) 704 { 705 myRow.setColumn(field, value); 706 } 707 708 714 public void setPassword(String s) 715 { 716 String encoded = Utils.getMD5(s); 718 719 myRow.setColumn("password", encoded); 720 } 721 722 729 public boolean checkPassword(String attempt) 730 { 731 String encoded = Utils.getMD5(attempt); 732 733 return (encoded.equals(myRow.getStringColumn("password"))); 734 } 735 736 739 public void update() throws SQLException , AuthorizeException 740 { 741 if (!myContext.ignoreAuthorization() 744 && ((myContext.getCurrentUser() == null) || (getID() != myContext 745 .getCurrentUser().getID()))) 746 { 747 AuthorizeManager.authorizeAction(myContext, this, Constants.WRITE); 748 } 749 750 DatabaseManager.update(myContext, myRow); 751 752 log.info(LogManager.getHeader(myContext, "update_eperson", 753 "eperson_id=" + getID())); 754 755 HistoryManager.saveHistory(myContext, this, HistoryManager.MODIFY, 756 myContext.getCurrentUser(), myContext.getExtraLogInfo()); 757 } 758 759 769 public boolean obsolete_equals(Object other) 770 { 771 if (!(other instanceof EPerson)) 772 { 773 return false; 774 } 775 776 return (getID() == ((EPerson) other).getID()); 777 } 778 779 782 public int getType() 783 { 784 return Constants.EPERSON; 785 } 786 787 797 public Vector getDeleteConstraints() throws SQLException 798 { 799 Vector tableList = new Vector (); 800 801 TableRowIterator tri = DatabaseManager.query(myContext, 803 "SELECT * from item where submitter_id= ? ", 804 getID()); 805 806 if (tri.hasNext()) 807 { 808 tableList.add("item"); 809 } 810 811 tri.close(); 812 813 tri = DatabaseManager.query(myContext, 815 "SELECT * from workflowitem where owner= ? ", 816 getID()); 817 818 if (tri.hasNext()) 819 { 820 tableList.add("workflowitem"); 821 } 822 823 tri.close(); 824 825 tri = DatabaseManager.query(myContext, 827 "SELECT * from tasklistitem where eperson_id= ? ", 828 getID()); 829 830 if (tri.hasNext()) 831 { 832 tableList.add("tasklistitem"); 833 } 834 835 tri.close(); 836 837 return tableList; 840 } 841 842 848 public Group[] getGroupMemberships() throws SQLException 849 { 850 Group[] specialGroups = myContext.getSpecialGroups(); 852 853 List groupList = new ArrayList (); 854 855 for (int i = 0; i < specialGroups.length; i++) 856 { 857 groupList.add(specialGroups[i]); 858 } 859 860 Group[] myGroups = Group.allMemberGroups(myContext, this); 862 863 for (int i = 0; i < myGroups.length; i++) 864 { 865 groupList.add(myGroups[i]); 866 } 867 868 return (Group[]) groupList.toArray(new Group[0]); 869 } 870 } 871 | Popular Tags |