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