001package org.kuali.ole.describe.controller;
002
003
004import org.apache.log4j.Logger;
005import org.kuali.ole.OLEConstants;
006import org.kuali.ole.describe.form.CallNumberBrowseForm;
007import org.kuali.ole.describe.service.BrowseService;
008import org.kuali.ole.describe.service.impl.BrowseServiceImpl;
009import org.kuali.ole.docstore.common.document.Holdings;
010import org.kuali.ole.docstore.common.document.Item;
011import org.kuali.ole.describe.bo.OleLocation;
012import org.kuali.rice.kim.api.permission.PermissionService;
013import org.kuali.rice.kim.api.services.KimApiServiceLocator;
014import org.kuali.rice.krad.service.BusinessObjectService;
015import org.kuali.rice.krad.service.KRADServiceLocator;
016import org.kuali.rice.krad.util.GlobalVariables;
017import org.kuali.rice.krad.util.KRADConstants;
018import org.kuali.rice.krad.web.controller.UifControllerBase;
019import org.kuali.rice.krad.web.form.UifFormBase;
020import org.springframework.stereotype.Controller;
021import org.springframework.validation.BindingResult;
022import org.springframework.web.bind.annotation.ModelAttribute;
023import org.springframework.web.bind.annotation.RequestMapping;
024import org.springframework.web.servlet.ModelAndView;
025
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028import java.util.Collection;
029import java.util.List;
030
031
032@Controller
033@RequestMapping(value = "/callnumberBrowseController")
034public class CallNumberBrowseController extends UifControllerBase {
035
036    private static final Logger LOG = Logger.getLogger(CallNumberBrowseController.class);
037
038    private BrowseService browseService;
039
040    public BrowseService getBrowseService() {
041        if (browseService == null)  {
042            browseService = new BrowseServiceImpl();
043        }
044        return browseService;
045    }
046
047    @Override
048    protected UifFormBase createInitialForm(HttpServletRequest request) {
049        return new CallNumberBrowseForm();
050    }
051
052    private boolean performCallNumberBrowse(String principalId) {
053        PermissionService service = KimApiServiceLocator.getPermissionService();
054        return service.hasPermission(principalId, OLEConstants.CAT_NAMESPACE, OLEConstants.CALL_NUMBER_BROWSE);
055    }
056
057    @Override
058    @RequestMapping(params = "methodToCall=start")
059    public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
060                              HttpServletRequest request, HttpServletResponse response) {
061        LOG.debug("Inside the start method");
062        CallNumberBrowseForm callNumberBrowseForm = (CallNumberBrowseForm) form;
063        boolean hasPermission = performCallNumberBrowse(GlobalVariables.getUserSession().getPrincipalId());
064        if (!hasPermission) {
065            GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_INFO, OLEConstants.ERROR_AUTHORIZATION);
066            return getUIFModelAndView(callNumberBrowseForm);
067        }
068        return super.start(callNumberBrowseForm, result, request, response);
069    }
070
071    @RequestMapping(params = "methodToCall=rowsBrowse")
072    public ModelAndView rowsBrowse(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
073                                   HttpServletRequest request, HttpServletResponse response) throws Exception {
074        LOG.debug("Inside the browse method");
075        CallNumberBrowseForm callNumberBrowseForm = (CallNumberBrowseForm) form;
076        if (callNumberBrowseForm.getDocType().equalsIgnoreCase("item")) {
077            List<Item> itemList = getBrowseService().callNumberBrowse(callNumberBrowseForm);
078            callNumberBrowseForm.setItemList(itemList);
079        } else {
080            List<Holdings> holdingsList = getBrowseService().callNumberBrowse(callNumberBrowseForm);
081            callNumberBrowseForm.setHoldingsList(holdingsList);
082        }
083        setPageNextPrevoiusAndEntriesIno(callNumberBrowseForm);
084        return getUIFModelAndView(callNumberBrowseForm, "CallNumberBrowseViewPage");
085    }
086
087    @RequestMapping(params = "methodToCall=browse")
088    public ModelAndView browse(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
089                               HttpServletRequest request, HttpServletResponse response) throws Exception {
090        LOG.debug("Inside the browse method");
091        CallNumberBrowseForm callNumberBrowseForm = (CallNumberBrowseForm) form;
092        boolean hasPermission = performCallNumberBrowse(GlobalVariables.getUserSession().getPrincipalId());
093        if (!hasPermission) {
094            GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_INFO, OLEConstants.ERROR_AUTHORIZATION);
095            return getUIFModelAndView(callNumberBrowseForm);
096        }
097        String location = validateLocation(callNumberBrowseForm.getLocation());
098        callNumberBrowseForm.setLocation(location);
099        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        //callNumberBrowseForm.setCloseBtnShowFlag(false);
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}