001: package org.emforge.projectmanager.web.bean;
002:
003: import java.util.Collection;
004: import java.util.List;
005:
006: import javax.faces.validator.ValidatorException;
007:
008: import org.apache.commons.lang.ArrayUtils;
009: import org.apache.commons.lang.StringUtils;
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012: import org.emforge.projectmanager.ProjectService;
013: import org.emforge.projectmanager.base.ProjectDO;
014: import org.emforge.projectmanager.base.ProjectRole;
015: import org.emforge.projectmanager.web.bean.ProjectController;
016:
017: import ru.emdev.EmForge.security.EmForgeUserDetails;
018: import ru.emdev.EmForge.security.UserFactory;
019: import ru.emdev.EmForge.security.dao.Role;
020: import ru.emdev.EmForge.security.dao.UserDao;
021: import ru.emdev.EmForge.security.web.validator.RoleValidator;
022: import ru.emdev.EmForge.wiki.web.bean.Crumb;
023:
024: /**
025: * Roles Controller is used for processing project roles
026: *
027: * @author akakunin
028: */
029: public class RolesController extends ProjectController {
030:
031: protected final Log logger = LogFactory.getLog(getClass());
032:
033: public static final String PROJECT_ROLE_ATTR = "role";
034: public static final String NEW_ROLE_ATTR = "new";
035: public static final String ADD_BUTTON_ATTR = "projectRolesForm:addbutton";
036: public static final String SAVE_BUTTON_ATTR = "projectRolesForm:saveButton";
037: public static final String DELETE_BUTTON_ATTR = "projectRolesForm:deleteButton";
038:
039: protected UserDao m_userDao;
040: protected UserFactory m_userFactory;
041:
042: // processed objects
043: protected Role m_role;
044: protected ProjectRole m_projectRole;
045:
046: /**
047: * Should we display form for new role
048: */
049: protected boolean m_showNewRoleForm = false;
050:
051: // role data:
052: protected String m_roleName;
053: protected String m_displayName;
054: protected String m_email;
055: protected String[] m_assignedUsers;
056:
057: /**
058: * @return
059: */
060: public UserFactory getUserFactory() {
061:
062: return m_userFactory;
063: }
064:
065: /**
066: * @param factory
067: */
068: public void setUserFactory(UserFactory factory) {
069:
070: m_userFactory = factory;
071: }
072:
073: /**
074: * @param i_userDao
075: */
076: public void setUserDao(UserDao i_userDao) {
077:
078: m_userDao = i_userDao;
079: }
080:
081: /**
082: * @see org.emforge.projectmanager.web.bean.ProjectController#init()
083: */
084: @Override
085: protected void init() {
086:
087: super .init();
088: // get information about role
089: String roleName = null;
090:
091: roleName = getParam(PROJECT_ROLE_ATTR);
092:
093: setCurrentRoleName(roleName);
094:
095: if (roleName == null) {
096: m_showNewRoleForm = Boolean
097: .parseBoolean(getParam(NEW_ROLE_ATTR));
098: }
099: }
100:
101: /**
102: * @see org.emforge.projectmanager.web.bean.ProjectController#getTitleImpl()
103: */
104: public String getTitleImpl() {
105:
106: return "Project Roles";
107: }
108:
109: /**
110: * @see org.emforge.projectmanager.web.bean.ProjectController#getTrailCrumbInfo()
111: */
112: @Override
113: public Crumb getTrailCrumbInfo() {
114:
115: ProjectDO project = getProject();
116:
117: if (project == null) {
118: return null;
119: }
120: String displayName;
121: String url;
122: try {
123: displayName = "Roles";
124: url = PROJECT_PAGE_NAME + "/" + project.getName();
125: url += "/roles";
126: } catch (Exception ex) {
127: logger.debug("Cannot get trail crumb info: ", ex);
128: return null;
129: }
130: return new Crumb(displayName, url);
131: }
132:
133: /**
134: * Get available roles for current project
135: *
136: * @return
137: */
138: public Collection<Role> getProjectRoles() {
139:
140: return m_projectService.getAvialableRoles(getProject());
141: }
142:
143: /**
144: * @return
145: */
146: public Role getCurrentRole() {
147:
148: return m_role;
149: }
150:
151: /**
152: * @return
153: */
154: public String getCurrentRoleName() {
155:
156: if (m_role != null) {
157: return m_role.getName();
158: } else {
159: return null;
160: }
161: }
162:
163: /**
164: * @param i_roleName
165: */
166: public void setCurrentRoleName(String i_roleName) {
167:
168: if (i_roleName != null) {
169: m_role = m_userDao.getRoleByName(i_roleName);
170: m_projectRole = m_userDao.getEmailByRoleNameAndProjectId(
171: i_roleName, getProjectId());
172: m_showNewRoleForm = false;
173: } else {
174: m_role = null;
175: m_projectRole = null;
176: }
177:
178: initData();
179: }
180:
181: /**
182: * @return
183: */
184: public String getListHeader() {
185:
186: if (m_role != null && m_role.getDisplayName() != null) {
187: return "Assigned to the role";
188: } else {
189: return "Assign users to the role";
190: }
191: }
192:
193: /**
194: * @return
195: */
196: public String getFormHeader() {
197:
198: if (m_role != null && m_role.getDisplayName() != null) {
199: return m_role.getDisplayName() + " Settings";
200: } else {
201: return "Create New Role";
202: }
203: }
204:
205: /**
206: * Initialize Role Data
207: */
208: private void initData() {
209:
210: if (getProject() != null) {
211: // get assigned users
212: if (m_role != null) {
213: // copy data from role into form
214: m_roleName = m_role.getName();
215: m_displayName = m_role.getDisplayName();
216: m_email = m_projectRole.getEmail();
217:
218: List<EmForgeUserDetails> assignedUsers = (List<EmForgeUserDetails>) m_projectService
219: .getUsers(getProject(), m_role);
220: m_assignedUsers = new String[assignedUsers.size()];
221: int i = 0;
222: for (EmForgeUserDetails user : assignedUsers) {
223: m_assignedUsers[i++] = user.getUsername();
224: }
225: } else {
226: m_assignedUsers = new String[0];
227:
228: m_roleName = null;
229: m_displayName = null;
230: m_email = null;
231: }
232: }
233: }
234:
235: // ////////////////////////////////////////////////
236: // Accessors
237: // ///////////////////////////////////////////////
238:
239: /**
240: * @return
241: */
242: public String[] getAssignedUsers() {
243:
244: return m_assignedUsers;
245: }
246:
247: /**
248: * @param users
249: */
250: public void setAssignedUsers(String[] users) {
251:
252: m_assignedUsers = users;
253: }
254:
255: /**
256: * @return
257: */
258: public String getRoleName() {
259:
260: return m_roleName;
261: }
262:
263: /**
264: * @param i_roleName
265: */
266: public void setRoleName(String i_roleName) {
267:
268: m_roleName = i_roleName;
269: }
270:
271: /**
272: * @return
273: */
274: public String getDisplayName() {
275:
276: return m_displayName;
277: }
278:
279: /**
280: * @param i_displayName
281: */
282: public void setDisplayName(String i_displayName) {
283:
284: m_displayName = i_displayName;
285: }
286:
287: /**
288: * @return
289: */
290: public String getEmail() {
291:
292: return m_email;
293: }
294:
295: /**
296: * @param i_email
297: */
298: public void setEmail(String i_email) {
299:
300: m_email = i_email;
301: }
302:
303: // /////////////////////////////////////////////
304: // Access Controll Accessors
305: // /////////////////////////////////////////////
306:
307: /**
308: * is current user allowed to add new roles?
309: *
310: * @return
311: */
312: public boolean isCanAddRole() {
313:
314: return m_projectService.hasRole(getProject(), m_userFactory
315: .getCurrentUser(), ProjectService.ROLE_MANAGER);
316: }
317:
318: /**
319: * @return
320: */
321: public boolean isCanSaveRole() {
322:
323: return m_projectService.hasRole(getProject(), m_userFactory
324: .getCurrentUser(), ProjectService.ROLE_MANAGER);
325: }
326:
327: /**
328: * is current user allowed to remove currently procesed role?
329: */
330: public boolean isCanDeleteRole() {
331:
332: if (m_role == null) {
333: return false;
334: }
335:
336: // it is not allowed to remove default roles
337: if (isDefaultRole()) {
338: return false;
339: }
340:
341: // only project manager allowed to remove roles
342: return m_projectService.hasRole(getProject(), m_userFactory
343: .getCurrentUser(), ProjectService.ROLE_MANAGER);
344: }
345:
346: /**
347: * @return
348: */
349: public boolean isShowNewRoleForm() {
350:
351: return m_showNewRoleForm;
352: }
353:
354: /**
355: * @return
356: */
357: public boolean isDisplayForm() {
358:
359: return m_showNewRoleForm || getCurrentRoleName() != null;
360: }
361:
362: /**
363: * @return
364: */
365: public boolean isDefaultRole() {
366:
367: String roleName = getCurrentRoleName();
368:
369: if (roleName == null) {
370: return false;
371: } else {
372: return (roleName.equals(ProjectService.ROLE_DEVELOPER)
373: || roleName.equals(ProjectService.ROLE_MANAGER)
374: || roleName.equals(ProjectService.ROLE_TESTER) || roleName
375: .equals(ProjectService.ROLE_USER));
376: }
377: }
378:
379: // //////////////////////////////////////////////////////
380: // Actions
381: // //////////////////////////////////////////////////////
382:
383: /**
384: * @return
385: */
386: public String newRoleAction() {
387:
388: m_showNewRoleForm = true;
389: setCurrentRoleName(null);
390: return null;
391: }
392:
393: /**
394: * @return
395: */
396: public String saveRole() {
397:
398: try {
399: if (StringUtils.isBlank(m_displayName)) {
400: addErrorMessage("Role Display Name cannot be blank",
401: "Role Display Name cannot be blank");
402: return null;
403: }
404:
405: if (m_role == null) {
406: if (!validateRoleName() || !validateRoleDisplayName()) {
407: return null;
408: }
409:
410: m_role = new Role();
411: m_role.setRoleType(Role.ROLE_TYPE_PROJECT);
412:
413: m_projectRole = new ProjectRole();
414: m_projectRole.setProject(getProject());
415: m_projectRole.setRole(m_role);
416: }
417:
418: // copy data from form into objects
419: if (!isDefaultRole()) {
420: if (!validateRoleName()) {
421: return null;
422: }
423:
424: m_role.setName(m_roleName);
425: }
426:
427: if (!validateRoleDisplayName()) {
428: return null;
429: }
430:
431: m_role.setDisplayName(m_displayName);
432: m_projectRole.setEmail(m_email);
433:
434: // first - add/save role
435: m_userDao.saveRole(m_role);
436: m_userDao.saveProjectRole(m_projectRole);
437:
438: // first - remove roles for all users - who are not in the list
439: Collection<EmForgeUserDetails> appliedUsers = m_projectService
440: .getUsers(getProject(), m_role);
441:
442: for (EmForgeUserDetails user : appliedUsers) {
443: // try to find user in specified list
444: if (!ArrayUtils.contains(m_assignedUsers, user
445: .getUsername())) {
446: // delete specified role
447: m_projectService.deleteUserRole(getProject(), user,
448: m_role);
449: }
450: }
451:
452: // now - add all users to role
453: for (int i = 0; i < m_assignedUsers.length; i++) {
454: EmForgeUserDetails newUser = m_userFactory
455: .getUser(m_assignedUsers[i]);
456: m_projectService.addUser(getProject(), newUser, m_role);
457: }
458:
459: // save message
460: addInfoMessage("The changes have been stored", null);
461:
462: // clear role form
463: cancel();
464:
465: return null;
466: } catch (Exception ex) {
467: logger.error("Cannot store role changes", ex);
468: addErrorMessage("Cannot store changes", ex.getMessage());
469: return null;
470: }
471: }
472:
473: public String cancel() {
474: setCurrentRoleName(null);
475: m_showNewRoleForm = false;
476:
477: return null;
478: }
479:
480: /**
481: * @return
482: */
483: public String deleteRole() {
484:
485: if (m_role != null) {
486: m_userDao.deleteRole(m_role);
487: }
488:
489: // clear role to do not display it
490: setCurrentRoleName(null);
491:
492: return null;
493: }
494:
495: /**
496: * @return
497: */
498: public String editRole() {
499:
500: return null;
501: }
502:
503: protected boolean validateRoleName() {
504: if (StringUtils.isBlank(m_roleName)) {
505: addErrorMessage("Role Name cannot be blank",
506: "Role Name cannot be blank");
507: return false;
508: }
509:
510: // user role validator for it
511: RoleValidator roleValidator = new RoleValidator();
512:
513: try {
514: if (m_role != null) {
515: roleValidator.setRoleId(m_role.getId());
516: }
517:
518: roleValidator.validate(getFacesContext(), null, m_roleName);
519: } catch (ValidatorException ex) {
520: addErrorMessage(ex.getFacesMessage().getSummary(), ex
521: .getFacesMessage().getDetail());
522: return false;
523: }
524:
525: return true;
526: }
527:
528: protected boolean validateRoleDisplayName() {
529: if (StringUtils.isBlank(m_displayName)) {
530: addErrorMessage("Role Display Name cannot be blank",
531: "Role Display Name cannot be blank");
532: return false;
533: }
534:
535: // check - role display name should be unique in the project
536: Collection<Role> projectRoles = getProjectRoles();
537: boolean found = false;
538: for (Role role : projectRoles) {
539: if (m_displayName.equals(role.getDisplayName())) {
540: // is it processed role?
541: if (m_role != null
542: && role.getId().equals(m_role.getId())) {
543: // ok - it is same role
544: return true;
545: } else {
546: found = true;
547: }
548:
549: }
550: }
551:
552: if (found) {
553: addErrorMessage(
554: "Role Display Name should be unique in project",
555: "Role Display Name should be unique in project");
556: return false;
557: }
558:
559: return true;
560: }
561: }
|