1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kns.service.impl; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.Collection; |
20 | |
import java.util.HashMap; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
import org.apache.commons.lang.StringUtils; |
25 | |
import org.kuali.rice.core.web.format.Formatter; |
26 | |
import org.kuali.rice.kim.bo.Person; |
27 | |
import org.kuali.rice.kns.authorization.BusinessObjectRestrictions; |
28 | |
import org.kuali.rice.kns.authorization.FieldRestriction; |
29 | |
import org.kuali.rice.kns.bo.BusinessObject; |
30 | |
import org.kuali.rice.kns.datadictionary.InactivationBlockingMetadata; |
31 | |
import org.kuali.rice.kns.datadictionary.mask.MaskFormatter; |
32 | |
import org.kuali.rice.kns.service.*; |
33 | |
import org.kuali.rice.kns.util.GlobalVariables; |
34 | |
import org.kuali.rice.kns.util.ObjectUtils; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class InactivationBlockingDisplayServiceImpl implements InactivationBlockingDisplayService { |
43 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(InactivationBlockingDetectionServiceImpl.class); |
44 | |
|
45 | |
private PersistenceService persistenceService; |
46 | |
private DataDictionaryService dataDictionaryService; |
47 | |
private PersistenceStructureService persistenceStructureService; |
48 | |
private BusinessObjectAuthorizationService businessObjectAuthorizationService; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public List<String> listAllBlockerRecords(BusinessObject blockedBo, InactivationBlockingMetadata inactivationBlockingMetadata) { |
56 | 0 | String inactivationBlockingDetectionServiceBeanName = inactivationBlockingMetadata.getInactivationBlockingDetectionServiceBeanName(); |
57 | 0 | if (StringUtils.isBlank(inactivationBlockingDetectionServiceBeanName)) { |
58 | 0 | inactivationBlockingDetectionServiceBeanName = KNSServiceLocatorWeb.DEFAULT_INACTIVATION_BLOCKING_DETECTION_SERVICE; |
59 | |
} |
60 | 0 | InactivationBlockingDetectionService inactivationBlockingDetectionService = KNSServiceLocatorWeb.getInactivationBlockingDetectionService(inactivationBlockingDetectionServiceBeanName); |
61 | |
|
62 | 0 | Collection<BusinessObject> collection = inactivationBlockingDetectionService.listAllBlockerRecords(blockedBo, inactivationBlockingMetadata); |
63 | |
|
64 | 0 | Map<String, Formatter> formatters = getFormattersForPrimaryKeyFields(inactivationBlockingMetadata.getBlockingReferenceBusinessObjectClass()); |
65 | |
|
66 | 0 | List<String> displayValues = new ArrayList<String>(); |
67 | 0 | List<String> pkFieldNames = persistenceStructureService.listPrimaryKeyFieldNames(inactivationBlockingMetadata.getBlockingReferenceBusinessObjectClass()); |
68 | 0 | Person user = GlobalVariables.getUserSession().getPerson(); |
69 | |
|
70 | 0 | for (BusinessObject element : collection) { |
71 | 0 | StringBuilder buf = new StringBuilder(); |
72 | |
|
73 | |
|
74 | 0 | BusinessObjectRestrictions businessObjectRestrictions = getBusinessObjectAuthorizationService().getLookupResultRestrictions(element, user); |
75 | 0 | for (int i = 0; i < pkFieldNames.size(); i++) { |
76 | 0 | String pkFieldName = pkFieldNames.get(i); |
77 | 0 | Object value = ObjectUtils.getPropertyValue(element, pkFieldName); |
78 | |
|
79 | 0 | String displayValue = null; |
80 | 0 | if (!businessObjectRestrictions.hasRestriction(pkFieldName)) { |
81 | 0 | Formatter formatter = formatters.get(pkFieldName); |
82 | 0 | if (formatter != null) { |
83 | 0 | displayValue = (String) formatter.format(value); |
84 | |
} |
85 | |
else { |
86 | 0 | displayValue = String.valueOf(value); |
87 | |
} |
88 | 0 | } |
89 | |
else { |
90 | 0 | FieldRestriction fieldRestriction = businessObjectRestrictions.getFieldRestriction(pkFieldName); |
91 | 0 | if (fieldRestriction.isMasked() || fieldRestriction.isPartiallyMasked()) { |
92 | 0 | MaskFormatter maskFormatter = fieldRestriction.getMaskFormatter(); |
93 | 0 | displayValue = maskFormatter.maskValue(value); |
94 | 0 | } |
95 | |
else { |
96 | |
|
97 | 0 | LOG.warn("Restriction was defined for class: " + element.getClass() + " field name: " + pkFieldName + ", but it was not honored by the inactivation blocking display framework"); |
98 | |
} |
99 | |
} |
100 | |
|
101 | 0 | buf.append(displayValue); |
102 | 0 | if (i < pkFieldNames.size() - 1) { |
103 | 0 | buf.append(" - "); |
104 | |
} |
105 | |
} |
106 | |
|
107 | 0 | displayValues.add(buf.toString()); |
108 | 0 | } |
109 | 0 | return displayValues; |
110 | |
} |
111 | |
|
112 | |
protected Map<String, Formatter> getFormattersForPrimaryKeyFields(Class boClass) { |
113 | 0 | List<String> keyNames = persistenceStructureService.listPrimaryKeyFieldNames(boClass); |
114 | 0 | Map<String, Formatter> formattersForPrimaryKeyFields = new HashMap<String, Formatter>(); |
115 | |
|
116 | 0 | for (String pkFieldName : keyNames) { |
117 | 0 | Formatter formatter = null; |
118 | |
|
119 | 0 | Class<? extends Formatter> formatterClass = dataDictionaryService.getAttributeFormatter(boClass, pkFieldName); |
120 | 0 | if (formatterClass != null) { |
121 | |
try { |
122 | 0 | formatter = formatterClass.newInstance(); |
123 | 0 | } catch (Exception e) { |
124 | 0 | e.printStackTrace(); |
125 | 0 | } |
126 | |
} |
127 | 0 | } |
128 | 0 | return formattersForPrimaryKeyFields; |
129 | |
} |
130 | |
|
131 | |
public void setPersistenceService(PersistenceService persistenceService) { |
132 | 0 | this.persistenceService = persistenceService; |
133 | 0 | } |
134 | |
|
135 | |
public void setPersistenceStructureService( |
136 | |
PersistenceStructureService persistenceStructureService) { |
137 | 0 | this.persistenceStructureService = persistenceStructureService; |
138 | 0 | } |
139 | |
|
140 | |
public void setDataDictionaryService(DataDictionaryService dataDictionaryService) { |
141 | 0 | this.dataDictionaryService = dataDictionaryService; |
142 | 0 | } |
143 | |
|
144 | |
protected BusinessObjectAuthorizationService getBusinessObjectAuthorizationService() { |
145 | 0 | if (businessObjectAuthorizationService == null) { |
146 | 0 | businessObjectAuthorizationService = KNSServiceLocatorWeb.getBusinessObjectAuthorizationService(); |
147 | |
} |
148 | 0 | return businessObjectAuthorizationService; |
149 | |
} |
150 | |
} |
151 | |
|