1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kns.lookup; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.HashSet; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
import java.util.Set; |
24 | |
import java.util.regex.Pattern; |
25 | |
import java.util.regex.PatternSyntaxException; |
26 | |
|
27 | |
import org.apache.commons.collections.CollectionUtils; |
28 | |
import org.apache.commons.collections.Predicate; |
29 | |
import org.apache.commons.lang.StringUtils; |
30 | |
import org.kuali.rice.kim.bo.impl.KimAttributes; |
31 | |
import org.kuali.rice.kim.bo.types.dto.AttributeSet; |
32 | |
import org.kuali.rice.kim.service.KIMServiceLocator; |
33 | |
import org.kuali.rice.kim.util.KimConstants; |
34 | |
import org.kuali.rice.kns.bo.BusinessObject; |
35 | |
import org.kuali.rice.kns.bo.Parameter; |
36 | |
import org.kuali.rice.kns.bo.ParameterDetailType; |
37 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
38 | |
import org.kuali.rice.kns.service.ParameterService; |
39 | |
import org.kuali.rice.kns.util.GlobalVariables; |
40 | |
import org.kuali.rice.kns.util.KNSConstants; |
41 | |
import org.kuali.rice.kns.util.ObjectUtils; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 0 | public class ParameterLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl { |
50 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ParameterLookupableHelperServiceImpl.class); |
51 | |
|
52 | |
private ParameterService parameterService; |
53 | |
|
54 | |
@Override |
55 | |
protected boolean allowsMaintenanceEditAction(BusinessObject businessObject) { |
56 | |
|
57 | 0 | boolean allowsEdit = false; |
58 | 0 | Parameter parm = (Parameter)businessObject; |
59 | |
|
60 | 0 | AttributeSet permissionDetails = new AttributeSet(); |
61 | 0 | permissionDetails.put(KimAttributes.NAMESPACE_CODE, parm.getParameterNamespaceCode()); |
62 | 0 | permissionDetails.put(KimAttributes.COMPONENT_NAME, parm.getParameterDetailTypeCode()); |
63 | 0 | permissionDetails.put(KimAttributes.PARAMETER_NAME, parm.getParameterName()); |
64 | 0 | allowsEdit = KIMServiceLocator.getIdentityManagementService().isAuthorizedByTemplateName( |
65 | |
GlobalVariables.getUserSession().getPerson().getPrincipalId(), |
66 | |
KNSConstants.KNS_NAMESPACE, |
67 | |
KimConstants.PermissionTemplateNames.MAINTAIN_SYSTEM_PARAMETER, |
68 | |
permissionDetails, null); |
69 | |
|
70 | 0 | return allowsEdit; |
71 | |
} |
72 | |
|
73 | |
@Override |
74 | |
public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) { |
75 | |
List<? extends BusinessObject> results; |
76 | |
|
77 | |
|
78 | 0 | List<ParameterDetailType> ddDetailTypes = KNSServiceLocator.getParameterServerService().getNonDatabaseComponents(); |
79 | 0 | if (fieldValues.containsKey("parameterDetailType.parameterDetailTypeName") && !StringUtils.isBlank(fieldValues.get("parameterDetailType.parameterDetailTypeName"))) { |
80 | 0 | final Set<ParameterDetailType> matchingDetailTypes = new HashSet<ParameterDetailType>(); |
81 | |
|
82 | 0 | String parameterNamespaceCode = fieldValues.get("parameterNamespaceCode"); |
83 | 0 | String parameterDetailTypeName = fieldValues.get("parameterDetailType.parameterDetailTypeName"); |
84 | |
|
85 | 0 | List<ParameterDetailType> dbDetailTypes = |
86 | |
(List<ParameterDetailType>)getBusinessObjectService().findAll(ParameterDetailType.class); |
87 | 0 | List<ParameterDetailType> allDetailTypes = new ArrayList<ParameterDetailType>(ddDetailTypes.size() + dbDetailTypes.size()); |
88 | 0 | allDetailTypes.addAll(dbDetailTypes); |
89 | 0 | allDetailTypes.addAll(ddDetailTypes); |
90 | |
|
91 | |
|
92 | 0 | reportDuplicateDetailTypes(allDetailTypes); |
93 | |
|
94 | |
|
95 | 0 | Pattern nameRegex = getParameterDetailTypeNameRegex(parameterDetailTypeName); |
96 | 0 | for (ParameterDetailType detailType : allDetailTypes) { |
97 | 0 | if (StringUtils.isBlank(parameterNamespaceCode) || detailType.getParameterNamespaceCode().equals(parameterNamespaceCode)) { |
98 | 0 | if (nameRegex == null || (detailType.getParameterDetailTypeName() != null && nameRegex.matcher(detailType.getParameterDetailTypeName().toUpperCase()).matches())) { |
99 | 0 | matchingDetailTypes.add(detailType); |
100 | |
} |
101 | |
} |
102 | |
} |
103 | |
|
104 | 0 | fieldValues.remove("parameterDetailType.parameterDetailTypeName"); |
105 | |
|
106 | 0 | results = super.getSearchResultsUnbounded(fieldValues); |
107 | |
|
108 | 0 | attachDataDictionaryDetailTypes(results, ddDetailTypes); |
109 | |
|
110 | 0 | CollectionUtils.filter(results, new Predicate() { |
111 | |
public boolean evaluate(Object object) { |
112 | 0 | return matchingDetailTypes.contains(((Parameter)object).getParameterDetailType()); |
113 | |
} |
114 | |
}); |
115 | 0 | } |
116 | |
else { |
117 | 0 | results = super.getSearchResultsUnbounded(fieldValues); |
118 | 0 | attachDataDictionaryDetailTypes(results, ddDetailTypes); |
119 | |
} |
120 | 0 | return results; |
121 | |
} |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
private void reportDuplicateDetailTypes( |
129 | |
List<ParameterDetailType> allDetailTypes) { |
130 | |
|
131 | 0 | Set<ParameterDetailType> dupCheck = new HashSet<ParameterDetailType>(); |
132 | 0 | for (ParameterDetailType detailType : allDetailTypes) { |
133 | 0 | if (dupCheck.contains(detailType)) { |
134 | 0 | ParameterDetailType duplicate = null; |
135 | 0 | for (ParameterDetailType d : dupCheck) if (d.equals(detailType)) { |
136 | 0 | duplicate = d; |
137 | 0 | break; |
138 | |
} |
139 | 0 | LOG.error(ParameterDetailType.class.getSimpleName() + "found with duplicate keys: " + detailType + " and " + duplicate); |
140 | 0 | } else { |
141 | 0 | dupCheck.add(detailType); |
142 | |
} |
143 | |
} |
144 | 0 | } |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
private Pattern getParameterDetailTypeNameRegex( |
153 | |
String parameterDetailTypeName) { |
154 | 0 | Pattern nameRegex = null; |
155 | 0 | if (StringUtils.isNotBlank(parameterDetailTypeName)) { |
156 | 0 | String patternStr = parameterDetailTypeName.replace("*", ".*").toUpperCase(); |
157 | |
try { |
158 | 0 | nameRegex = Pattern.compile(patternStr); |
159 | |
} |
160 | 0 | catch (PatternSyntaxException ex) { |
161 | 0 | LOG.error("Unable to parse parameterDetailTypeName pattern, ignoring.", ex); |
162 | 0 | } |
163 | |
} |
164 | 0 | return nameRegex; |
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
private void attachDataDictionaryDetailTypes( |
174 | |
List<? extends BusinessObject> parameters, |
175 | |
List<ParameterDetailType> ddDetailTypes) { |
176 | |
|
177 | 0 | Map<String, ParameterDetailType> ddDetailTypeMap = new HashMap<String, ParameterDetailType>(ddDetailTypes.size()); |
178 | 0 | for (ParameterDetailType detailType : ddDetailTypes) { |
179 | 0 | ddDetailTypeMap.put(detailType.getParameterDetailTypeCode(), detailType); |
180 | |
} |
181 | 0 | for (BusinessObject obj : parameters) { |
182 | 0 | if (ObjectUtils.isNull(((Parameter) obj).getParameterDetailType())) { |
183 | 0 | ((Parameter) obj).setParameterDetailType(ddDetailTypeMap.get(((Parameter) obj).getParameterDetailTypeCode())); |
184 | |
} |
185 | |
} |
186 | 0 | } |
187 | |
|
188 | |
public void setParameterService(ParameterService parameterService) { |
189 | 0 | this.parameterService = parameterService; |
190 | 0 | } |
191 | |
|
192 | |
} |
193 | |
|