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.Iterator; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
import org.apache.commons.lang.StringUtils; |
25 | |
import org.kuali.rice.kns.bo.BusinessObject; |
26 | |
import org.kuali.rice.kns.bo.BusinessObjectRelationship; |
27 | |
import org.kuali.rice.kns.bo.Inactivateable; |
28 | |
import org.kuali.rice.kns.datadictionary.InactivationBlockingMetadata; |
29 | |
import org.kuali.rice.kns.service.BusinessObjectMetaDataService; |
30 | |
import org.kuali.rice.kns.service.BusinessObjectService; |
31 | |
import org.kuali.rice.kns.service.InactivationBlockingDetectionService; |
32 | |
import org.kuali.rice.kns.util.ObjectUtils; |
33 | |
import org.springframework.transaction.annotation.Transactional; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
@Transactional |
41 | 0 | public class InactivationBlockingDetectionServiceImpl implements InactivationBlockingDetectionService { |
42 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(InactivationBlockingDetectionServiceImpl.class); |
43 | |
|
44 | |
protected BusinessObjectMetaDataService businessObjectMetaDataService; |
45 | |
protected BusinessObjectService businessObjectService; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
@SuppressWarnings("unchecked") |
56 | |
public Collection<BusinessObject> listAllBlockerRecords(BusinessObject blockedBo, InactivationBlockingMetadata inactivationBlockingMetadata) { |
57 | 0 | Collection<BusinessObject> blockingRecords = new ArrayList<BusinessObject>(); |
58 | |
|
59 | 0 | Map<String, String> queryMap = buildInactivationBlockerQueryMap(blockedBo, inactivationBlockingMetadata); |
60 | 0 | if (LOG.isDebugEnabled()) { |
61 | 0 | LOG.debug("Checking for blocker records for object: " + blockedBo); |
62 | 0 | LOG.debug(" With Metadata: " + inactivationBlockingMetadata); |
63 | 0 | LOG.debug(" Resulting Query Map: " + queryMap); |
64 | |
} |
65 | |
|
66 | 0 | if (queryMap != null) { |
67 | 0 | Collection potentialBlockingRecords = businessObjectService.findMatching( |
68 | |
inactivationBlockingMetadata.getBlockingReferenceBusinessObjectClass(), queryMap); |
69 | 0 | for (Iterator iterator = potentialBlockingRecords.iterator(); iterator.hasNext();) { |
70 | 0 | Inactivateable businessObject = (Inactivateable) iterator.next(); |
71 | 0 | if (businessObject.isActive()) { |
72 | 0 | blockingRecords.add((BusinessObject) businessObject); |
73 | |
} |
74 | 0 | } |
75 | |
} |
76 | |
|
77 | 0 | return blockingRecords; |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public boolean hasABlockingRecord(BusinessObject blockedBo, InactivationBlockingMetadata inactivationBlockingMetadata) { |
90 | 0 | boolean hasBlockingRecord = false; |
91 | |
|
92 | 0 | Map<String, String> queryMap = buildInactivationBlockerQueryMap(blockedBo, inactivationBlockingMetadata); |
93 | 0 | if (queryMap != null) { |
94 | 0 | Collection potentialBlockingRecords = businessObjectService.findMatching( |
95 | |
inactivationBlockingMetadata.getBlockingReferenceBusinessObjectClass(), queryMap); |
96 | 0 | for (Iterator iterator = potentialBlockingRecords.iterator(); iterator.hasNext();) { |
97 | 0 | Inactivateable businessObject = (Inactivateable) iterator.next(); |
98 | 0 | if (businessObject.isActive()) { |
99 | 0 | hasBlockingRecord = true; |
100 | 0 | break; |
101 | |
} |
102 | 0 | } |
103 | |
} |
104 | |
|
105 | |
|
106 | 0 | return hasBlockingRecord; |
107 | |
} |
108 | |
|
109 | |
protected Map<String, String> buildInactivationBlockerQueryMap(BusinessObject blockedBo, InactivationBlockingMetadata inactivationBlockingMetadata) { |
110 | 0 | BusinessObject blockingBo = (BusinessObject) ObjectUtils.createNewObjectFromClass(inactivationBlockingMetadata |
111 | |
.getBlockingReferenceBusinessObjectClass()); |
112 | |
|
113 | 0 | BusinessObjectRelationship businessObjectRelationship = businessObjectMetaDataService |
114 | |
.getBusinessObjectRelationship(blockingBo, |
115 | |
inactivationBlockingMetadata.getBlockedReferencePropertyName()); |
116 | |
|
117 | |
|
118 | 0 | if (businessObjectRelationship != null) { |
119 | 0 | Map<String, String> parentToChildReferences = businessObjectRelationship.getParentToChildReferences(); |
120 | 0 | Map<String, String> queryMap = new HashMap<String, String>(); |
121 | 0 | for (Map.Entry<String, String> parentToChildReference : parentToChildReferences.entrySet()) { |
122 | 0 | String fieldName = parentToChildReference.getKey(); |
123 | 0 | Object fieldValue = ObjectUtils.getPropertyValue(blockedBo, parentToChildReference.getValue()); |
124 | 0 | if (fieldValue != null && StringUtils.isNotBlank(fieldValue.toString())) { |
125 | 0 | queryMap.put(fieldName, fieldValue.toString()); |
126 | |
} else { |
127 | 0 | LOG.error("Found null value for foreign key field " + fieldName |
128 | |
+ " while building inactivation blocking query map."); |
129 | 0 | throw new RuntimeException("Found null value for foreign key field '" + fieldName |
130 | |
+ "' while building inactivation blocking query map."); |
131 | |
} |
132 | 0 | } |
133 | |
|
134 | 0 | return queryMap; |
135 | |
} |
136 | |
|
137 | 0 | return null; |
138 | |
} |
139 | |
|
140 | |
public void setBusinessObjectMetaDataService(BusinessObjectMetaDataService businessObjectMetaDataService) { |
141 | 0 | this.businessObjectMetaDataService = businessObjectMetaDataService; |
142 | 0 | } |
143 | |
|
144 | |
public void setBusinessObjectService(BusinessObjectService businessObjectService) { |
145 | 0 | this.businessObjectService = businessObjectService; |
146 | 0 | } |
147 | |
|
148 | |
} |