View Javadoc
1   package org.kuali.ole.deliver.controller;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.deliver.bo.OleCirculationDesk;
6   import org.kuali.ole.deliver.bo.OleCirculationDeskLocation;
7   import org.kuali.ole.describe.bo.OleLocation;
8   import org.kuali.rice.krad.maintenance.MaintenanceDocument;
9   import org.kuali.rice.krad.service.KRADServiceLocator;
10  import org.kuali.rice.krad.uif.UifParameters;
11  import org.kuali.rice.krad.uif.container.CollectionGroup;
12  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
13  import org.kuali.rice.krad.uif.view.View;
14  import org.kuali.rice.krad.util.GlobalVariables;
15  import org.kuali.rice.krad.util.KRADConstants;
16  import org.kuali.rice.krad.web.controller.MaintenanceDocumentController;
17  import org.kuali.rice.krad.web.form.DocumentFormBase;
18  import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
19  import org.kuali.rice.krad.web.form.UifFormBase;
20  import org.springframework.stereotype.Controller;
21  import org.springframework.validation.BindingResult;
22  import org.springframework.web.bind.annotation.ModelAttribute;
23  import org.springframework.web.bind.annotation.RequestMapping;
24  import org.springframework.web.bind.annotation.RequestMethod;
25  import org.springframework.web.servlet.ModelAndView;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  import java.util.ArrayList;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  /**
35   * Created with IntelliJ IDEA.
36   * User: aurojyotit
37   * Date: 7/6/13
38   * Time: 4:09 PM
39   * To change this template use File | Settings | File Templates.
40   */
41  @Controller
42  @RequestMapping(value = "/oleCirculationDeskMaintenance")
43  public class OleCirculationDeskMaintenanceController extends MaintenanceDocumentController {
44  
45      /**
46       * Default method for controller that setups a new
47       * <code>MaintenanceView</code> with the default new action
48       */
49      @RequestMapping(params = "methodToCall=" + KRADConstants.Maintenance.METHOD_TO_CALL_NEW)
50      @Override
51      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
52                                HttpServletRequest request, HttpServletResponse response) {
53  
54          return super.start(form, result, request, response);
55      }
56  
57  
58      @Override
59      @RequestMapping(params = "methodToCall=route")
60      public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
61                                HttpServletRequest request, HttpServletResponse response) {
62          MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
63          MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
64          OleCirculationDesk oleCirculationDesk = (OleCirculationDesk) document.getNewMaintainableObject().getDataObject();
65          if(!CollectionUtils.isNotEmpty(oleCirculationDesk.getOleCirculationDeskLocations())){
66              oleCirculationDesk.setOleCirculationDeskLocations(new ArrayList<OleCirculationDeskLocation>());
67          }
68          oleCirculationDesk.getOleCirculationDeskLocations().clear();
69          if(CollectionUtils.isNotEmpty(oleCirculationDesk.getOleCirculationDeskLocationList())){
70              oleCirculationDesk.getOleCirculationDeskLocations().addAll(oleCirculationDesk.getOleCirculationDeskLocationList());
71          }
72          if(CollectionUtils.isNotEmpty(oleCirculationDesk.getOlePickupCirculationDeskLocations())){
73              oleCirculationDesk.getOleCirculationDeskLocations().addAll(oleCirculationDesk.getOlePickupCirculationDeskLocations());
74          }
75          return super.route(form, result, request, response);
76      }
77  
78      /**
79       * This method populates date of the eventlog object thereby adding to the existing list.
80       *
81       * @param uifForm
82       * @param result
83       * @param request
84       * @param response
85       * @return ModelAndView
86       */
87      @RequestMapping(params = "methodToCall=addCirculationLine")
88      public ModelAndView addCirculationLine(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
89                                             HttpServletRequest request, HttpServletResponse response) throws Exception {
90          MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
91          MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
92  
93          OleCirculationDesk oleCirculationDesk = (OleCirculationDesk) document.getNewMaintainableObject().getDataObject();
94  
95          List<OleCirculationDeskLocation> oleCirculationDeskLocations = oleCirculationDesk.getOleCirculationDeskLocationList();
96          /*OleCirculationDeskLocation oleCirculationDeskLocation=oleCirculationDeskLocations.get(0);
97          if(oleCirculationDeskLocation.getCirculationDeskLocationId()==null || oleCirculationDeskLocation.getCirculationLocationCode()==null){
98              GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, "Invalid Location");
99          }*/
100         oleCirculationDesk.setErrorMessage(null);
101         String selectedCollectionPath = form.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
102         CollectionGroup collectionGroup = form.getPostedView().getViewIndex().getCollectionGroupByPath(selectedCollectionPath);
103         String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath();
104 
105         Object eventObject = ObjectPropertyUtils.getPropertyValue(form, addLinePath);
106         OleCirculationDeskLocation oleCirculationDeskLocation = (OleCirculationDeskLocation) eventObject;
107         String fullLocationPath = oleCirculationDeskLocation.getCirculationFullLocationCode();
108         String[] locations = fullLocationPath.split("/");
109         Map<String, String> map = new HashMap<String, String>();
110         if (locations.length > 1) {
111             map.put("locationCode", locations[locations.length - 1]);
112         } else {
113             map.put("locationCode", locations[0]);
114         }
115         List<OleLocation> oleLocations = (List<OleLocation>) KRADServiceLocator.getBusinessObjectService().findMatching(OleLocation.class, map);
116         if (oleLocations.size() == 0) {
117             GlobalVariables.getMessageMap().putErrorForSectionId("OleCirculationDesk-Locations", OLEConstants.OleCirculationDesk.OLE_INVALID_CIRCULATION_DESK_LOCATION);
118             return getUIFModelAndView(form);
119         }
120         OleLocation oleLocation = null;
121         if (oleLocations.size() > 0) {
122             oleLocation = oleLocations.get(0);
123             oleCirculationDeskLocation.setCirculationLocationCode(oleLocation.getLocationCode());
124             oleCirculationDeskLocation.setCirculationDeskLocation(oleLocation.getLocationId());
125             oleCirculationDeskLocation.setOleCirculationDesk(oleCirculationDesk);
126         }
127         for (OleCirculationDeskLocation oleCirculationDeskLocation1 : oleCirculationDeskLocations) {
128             if (oleCirculationDeskLocation1.getCirculationDeskLocation().equalsIgnoreCase(oleCirculationDeskLocation.getCirculationDeskLocation())) {
129                 oleCirculationDeskLocation.setCirculationDeskLocation(null);
130                 oleCirculationDeskLocation.setCirculationLocationCode(null);
131 
132                 GlobalVariables.getMessageMap().putErrorForSectionId("OleCirculationDesk-Locations", OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION_DUPLICATE_ERROR);
133                 return getUIFModelAndView(form);
134             }
135         }
136 
137 
138         View view = form.getPostedView();
139         view.getViewHelperService().processCollectionAddLine(view, form, selectedCollectionPath);
140         return getUIFModelAndView(form);
141     }
142     @RequestMapping(params = "methodToCall=addPickupCirculationLine")
143     public ModelAndView addPickupCirculationLine(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
144                                            HttpServletRequest request, HttpServletResponse response) throws Exception {
145         MaintenanceDocumentForm form = (MaintenanceDocumentForm) uifForm;
146         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
147 
148         OleCirculationDesk oleCirculationDesk = (OleCirculationDesk) document.getNewMaintainableObject().getDataObject();
149 
150         List<OleCirculationDeskLocation> oleCirculationDeskLocations = oleCirculationDesk.getOlePickupCirculationDeskLocations();
151         /*OleCirculationDeskLocation oleCirculationDeskLocation=oleCirculationDeskLocations.get(0);
152         if(oleCirculationDeskLocation.getCirculationDeskLocationId()==null || oleCirculationDeskLocation.getCirculationLocationCode()==null){
153             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, "Invalid Location");
154         }*/
155         oleCirculationDesk.setErrorMessage(null);
156         String selectedCollectionPath = form.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
157         CollectionGroup collectionGroup = form.getPostedView().getViewIndex().getCollectionGroupByPath(selectedCollectionPath);
158         String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath();
159 
160         Object eventObject = ObjectPropertyUtils.getPropertyValue(form, addLinePath);
161         OleCirculationDeskLocation oleCirculationDeskLocation = (OleCirculationDeskLocation) eventObject;
162         String fullLocationPath = oleCirculationDeskLocation.getCirculationFullLocationCode();
163         String[] locations = fullLocationPath.split("/");
164         Map<String, String> map = new HashMap<String, String>();
165         if (locations.length > 1) {
166             map.put("locationCode", locations[locations.length - 1]);
167         } else {
168             map.put("locationCode", locations[0]);
169         }
170         List<OleLocation> oleLocations = (List<OleLocation>) KRADServiceLocator.getBusinessObjectService().findMatching(OleLocation.class, map);
171         if (oleLocations.size() == 0) {
172             GlobalVariables.getMessageMap().putErrorForSectionId("OleCirculationDesk-Pickup-Locations", OLEConstants.OleCirculationDesk.OLE_INVALID_CIRCULATION_DESK_LOCATION);
173             return getUIFModelAndView(form);
174         }
175         OleLocation oleLocation = null;
176         if (oleLocations.size() > 0) {
177             oleLocation = oleLocations.get(0);
178             oleCirculationDeskLocation.setCirculationLocationCode(oleLocation.getLocationCode());
179             oleCirculationDeskLocation.setCirculationDeskLocation(oleLocation.getLocationId());
180             oleCirculationDeskLocation.setCirculationPickUpDeskLocation(fullLocationPath);
181             oleCirculationDeskLocation.setOleCirculationDesk(oleCirculationDesk);
182         }
183         for (OleCirculationDeskLocation oleCirculationDeskLocation1 : oleCirculationDeskLocations) {
184             if (oleCirculationDeskLocation1.getCirculationDeskLocation().equalsIgnoreCase(oleCirculationDeskLocation.getCirculationDeskLocation())) {
185                 oleCirculationDeskLocation.setCirculationDeskLocation(null);
186                 oleCirculationDeskLocation.setCirculationLocationCode(null);
187 
188                 GlobalVariables.getMessageMap().putErrorForSectionId("OleCirculationDesk-Pickup-Locations", OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_LOCATION_DUPLICATE_ERROR);
189                 return getUIFModelAndView(form);
190             }
191         }
192 
193 
194         View view = form.getPostedView();
195         view.getViewHelperService().processCollectionAddLine(view, form, selectedCollectionPath);
196         return getUIFModelAndView(form);
197     }
198 
199 }
200