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