View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.help.web;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.apache.struts.action.ActionForm;
27  import org.apache.struts.action.ActionForward;
28  import org.apache.struts.action.ActionMapping;
29  import org.kuali.rice.core.util.RiceConstants;
30  import org.kuali.rice.kew.export.ExportDataSet;
31  import org.kuali.rice.kew.export.web.ExportServlet;
32  import org.kuali.rice.kew.help.HelpEntry;
33  import org.kuali.rice.kew.help.service.HelpService;
34  import org.kuali.rice.kew.service.KEWServiceLocator;
35  import org.kuali.rice.kew.util.KEWConstants;
36  import org.kuali.rice.kew.web.KewKualiAction;
37  import org.kuali.rice.kns.exception.ValidationException;
38  import org.kuali.rice.kns.util.GlobalVariables;
39  import org.kuali.rice.kns.util.KNSConstants;
40  
41  
42  /**
43   * Struts action for interfacing with the Help system.
44   *
45   * @see HelpService
46   * @see HelpEntry
47   *
48   * @author Kuali Rice Team (rice.collab@kuali.org)
49   */
50  public class HelpAction extends KewKualiAction {
51      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(HelpAction.class);
52      private static final String HELP_ID_KEY = "helpId";
53      private static final String ID_INVALID = "helpentry.id.invalid";
54  
55  
56      public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
57          HelpForm helpForm = (HelpForm) form;
58          HelpEntry helpEntry = helpForm.getHelpEntry();
59          getHelpService().save(helpEntry);
60          GlobalVariables.getMessageMap().putInfo(KNSConstants.GLOBAL_MESSAGES, "helpentry.saved");
61          return mapping.findForward("summary");
62      }
63  
64      public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
65      	HelpForm helpForm=(HelpForm)form;
66      	HelpEntry helpEntry=helpForm.getHelpEntry();
67      	LOG.info(helpEntry.getHelpName());
68      	getHelpService().delete(helpEntry);
69          helpForm.setShowDelete("no");
70          GlobalVariables.getMessageMap().putInfo(KNSConstants.GLOBAL_MESSAGES, "helpentry.deleted");
71      	return mapping.findForward("delete");
72      }
73  
74      public ActionForward getSearch (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
75      	LOG.debug("getSearch");
76          HelpForm helpForm = (HelpForm) form;
77          // TODO hook up KIM permissions to this
78          helpForm.setIsAdmin(false);
79         	return mapping.findForward("getSearch");
80      }
81  
82      public ActionForward search (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
83          HelpForm helpForm = (HelpForm) form;
84          HelpEntry helpEntry = helpForm.getHelpEntry();
85          if(helpForm.getHelpId() != null && !StringUtils.isNumeric(helpForm.getHelpId())){
86              GlobalVariables.getMessageMap().putError(HELP_ID_KEY, ID_INVALID);
87          } else {
88              if (helpForm.getHelpId() != null) {
89                  helpEntry.setHelpId(new Long(helpForm.getHelpId()));
90              }
91          }
92  
93          List searchResults = getHelpService().search(helpEntry);
94  
95          if(searchResults != null && searchResults.size() > 0){
96              request.setAttribute("reqSearchResults", searchResults);
97          }
98          // TODO hook up KIM permissions to this
99          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 }