View Javadoc

1   /**
2    * Copyright 2005-2012 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 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   * This is a description of what this class does - wliang don't forget to fill this in.
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   *
43   */
44  public class InactivationBlockingDisplayServiceImpl implements InactivationBlockingDisplayService {
45  	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  	 * This overridden method ...
54  	 *
55  	 * @see org.kuali.rice.krad.service.InactivationBlockingDisplayService#listAllBlockerRecords(org.kuali.rice.krad.bo.BusinessObject, org.kuali.rice.krad.datadictionary.InactivationBlockingMetadata)
56  	 */
57  	public List<String> listAllBlockerRecords(BusinessObject blockedBo, InactivationBlockingMetadata inactivationBlockingMetadata) {
58          String inactivationBlockingDetectionServiceBeanName = inactivationBlockingMetadata.getInactivationBlockingDetectionServiceBeanName();
59          if (StringUtils.isBlank(inactivationBlockingDetectionServiceBeanName)) {
60              inactivationBlockingDetectionServiceBeanName = KRADServiceLocatorWeb.DEFAULT_INACTIVATION_BLOCKING_DETECTION_SERVICE;
61          }
62          InactivationBlockingDetectionService inactivationBlockingDetectionService = KRADServiceLocatorWeb
63                  .getInactivationBlockingDetectionService(inactivationBlockingDetectionServiceBeanName);
64  
65          Collection<BusinessObject> collection = inactivationBlockingDetectionService.listAllBlockerRecords(blockedBo, inactivationBlockingMetadata);
66  
67          Map<String, Formatter> formatters = getFormattersForPrimaryKeyFields(inactivationBlockingMetadata.getBlockingReferenceBusinessObjectClass());
68  
69          List<String> displayValues = new ArrayList<String>();
70          List<String> pkFieldNames = persistenceStructureService.listPrimaryKeyFieldNames(inactivationBlockingMetadata.getBlockingReferenceBusinessObjectClass());
71          Person user = GlobalVariables.getUserSession().getPerson();
72          
73          for (BusinessObject element : collection) {
74          	StringBuilder buf = new StringBuilder();
75  
76          	// the following method will return a restriction for all DD-defined attributes
77          	BusinessObjectRestrictions businessObjectRestrictions = getBusinessObjectAuthorizationService().getLookupResultRestrictions(element, user);
78          	for (int i = 0; i < pkFieldNames.size(); i++) {
79          		String pkFieldName = pkFieldNames.get(i);
80          		Object value = ObjectUtils.getPropertyValue(element, pkFieldName);
81  
82          		String displayValue = null;
83          		if (!businessObjectRestrictions.hasRestriction(pkFieldName)) {
84          			Formatter formatter = formatters.get(pkFieldName);
85          			if (formatter != null) {
86          				displayValue = (String) formatter.format(value);
87          			}
88          			else {
89          				displayValue = String.valueOf(value);
90          			}
91          		}
92          		else {
93          			FieldRestriction fieldRestriction = businessObjectRestrictions.getFieldRestriction(pkFieldName);
94          			if (fieldRestriction.isMasked() || fieldRestriction.isPartiallyMasked()) {
95  		    			MaskFormatter maskFormatter = fieldRestriction.getMaskFormatter();
96  						displayValue = maskFormatter.maskValue(value);
97          			}
98          			else {
99          				// there was a restriction, but we did not know how to obey it.
100         				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         		buf.append(displayValue);
105         		if (i < pkFieldNames.size() - 1) {
106         			buf.append(" - ");
107         		}
108         	}
109 
110         	displayValues.add(buf.toString());
111         }
112 		return displayValues;
113 	}
114 
115 	protected Map<String, Formatter> getFormattersForPrimaryKeyFields(Class boClass) {
116 		List<String> keyNames = persistenceStructureService.listPrimaryKeyFieldNames(boClass);
117 		Map<String, Formatter> formattersForPrimaryKeyFields = new HashMap<String, Formatter>();
118 
119 		for (String pkFieldName : keyNames) {
120 			Formatter formatter = null;
121 
122 			Class<? extends Formatter> formatterClass = dataDictionaryService.getAttributeFormatter(boClass, pkFieldName);
123 			if (formatterClass != null) {
124 				try {
125 					formatter = formatterClass.newInstance();
126 				} catch (Exception e) {
127 					e.printStackTrace();
128 				}
129 			}
130         }
131 		return formattersForPrimaryKeyFields;
132 	}
133 
134 	public void setPersistenceService(PersistenceService persistenceService) {
135 		this.persistenceService = persistenceService;
136 	}
137 
138 	public void setPersistenceStructureService(
139 			PersistenceStructureService persistenceStructureService) {
140 		this.persistenceStructureService = persistenceStructureService;
141 	}
142 
143 	public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
144 		this.dataDictionaryService = dataDictionaryService;
145 	}
146 	
147 	protected BusinessObjectAuthorizationService getBusinessObjectAuthorizationService() {
148 		if (businessObjectAuthorizationService == null) {
149 			businessObjectAuthorizationService = KNSServiceLocator.getBusinessObjectAuthorizationService();
150 		}
151 		return businessObjectAuthorizationService;
152 	}
153 }
154