1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kns.util; |
18 | |
|
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.kuali.rice.core.api.config.property.ConfigurationService; |
21 | |
import org.kuali.rice.kns.datadictionary.MaintainableCollectionDefinition; |
22 | |
import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition; |
23 | |
import org.kuali.rice.kns.datadictionary.MaintainableItemDefinition; |
24 | |
import org.kuali.rice.kns.datadictionary.MaintainableSectionDefinition; |
25 | |
import org.kuali.rice.kns.lookup.LookupUtils; |
26 | |
import org.kuali.rice.kns.maintenance.Maintainable; |
27 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
28 | |
import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService; |
29 | |
import org.kuali.rice.kns.web.ui.Field; |
30 | |
import org.kuali.rice.kns.web.ui.Row; |
31 | |
import org.kuali.rice.kns.web.ui.Section; |
32 | |
import org.kuali.rice.krad.bo.BusinessObject; |
33 | |
import org.kuali.rice.krad.datadictionary.AttributeSecurity; |
34 | |
import org.kuali.rice.kns.datadictionary.MaintenanceDocumentEntry; |
35 | |
import org.kuali.rice.krad.exception.ValidationException; |
36 | |
import org.kuali.rice.krad.lookup.SelectiveReferenceRefresher; |
37 | |
import org.kuali.rice.krad.service.DataDictionaryService; |
38 | |
import org.kuali.rice.krad.service.KRADServiceLocator; |
39 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
40 | |
import org.kuali.rice.krad.service.KualiExceptionIncidentService; |
41 | |
import org.kuali.rice.krad.service.MaintenanceDocumentService; |
42 | |
import org.kuali.rice.krad.util.KRADConstants; |
43 | |
import org.kuali.rice.krad.workflow.service.WorkflowDocumentService; |
44 | |
|
45 | |
import java.util.Collection; |
46 | |
import java.util.HashMap; |
47 | |
import java.util.HashSet; |
48 | |
import java.util.Iterator; |
49 | |
import java.util.List; |
50 | |
import java.util.Map; |
51 | |
import java.util.Set; |
52 | |
import java.util.StringTokenizer; |
53 | |
|
54 | |
public final class MaintenanceUtils { |
55 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MaintenanceUtils.class); |
56 | |
|
57 | |
private static MaintenanceDocumentService maintenanceDocumentService; |
58 | |
private static WorkflowDocumentService workflowDocumentService; |
59 | |
private static ConfigurationService kualiConfigurationService; |
60 | |
private static KualiExceptionIncidentService kualiExceptionIncidentService; |
61 | |
private static MaintenanceDocumentDictionaryService maintenanceDocumentDictionaryService; |
62 | |
private static DataDictionaryService dataDictionaryService; |
63 | |
|
64 | 0 | private MaintenanceUtils() { |
65 | 0 | throw new UnsupportedOperationException("do not call"); |
66 | |
} |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public static Map<String, String> generateMultipleValueLookupBOTemplate(List<MaintainableSectionDefinition> sections, String collectionName) { |
82 | 0 | MaintainableCollectionDefinition definition = findMaintainableCollectionDefinition(sections, collectionName); |
83 | 0 | if (definition == null) { |
84 | 0 | return null; |
85 | |
} |
86 | 0 | Map<String, String> template = null; |
87 | |
|
88 | 0 | for (MaintainableFieldDefinition maintainableField : definition.getMaintainableFields()) { |
89 | 0 | String templateString = maintainableField.getTemplate(); |
90 | 0 | if (StringUtils.isNotBlank(templateString)) { |
91 | 0 | if (template == null) { |
92 | 0 | template = new HashMap<String, String>(); |
93 | |
} |
94 | 0 | template.put(maintainableField.getName(), templateString); |
95 | |
} |
96 | 0 | } |
97 | 0 | return template; |
98 | |
} |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public static MaintainableCollectionDefinition findMaintainableCollectionDefinition(List<MaintainableSectionDefinition> sections, String collectionName) { |
111 | 0 | String[] collectionNameParts = StringUtils.split(collectionName, "."); |
112 | 0 | for (MaintainableSectionDefinition section : sections) { |
113 | 0 | MaintainableCollectionDefinition collDefinition = findMaintainableCollectionDefinitionHelper(section.getMaintainableItems(), collectionNameParts, 0); |
114 | 0 | if (collDefinition != null) { |
115 | 0 | return collDefinition; |
116 | |
} |
117 | 0 | } |
118 | 0 | return null; |
119 | |
} |
120 | |
|
121 | |
private static <E extends MaintainableItemDefinition> MaintainableCollectionDefinition findMaintainableCollectionDefinitionHelper(Collection<E> items, String[] collectionNameParts, int collectionNameIndex) { |
122 | 0 | if (collectionNameParts.length <= collectionNameIndex) { |
123 | |
|
124 | 0 | return null; |
125 | |
} |
126 | |
|
127 | |
|
128 | |
|
129 | 0 | String collectionToFind = StringUtils.substringBefore(collectionNameParts[collectionNameIndex], "["); |
130 | 0 | for (MaintainableItemDefinition item : items) { |
131 | 0 | if (item instanceof MaintainableCollectionDefinition) { |
132 | 0 | MaintainableCollectionDefinition collection = (MaintainableCollectionDefinition) item; |
133 | 0 | if (collection.getName().equals(collectionToFind)) { |
134 | |
|
135 | |
|
136 | 0 | if (collectionNameIndex == collectionNameParts.length - 1) { |
137 | |
|
138 | 0 | return collection; |
139 | |
} else { |
140 | |
|
141 | 0 | return findMaintainableCollectionDefinitionHelper(collection.getMaintainableCollections(), collectionNameParts, collectionNameIndex + 1); |
142 | |
} |
143 | |
} |
144 | 0 | } |
145 | |
} |
146 | 0 | return null; |
147 | |
} |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
public static final Field setFieldQuickfinder(BusinessObject businessObject, String attributeName, MaintainableFieldDefinition maintainableFieldDefinition, Field field, List<String> displayedFieldNames, SelectiveReferenceRefresher srr) { |
157 | 0 | if (maintainableFieldDefinition.getOverrideLookupClass() != null && StringUtils.isNotBlank(maintainableFieldDefinition.getOverrideFieldConversions())) { |
158 | 0 | field.setQuickFinderClassNameImpl(maintainableFieldDefinition.getOverrideLookupClass().getName()); |
159 | 0 | field.setFieldConversions(maintainableFieldDefinition.getOverrideFieldConversions()); |
160 | 0 | field.setBaseLookupUrl(LookupUtils.getBaseLookupUrl(false)); |
161 | 0 | field.setReferencesToRefresh(LookupUtils.convertReferencesToSelectCollectionToString( |
162 | |
srr.getAffectedReferencesFromLookup(businessObject, attributeName, ""))); |
163 | 0 | return field; |
164 | |
} |
165 | 0 | if (maintainableFieldDefinition.isNoLookup()) { |
166 | 0 | return field; |
167 | |
} |
168 | 0 | return LookupUtils.setFieldQuickfinder(businessObject, null, false, 0, attributeName, field, displayedFieldNames, maintainableFieldDefinition.isNoLookup()); |
169 | |
} |
170 | |
|
171 | |
public static final Field setFieldQuickfinder(BusinessObject businessObject, String collectionName, boolean addLine, int index, |
172 | |
String attributeName, Field field, List<String> displayedFieldNames, Maintainable maintainable, MaintainableFieldDefinition maintainableFieldDefinition) { |
173 | 0 | if (maintainableFieldDefinition.getOverrideLookupClass() != null && StringUtils.isNotBlank(maintainableFieldDefinition.getOverrideFieldConversions())) { |
174 | 0 | if (maintainable != null) { |
175 | 0 | String collectionPrefix = ""; |
176 | 0 | if (collectionName != null) { |
177 | 0 | if (addLine) { |
178 | 0 | collectionPrefix = KRADConstants.MAINTENANCE_ADD_PREFIX + collectionName + "."; |
179 | |
} else { |
180 | 0 | collectionPrefix = collectionName + "[" + index + "]."; |
181 | |
} |
182 | |
} |
183 | 0 | field.setQuickFinderClassNameImpl(maintainableFieldDefinition.getOverrideLookupClass().getName()); |
184 | |
|
185 | 0 | String prefixedFieldConversions = prefixFieldConversionsDestinationsWithCollectionPrefix(maintainableFieldDefinition.getOverrideFieldConversions(), collectionPrefix); |
186 | 0 | field.setFieldConversions(prefixedFieldConversions); |
187 | 0 | field.setBaseLookupUrl(LookupUtils.getBaseLookupUrl(false)); |
188 | 0 | field.setReferencesToRefresh(LookupUtils.convertReferencesToSelectCollectionToString( |
189 | |
maintainable.getAffectedReferencesFromLookup(businessObject, attributeName, collectionPrefix))); |
190 | |
} |
191 | 0 | return field; |
192 | |
} |
193 | 0 | if (maintainableFieldDefinition.isNoLookup()) { |
194 | 0 | return field; |
195 | |
} |
196 | 0 | return LookupUtils.setFieldQuickfinder(businessObject, collectionName, addLine, index, |
197 | |
attributeName, field, displayedFieldNames, maintainable); |
198 | |
} |
199 | |
|
200 | |
private static String prefixFieldConversionsDestinationsWithCollectionPrefix(String originalFieldConversions, String collectionPrefix) { |
201 | 0 | StringBuilder buf = new StringBuilder(); |
202 | 0 | StringTokenizer tok = new StringTokenizer(originalFieldConversions, KRADConstants.FIELD_CONVERSIONS_SEPARATOR); |
203 | 0 | boolean needsSeparator = false; |
204 | 0 | while (tok.hasMoreTokens()) { |
205 | 0 | String conversionPair = tok.nextToken(); |
206 | 0 | if (StringUtils.isBlank(conversionPair)) { |
207 | 0 | continue; |
208 | |
} |
209 | |
|
210 | 0 | String fromValue = StringUtils.substringBefore(conversionPair, KRADConstants.FIELD_CONVERSION_PAIR_SEPARATOR); |
211 | 0 | String toValue = StringUtils.substringAfter(conversionPair, KRADConstants.FIELD_CONVERSION_PAIR_SEPARATOR); |
212 | |
|
213 | 0 | if (needsSeparator) { |
214 | 0 | buf.append(KRADConstants.FIELD_CONVERSIONS_SEPARATOR); |
215 | |
} |
216 | 0 | needsSeparator = true; |
217 | |
|
218 | 0 | buf.append(fromValue).append(KRADConstants.FIELD_CONVERSION_PAIR_SEPARATOR).append(collectionPrefix).append(toValue); |
219 | 0 | } |
220 | 0 | return buf.toString(); |
221 | |
} |
222 | |
|
223 | |
public static final void setFieldDirectInquiry(BusinessObject businessObject, String attributeName, MaintainableFieldDefinition maintainableFieldDefinition, Field field, List<String> displayedFieldNames) { |
224 | 0 | LookupUtils.setFieldDirectInquiry(businessObject, attributeName, field); |
225 | 0 | } |
226 | |
|
227 | |
public static final void setFieldDirectInquiry(BusinessObject businessObject, String collectionName, boolean addLine, int index, |
228 | |
String attributeName, Field field, List<String> displayedFieldNames, Maintainable maintainable, MaintainableFieldDefinition maintainableFieldDefinition) { |
229 | 0 | LookupUtils.setFieldDirectInquiry(businessObject, attributeName, field); |
230 | 0 | } |
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
public static String generateErrorKeyForSection(Section section) { |
239 | 0 | Set<String> fieldPropertyNames = new HashSet<String>(); |
240 | 0 | addRowsToErrorKeySet(section.getRows(), fieldPropertyNames); |
241 | |
|
242 | 0 | StringBuilder buf = new StringBuilder(); |
243 | 0 | buf.append(section.getSectionId()).append(","); |
244 | |
|
245 | 0 | Iterator<String> nameIter = fieldPropertyNames.iterator(); |
246 | 0 | while (nameIter.hasNext()) { |
247 | 0 | buf.append(nameIter.next()); |
248 | 0 | if (nameIter.hasNext()) { |
249 | 0 | buf.append(","); |
250 | |
} |
251 | |
} |
252 | |
|
253 | 0 | if (section.getContainedCollectionNames() != null && section.getContainedCollectionNames().size() > 0) { |
254 | 0 | buf.append(","); |
255 | |
|
256 | 0 | Iterator<String> collectionIter = section.getContainedCollectionNames().iterator(); |
257 | 0 | while (collectionIter.hasNext()) { |
258 | 0 | buf.append(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE + collectionIter.next()); |
259 | 0 | if (collectionIter.hasNext()) { |
260 | 0 | buf.append(","); |
261 | |
} |
262 | |
} |
263 | |
} |
264 | |
|
265 | 0 | return buf.toString(); |
266 | |
} |
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
protected static void addRowsToErrorKeySet(List<Row> listOfRows, Set<String> errorKeys) { |
277 | 0 | if (listOfRows == null) { |
278 | 0 | return; |
279 | |
} |
280 | 0 | for (Row row : listOfRows) { |
281 | 0 | List<Field> fields = row.getFields(); |
282 | 0 | if (fields == null) { |
283 | 0 | continue; |
284 | |
} |
285 | 0 | for (Field field : fields) { |
286 | 0 | String fieldPropertyName = field.getPropertyName(); |
287 | 0 | if (fieldPropertyName != null && fieldPropertyName.startsWith(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE)) { |
288 | 0 | errorKeys.add(field.getPropertyName()); |
289 | |
} |
290 | 0 | addRowsToErrorKeySet(field.getContainerRows(), errorKeys); |
291 | 0 | } |
292 | 0 | } |
293 | 0 | } |
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
public static void checkForLockingDocument(Maintainable maintainable, boolean throwExceptionIfLocked) { |
299 | 0 | LOG.info("starting checkForLockingDocument (by Maintainable)"); |
300 | |
|
301 | |
|
302 | |
|
303 | 0 | String blockingDocId = maintainable.getLockingDocumentId(); |
304 | 0 | org.kuali.rice.krad.maintenance.MaintenanceUtils |
305 | |
.checkDocumentBlockingDocumentId(blockingDocId, throwExceptionIfLocked); |
306 | 0 | } |
307 | |
|
308 | |
private static MaintenanceDocumentDictionaryService getMaintenanceDocumentDictionaryService() { |
309 | 0 | if (maintenanceDocumentDictionaryService == null) { |
310 | 0 | maintenanceDocumentDictionaryService = KNSServiceLocator.getMaintenanceDocumentDictionaryService(); |
311 | |
} |
312 | 0 | return maintenanceDocumentDictionaryService; |
313 | |
} |
314 | |
|
315 | |
private static DataDictionaryService getDataDictionaryService() { |
316 | 0 | if (dataDictionaryService == null) { |
317 | 0 | dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService(); |
318 | |
} |
319 | 0 | return dataDictionaryService; |
320 | |
} |
321 | |
|
322 | |
public static Map<String, AttributeSecurity> retrievePropertyPathToAttributeSecurityMappings(String docTypeName) { |
323 | 0 | Map<String, AttributeSecurity> results = new HashMap<String, AttributeSecurity>(); |
324 | 0 | MaintenanceDocumentEntry entry = getMaintenanceDocumentDictionaryService().getMaintenanceDocumentEntry(docTypeName); |
325 | 0 | String className = entry.getDataObjectClass().getName(); |
326 | |
|
327 | 0 | for (MaintainableSectionDefinition section : entry.getMaintainableSections()) { |
328 | 0 | for (MaintainableItemDefinition item : section.getMaintainableItems()) { |
329 | 0 | if (item instanceof MaintainableFieldDefinition) { |
330 | 0 | MaintainableFieldDefinition field = (MaintainableFieldDefinition) item; |
331 | 0 | AttributeSecurity attributeSecurity = getDataDictionaryService().getAttributeSecurity(className, field.getName()); |
332 | 0 | if (attributeSecurity != null) { |
333 | 0 | results.put(field.getName(), attributeSecurity); |
334 | |
} |
335 | 0 | } else if (item instanceof MaintainableCollectionDefinition) { |
336 | 0 | addMaintenanceDocumentCollectionPathToSecurityMappings(results, "", (MaintainableCollectionDefinition) item); |
337 | |
} |
338 | |
} |
339 | |
} |
340 | 0 | return results; |
341 | |
} |
342 | |
|
343 | |
private static void addMaintenanceDocumentCollectionPathToSecurityMappings(Map<String, AttributeSecurity> mappings, String propertyPathPrefix, MaintainableCollectionDefinition collectionDefinition) { |
344 | 0 | propertyPathPrefix = propertyPathPrefix + collectionDefinition.getName() + "."; |
345 | 0 | String boClassName = collectionDefinition.getBusinessObjectClass().getName(); |
346 | 0 | for (MaintainableFieldDefinition field : collectionDefinition.getMaintainableFields()) { |
347 | 0 | AttributeSecurity attributeSecurity = getDataDictionaryService().getAttributeSecurity(boClassName, field.getName()); |
348 | 0 | if (attributeSecurity != null) { |
349 | 0 | mappings.put(propertyPathPrefix + field.getName(), attributeSecurity); |
350 | |
} |
351 | 0 | } |
352 | 0 | for (MaintainableCollectionDefinition nestedCollection : collectionDefinition.getMaintainableCollections()) { |
353 | 0 | addMaintenanceDocumentCollectionPathToSecurityMappings(mappings, propertyPathPrefix, nestedCollection); |
354 | |
} |
355 | 0 | } |
356 | |
|
357 | |
private static MaintenanceDocumentService getMaintenanceDocumentService() { |
358 | 0 | if (maintenanceDocumentService == null) { |
359 | 0 | maintenanceDocumentService = KRADServiceLocatorWeb.getMaintenanceDocumentService(); |
360 | |
} |
361 | 0 | return maintenanceDocumentService; |
362 | |
} |
363 | |
|
364 | |
private static WorkflowDocumentService getWorkflowDocumentService() { |
365 | 0 | if (workflowDocumentService == null) { |
366 | 0 | workflowDocumentService = KRADServiceLocatorWeb.getWorkflowDocumentService(); |
367 | |
} |
368 | 0 | return workflowDocumentService; |
369 | |
} |
370 | |
|
371 | |
private static ConfigurationService getKualiConfigurationService() { |
372 | 0 | if (kualiConfigurationService == null) { |
373 | 0 | kualiConfigurationService = KRADServiceLocator.getKualiConfigurationService(); |
374 | |
} |
375 | 0 | return kualiConfigurationService; |
376 | |
} |
377 | |
|
378 | |
private static KualiExceptionIncidentService getKualiExceptionIncidentService() { |
379 | 0 | if (kualiExceptionIncidentService == null) { |
380 | 0 | kualiExceptionIncidentService = KRADServiceLocatorWeb.getKualiExceptionIncidentService(); |
381 | |
} |
382 | 0 | return kualiExceptionIncidentService; |
383 | |
} |
384 | |
} |