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