001/**
002 * Copyright 2004-2014 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.kpme.core.web;
017
018import java.util.Map;
019
020import javax.servlet.http.HttpServletRequest;
021import javax.servlet.http.HttpServletResponse;
022
023import org.apache.log4j.Logger;
024import org.apache.struts.action.ActionForm;
025import org.apache.struts.action.ActionForward;
026import org.apache.struts.action.ActionMapping;
027import org.kuali.kpme.core.util.HrConstants;
028import org.kuali.rice.core.api.util.RiceConstants;
029import org.kuali.rice.core.api.util.RiceKeyConstants;
030import org.kuali.rice.kns.inquiry.Inquirable;
031import org.kuali.rice.kns.web.struts.action.KualiInquiryAction;
032import org.kuali.rice.kns.web.struts.form.InquiryForm;
033import org.kuali.rice.krad.bo.BusinessObject;
034import org.kuali.rice.krad.util.GlobalVariables;
035import org.kuali.rice.krad.util.KRADConstants;
036
037public class KPMEInquiryAction extends KualiInquiryAction {
038        
039        private static final Logger LOG = Logger.getLogger(KPMEInquiryAction.class);
040        
041        protected BusinessObject retrieveBOUsingKeyMap(Map<String, String> keyMap, Inquirable kualiInquirable) {
042                BusinessObject bo = kualiInquirable.getBusinessObject(keyMap);
043        if (bo == null) {
044            GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_INQUIRY);
045        }
046        return bo;
047        }
048        
049        @Override
050        public ActionForward continueWithInquiry(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
051        InquiryForm inquiryForm = (InquiryForm) form;
052        
053        if (inquiryForm.getBusinessObjectClassName() == null) {
054                LOG.error("Business object name not given.");
055                throw new RuntimeException("Business object name not given.");
056        }
057        BusinessObject bo;
058        if(HrConstants.CLASS_INQUIRY_KEY_MAP.containsKey(inquiryForm.getBusinessObjectClassName())) {
059                Map<String, String> keyMap = inquiryForm.retrieveInquiryDecryptedPrimaryKeys();
060                for(String aKey : HrConstants.CLASS_INQUIRY_KEY_MAP.get(inquiryForm.getBusinessObjectClassName())) {
061                        if(request.getParameter(aKey) != null) {
062                                keyMap.put(aKey, request.getParameter(aKey).toString());
063                        }
064                }
065                bo = retrieveBOUsingKeyMap(keyMap, inquiryForm.getInquirable());
066        } else {
067                bo = retrieveBOFromInquirable(inquiryForm);
068        }
069        checkBO(bo);
070        
071        populateSections(mapping, request, inquiryForm, bo);
072        
073        return mapping.findForward(RiceConstants.MAPPING_BASIC);
074    }
075        
076        private void checkBO(BusinessObject bo) {
077        if (bo == null && GlobalVariables.getMessageMap().hasNoMessages()) {
078                throw new UnsupportedOperationException("The record you have inquired on does not exist.");
079        }
080    }
081        
082}