001    /*****************************************************************************
002     * Copyright (C) NanoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     *                                                                           *
008     *****************************************************************************/
009    package org.nanocontainer.nanowar.sample.struts;
010    
011    import java.util.Collection;
012    
013    import javax.servlet.http.HttpServletRequest;
014    import javax.servlet.http.HttpServletResponse;
015    
016    import org.apache.struts.action.Action;
017    import org.apache.struts.action.ActionForm;
018    import org.apache.struts.action.ActionForward;
019    import org.apache.struts.action.ActionMapping;
020    import org.nanocontainer.nanowar.sample.model.Cheese;
021    import org.nanocontainer.nanowar.sample.service.CheeseService;
022    
023    
024    /**
025     * @author Stephen Molitor
026     * @author Mauro Talevi
027     */
028    public class CheeseAction extends Action {
029    
030        private final CheeseService cheeseService;
031    
032        public CheeseAction(CheeseService cheeseService) {
033            this.cheeseService = cheeseService;
034        }
035    
036        public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
037                HttpServletResponse response) throws Exception {
038            CheeseForm cheeseForm = (CheeseForm) form;
039    
040            if (!isEmpty(cheeseForm.getName())) {
041                Cheese cheese = new Cheese(cheeseForm.getName(), cheeseForm.getCountry());
042                cheeseService.save(cheese);
043            }
044    
045            Collection cheeses = cheeseService.getCheeses();
046            request.setAttribute("cheesesOfTheWord", cheeses);
047    
048            return mapping.findForward("next");
049        }
050    
051        private boolean isEmpty(String s) {
052            return s == null || "".equals(s.trim());
053        }
054    
055    }