added controllers

This commit is contained in:
Mattias Arthursson
2007-12-06 16:38:57 +00:00
parent f253d01f7e
commit cf505202ba
5 changed files with 97 additions and 173 deletions

View File

@@ -1,103 +0,0 @@
/*
* Copyright 2005-2007 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.springframework.ldap.samples.person.web;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.ldap.samples.person.domain.Group;
import org.springframework.ldap.samples.person.service.GroupService;
import org.springframework.util.Assert;
import org.springframework.webflow.action.FormAction;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;
/**
* Action implementation that handles edit of a group. This action has the
* capability to add and remove members of the current Group form object.
*
* @author Ulrik Sandberg
*/
public class EditGroupFormAction extends FormAction {
private GroupService groupService;
public void setGroupService(GroupService groupService) {
this.groupService = groupService;
}
/*
* @see org.springframework.webflow.action.FormAction#initAction()
*/
protected void initAction() {
super.initAction();
Assert.notNull(groupService, "A GroupService object is required");
}
/*
* @see org.springframework.webflow.action.FormAction#createFormObject(org.springframework.webflow.execution.RequestContext)
*/
protected Object createFormObject(RequestContext context) throws Exception {
String name = context.getFlowScope().getRequiredString("name");
Group group = groupService.findByPrimaryKey(name);
return group;
}
/**
* Add the member in the <code>member</code> property to the form object
* <code>members</code> property.
*
* @param context
* RequestContext with parameters and attributes.
* @return The <code>success</code> event if member was added successfully
* @throws Exception
* if an unexpected error occurs
*/
public Event add(RequestContext context) throws Exception {
Group group = (Group) getFormObject(context);
Set members = group.getMembers();
String member = (String) context.getRequestParameters().get("member");
members.add(member);
return success();
}
/**
* Removes the members in the <code>selectedMembers</code> property from
* the form object <code>members</code> property.
*
* @param context
* RequestContext with parameters and attributes.
* @return The <code>success</code> event if members were removed
* successfully
* @throws Exception
* if an unexpected error occurs
*/
public Event remove(RequestContext context) throws Exception {
Group group = (Group) getFormObject(context);
Set members = group.getMembers();
String[] selectedMembers = (String[]) context.getRequestParameters()
.getArray("selectedMembers");
List membersToRemove = Arrays.asList(selectedMembers);
Collection adjustedMembers = CollectionUtils.subtract(members,
membersToRemove);
group.setMembers(new TreeSet(adjustedMembers));
return success();
}
}

View File

@@ -1,70 +0,0 @@
/*
* Copyright 2005-2007 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.springframework.ldap.samples.person.web;
import net.sf.chainedoptions.ChainedOptionManager;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.propertyeditors.StringArrayPropertyEditor;
import org.springframework.util.Assert;
import org.springframework.webflow.action.FormAction;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;
/**
* Action implementation that handles edit of a person. Registers appropriate
* property editors, and provides an action method for setting up reference
* data.
*
* @author Mattias Arthursson
* @author Ulrik Sandberg
*/
public class EditPersonFormAction extends FormAction {
private ChainedOptionManager chainedOptionManager;
/*
* @see org.springframework.webflow.action.FormAction#registerPropertyEditors(org.springframework.webflow.execution.RequestContext,
* org.springframework.beans.PropertyEditorRegistry)
*/
protected void registerPropertyEditors(RequestContext context,
PropertyEditorRegistry registry) {
registry.registerCustomEditor(String[].class,
new StringArrayPropertyEditor());
}
public Event referenceData(RequestContext context) throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("Executing referenceData");
}
Object formObject = getFormObject(context);
chainedOptionManager.referenceData(context.getRequestScope().asMap(),
formObject, null);
return success();
}
protected void initAction() {
Assert.notNull(chainedOptionManager,
"The property 'chainedOptionManager' must not be null");
}
public void setChainedOptionManager(
ChainedOptionManager chainedOptionManager) {
this.chainedOptionManager = chainedOptionManager;
}
}

View File

@@ -0,0 +1,28 @@
package se.jayway.demo.spring.ldap.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import se.jayway.demo.spring.ldap.service.GroupService;
public class GroupController extends AbstractController {
private GroupService groupService;
public final void setGroupService(GroupService groupService) {
this.groupService = groupService;
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String name = request.getParameter("name");
return new ModelAndView("showgroup", "group", groupService
.findByPrimaryKey(name));
}
}

View File

@@ -0,0 +1,36 @@
package se.jayway.demo.spring.ldap.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import se.jayway.demo.spring.ldap.service.GroupService;
import se.jayway.demo.spring.ldap.service.PersonService;
public class ListController extends AbstractController {
private GroupService groupService;
private PersonService personService;
public final void setGroupService(GroupService groupService) {
this.groupService = groupService;
}
public final void setPersonService(PersonService personService) {
this.personService = personService;
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelMap map = new ModelMap();
map.addObject("groups", groupService.findAll()).addObject("persons",
personService.findAll());
return new ModelAndView("list", map);
}
}

View File

@@ -0,0 +1,33 @@
package se.jayway.demo.spring.ldap.web;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.servlet.mvc.SimpleFormController;
import se.jayway.demo.spring.ldap.domain.Person;
import se.jayway.demo.spring.ldap.service.PersonService;
public class PersonFormController extends SimpleFormController {
private PersonService personService;
@Override
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
String fullName = (String) request.getParameter("name");
String country = (String) request.getParameter("country");
String company = (String) request.getParameter("company");
return personService.findByPrimaryKey(country, company, fullName);
}
public final void setPersonService(PersonService personService) {
this.personService = personService;
}
@Override
protected void doSubmitAction(Object command) throws Exception {
personService.update((Person)command);
}
}