001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.krad.service.impl;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.web.format.Formatter;
020import org.kuali.rice.kim.api.identity.Person;
021import org.kuali.rice.kns.document.authorization.BusinessObjectRestrictions;
022import org.kuali.rice.kns.document.authorization.FieldRestriction;
023import org.kuali.rice.kns.service.BusinessObjectAuthorizationService;
024import org.kuali.rice.kns.service.KNSServiceLocator;
025import org.kuali.rice.krad.bo.BusinessObject;
026import org.kuali.rice.krad.data.KradDataServiceLocator;
027import org.kuali.rice.krad.datadictionary.InactivationBlockingMetadata;
028import org.kuali.rice.krad.datadictionary.mask.MaskFormatter;
029import org.kuali.rice.krad.service.DataDictionaryService;
030import org.kuali.rice.krad.service.InactivationBlockingDetectionService;
031import org.kuali.rice.krad.service.InactivationBlockingDisplayService;
032import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
033import org.kuali.rice.krad.service.LegacyDataAdapter;
034import org.kuali.rice.krad.util.GlobalVariables;
035
036import java.util.ArrayList;
037import java.util.Collection;
038import java.util.HashMap;
039import java.util.List;
040import java.util.Map;
041
042/**
043 * This is a description of what this class does - wliang don't forget to fill this in.
044 *
045 * @author Kuali Rice Team (rice.collab@kuali.org)
046 *
047 */
048public class InactivationBlockingDisplayServiceImpl implements InactivationBlockingDisplayService {
049        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(InactivationBlockingDetectionServiceImpl.class);
050
051        private DataDictionaryService dataDictionaryService;
052        private BusinessObjectAuthorizationService businessObjectAuthorizationService;
053    private LegacyDataAdapter legacyDataAdapter;
054
055        @Override
056        public List<String> listAllBlockerRecords(BusinessObject blockedBo, InactivationBlockingMetadata inactivationBlockingMetadata) {
057        String inactivationBlockingDetectionServiceBeanName = inactivationBlockingMetadata.getInactivationBlockingDetectionServiceBeanName();
058        if (StringUtils.isBlank(inactivationBlockingDetectionServiceBeanName)) {
059            inactivationBlockingDetectionServiceBeanName = KRADServiceLocatorWeb.DEFAULT_INACTIVATION_BLOCKING_DETECTION_SERVICE;
060        }
061        InactivationBlockingDetectionService inactivationBlockingDetectionService = KRADServiceLocatorWeb
062                .getInactivationBlockingDetectionService(inactivationBlockingDetectionServiceBeanName);
063
064        Collection<BusinessObject> collection = inactivationBlockingDetectionService.listAllBlockerRecords(blockedBo, inactivationBlockingMetadata);
065
066        Map<String, Formatter> formatters = getFormattersForPrimaryKeyFields(inactivationBlockingMetadata.getBlockingReferenceBusinessObjectClass());
067
068        List<String> displayValues = new ArrayList<String>();
069        List<String> pkFieldNames = getLegacyDataAdapter().listPrimaryKeyFieldNames(inactivationBlockingMetadata.getBlockingReferenceBusinessObjectClass());
070        Person user = GlobalVariables.getUserSession().getPerson();
071
072        for (BusinessObject element : collection) {
073                StringBuilder buf = new StringBuilder();
074
075                // the following method will return a restriction for all DD-defined attributes
076                BusinessObjectRestrictions businessObjectRestrictions = getBusinessObjectAuthorizationService().getLookupResultRestrictions(element, user);
077                for (int i = 0; i < pkFieldNames.size(); i++) {
078                        String pkFieldName = pkFieldNames.get(i);
079                        Object value = KradDataServiceLocator.getDataObjectService().wrap(element).getPropertyValueNullSafe(pkFieldName);
080
081                        String displayValue = null;
082                        if (!businessObjectRestrictions.hasRestriction(pkFieldName)) {
083                                Formatter formatter = formatters.get(pkFieldName);
084                                if (formatter != null) {
085                                        displayValue = (String) formatter.format(value);
086                                }
087                                else {
088                                        displayValue = String.valueOf(value);
089                                }
090                        }
091                        else {
092                                FieldRestriction fieldRestriction = businessObjectRestrictions.getFieldRestriction(pkFieldName);
093                                if (fieldRestriction.isMasked() || fieldRestriction.isPartiallyMasked()) {
094                                        MaskFormatter maskFormatter = fieldRestriction.getMaskFormatter();
095                                                displayValue = maskFormatter.maskValue(value);
096                                }
097                                else {
098                                        // there was a restriction, but we did not know how to obey it.
099                                        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