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