1 | /* |
---|
2 | * To change this template, choose Tools | Templates |
---|
3 | * and open the template in the editor. |
---|
4 | */ |
---|
5 | package clarin.cmdi.componentregistry.frontend; |
---|
6 | |
---|
7 | import clarin.cmdi.componentregistry.ComponentRegistryFactory; |
---|
8 | import clarin.cmdi.componentregistry.UserCredentials; |
---|
9 | import clarin.cmdi.componentregistry.model.RegistryUser; |
---|
10 | import clarin.cmdi.componentregistry.persistence.UserDao; |
---|
11 | |
---|
12 | import java.security.Principal; |
---|
13 | |
---|
14 | import org.apache.wicket.PageParameters; |
---|
15 | import org.apache.wicket.RequestCycle; |
---|
16 | import org.apache.wicket.markup.html.WebPage; |
---|
17 | import org.apache.wicket.markup.html.basic.Label; |
---|
18 | import org.apache.wicket.markup.html.form.Form; |
---|
19 | import org.apache.wicket.markup.html.form.RequiredTextField; |
---|
20 | import org.apache.wicket.markup.html.form.TextField; |
---|
21 | import org.apache.wicket.markup.html.panel.FeedbackPanel; |
---|
22 | import org.apache.wicket.model.IModel; |
---|
23 | import org.apache.wicket.protocol.http.WebRequestCycle; |
---|
24 | import org.apache.wicket.spring.injection.annot.SpringBean; |
---|
25 | import org.springframework.dao.DataAccessException; |
---|
26 | |
---|
27 | /** |
---|
28 | * |
---|
29 | * @author Twan Goosen <twan.goosen@mpi.nl> |
---|
30 | */ |
---|
31 | public class UserSettingsPage extends WebPage { |
---|
32 | |
---|
33 | @SpringBean |
---|
34 | private UserDao userDao; |
---|
35 | @SpringBean(name = "componentRegistryFactory") |
---|
36 | private ComponentRegistryFactory componentRegistryFactory; |
---|
37 | private RegistryUser registryUser; |
---|
38 | |
---|
39 | public UserSettingsPage(PageParameters parameters) { |
---|
40 | super(parameters); |
---|
41 | |
---|
42 | Principal userPrincipal = ((WebRequestCycle) (RequestCycle.get())).getWebRequest().getHttpServletRequest().getUserPrincipal(); |
---|
43 | registryUser = componentRegistryFactory.getOrCreateUser(new UserCredentials(userPrincipal)); |
---|
44 | |
---|
45 | add(new Label("userName", registryUser.getPrincipalName())); |
---|
46 | |
---|
47 | add(new FeedbackPanel("feedback")); |
---|
48 | add(new SettingsForm("settingsForm")); |
---|
49 | } |
---|
50 | |
---|
51 | private class SettingsForm extends Form { |
---|
52 | |
---|
53 | public SettingsForm(String id) { |
---|
54 | super(id); |
---|
55 | |
---|
56 | final TextField displayNameField = new RequiredTextField<String>("displayName", new IModel<String>() { |
---|
57 | |
---|
58 | @Override |
---|
59 | public String getObject() { |
---|
60 | return registryUser.getName(); |
---|
61 | } |
---|
62 | |
---|
63 | @Override |
---|
64 | public void setObject(String newName) { |
---|
65 | registryUser.setName(newName); |
---|
66 | try { |
---|
67 | userDao.updateUser(registryUser.getId(), registryUser); |
---|
68 | info("User info has been updated"); |
---|
69 | } catch (DataAccessException daEx) { |
---|
70 | error("Database error: " + daEx.getMessage()); |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | @Override |
---|
75 | public void detach() { |
---|
76 | } |
---|
77 | }); |
---|
78 | add(displayNameField); |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|