001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.ken.web.spring;
017
018import java.io.IOException;
019import java.util.Collection;
020import java.util.HashMap;
021import java.util.Map;
022
023import javax.servlet.ServletException;
024import javax.servlet.http.HttpServletRequest;
025import javax.servlet.http.HttpServletResponse;
026
027import org.apache.log4j.Logger;
028import org.kuali.rice.ken.bo.NotificationContentTypeBo;
029import org.kuali.rice.ken.service.NotificationAuthorizationService;
030import org.kuali.rice.ken.service.NotificationContentTypeService;
031import org.kuali.rice.ken.service.NotificationService;
032import org.springframework.web.servlet.ModelAndView;
033import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
034
035/**
036 * Controller that manages ContentTypes (add/update)
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 */
039public class ContentTypeController extends MultiActionController {
040   
041   private static String view = "";
042   protected NotificationService notificationService;
043   protected NotificationContentTypeService notificationContentTypeService;
044   protected NotificationAuthorizationService notificationAuthzService;
045   
046   /**
047    * Set the Notification Services
048    * @param notificationService
049    */   
050   public void setNotificationService(NotificationService notificationService) {
051      this.notificationService = notificationService;
052   }
053   
054   /**
055    * Set the Notification Services
056    * @param notificationService
057    */   
058   public void setNotificationContentTypeService(NotificationContentTypeService notificationContentTypeService) {
059      this.notificationContentTypeService = notificationContentTypeService;
060   }
061   
062   /**
063    * Set the Notification Authorization Services
064    * @param notificationAuthzService
065    */   
066   public void setNotificationAuthorizationService(NotificationAuthorizationService notificationAuthzService) {
067      this.notificationAuthzService = notificationAuthzService;
068   }
069
070   /** Logger for this class and subclasses */
071   private static final Logger LOG = Logger.getLogger(NotificationController.class);
072   
073   @Override
074   protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
075       // only allow admins access to this controller
076       // yuck, we should implement ACEGI down the road
077       String user = request.getRemoteUser();
078       if (!notificationAuthzService.isUserAdministrator(user)) {
079           response.setStatus(HttpServletResponse.SC_FORBIDDEN);
080           //response.sendError(HttpServletResponse.SC_FORBIDDEN, "User " + user + " is not a Notification System administrator");
081           throw new SecurityException("User " + user + " is not a Notification System administrator");
082       }
083       return super.handleRequestInternal(request, response);
084   }
085
086   /**
087    * display ContentTypes
088    * @param request : a servlet request
089    * @param response : a servlet response
090    * @throws ServletException : an exception
091    * @throws IOException : an exception
092    * @return a ModelAndView object
093    */   
094   public ModelAndView displayContentTypes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
095
096      view = "ContentTypeManager";
097      
098      Collection<NotificationContentTypeBo> contentTypes = this.notificationContentTypeService.getAllCurrentContentTypes();
099      Map<String, Object> model = new HashMap<String, Object>();
100      model.put("contentTypes", contentTypes);
101      return new ModelAndView(view, model);
102   }
103
104/**
105    * display ContentTypeForm
106    * @param request : a servlet request
107    * @param response : a servlet response
108    * @throws ServletException : an exception
109    * @throws IOException : an exception
110    * @return a ModelAndView object
111    */   
112   public ModelAndView displayContentTypeForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
113
114      view = "ContentTypeForm";
115      NotificationContentTypeBo notificationContentType;
116      String actionrequested; 
117      String name = request.getParameter("name");
118      LOG.debug("name param: "+name);
119 
120      if (name == null) {
121         actionrequested = new String("add");
122         notificationContentType = new NotificationContentTypeBo();
123      } else {
124          actionrequested = new String("update"); 
125         notificationContentType = this.notificationContentTypeService.getNotificationContentType(name);
126      }
127
128      Map<String, Object> model = new HashMap<String, Object>();
129      model.put("notificationContentType",notificationContentType);
130      model.put("actionrequested", actionrequested);
131      return new ModelAndView(view, model);
132   }
133   
134   /**
135    * addContentType
136    * @param request : a servlet request
137    * @param response : a servlet response
138    * @throws ServletException : an exception
139    * @throws IOException : an exception
140    * @return a ModelAndView object
141    */   
142   public ModelAndView addContentType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
143       view = "ContentTypeManager";
144       String id = request.getParameter("id");
145       String name = request.getParameter("name");
146       String description = request.getParameter("description");
147       String namespace = request.getParameter("namespace");
148       String xsd = request.getParameter("xsd");
149       String xsl = request.getParameter("xsl");
150       
151       LOG.debug("id: "+id);
152       LOG.debug("name: "+name);
153       LOG.debug("description: "+description);
154       LOG.debug("namespace: "+namespace);
155       LOG.debug("xsd: "+xsd);
156       LOG.debug("xsl: "+xsl);
157       
158       NotificationContentTypeBo notificationContentType = new NotificationContentTypeBo();
159       notificationContentType.setName(name);
160       notificationContentType.setDescription(description);
161       notificationContentType.setNamespace(namespace);
162       notificationContentType.setXsd(xsd);
163       notificationContentType.setXsl(xsl);
164       
165       notificationContentTypeService.saveNotificationContentType(notificationContentType);
166       
167       Collection<NotificationContentTypeBo> contentTypes = this.notificationContentTypeService.getAllCurrentContentTypes();
168       Map<String, Object> model = new HashMap<String, Object>();
169       model.put("contentTypes", contentTypes);            
170       return new ModelAndView(view, model);     
171   }
172   
173   /**
174    * updateContentType
175    * @param request : a servlet request
176    * @param response : a servlet response
177    * @throws ServletException : an exception
178    * @throws IOException : an exception
179    * @return a ModelAndView object
180    */   
181   public ModelAndView updateContentType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
182       view = "ContentTypeManager";
183       String id = request.getParameter("id");
184       String name = request.getParameter("name");
185       String description = request.getParameter("description");
186       String namespace = request.getParameter("namespace");
187       String xsd = request.getParameter("xsd");
188       String xsl = request.getParameter("xsl");
189       
190       LOG.debug("id: "+id);
191       LOG.debug("name: "+name);
192       LOG.debug("description: "+description);
193       LOG.debug("namespace: "+namespace);
194       LOG.debug("xsd: "+xsd);
195       LOG.debug("xsl: "+xsl);
196       
197       NotificationContentTypeBo notificationContentType = this.notificationContentTypeService.getNotificationContentType(name);
198        
199       notificationContentType.setName(name);
200       notificationContentType.setDescription(description);
201       notificationContentType.setNamespace(namespace);
202       notificationContentType.setXsd(xsd);
203       notificationContentType.setXsl(xsl);
204       
205       this.notificationContentTypeService.saveNotificationContentType(notificationContentType);
206       
207       
208       // get updated content type collection
209       Collection<NotificationContentTypeBo> contentTypes = this.notificationContentTypeService.getAllCurrentContentTypes();
210       Map<String, Object> model = new HashMap<String, Object>();
211       model.put("contentTypes", contentTypes);
212       return new ModelAndView(view, model);     
213   }
214}