001/**
002 * Copyright 2005-2017 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       String user = request.getRemoteUser();
077       if (!notificationAuthzService.isUserAdministrator(user)) {
078           response.setStatus(HttpServletResponse.SC_FORBIDDEN);
079           //response.sendError(HttpServletResponse.SC_FORBIDDEN, "User " + user + " is not a Notification System administrator");
080           throw new SecurityException("User " + user + " is not a Notification System administrator");
081       }
082       return super.handleRequestInternal(request, response);
083   }
084
085   /**
086    * display ContentTypes
087    * @param request : a servlet request
088    * @param response : a servlet response
089    * @throws ServletException : an exception
090    * @throws IOException : an exception
091    * @return a ModelAndView object
092    */   
093   public ModelAndView displayContentTypes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
094
095      view = "ContentTypeManager";
096      
097      Collection<NotificationContentTypeBo> contentTypes = this.notificationContentTypeService.getAllCurrentContentTypes();
098      Map<String, Object> model = new HashMap<String, Object>();
099      model.put("contentTypes", contentTypes);
100      return new ModelAndView(view, model);
101   }
102
103/**
104    * display ContentTypeForm
105    * @param request : a servlet request
106    * @param response : a servlet response
107    * @throws ServletException : an exception
108    * @throws IOException : an exception
109    * @return a ModelAndView object
110    */   
111   public ModelAndView displayContentTypeForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
112
113      view = "ContentTypeForm";
114      NotificationContentTypeBo notificationContentType;
115      String actionrequested; 
116      String name = request.getParameter("name");
117      LOG.debug("name param: "+name);
118 
119      if (name == null) {
120         actionrequested = new String("add");
121         notificationContentType = new NotificationContentTypeBo();
122      } else {
123          actionrequested = new String("update"); 
124         notificationContentType = this.notificationContentTypeService.getNotificationContentType(name);
125      }
126
127      Map<String, Object> model = new HashMap<String, Object>();
128      model.put("notificationContentType",notificationContentType);
129      model.put("actionrequested", actionrequested);
130      return new ModelAndView(view, model);
131   }
132   
133   /**
134    * addContentType
135    * @param request : a servlet request
136    * @param response : a servlet response
137    * @throws ServletException : an exception
138    * @throws IOException : an exception
139    * @return a ModelAndView object
140    */   
141   public ModelAndView addContentType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
142       view = "ContentTypeManager";
143       String id = request.getParameter("id");
144       String name = request.getParameter("name");
145       String description = request.getParameter("description");
146       String namespace = request.getParameter("namespace");
147       String xsd = request.getParameter("xsd");
148       String xsl = request.getParameter("xsl");
149       
150       LOG.debug("id: "+id);
151       LOG.debug("name: "+name);
152       LOG.debug("description: "+description);
153       LOG.debug("namespace: "+namespace);
154       LOG.debug("xsd: "+xsd);
155       LOG.debug("xsl: "+xsl);
156       
157       NotificationContentTypeBo notificationContentType = new NotificationContentTypeBo();
158       notificationContentType.setName(name);
159       notificationContentType.setDescription(description);
160       notificationContentType.setNamespace(namespace);
161       notificationContentType.setXsd(xsd);
162       notificationContentType.setXsl(xsl);
163       
164       notificationContentTypeService.saveNotificationContentType(notificationContentType);
165       
166       Collection<NotificationContentTypeBo> contentTypes = this.notificationContentTypeService.getAllCurrentContentTypes();
167       Map<String, Object> model = new HashMap<String, Object>();
168       model.put("contentTypes", contentTypes);            
169       return new ModelAndView(view, model);     
170   }
171   
172   /**
173    * updateContentType
174    * @param request : a servlet request
175    * @param response : a servlet response
176    * @throws ServletException : an exception
177    * @throws IOException : an exception
178    * @return a ModelAndView object
179    */   
180   public ModelAndView updateContentType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
181       view = "ContentTypeManager";
182       String id = request.getParameter("id");
183       String name = request.getParameter("name");
184       String description = request.getParameter("description");
185       String namespace = request.getParameter("namespace");
186       String xsd = request.getParameter("xsd");
187       String xsl = request.getParameter("xsl");
188       
189       LOG.debug("id: "+id);
190       LOG.debug("name: "+name);
191       LOG.debug("description: "+description);
192       LOG.debug("namespace: "+namespace);
193       LOG.debug("xsd: "+xsd);
194       LOG.debug("xsl: "+xsl);
195       
196       NotificationContentTypeBo notificationContentType = this.notificationContentTypeService.getNotificationContentType(name);
197        
198       notificationContentType.setName(name);
199       notificationContentType.setDescription(description);
200       notificationContentType.setNamespace(namespace);
201       notificationContentType.setXsd(xsd);
202       notificationContentType.setXsl(xsl);
203       
204       this.notificationContentTypeService.saveNotificationContentType(notificationContentType);
205       
206       
207       // get updated content type collection
208       Collection<NotificationContentTypeBo> contentTypes = this.notificationContentTypeService.getAllCurrentContentTypes();
209       Map<String, Object> model = new HashMap<String, Object>();
210       model.put("contentTypes", contentTypes);
211       return new ModelAndView(view, model);     
212   }
213}