/* * Copyright 2005-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wamblee.photos.wicket; import java.util.List; import java.util.logging.Logger; import javax.inject.Inject; import org.apache.wicket.PageParameters; import org.apache.wicket.markup.html.WebMarkupContainer; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.TextField; import org.apache.wicket.markup.html.link.Link; import org.apache.wicket.markup.repeater.RepeatingView; import org.apache.wicket.model.Model; import org.wamblee.security.authentication.UserAdministration; import org.wamblee.security.authentication.UserMgtException; /** * Homepage */ public class AdminPage extends BasePage { private static final Logger LOGGER = Logger.getLogger(AdminPage.class.getName()); private static final long serialVersionUID = 1L; @Inject private transient UserAdministration _userAdmin; private TextField _groupName; /** * Constructor that is invoked when page is invoked without a session. * * @param parameters Page parameters */ public AdminPage(final PageParameters parameters) throws Exception { super(); RepeatingView groupDelete = new RepeatingView("groupDelete") { @Override protected void onPopulate() { removeAll(); List groups = _userAdmin.getGroups(); for (final String group : groups) { WebMarkupContainer container = new WebMarkupContainer(newChildId()); add(container); container.add(new Label("groupName", group)); container.add(new Link("groupDelete") { @Override public void onClick() { try { _userAdmin.removeGroup(group); info("Group '" + group + "' removed"); } catch (UserMgtException e) { error("Could not remove group '" + group + "'"); } } @Override public boolean isVisible() { return _userAdmin.getUsers(group).isEmpty(); } }); } } }; add(groupDelete); Form groupCreate = new Form("groupCreate") { @Override protected void onSubmit() { String name = _groupName.getValue(); if (name == null || name.length() == 0) { return; } try { _userAdmin.createGroup(name); _groupName.setModel(new Model("")); info("Group '" + name + "' created"); } catch (UserMgtException e) { error("Group '" + name + "' could not be created"); } } }; add(groupCreate); _groupName = new TextField("groupName", new Model("")); groupCreate.add(_groupName); } @Override protected boolean isAdminPage() { return true; } }