1 5 package org.exoplatform.portlets.user.component; 6 7 import java.text.SimpleDateFormat ; 8 import org.exoplatform.faces.core.component.UIGrid; 9 import org.exoplatform.faces.core.component.UIPageListIterator; 10 import org.exoplatform.faces.core.component.model.*; 11 import org.exoplatform.faces.core.event.CheckRoleInterceptor; 12 import org.exoplatform.faces.core.event.ExoActionEvent; 13 import org.exoplatform.faces.core.event.ExoActionListener; 14 import org.exoplatform.services.organization.OrganizationService; 15 import org.exoplatform.services.organization.Query; 16 import org.exoplatform.services.organization.User; 17 23 public class UIListUser extends UIGrid { 24 private static SimpleDateFormat ft_ = new SimpleDateFormat ("dd/MM/yyyy HH:mm:ss") ; 25 26 static Parameter[] VIEW_PROFILE = { new Parameter(ACTION , "viewProfile") } ; 27 static Parameter[] VIEW_INFO = { new Parameter(ACTION , "viewUserInfo") } ; 28 static Parameter[] DELETE = { new Parameter(ACTION , DELETE_ACTION) } ; 29 30 private UIPageListIterator uiPageIterator_ ; 31 private boolean adminRole_ ; 32 private OrganizationService service_ ; 33 private Query lastQuery_ ; 34 35 public UIListUser(OrganizationService service) throws Exception { 36 setId("UIListUser") ; 37 service_ = service ; 38 uiPageIterator_ = new UIPageListIterator(new UserDataHandler()) ; 39 adminRole_ = hasRole("admin") ; 40 add(new Rows(uiPageIterator_.getPageListDataHandler(), "even", "odd"). 41 add(new Column("#{UIListUser.header.user-name}", "userName")). 42 add(new Column("#{UIListUser.header.first-name}", "firstName")). 43 add(new Column("#{UIListUser.header.last-name}", "lastName")). 44 add(new Column("#{UIListUser.header.email}", "email")). 45 add(new Column("#{UIListUser.header.last-login}", "lastLogin")). 46 add(new ActionColumn("#{UIListUser.header.action}", "userName"). 47 add(new Button("#{UIListUser.button.view-profile}", VIEW_PROFILE)). 48 add(adminRole_, new Button("#{UIListUser.button.view}", VIEW_INFO)). 49 add(adminRole_, new Button("#{UIListUser.button.delete}", DELETE)). 50 setCellClass("action-column"))) ; 51 add(new Row(). 52 add(new ComponentCell(this, uiPageIterator_). 53 addColspan("6").addStyle("text-align: right"))); 54 55 addActionListener(DeleteUserActionListener.class, DELETE_ACTION) ; 56 addActionListener(ViewProfileActionListener.class, "viewProfile") ; 57 addActionListener(ViewUserInfoActionListener.class, "viewUserInfo") ; 58 search(new Query()) ; 59 } 60 61 62 public void search(Query query) throws Exception { 63 lastQuery_ = query ; 64 uiPageIterator_.setPageList(service_.findUsers(query)) ; 65 } 66 67 public void refresh() throws Exception { 68 uiPageIterator_.setPageList(service_.findUsers(lastQuery_)) ; 69 } 70 71 public boolean hasAdminRole() { return adminRole_ ; } 72 73 static public class UserDataHandler extends PageListDataHandler { 74 private User user_ ; 75 76 public String getData(String fieldName) { 77 if("userName".equals(fieldName)) return user_.getUserName() ; 78 if("lastName".equals(fieldName)) return user_.getLastName() ; 79 if("firstName".equals(fieldName)) return user_.getFirstName() ; 80 if("email".equals(fieldName)) return user_.getEmail() ; 81 if("lastLogin".equals(fieldName)) return ft_.format(user_.getLastLoginTime()); 82 return null ; 83 } 84 85 public void setCurrentObject(Object o) { user_ = (User) o; } 86 } 87 88 static public class ViewUserInfoActionListener extends ExoActionListener { 89 public ViewUserInfoActionListener() { 90 addInterceptor(new CheckRoleInterceptor("admin")) ; 91 } 92 93 public void execute(ExoActionEvent event) throws Exception { 94 UIListUser uiListUser = (UIListUser) event.getComponent() ; 95 String userName = event.getParameter(OBJECTID) ; 96 UIUserInfo uiUserInfo = 97 (UIUserInfo)uiListUser.getSibling(UIUserInfo.class) ; 98 uiUserInfo.changeUser(userName) ; 99 uiListUser.setRenderedSibling(UIUserInfo.class) ; 100 } 101 } 102 103 static public class ViewProfileActionListener extends ExoActionListener { 104 public void execute(ExoActionEvent event) throws Exception { 105 UIListUser uiListUser = (UIListUser) event.getComponent() ; 106 String userName = event.getParameter(OBJECTID); 107 UIUserProfileSummary uiUserProfile = 108 (UIUserProfileSummary)uiListUser.getSibling(UIUserProfileSummary.class) ; 109 uiUserProfile.setUserProfile(userName) ; 110 uiListUser.setRenderedSibling(UIUserProfileSummary.class) ; 111 } 112 } 113 114 static public class DeleteUserActionListener extends ExoActionListener { 115 public DeleteUserActionListener() { 116 addInterceptor(new CheckRoleInterceptor("admin")) ; 117 } 118 119 public void execute(ExoActionEvent event) throws Exception { 120 UIListUser uiListUser = (UIListUser) event.getComponent() ; 121 String userName = event.getParameter(OBJECTID) ; 122 uiListUser.service_.removeUser(userName) ; 123 uiListUser.refresh() ; 124 } 125 } 126 } | Popular Tags |