001    /*
002     * Copyright 2005-2007 The Kuali Foundation
003     *
004     *
005     * Licensed under the Educational Community License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     * http://www.opensource.org/licenses/ecl2.php
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.kuali.rice.kew.help.web;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import javax.servlet.http.HttpServletRequest;
023    import javax.servlet.http.HttpServletResponse;
024    
025    import org.apache.commons.lang.StringUtils;
026    import org.apache.struts.action.ActionForm;
027    import org.apache.struts.action.ActionForward;
028    import org.apache.struts.action.ActionMapping;
029    import org.kuali.rice.core.util.RiceConstants;
030    import org.kuali.rice.kew.export.ExportDataSet;
031    import org.kuali.rice.kew.export.web.ExportServlet;
032    import org.kuali.rice.kew.help.HelpEntry;
033    import org.kuali.rice.kew.help.service.HelpService;
034    import org.kuali.rice.kew.service.KEWServiceLocator;
035    import org.kuali.rice.kew.util.KEWConstants;
036    import org.kuali.rice.kew.web.KewKualiAction;
037    import org.kuali.rice.kns.exception.ValidationException;
038    import org.kuali.rice.kns.util.GlobalVariables;
039    import org.kuali.rice.kns.util.KNSConstants;
040    
041    
042    /**
043     * Struts action for interfacing with the Help system.
044     *
045     * @see HelpService
046     * @see HelpEntry
047     *
048     * @author Kuali Rice Team (rice.collab@kuali.org)
049     */
050    public class HelpAction extends KewKualiAction {
051        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(HelpAction.class);
052        private static final String HELP_ID_KEY = "helpId";
053        private static final String ID_INVALID = "helpentry.id.invalid";
054    
055    
056        public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
057            HelpForm helpForm = (HelpForm) form;
058            HelpEntry helpEntry = helpForm.getHelpEntry();
059            getHelpService().save(helpEntry);
060            GlobalVariables.getMessageMap().putInfo(KNSConstants.GLOBAL_MESSAGES, "helpentry.saved");
061            return mapping.findForward("summary");
062        }
063    
064        public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
065            HelpForm helpForm=(HelpForm)form;
066            HelpEntry helpEntry=helpForm.getHelpEntry();
067            LOG.info(helpEntry.getHelpName());
068            getHelpService().delete(helpEntry);
069            helpForm.setShowDelete("no");
070            GlobalVariables.getMessageMap().putInfo(KNSConstants.GLOBAL_MESSAGES, "helpentry.deleted");
071            return mapping.findForward("delete");
072        }
073    
074        public ActionForward getSearch (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
075            LOG.debug("getSearch");
076            HelpForm helpForm = (HelpForm) form;
077            // TODO hook up KIM permissions to this
078            helpForm.setIsAdmin(false);
079            return mapping.findForward("getSearch");
080        }
081    
082        public ActionForward search (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
083            HelpForm helpForm = (HelpForm) form;
084            HelpEntry helpEntry = helpForm.getHelpEntry();
085            if(helpForm.getHelpId() != null && !StringUtils.isNumeric(helpForm.getHelpId())){
086                GlobalVariables.getMessageMap().putError(HELP_ID_KEY, ID_INVALID);
087            } else {
088                if (helpForm.getHelpId() != null) {
089                    helpEntry.setHelpId(new Long(helpForm.getHelpId()));
090                }
091            }
092    
093            List searchResults = getHelpService().search(helpEntry);
094    
095            if(searchResults != null && searchResults.size() > 0){
096                request.setAttribute("reqSearchResults", searchResults);
097            }
098            // TODO hook up KIM permissions to this
099            helpForm.setIsAdmin(false);
100    
101            return mapping.findForward("getSearch");
102        }
103    
104        public ActionForward clearSearch (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
105            HelpForm helpForm = (HelpForm) form;
106            helpForm.getHelpEntry().setHelpId(null);
107            helpForm.getHelpEntry().setHelpName(null);
108            helpForm.getHelpEntry().setHelpText(null);
109            request.setAttribute("reqSearchResults", null);
110            // TODO hook up KIM permissions to this
111            helpForm.setIsAdmin(false);
112            return mapping.findForward("getSearch");
113        }
114    
115        public ActionForward report(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
116            HelpForm helpForm = (HelpForm) form;
117            helpForm.setHelpEntry(getHelpService().findById(new Long(request.getParameter("helpId"))));
118            return mapping.findForward("report");
119        }
120    
121        public ActionForward showEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
122            HelpForm helpForm = (HelpForm) form;
123            if(helpForm.getHelpEntry().getHelpId() == null){
124                Long helpId = new Long(request.getParameter("helpId"));
125                helpForm.setHelpEntry(getHelpService().findById(helpId));
126            }
127            return mapping.findForward("basic");
128        }
129    
130        public ActionForward showDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
131            HelpForm helpForm=(HelpForm)form;
132            if(helpForm.getHelpEntry().getHelpId()==null){
133                    Long helpId=new Long(request.getParameter("helpId"));
134                    helpForm.setHelpEntry(getHelpService().findById(helpId));
135            }
136            // TOOD hook up KIM permissions to this
137            helpForm.setIsAdmin(false);
138            return mapping.findForward("delete");
139        }
140    
141        private HelpService getHelpService(){
142            return  (HelpService) KEWServiceLocator.getService(KEWServiceLocator.HELP_SERVICE);
143        }
144    
145        public ActionForward getHelpEntry(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
146            HelpForm helpForm = (HelpForm) form;
147            String helpKey = request.getParameter("helpKey");
148            helpForm.setHelpEntry(getHelpService().findByKey(helpKey));
149            helpForm.setShowEdit(KEWConstants.NO_LABEL);
150            return mapping.findForward("popHelp");
151        }
152    
153        /**
154         * TODO implement the help search as a lookupable, rendering this code redundant
155         */
156        public ActionForward export(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
157            HelpForm helpForm = (HelpForm) form;
158            HelpEntry helpEntry = helpForm.getHelpEntry();
159            List searchResults = getHelpService().search(helpEntry);
160            if(searchResults == null) {
161                    searchResults = new ArrayList();
162            }
163            ExportDataSet dataSet = new ExportDataSet();
164            dataSet.getHelp().addAll(searchResults);
165            request.getSession().setAttribute(ExportServlet.EXPORT_DATA_SET_KEY, dataSet);
166            return new ActionForward(ExportServlet.generateExportPath(request, dataSet), true);
167        }
168    
169    }