1 | package com.ether; |
---|
2 | |
---|
3 | import com.ether.annotation.ControllerMethod; |
---|
4 | import com.ether.annotation.ParamName; |
---|
5 | import com.ether.user.User; |
---|
6 | import com.ether.user.UserRole; |
---|
7 | import com.ether.user.UserState; |
---|
8 | import com.medias.mail.Mail; |
---|
9 | import com.medias.mail.MailFactory; |
---|
10 | import net.sf.json.JSONObject; |
---|
11 | import org.apache.commons.logging.Log; |
---|
12 | import org.apache.commons.logging.LogFactory; |
---|
13 | import org.jetbrains.annotations.NotNull; |
---|
14 | import org.jetbrains.annotations.Nullable; |
---|
15 | |
---|
16 | import javax.mail.MessagingException; |
---|
17 | import java.security.DigestException; |
---|
18 | import java.security.NoSuchAlgorithmException; |
---|
19 | import java.util.ArrayList; |
---|
20 | import java.util.Date; |
---|
21 | import java.util.HashMap; |
---|
22 | import java.util.List; |
---|
23 | import java.util.Map; |
---|
24 | |
---|
25 | /** |
---|
26 | * @author vmipsl |
---|
27 | * @date 13 nov 2011 |
---|
28 | */ |
---|
29 | public class ControllerBackoffice |
---|
30 | extends ControllerEther |
---|
31 | { |
---|
32 | /** *********************************************************** **/ |
---|
33 | /** *********************** VIEWS ***************************** **/ |
---|
34 | /** *********************************************************** **/ |
---|
35 | @ControllerMethod(view = VIEW_BO_INDEX, backofficeMethod = true, defaultView = VIEW_BO_INDEX) |
---|
36 | public Map<String, Object> home() |
---|
37 | throws ServiceException |
---|
38 | { |
---|
39 | return new HashMap<String, Object>(); |
---|
40 | } |
---|
41 | |
---|
42 | @ControllerMethod(view = VIEW_BO_USER, backofficeMethod = true, defaultView = VIEW_BO_INDEX) |
---|
43 | public Map<String, Object> viewUser() |
---|
44 | throws ServiceException |
---|
45 | { |
---|
46 | final List<User> waitingUsers = getEtherService().getUsersByState( UserState.WAITING ); |
---|
47 | final List<User> users = getEtherService().getAllUsersByNameOrder(); |
---|
48 | |
---|
49 | final Map<String, Object> model = new HashMap<String, Object>(); |
---|
50 | model.put( "jSonWaitingUsers", getJsonHelper().toJSON( waitingUsers ) ); |
---|
51 | model.put( "jSonUsers", getJsonHelper().toJSON( users ) ); |
---|
52 | model.put( "jSonUserStates", getJSONUserStates() ); |
---|
53 | model.put( "jSonUserRoles", getJSONUserRoles() ); |
---|
54 | return model; |
---|
55 | } |
---|
56 | |
---|
57 | /** *********************************************************** **/ |
---|
58 | /** *********************** CALLS ***************************** **/ |
---|
59 | /** *********************************************************** **/ |
---|
60 | @ControllerMethod(jsonResult = true) |
---|
61 | public JSONObject addUser( @NotNull @ParamName(ParameterConstants.PARAMETER_NAME) final String lastName, |
---|
62 | @Nullable @ParamName(ParameterConstants.PARAMETER_FIRST_NAME) final String firstName, |
---|
63 | @NotNull @ParamName(ParameterConstants.PARAMETER_EMAIL) final String email, |
---|
64 | @NotNull @ParamName(ParameterConstants.PARAMETER_PWD) final String pwd, |
---|
65 | @Nullable @ParamName(ParameterConstants.PARAMETER_ROLE) final String role, |
---|
66 | @NotNull @ParamName(ParameterConstants.PARAMETER_STATE) final String state, |
---|
67 | @NotNull @ParamName(ParameterConstants.PARAMETER_HAS_ACCESS) final Boolean hasAccessToBO ) |
---|
68 | throws WebException, ServiceException |
---|
69 | { |
---|
70 | try |
---|
71 | { |
---|
72 | final User existingUser = getEtherService().getUserByEmail( email ); |
---|
73 | if( null == existingUser ) |
---|
74 | { |
---|
75 | final Date creationDate = new Date(); |
---|
76 | final String encryptedPassword = EtherHelper.encryptPassword( pwd ); |
---|
77 | final User user = new User( lastName, firstName, email, encryptedPassword, role, state, hasAccessToBO, creationDate ); |
---|
78 | |
---|
79 | getEtherService().createUser( user ); |
---|
80 | } |
---|
81 | else |
---|
82 | throw new WebException( WebException.WebCode.USER_ALREADY_EXISTS, "This email already corresponds to an User", WebException.getExceptionThrowable() ); |
---|
83 | |
---|
84 | } |
---|
85 | catch( DigestException e ) |
---|
86 | { |
---|
87 | throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); |
---|
88 | } |
---|
89 | catch( NoSuchAlgorithmException e ) |
---|
90 | { |
---|
91 | throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); |
---|
92 | } |
---|
93 | |
---|
94 | return getAllAndWaitingUsers(); |
---|
95 | } |
---|
96 | |
---|
97 | @ControllerMethod(jsonResult = true) |
---|
98 | public JSONObject removeUser( @NotNull @ParamName(ParameterConstants.PARAMETER_ID) final Integer userId ) |
---|
99 | throws WebException, ServiceException |
---|
100 | { |
---|
101 | getEtherService().removeUserById( userId ); |
---|
102 | |
---|
103 | return getAllAndWaitingUsers(); |
---|
104 | } |
---|
105 | |
---|
106 | @ControllerMethod(jsonResult = true) |
---|
107 | public JSONObject modifyUser( @NotNull @ParamName(ParameterConstants.PARAMETER_ID) final Integer userId, |
---|
108 | @NotNull @ParamName(ParameterConstants.PARAMETER_NAME) final String lastName, |
---|
109 | @Nullable @ParamName(ParameterConstants.PARAMETER_FIRST_NAME) final String firstName, |
---|
110 | @NotNull @ParamName(ParameterConstants.PARAMETER_EMAIL) final String email, |
---|
111 | @Nullable @ParamName(ParameterConstants.PARAMETER_PWD) final String pwd, |
---|
112 | @Nullable @ParamName(ParameterConstants.PARAMETER_ROLE) final String role, |
---|
113 | @NotNull @ParamName(ParameterConstants.PARAMETER_STATE) final String state, |
---|
114 | @NotNull @ParamName(ParameterConstants.PARAMETER_HAS_ACCESS) final Boolean hasAccessToBO, |
---|
115 | @NotNull @ParamName(ParameterConstants.PARAMETER_KEEP_SAME_PASSWORD) final Boolean keepSamePassword ) |
---|
116 | throws WebException, ServiceException |
---|
117 | { |
---|
118 | try |
---|
119 | { |
---|
120 | final User existingUser = getEtherService().getUserByEmail( email ); |
---|
121 | if( null == existingUser || userId.equals( existingUser.getId() ) ) |
---|
122 | { |
---|
123 | final User user = getEtherService().getUserById( userId ); |
---|
124 | user.setLastName( lastName ); |
---|
125 | user.setFirstName( firstName ); |
---|
126 | user.setEmail( email ); |
---|
127 | if( !keepSamePassword && null != pwd ) |
---|
128 | { |
---|
129 | final String encryptedPassword = EtherHelper.encryptPassword( pwd ); |
---|
130 | user.setPassword( encryptedPassword ); |
---|
131 | } |
---|
132 | user.setRole( UserRole.valueOf( role ) ); |
---|
133 | user.setState( UserState.valueOf( state ) ); |
---|
134 | user.setAccessToBO( hasAccessToBO ); |
---|
135 | |
---|
136 | getEtherService().updateUser( user ); |
---|
137 | } |
---|
138 | else |
---|
139 | throw new WebException( WebException.WebCode.USER_ALREADY_EXISTS, "This email already corresponds to an User", WebException.getExceptionThrowable() ); |
---|
140 | } |
---|
141 | catch( DigestException e ) |
---|
142 | { |
---|
143 | throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); |
---|
144 | } |
---|
145 | catch( NoSuchAlgorithmException e ) |
---|
146 | { |
---|
147 | throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); |
---|
148 | } |
---|
149 | |
---|
150 | return getAllAndWaitingUsers(); |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * This method changes the state user and sends an email to inform of the administrator decision. |
---|
155 | * If the email can't be sent, the state will be not changed |
---|
156 | * |
---|
157 | * @param userId |
---|
158 | * @param isAccepted |
---|
159 | * @return |
---|
160 | * @throws WebException |
---|
161 | * @throws ServiceException |
---|
162 | */ |
---|
163 | @ControllerMethod(jsonResult = true) |
---|
164 | public JSONObject acceptOrRefuseUser( @NotNull @ParamName(ParameterConstants.PARAMETER_ID) final Integer userId, |
---|
165 | @ParamName(ParameterConstants.PARAMETER_OK) final boolean isAccepted ) |
---|
166 | throws WebException, ServiceException |
---|
167 | { |
---|
168 | final User user = getEtherService().getUserById( userId ); |
---|
169 | try |
---|
170 | { |
---|
171 | sendEmailToUser( user, isAccepted ); |
---|
172 | getEtherService().acceptOrRefuseUser( userId, isAccepted ); |
---|
173 | } |
---|
174 | catch( MessagingException e ) |
---|
175 | { |
---|
176 | throw new WebException( WebException.WebCode.ERROR_EMAIL_CANNOT_BE_SEND, "The email cannot be send to the user : " + user.getEmail(), WebException.getExceptionThrowable() ); |
---|
177 | } |
---|
178 | |
---|
179 | return getAllAndWaitingUsers(); |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * This method create and send an email to the user to inform of the administrator decision |
---|
184 | * |
---|
185 | * @param user |
---|
186 | * @param isAccepted |
---|
187 | * @throws WebException |
---|
188 | */ |
---|
189 | private void sendEmailToUser( @NotNull final User user, final boolean isAccepted ) |
---|
190 | throws MessagingException |
---|
191 | { |
---|
192 | final MailFactory mailFactory = (MailFactory) getServletContext().getAttribute( "APP_MAILFACTORY" ); |
---|
193 | final String from = (String) getServletContext().getAttribute( "APP_WEBMASTER" ); |
---|
194 | final String subject = "[MEGAPOLI] User inscription"; |
---|
195 | String content = "Dear user, \n\nThe Megapoli administrator has "; |
---|
196 | if( isAccepted ) |
---|
197 | content += "accepted your inscription. You can now access to data with you login (" + user.getEmail() + ") and the password you gave during inscription.\n"; |
---|
198 | else |
---|
199 | content += "refused your inscription. You can reply to this email if you want more information.\n"; |
---|
200 | |
---|
201 | content += "\nBest regards,\nMegapoli administrator"; |
---|
202 | |
---|
203 | final Mail mail = new Mail( from, user.getEmail(), null, content, subject ); |
---|
204 | mailFactory.sendMail( mail ); |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
208 | private JSONObject getAllAndWaitingUsers() |
---|
209 | throws ServiceException |
---|
210 | { |
---|
211 | final List<User> waitingUsers = getEtherService().getUsersByState( UserState.WAITING ); |
---|
212 | final List<User> users = getEtherService().getAllUsersByNameOrder(); |
---|
213 | |
---|
214 | final JSONObject result = new JSONObject(); |
---|
215 | result.put( "jSonWaitingUsers", getJsonHelper().toJSON( waitingUsers ) ); |
---|
216 | result.put( "jSonUsers", getJsonHelper().toJSON( users ) ); |
---|
217 | return result; |
---|
218 | } |
---|
219 | |
---|
220 | private List<JSONObject> getJSONUserStates() |
---|
221 | { |
---|
222 | final UserState[] userStates = UserState.values(); |
---|
223 | |
---|
224 | final List<JSONObject> jsonUserStates = new ArrayList<JSONObject>( userStates.length ); |
---|
225 | |
---|
226 | for( final UserState userState : userStates ) |
---|
227 | { |
---|
228 | final JSONObject jsonUserState = new JSONObject(); |
---|
229 | jsonUserState.put( "text", userState.name() ); |
---|
230 | jsonUserState.put( "value", userState.name() ); |
---|
231 | jsonUserStates.add( jsonUserState ); |
---|
232 | } |
---|
233 | return jsonUserStates; |
---|
234 | } |
---|
235 | |
---|
236 | private List<JSONObject> getJSONUserRoles() |
---|
237 | { |
---|
238 | final UserRole[] userRoles = UserRole.values(); |
---|
239 | |
---|
240 | final List<JSONObject> jsonUserRoles = new ArrayList<JSONObject>( userRoles.length ); |
---|
241 | |
---|
242 | for( final UserRole userRole : userRoles ) |
---|
243 | { |
---|
244 | final JSONObject jsonUserRole = new JSONObject(); |
---|
245 | jsonUserRole.put( "text", userRole.name() ); |
---|
246 | jsonUserRole.put( "value", userRole.name() ); |
---|
247 | jsonUserRoles.add( jsonUserRole ); |
---|
248 | } |
---|
249 | return jsonUserRoles; |
---|
250 | } |
---|
251 | |
---|
252 | private static final Log LOGGER = LogFactory.getLog( ControllerBackoffice.class ); |
---|
253 | |
---|
254 | private static final String VIEW_BO_INDEX = "backoffice/index"; |
---|
255 | private static final String VIEW_BO_USER = "backoffice/user"; |
---|
256 | } |
---|