| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.krad.web.controller; |
| 17 | |
|
| 18 | |
import org.apache.commons.lang.StringUtils; |
| 19 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
| 20 | |
import org.kuali.rice.kim.bo.Person; |
| 21 | |
import org.kuali.rice.kim.util.KimConstants; |
| 22 | |
import org.kuali.rice.krad.exception.AuthorizationException; |
| 23 | |
import org.kuali.rice.krad.lookup.CollectionIncomplete; |
| 24 | |
import org.kuali.rice.krad.lookup.Lookupable; |
| 25 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
| 26 | |
import org.kuali.rice.krad.uif.UifConstants; |
| 27 | |
import org.kuali.rice.krad.uif.UifParameters; |
| 28 | |
import org.kuali.rice.krad.uif.container.LookupView; |
| 29 | |
import org.kuali.rice.krad.util.GlobalVariables; |
| 30 | |
import org.kuali.rice.krad.util.KRADConstants; |
| 31 | |
import org.kuali.rice.krad.util.KRADUtils; |
| 32 | |
import org.kuali.rice.krad.web.form.LookupForm; |
| 33 | |
import org.kuali.rice.krad.web.form.UifFormBase; |
| 34 | |
import org.springframework.stereotype.Controller; |
| 35 | |
import org.springframework.validation.BindingResult; |
| 36 | |
import org.springframework.web.bind.annotation.ModelAttribute; |
| 37 | |
import org.springframework.web.bind.annotation.RequestMapping; |
| 38 | |
import org.springframework.web.servlet.ModelAndView; |
| 39 | |
|
| 40 | |
import javax.servlet.http.HttpServletRequest; |
| 41 | |
import javax.servlet.http.HttpServletResponse; |
| 42 | |
import java.util.Collection; |
| 43 | |
import java.util.Properties; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | 0 | @Controller |
| 51 | |
@RequestMapping(value = "/lookup") |
| 52 | 0 | public class LookupController extends UifControllerBase { |
| 53 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupController.class); |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
@Override |
| 59 | |
protected LookupForm createInitialForm(HttpServletRequest request) { |
| 60 | 0 | return new LookupForm(); |
| 61 | |
} |
| 62 | |
|
| 63 | |
protected void supressActionsIfNeeded(LookupForm lookupForm) { |
| 64 | |
try { |
| 65 | 0 | Class<?> dataObjectClass = Class.forName(lookupForm.getDataObjectClassName()); |
| 66 | 0 | Person user = GlobalVariables.getUserSession().getPerson(); |
| 67 | |
|
| 68 | 0 | String documentTypeName = KRADServiceLocatorWeb.getDocumentDictionaryService() |
| 69 | |
.getMaintenanceDocumentTypeName(dataObjectClass); |
| 70 | 0 | if ((documentTypeName != null) && |
| 71 | |
!KRADServiceLocatorWeb.getDocumentHelperService().getDocumentAuthorizer(documentTypeName) |
| 72 | |
.canInitiate(documentTypeName, user)) { |
| 73 | 0 | ((LookupView) lookupForm.getView()).setSuppressActions(true); |
| 74 | |
} |
| 75 | 0 | } catch (ClassNotFoundException e) { |
| 76 | 0 | LOG.warn("Unable to load Data Object Class: " + lookupForm.getDataObjectClassName(), e); |
| 77 | 0 | } |
| 78 | 0 | } |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
@Override |
| 84 | |
public void checkAuthorization(UifFormBase form, String methodToCall) throws AuthorizationException { |
| 85 | 0 | if (!(form instanceof LookupForm)) { |
| 86 | 0 | super.checkAuthorization(form, methodToCall); |
| 87 | |
} else { |
| 88 | 0 | LookupForm lookupForm = (LookupForm) form; |
| 89 | |
try { |
| 90 | 0 | Class<?> dataObjectClass = Class.forName(lookupForm.getDataObjectClassName()); |
| 91 | 0 | Person user = GlobalVariables.getUserSession().getPerson(); |
| 92 | |
|
| 93 | 0 | if (!KimApiServiceLocator.getPermissionService() |
| 94 | |
.isAuthorizedByTemplateName(user.getPrincipalId(), KRADConstants.KRAD_NAMESPACE, |
| 95 | |
KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS, |
| 96 | |
KRADUtils.getNamespaceAndComponentSimpleName(dataObjectClass), |
| 97 | |
null)) { |
| 98 | 0 | throw new AuthorizationException(user.getPrincipalName(), |
| 99 | |
KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS, dataObjectClass.getSimpleName()); |
| 100 | |
} |
| 101 | 0 | } catch (ClassNotFoundException e) { |
| 102 | 0 | LOG.warn("Unable to load Data Object Class class: " + lookupForm.getDataObjectClassName(), e); |
| 103 | 0 | super.checkAuthorization(lookupForm, methodToCall); |
| 104 | 0 | } |
| 105 | |
} |
| 106 | 0 | } |
| 107 | |
|
| 108 | |
@RequestMapping(params = "methodToCall=start") |
| 109 | |
@Override |
| 110 | |
public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, |
| 111 | |
HttpServletRequest request, HttpServletResponse response) { |
| 112 | 0 | LookupForm lookupForm = (LookupForm) form; |
| 113 | |
|
| 114 | 0 | supressActionsIfNeeded(lookupForm); |
| 115 | |
|
| 116 | 0 | return super.start(lookupForm, result, request, response); |
| 117 | |
} |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
@Override |
| 123 | |
@RequestMapping(params = "methodToCall=cancel") |
| 124 | |
public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, |
| 125 | |
HttpServletRequest request, HttpServletResponse response) { |
| 126 | 0 | LookupForm lookupForm = (LookupForm) form; |
| 127 | 0 | supressActionsIfNeeded(lookupForm); |
| 128 | |
|
| 129 | 0 | Properties props = new Properties(); |
| 130 | 0 | props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH); |
| 131 | 0 | if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) { |
| 132 | 0 | props.put(UifParameters.FORM_KEY, lookupForm.getReturnFormKey()); |
| 133 | |
} |
| 134 | 0 | if (StringUtils.isNotBlank(lookupForm.getDocNum())) { |
| 135 | 0 | props.put(UifParameters.DOC_NUM, lookupForm.getDocNum()); |
| 136 | |
} |
| 137 | |
|
| 138 | 0 | return performRedirect(lookupForm, lookupForm.getReturnLocation(), props); |
| 139 | |
} |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
@RequestMapping(params = "methodToCall=clearValues") |
| 145 | |
public ModelAndView clearValues(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result, |
| 146 | |
HttpServletRequest request, HttpServletResponse response) { |
| 147 | 0 | supressActionsIfNeeded(lookupForm); |
| 148 | |
|
| 149 | 0 | Lookupable lookupable = (Lookupable) lookupForm.getLookupable(); |
| 150 | 0 | lookupForm.setCriteriaFields(lookupable.performClear(lookupForm, lookupForm.getCriteriaFields())); |
| 151 | |
|
| 152 | 0 | return getUIFModelAndView(lookupForm); |
| 153 | |
} |
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
@RequestMapping(params = "methodToCall=search") |
| 160 | |
public ModelAndView search(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result, |
| 161 | |
HttpServletRequest request, HttpServletResponse response) { |
| 162 | 0 | supressActionsIfNeeded(lookupForm); |
| 163 | 0 | GlobalVariables.getUserSession().removeObjectsByPrefix(KRADConstants.SEARCH_METHOD); |
| 164 | |
|
| 165 | 0 | Lookupable lookupable = lookupForm.getLookupable(); |
| 166 | 0 | if (lookupable == null) { |
| 167 | 0 | LOG.error("Lookupable is null."); |
| 168 | 0 | throw new RuntimeException("Lookupable is null."); |
| 169 | |
} |
| 170 | |
|
| 171 | |
|
| 172 | 0 | lookupable.validateSearchParameters(lookupForm, lookupForm.getCriteriaFields()); |
| 173 | |
|
| 174 | 0 | Collection<?> displayList = |
| 175 | |
lookupable.performSearch(lookupForm, lookupForm.getCriteriaFields(), true); |
| 176 | |
|
| 177 | 0 | if (displayList instanceof CollectionIncomplete<?>) { |
| 178 | 0 | request.setAttribute("reqSearchResultsActualSize", |
| 179 | |
((CollectionIncomplete<?>) displayList).getActualSizeIfTruncated()); |
| 180 | |
} else { |
| 181 | 0 | request.setAttribute("reqSearchResultsActualSize", new Integer(displayList.size())); |
| 182 | |
} |
| 183 | |
|
| 184 | 0 | lookupForm.setSearchResults(displayList); |
| 185 | |
|
| 186 | 0 | return getUIFModelAndView(lookupForm); |
| 187 | |
} |
| 188 | |
} |