6ef38c6c07016a15a0f79cdb87767a4d45fcc880
[photos] / src / main / java / org / wamblee / photos / wicket / AdminPage.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.photos.wicket;
17
18 import java.util.List;
19 import java.util.logging.Logger;
20 import javax.inject.Inject;
21
22 import org.apache.wicket.PageParameters;
23 import org.apache.wicket.markup.html.WebMarkupContainer;
24 import org.apache.wicket.markup.html.basic.Label;
25 import org.apache.wicket.markup.html.form.Form;
26 import org.apache.wicket.markup.html.form.TextField;
27 import org.apache.wicket.markup.html.link.Link;
28 import org.apache.wicket.markup.repeater.RepeatingView;
29 import org.apache.wicket.model.Model;
30 import org.wamblee.security.authentication.UserAdministration;
31 import org.wamblee.security.authentication.UserMgtException;
32
33 /**
34  * Homepage
35  */
36 public class AdminPage extends BasePage {
37
38     private static final Logger LOGGER = Logger.getLogger(AdminPage.class.getName());
39
40     private static final long serialVersionUID = 1L;
41
42     @Inject
43     private transient UserAdministration _userAdmin;
44
45     private TextField _groupName;
46
47     /**
48      * Constructor that is invoked when page is invoked without a session.
49      *
50      * @param parameters Page parameters
51      */
52     public AdminPage(final PageParameters parameters) throws Exception {
53         super();
54
55         RepeatingView groupDelete = new RepeatingView("groupDelete") {
56             @Override
57             protected void onPopulate() {
58                 removeAll();
59                 List<String> groups = _userAdmin.getGroups();
60                 for (final String group : groups) {
61                     WebMarkupContainer container = new WebMarkupContainer(newChildId());
62                     add(container);
63                     container.add(new Label("groupName", group));
64                     container.add(new Link("groupDelete") {
65                         @Override
66                         public void onClick() {
67                             try {
68                                 _userAdmin.removeGroup(group);
69                                 info("Group '" + group + "' removed");
70                             }
71                             catch (UserMgtException e) {
72                                 error("Could not remove group '" + group + "'");
73                             }
74                         }
75
76                         @Override
77                         public boolean isVisible() {
78                             return _userAdmin.getUsers(group).isEmpty();
79                         }
80                     });
81                 }
82             }
83         };
84         add(groupDelete);
85
86         Form groupCreate = new Form("groupCreate") {
87             @Override
88             protected void onSubmit() {
89                 String name = _groupName.getValue();
90                 if (name == null || name.length() == 0) {
91                     return;
92                 }
93                 try {
94                     _userAdmin.createGroup(name);
95                     _groupName.setModel(new Model(""));
96                     info("Group '" + name + "' created");
97                 }
98                 catch (UserMgtException e) {
99                     error("Group '" + name + "' could not be created");
100                 }
101             }
102         };
103         add(groupCreate);
104         _groupName = new TextField("groupName", new Model(""));
105         groupCreate.add(_groupName);
106     }
107
108     @Override
109     protected boolean isAdminPage() {
110         return true;
111     }
112 }