From cf505202ba03fd7ab025d104e2ec354de09ab436 Mon Sep 17 00:00:00 2001 From: Mattias Arthursson Date: Thu, 6 Dec 2007 16:38:57 +0000 Subject: [PATCH] added controllers --- .../person/web/EditGroupFormAction.java | 103 ------------------ .../person/web/EditPersonFormAction.java | 70 ------------ .../samples/person/web/GroupController.java | 28 +++++ .../samples/person/web/ListController.java | 36 ++++++ .../person/web/PersonFormController.java | 33 ++++++ 5 files changed, 97 insertions(+), 173 deletions(-) delete mode 100644 spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/EditGroupFormAction.java delete mode 100644 spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/EditPersonFormAction.java create mode 100644 spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/GroupController.java create mode 100644 spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/ListController.java create mode 100644 spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/PersonFormController.java diff --git a/spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/EditGroupFormAction.java b/spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/EditGroupFormAction.java deleted file mode 100644 index 37734700..00000000 --- a/spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/EditGroupFormAction.java +++ /dev/null @@ -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 member property to the form object - * members property. - * - * @param context - * RequestContext with parameters and attributes. - * @return The success 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 selectedMembers property from - * the form object members property. - * - * @param context - * RequestContext with parameters and attributes. - * @return The success 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(); - } -} diff --git a/spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/EditPersonFormAction.java b/spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/EditPersonFormAction.java deleted file mode 100644 index 99f9bdfc..00000000 --- a/spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/EditPersonFormAction.java +++ /dev/null @@ -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; - } -} diff --git a/spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/GroupController.java b/spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/GroupController.java new file mode 100644 index 00000000..466baf3d --- /dev/null +++ b/spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/GroupController.java @@ -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)); + } + +} diff --git a/spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/ListController.java b/spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/ListController.java new file mode 100644 index 00000000..8732561a --- /dev/null +++ b/spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/ListController.java @@ -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); + } + +} diff --git a/spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/PersonFormController.java b/spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/PersonFormController.java new file mode 100644 index 00000000..9aef3123 --- /dev/null +++ b/spring-ldap-person/src/main/java/org/springframework/ldap/samples/person/web/PersonFormController.java @@ -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); + } +}