1 package org.kuali.ole.describe.controller;
2
3
4 import org.apache.log4j.Logger;
5 import org.kuali.ole.OLEConstants;
6 import org.kuali.ole.describe.form.CallNumberBrowseForm;
7 import org.kuali.ole.describe.service.BrowseService;
8 import org.kuali.ole.describe.service.impl.BrowseServiceImpl;
9 import org.kuali.ole.docstore.common.document.Holdings;
10 import org.kuali.ole.docstore.common.document.Item;
11 import org.kuali.ole.describe.bo.OleLocation;
12 import org.kuali.rice.kim.api.permission.PermissionService;
13 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
14 import org.kuali.rice.krad.service.BusinessObjectService;
15 import org.kuali.rice.krad.service.KRADServiceLocator;
16 import org.kuali.rice.krad.util.GlobalVariables;
17 import org.kuali.rice.krad.util.KRADConstants;
18 import org.kuali.rice.krad.web.controller.UifControllerBase;
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.servlet.ModelAndView;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import java.util.Collection;
29 import java.util.List;
30
31
32 @Controller
33 @RequestMapping(value = "/callnumberBrowseController")
34 public class CallNumberBrowseController extends UifControllerBase {
35
36 private static final Logger LOG = Logger.getLogger(CallNumberBrowseController.class);
37
38 private BrowseService browseService;
39
40 public BrowseService getBrowseService() {
41 if (browseService == null) {
42 browseService = new BrowseServiceImpl();
43 }
44 return browseService;
45 }
46
47 @Override
48 protected UifFormBase createInitialForm(HttpServletRequest request) {
49 return new CallNumberBrowseForm();
50 }
51
52 private boolean performCallNumberBrowse(String principalId) {
53 PermissionService service = KimApiServiceLocator.getPermissionService();
54 return service.hasPermission(principalId, OLEConstants.CAT_NAMESPACE, OLEConstants.CALL_NUMBER_BROWSE);
55 }
56
57 @Override
58 @RequestMapping(params = "methodToCall=start")
59 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
60 HttpServletRequest request, HttpServletResponse response) {
61 LOG.debug("Inside the start method");
62 CallNumberBrowseForm callNumberBrowseForm = (CallNumberBrowseForm) form;
63 boolean hasPermission = performCallNumberBrowse(GlobalVariables.getUserSession().getPrincipalId());
64 if (!hasPermission) {
65 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_INFO, OLEConstants.ERROR_AUTHORIZATION);
66 return getUIFModelAndView(callNumberBrowseForm);
67 }
68 return super.start(callNumberBrowseForm, result, request, response);
69 }
70
71 @RequestMapping(params = "methodToCall=rowsBrowse")
72 public ModelAndView rowsBrowse(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
73 HttpServletRequest request, HttpServletResponse response) throws Exception {
74 LOG.debug("Inside the browse method");
75 CallNumberBrowseForm callNumberBrowseForm = (CallNumberBrowseForm) form;
76 if (callNumberBrowseForm.getDocType().equalsIgnoreCase("item")) {
77 List<Item> itemList = getBrowseService().callNumberBrowse(callNumberBrowseForm);
78 callNumberBrowseForm.setItemList(itemList);
79 } else {
80 List<Holdings> holdingsList = getBrowseService().callNumberBrowse(callNumberBrowseForm);
81 callNumberBrowseForm.setHoldingsList(holdingsList);
82 }
83 setPageNextPrevoiusAndEntriesIno(callNumberBrowseForm);
84 return getUIFModelAndView(callNumberBrowseForm, "CallNumberBrowseViewPage");
85 }
86
87 @RequestMapping(params = "methodToCall=browse")
88 public ModelAndView browse(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
89 HttpServletRequest request, HttpServletResponse response) throws Exception {
90 LOG.debug("Inside the browse method");
91 CallNumberBrowseForm callNumberBrowseForm = (CallNumberBrowseForm) form;
92 boolean hasPermission = performCallNumberBrowse(GlobalVariables.getUserSession().getPrincipalId());
93 if (!hasPermission) {
94 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_INFO, OLEConstants.ERROR_AUTHORIZATION);
95 return getUIFModelAndView(callNumberBrowseForm);
96 }
97 String location = validateLocation(callNumberBrowseForm.getLocation());
98 callNumberBrowseForm.setLocation(location);
99 callNumberBrowseForm.setPageSize(10);
100 if (callNumberBrowseForm.getDocType().equalsIgnoreCase("item")) {
101 List<Item> itemList = getBrowseService().callNumberBrowse(callNumberBrowseForm);
102 callNumberBrowseForm.setItemList(itemList);
103 } else {
104 List<Holdings> holdingsList = getBrowseService().callNumberBrowse(callNumberBrowseForm);
105 callNumberBrowseForm.setHoldingsList(holdingsList);
106 }
107 setPageNextPrevoiusAndEntriesIno(callNumberBrowseForm);
108 return getUIFModelAndView(callNumberBrowseForm, "CallNumberBrowseViewPage");
109 }
110
111 @RequestMapping(params = "methodToCall=previous")
112 public ModelAndView previous(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
113 HttpServletRequest request, HttpServletResponse response) throws Exception {
114 LOG.debug("Inside the browse method");
115 CallNumberBrowseForm callNumberBrowseForm = (CallNumberBrowseForm) form;
116 if (callNumberBrowseForm.getDocType().equalsIgnoreCase("item")) {
117 List<Item> itemList = getBrowseService().callNumberBrowsePrev(callNumberBrowseForm);
118 callNumberBrowseForm.setItemList(itemList);
119 } else {
120 List<Holdings> holdingsList = getBrowseService().callNumberBrowsePrev(callNumberBrowseForm);
121 callNumberBrowseForm.setHoldingsList(holdingsList);
122 }
123 setPageNextPrevoiusAndEntriesIno(callNumberBrowseForm);
124 return getUIFModelAndView(callNumberBrowseForm, "CallNumberBrowseViewPage");
125 }
126
127 @RequestMapping(params = "methodToCall=next")
128 public ModelAndView next(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
129 HttpServletRequest request, HttpServletResponse response) throws Exception {
130 LOG.debug("Inside the browse method");
131 CallNumberBrowseForm callNumberBrowseForm = (CallNumberBrowseForm) form;
132 if (callNumberBrowseForm.getDocType().equalsIgnoreCase("item")) {
133 List<Item> itemList = getBrowseService().callNumberBrowseNext(callNumberBrowseForm);
134 callNumberBrowseForm.setItemList(itemList);
135 } else {
136 List<Holdings> holdingsList = getBrowseService().callNumberBrowseNext(callNumberBrowseForm);
137 callNumberBrowseForm.setHoldingsList(holdingsList);
138 }
139 setPageNextPrevoiusAndEntriesIno(callNumberBrowseForm);
140 return getUIFModelAndView(callNumberBrowseForm, "CallNumberBrowseViewPage");
141 }
142
143
144 @RequestMapping(params = "methodToCall=clear")
145 public ModelAndView clear(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
146 HttpServletRequest request, HttpServletResponse response) {
147 LOG.debug("Inside the clear method");
148 CallNumberBrowseForm callNumberBrowseForm = (CallNumberBrowseForm) form;
149 callNumberBrowseForm.setMessage("");
150 callNumberBrowseForm.setInformation("");
151 callNumberBrowseForm.setHoldingsList(null);
152 callNumberBrowseForm.setItemList(null);
153 callNumberBrowseForm.setPageSize(10);
154 callNumberBrowseForm.setPreviousFlag(false);
155 callNumberBrowseForm.setNextFlag(false);
156 callNumberBrowseForm.setCallNumberBrowseText("");
157
158 return super.start(callNumberBrowseForm, result, request, response);
159 }
160
161 @RequestMapping(params = "methodToCall=back")
162 public ModelAndView back(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
163 HttpServletRequest request, HttpServletResponse response) {
164 LOG.debug("Inside the clear method");
165 return super.back(form, result, request, response);
166 }
167
168 public void setPageNextPrevoiusAndEntriesIno(CallNumberBrowseForm callNumberBrowseForm) {
169 callNumberBrowseForm.setPreviousFlag(getBrowseService().getPreviosFlag());
170 callNumberBrowseForm.setNextFlag(getBrowseService().getNextFlag());
171 callNumberBrowseForm.setPageShowEntries(getBrowseService().getPageShowEntries());
172 }
173
174 public String validateLocation(String locationString) {
175 if (locationString != null) {
176 String[] arr = locationString.split("/");
177 for (String location : arr) {
178 if (isValidLibraryLevel(location)) {
179 return location;
180 }
181 }
182 }
183 return null;
184 }
185
186 public boolean isValidLibraryLevel(String location) {
187 boolean valid = false;
188 BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
189 Collection<OleLocation> oleLocationCollection = businessObjectService.findAll(OleLocation.class);
190 for (OleLocation oleLocation : oleLocationCollection) {
191 String locationCode = oleLocation.getLocationCode();
192 String levelCode = oleLocation.getOleLocationLevel().getLevelCode();
193 if ("LIBRARY".equals(levelCode) && (locationCode.equals(location))) {
194 valid = true;
195 return valid;
196 }
197 }
198 return valid;
199 }
200
201 }