001 // Copyright 2008 Thiago H. de Paula Figueiredo
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package br.com.arsmachina.example.web.pages.user;
016
017 import java.util.ArrayList;
018 import java.util.Collections;
019 import java.util.HashSet;
020 import java.util.List;
021 import java.util.Set;
022
023 import org.apache.tapestry5.PersistenceConstants;
024 import org.apache.tapestry5.SelectModel;
025 import org.apache.tapestry5.ValueEncoder;
026 import org.apache.tapestry5.annotations.Mixin;
027 import org.apache.tapestry5.annotations.Persist;
028 import org.apache.tapestry5.ioc.annotations.Inject;
029 import org.springframework.security.annotation.Secured;
030
031 import br.com.arsmachina.authentication.controller.PermissionGroupController;
032 import br.com.arsmachina.authentication.entity.Permission;
033 import br.com.arsmachina.authentication.entity.PermissionGroup;
034 import br.com.arsmachina.authentication.entity.User;
035 import br.com.arsmachina.example.entity.Manager;
036 import br.com.arsmachina.tapestrycrud.base.BaseEditPage;
037 import br.com.arsmachina.tapestrycrud.hibernatevalidator.mixins.HibernateValidatorMixin;
038
039 /**
040 * {@link User} edit page.
041 *
042 * @author Thiago H. de Paula Figueiredo
043 */
044 @Secured("ROLE_MANAGER")
045 public class EditUser extends BaseEditPage<User, Integer> {
046
047 @Mixin
048 @SuppressWarnings("unused")
049 private HibernateValidatorMixin hibernateValidatorMixin;
050
051 @Persist(PersistenceConstants.FLASH)
052 private boolean manager;
053
054 @Inject
055 private PermissionGroupController permissionGroupController;
056
057 /**
058 * Adds or removes the {@link Manager} role according to the <code>manager</code> property.
059 *
060 * @see br.com.arsmachina.tapestrycrud.base.BaseEditPage#prepareObjectForSaveOrUpdate()
061 */
062 @Override
063 protected void prepareObjectForSaveOrUpdate() {
064
065 PermissionGroup managers = permissionGroupController.findByName("Managers");
066 final User user = getObject();
067
068 if (manager) {
069
070 user.add(new Manager());
071 user.add(managers);
072
073 }
074 else {
075
076 user.removeRole(Manager.class);
077 user.remove(managers);
078
079 }
080
081 super.prepareObjectForSaveOrUpdate();
082
083 }
084
085 /**
086 * Returns the value of the <code>manager</code> property.
087 *
088 * @return a {@link boolean}.
089 */
090 public boolean isManager() {
091 return getObject().getRole(Manager.class) != null;
092 }
093
094 /**
095 * Changes the value of the <code>manager</code> property.
096 *
097 * @param manager a {@link boolean}.
098 */
099 public void setManager(boolean manager) {
100 this.manager = manager;
101 }
102
103 /**
104 * Returns a {@link SelectModel} for the <code>permissionGroups</code> property.
105 *
106 * @return a {@link SelectModel}.
107 */
108 public SelectModel getPermissionGroupsSM() {
109 return getSelectModelFactory().create(PermissionGroup.class);
110 }
111
112 /**
113 * Returns a {@link ValueEncoder} for the <code>PermissionGroups</code> property.
114 *
115 * @return a {@link ValueEncoder}.
116 */
117 public ValueEncoder<PermissionGroup> getPermissionGroupsVE() {
118 return getValueEncoder(PermissionGroup.class);
119 }
120
121 /**
122 * Returns a {@link SelectModel} for the <code>removedPermissions</code> property.
123 *
124 * @return a {@link SelectModel}.
125 */
126 public SelectModel getRemovedPermissionsSM() {
127
128 final User user = getObject();
129
130 Set<Permission> set = new HashSet<Permission>();
131
132 final List<PermissionGroup> permissionGroups = user.getPermissionGroups();
133
134 for (PermissionGroup permissionGroup : permissionGroups) {
135 for (Permission permission : permissionGroup.getPermissions()) {
136 set.add(permission);
137 }
138 }
139
140 List<Permission> list = new ArrayList<Permission>(set);
141 Collections.sort(list);
142
143 return getSelectModelFactory().create(Permission.class, list);
144
145 }
146
147 /**
148 * Returns a {@link ValueEncoder} for the <code>removedPermissions</code> property.
149 *
150 * @return a {@link ValueEncoder}.
151 */
152 public ValueEncoder<Permission> getRemovedPermissionsVE() {
153 return getValueEncoder(Permission.class);
154 }
155
156 }