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