001 /** 002 * Copyright 2005-2012 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.krad.service.impl; 017 018 import java.util.ArrayList; 019 import java.util.Collection; 020 import java.util.HashMap; 021 import java.util.List; 022 import java.util.Map; 023 024 import org.apache.commons.lang.StringUtils; 025 import org.kuali.rice.core.web.format.Formatter; 026 import org.kuali.rice.kim.api.identity.Person; 027 import org.kuali.rice.kns.document.authorization.BusinessObjectRestrictions; 028 import org.kuali.rice.kns.document.authorization.FieldRestriction; 029 import org.kuali.rice.kns.service.BusinessObjectAuthorizationService; 030 import org.kuali.rice.kns.service.KNSServiceLocator; 031 import org.kuali.rice.krad.bo.BusinessObject; 032 import org.kuali.rice.krad.datadictionary.InactivationBlockingMetadata; 033 import org.kuali.rice.krad.datadictionary.mask.MaskFormatter; 034 import org.kuali.rice.krad.service.*; 035 import org.kuali.rice.krad.util.GlobalVariables; 036 import org.kuali.rice.krad.util.ObjectUtils; 037 038 /** 039 * This is a description of what this class does - wliang don't forget to fill this in. 040 * 041 * @author Kuali Rice Team (rice.collab@kuali.org) 042 * 043 */ 044 public class InactivationBlockingDisplayServiceImpl implements InactivationBlockingDisplayService { 045 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(InactivationBlockingDetectionServiceImpl.class); 046 047 private PersistenceService persistenceService; 048 private DataDictionaryService dataDictionaryService; 049 private PersistenceStructureService persistenceStructureService; 050 private BusinessObjectAuthorizationService businessObjectAuthorizationService; 051 052 /** 053 * This overridden method ... 054 * 055 * @see org.kuali.rice.krad.service.InactivationBlockingDisplayService#listAllBlockerRecords(org.kuali.rice.krad.bo.BusinessObject, org.kuali.rice.krad.datadictionary.InactivationBlockingMetadata) 056 */ 057 public List<String> listAllBlockerRecords(BusinessObject blockedBo, InactivationBlockingMetadata inactivationBlockingMetadata) { 058 String inactivationBlockingDetectionServiceBeanName = inactivationBlockingMetadata.getInactivationBlockingDetectionServiceBeanName(); 059 if (StringUtils.isBlank(inactivationBlockingDetectionServiceBeanName)) { 060 inactivationBlockingDetectionServiceBeanName = KRADServiceLocatorWeb.DEFAULT_INACTIVATION_BLOCKING_DETECTION_SERVICE; 061 } 062 InactivationBlockingDetectionService inactivationBlockingDetectionService = KRADServiceLocatorWeb 063 .getInactivationBlockingDetectionService(inactivationBlockingDetectionServiceBeanName); 064 065 Collection<BusinessObject> collection = inactivationBlockingDetectionService.listAllBlockerRecords(blockedBo, inactivationBlockingMetadata); 066 067 Map<String, Formatter> formatters = getFormattersForPrimaryKeyFields(inactivationBlockingMetadata.getBlockingReferenceBusinessObjectClass()); 068 069 List<String> displayValues = new ArrayList<String>(); 070 List<String> pkFieldNames = persistenceStructureService.listPrimaryKeyFieldNames(inactivationBlockingMetadata.getBlockingReferenceBusinessObjectClass()); 071 Person user = GlobalVariables.getUserSession().getPerson(); 072 073 for (BusinessObject element : collection) { 074 StringBuilder buf = new StringBuilder(); 075 076 // the following method will return a restriction for all DD-defined attributes 077 BusinessObjectRestrictions businessObjectRestrictions = getBusinessObjectAuthorizationService().getLookupResultRestrictions(element, user); 078 for (int i = 0; i < pkFieldNames.size(); i++) { 079 String pkFieldName = pkFieldNames.get(i); 080 Object value = ObjectUtils.getPropertyValue(element, pkFieldName); 081 082 String displayValue = null; 083 if (!businessObjectRestrictions.hasRestriction(pkFieldName)) { 084 Formatter formatter = formatters.get(pkFieldName); 085 if (formatter != null) { 086 displayValue = (String) formatter.format(value); 087 } 088 else { 089 displayValue = String.valueOf(value); 090 } 091 } 092 else { 093 FieldRestriction fieldRestriction = businessObjectRestrictions.getFieldRestriction(pkFieldName); 094 if (fieldRestriction.isMasked() || fieldRestriction.isPartiallyMasked()) { 095 MaskFormatter maskFormatter = fieldRestriction.getMaskFormatter(); 096 displayValue = maskFormatter.maskValue(value); 097 } 098 else { 099 // 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