View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.service.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.web.format.Formatter;
20  import org.kuali.rice.kim.api.identity.Person;
21  import org.kuali.rice.kns.document.authorization.BusinessObjectRestrictions;
22  import org.kuali.rice.kns.document.authorization.FieldRestriction;
23  import org.kuali.rice.kns.service.BusinessObjectAuthorizationService;
24  import org.kuali.rice.kns.service.KNSServiceLocator;
25  import org.kuali.rice.krad.bo.BusinessObject;
26  import org.kuali.rice.krad.data.KradDataServiceLocator;
27  import org.kuali.rice.krad.datadictionary.InactivationBlockingMetadata;
28  import org.kuali.rice.krad.datadictionary.mask.MaskFormatter;
29  import org.kuali.rice.krad.service.DataDictionaryService;
30  import org.kuali.rice.krad.service.InactivationBlockingDetectionService;
31  import org.kuali.rice.krad.service.InactivationBlockingDisplayService;
32  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
33  import org.kuali.rice.krad.service.LegacyDataAdapter;
34  import org.kuali.rice.krad.util.GlobalVariables;
35  
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  /**
43   * This is a description of what this class does - wliang don't forget to fill this in.
44   *
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   *
47   */
48  public class InactivationBlockingDisplayServiceImpl implements InactivationBlockingDisplayService {
49  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(InactivationBlockingDetectionServiceImpl.class);
50  
51  	private DataDictionaryService dataDictionaryService;
52  	private BusinessObjectAuthorizationService businessObjectAuthorizationService;
53      private LegacyDataAdapter legacyDataAdapter;
54  
55  	@Override
56  	public List<String> listAllBlockerRecords(BusinessObject blockedBo, InactivationBlockingMetadata inactivationBlockingMetadata) {
57          String inactivationBlockingDetectionServiceBeanName = inactivationBlockingMetadata.getInactivationBlockingDetectionServiceBeanName();
58          if (StringUtils.isBlank(inactivationBlockingDetectionServiceBeanName)) {
59              inactivationBlockingDetectionServiceBeanName = KRADServiceLocatorWeb.DEFAULT_INACTIVATION_BLOCKING_DETECTION_SERVICE;
60          }
61          InactivationBlockingDetectionService inactivationBlockingDetectionService = KRADServiceLocatorWeb
62                  .getInactivationBlockingDetectionService(inactivationBlockingDetectionServiceBeanName);
63  
64          Collection<BusinessObject> collection = inactivationBlockingDetectionService.listAllBlockerRecords(blockedBo, inactivationBlockingMetadata);
65  
66          Map<String, Formatter> formatters = getFormattersForPrimaryKeyFields(inactivationBlockingMetadata.getBlockingReferenceBusinessObjectClass());
67  
68          List<String> displayValues = new ArrayList<String>();
69          List<String> pkFieldNames = getLegacyDataAdapter().listPrimaryKeyFieldNames(inactivationBlockingMetadata.getBlockingReferenceBusinessObjectClass());
70          Person user = GlobalVariables.getUserSession().getPerson();
71  
72          for (BusinessObject element : collection) {
73          	StringBuilder buf = new StringBuilder();
74  
75          	// the following method will return a restriction for all DD-defined attributes
76          	BusinessObjectRestrictions businessObjectRestrictions = getBusinessObjectAuthorizationService().getLookupResultRestrictions(element, user);
77          	for (int i = 0; i < pkFieldNames.size(); i++) {
78          		String pkFieldName = pkFieldNames.get(i);
79          		Object value = KradDataServiceLocator.getDataObjectService().wrap(element).getPropertyValueNullSafe(pkFieldName);
80  
81          		String displayValue = null;
82          		if (!businessObjectRestrictions.hasRestriction(pkFieldName)) {
83          			Formatter formatter = formatters.get(pkFieldName);
84          			if (formatter != null) {
85          				displayValue = (String) formatter.format(value);
86          			}
87          			else {
88          				displayValue = String.valueOf(value);
89          			}
90          		}
91          		else {
92          			FieldRestriction fieldRestriction = businessObjectRestrictions.getFieldRestriction(pkFieldName);
93          			if (fieldRestriction.isMasked() || fieldRestriction.isPartiallyMasked()) {
94  		    			MaskFormatter maskFormatter = fieldRestriction.getMaskFormatter();
95  						displayValue = maskFormatter.maskValue(value);
96          			}
97          			else {
98          				// there was a restriction, but we did not know how to obey it.
99          				LOG.warn("Restriction was defined for class: " + element.getClass() + " field name: " + pkFieldName + ", but it was not honored by the inactivation blocking display framework");
100         			}
101         		}
102 
103         		buf.append(displayValue);
104         		if (i < pkFieldNames.size() - 1) {
105         			buf.append(" - ");
106         		}
107         	}
108 
109         	displayValues.add(buf.toString());
110         }
111 		return displayValues;
112 	}
113 
114     @Override
115     public List<String> displayAllBlockingRecords(Object blockedBo, InactivationBlockingMetadata inactivationBlockingMetadata) {
116         String inactivationBlockingDetectionServiceBeanName = inactivationBlockingMetadata.getInactivationBlockingDetectionServiceBeanName();
117         if (StringUtils.isBlank(inactivationBlockingDetectionServiceBeanName)) {
118             inactivationBlockingDetectionServiceBeanName = KRADServiceLocatorWeb.DEFAULT_INACTIVATION_BLOCKING_DETECTION_SERVICE;
119         }
120         InactivationBlockingDetectionService inactivationBlockingDetectionService = KRADServiceLocatorWeb
121                 .getInactivationBlockingDetectionService(inactivationBlockingDetectionServiceBeanName);
122 
123         Collection<?> collection = inactivationBlockingDetectionService.detectAllBlockingRecords(blockedBo, inactivationBlockingMetadata);
124 
125         Map<String, Formatter> formatters = getFormattersForPrimaryKeyFields(inactivationBlockingMetadata.getBlockingReferenceBusinessObjectClass());
126 
127         List<String> displayValues = new ArrayList<String>();
128         List<String> pkFieldNames = getLegacyDataAdapter().listPrimaryKeyFieldNames(inactivationBlockingMetadata.getBlockingReferenceBusinessObjectClass());
129         Person user = GlobalVariables.getUserSession().getPerson();
130 
131         for (Object element : collection) {
132             StringBuilder buf = new StringBuilder();
133 
134             // the following method will return a restriction for all DD-defined attributes
135             BusinessObjectRestrictions businessObjectRestrictions = getBusinessObjectAuthorizationService().getLookupResultRestrictions(element, user);
136             for (int i = 0; i < pkFieldNames.size(); i++) {
137                 String pkFieldName = pkFieldNames.get(i);
138                 Object value = KradDataServiceLocator.getDataObjectService().wrap(element).getPropertyValueNullSafe(pkFieldName);
139 
140                 String displayValue = null;
141                 if (!businessObjectRestrictions.hasRestriction(pkFieldName)) {
142                     Formatter formatter = formatters.get(pkFieldName);
143                     if (formatter != null) {
144                         displayValue = (String) formatter.format(value);
145                     }
146                     else {
147                         displayValue = String.valueOf(value);
148                     }
149                 }
150                 else {
151                     FieldRestriction fieldRestriction = businessObjectRestrictions.getFieldRestriction(pkFieldName);
152                     if (fieldRestriction.isMasked() || fieldRestriction.isPartiallyMasked()) {
153                         MaskFormatter maskFormatter = fieldRestriction.getMaskFormatter();
154                         displayValue = maskFormatter.maskValue(value);
155                     }
156                     else {
157                         // there was a restriction, but we did not know how to obey it.
158                         LOG.warn("Restriction was defined for class: " + element.getClass() + " field name: " + pkFieldName + ", but it was not honored by the inactivation blocking display framework");
159                     }
160                 }
161 
162                 buf.append(displayValue);
163                 if (i < pkFieldNames.size() - 1) {
164                     buf.append(" - ");
165                 }
166             }
167 
168             displayValues.add(buf.toString());
169         }
170         return displayValues;
171     }
172 
173 
174     @Deprecated
175 	protected Map<String, Formatter> getFormattersForPrimaryKeyFields(Class<?> boClass) {
176 		List<String> keyNames = getLegacyDataAdapter().listPrimaryKeyFieldNames(boClass);
177 		Map<String, Formatter> formattersForPrimaryKeyFields = new HashMap<String, Formatter>();
178 
179 		for (String pkFieldName : keyNames) {
180 			Formatter formatter = null;
181 
182 			Class<? extends Formatter> formatterClass = dataDictionaryService.getAttributeFormatter(boClass, pkFieldName);
183 			if (formatterClass != null) {
184 				try {
185 					formatter = formatterClass.newInstance();
186 				} catch (Exception e) {
187 					e.printStackTrace();
188 				}
189 			}
190         }
191 		return formattersForPrimaryKeyFields;
192 	}
193 
194 	public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
195 		this.dataDictionaryService = dataDictionaryService;
196 	}
197 
198 	protected BusinessObjectAuthorizationService getBusinessObjectAuthorizationService() {
199 		if (businessObjectAuthorizationService == null) {
200 			businessObjectAuthorizationService = KNSServiceLocator.getBusinessObjectAuthorizationService();
201 		}
202 		return businessObjectAuthorizationService;
203 	}
204 
205     public void setLegacyDataAdapter(LegacyDataAdapter legacyDataAdapter) {
206         this.legacyDataAdapter = legacyDataAdapter;
207     }
208 
209     protected LegacyDataAdapter getLegacyDataAdapter() {
210         if (legacyDataAdapter == null) {
211             legacyDataAdapter = KRADServiceLocatorWeb.getLegacyDataAdapter();
212         }
213         return legacyDataAdapter;
214     }
215 }
216