001/*
002 * Copyright 2007-2008 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.ole.sys.businessobject.lookup;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.commons.lang.StringUtils;
025import org.apache.log4j.Logger;
026import org.kuali.ole.sys.OLEPropertyConstants;
027import org.kuali.ole.sys.businessobject.BusinessObjectProperty;
028import org.kuali.ole.sys.businessobject.DataMappingFieldDefinition;
029import org.kuali.ole.sys.businessobject.FunctionalFieldDescription;
030import org.kuali.ole.sys.service.OleBusinessObjectMetaDataService;
031import org.kuali.rice.kns.lookup.HtmlData;
032import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
033import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
034import org.kuali.rice.kns.lookup.LookupUtils;
035import org.kuali.rice.krad.bo.BusinessObject;
036import org.kuali.rice.krad.lookup.CollectionIncomplete;
037import org.kuali.rice.krad.util.BeanPropertyComparator;
038
039public class DataMappingFieldDefinitionLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
040    private Logger LOG = Logger.getLogger(DataMappingFieldDefinitionLookupableHelperServiceImpl.class);
041    private static final List<String> SORT_PROPERTIES = new ArrayList<String>();
042    static {
043        SORT_PROPERTIES.add(OLEPropertyConstants.NAMESPACE_CODE);
044        SORT_PROPERTIES.add(OLEPropertyConstants.FUNCTIONAL_FIELD_DESCRIPTION_BUSINESS_OBJECT_PROPERTY_COMPONENT_LABEL);
045        SORT_PROPERTIES.add(OLEPropertyConstants.FUNCTIONAL_FIELD_DESCRIPTION_BUSINESS_OBJECT_PROPERTY_LABEL);
046    }
047    protected OleBusinessObjectMetaDataService kfsBusinessObjectMetaDataService;
048
049    @Override
050    public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
051        List<FunctionalFieldDescription> functionalFieldDescriptions = kfsBusinessObjectMetaDataService.findFunctionalFieldDescriptions(fieldValues.get(OLEPropertyConstants.NAMESPACE_CODE), fieldValues.get(OLEPropertyConstants.FUNCTIONAL_FIELD_DESCRIPTION_BUSINESS_OBJECT_PROPERTY_COMPONENT_LABEL), fieldValues.get(OLEPropertyConstants.FUNCTIONAL_FIELD_DESCRIPTION_BUSINESS_OBJECT_PROPERTY_LABEL), fieldValues.get(OLEPropertyConstants.FUNCTIONAL_FIELD_DESCRIPTION_DESCRIPTION), "Y");
052        List<BusinessObjectProperty> businessObjectProperties = kfsBusinessObjectMetaDataService.findBusinessObjectProperties(fieldValues.get(OLEPropertyConstants.NAMESPACE_CODE), fieldValues.get(OLEPropertyConstants.FUNCTIONAL_FIELD_DESCRIPTION_BUSINESS_OBJECT_PROPERTY_COMPONENT_LABEL), fieldValues.get(OLEPropertyConstants.FUNCTIONAL_FIELD_DESCRIPTION_BUSINESS_OBJECT_PROPERTY_LABEL));
053        Map<String, BusinessObject> matches = new HashMap<String, BusinessObject>();
054        for (FunctionalFieldDescription functionalFieldDescription : functionalFieldDescriptions) {
055            if (kfsBusinessObjectMetaDataService.isMatch(functionalFieldDescription.getComponentClass(), functionalFieldDescription.getPropertyName(), fieldValues.get(OLEPropertyConstants.TABLE_NAME), fieldValues.get(OLEPropertyConstants.FIELD_NAME))) {
056                matches.put(functionalFieldDescription.getComponentClass() + functionalFieldDescription.getPropertyName(), functionalFieldDescription);
057            }
058        }
059        if (StringUtils.isBlank(fieldValues.get(OLEPropertyConstants.FUNCTIONAL_FIELD_DESCRIPTION_DESCRIPTION))) {
060            for (BusinessObjectProperty businessObjectProperty : businessObjectProperties) {
061                if (!matches.containsKey(businessObjectProperty.getComponentClass() + businessObjectProperty.getPropertyName()) && kfsBusinessObjectMetaDataService.isMatch(businessObjectProperty.getComponentClass(), businessObjectProperty.getPropertyName(), fieldValues.get(OLEPropertyConstants.TABLE_NAME), fieldValues.get(OLEPropertyConstants.FIELD_NAME))) {
062                    matches.put(businessObjectProperty.getComponentClass() + businessObjectProperty.getPropertyName(), businessObjectProperty);
063                }
064            }
065        }
066
067        Map<String, DataMappingFieldDefinition> dataMappingFieldDefinitions = new HashMap<String, DataMappingFieldDefinition>();
068        int searchResultsLimit = LookupUtils.getSearchResultsLimit(DataMappingFieldDefinition.class);
069        int iterationCount = 0;
070        for (BusinessObject businessObject : matches.values()) {
071            if (++iterationCount <= searchResultsLimit) {
072                if (businessObject instanceof FunctionalFieldDescription) {
073                    FunctionalFieldDescription functionalFieldDescription = (FunctionalFieldDescription) businessObject;
074                    dataMappingFieldDefinitions.put(functionalFieldDescription.getComponentClass() + functionalFieldDescription.getPropertyName(), kfsBusinessObjectMetaDataService.getDataMappingFieldDefinition(functionalFieldDescription));
075                }
076                else {
077                    BusinessObjectProperty businessObjectProperty = (BusinessObjectProperty) businessObject;
078                    dataMappingFieldDefinitions.put(businessObjectProperty.getComponentClass() + businessObjectProperty.getPropertyName(), kfsBusinessObjectMetaDataService.getDataMappingFieldDefinition(businessObjectProperty.getComponentClass(), businessObjectProperty.getPropertyName()));
079                }
080            }
081            else {
082                break;
083            }
084        }
085
086        List<DataMappingFieldDefinition> searchResults = null;
087        if (matches.size() > searchResultsLimit) {
088            searchResults = new CollectionIncomplete(dataMappingFieldDefinitions.values(), Long.valueOf(matches.size()));
089        }
090        else {
091            searchResults = new CollectionIncomplete(dataMappingFieldDefinitions.values(), 0L);
092        }
093        Collections.sort(searchResults, new BeanPropertyComparator(getSortProperties(), true));
094        return searchResults;
095    }
096
097    @Override
098    public HtmlData getInquiryUrl(BusinessObject bo, String propertyName) {
099        AnchorHtmlData inquiryHref = (AnchorHtmlData)super.getInquiryUrl(bo, propertyName);
100        if (StringUtils.isNotBlank(inquiryHref.getHref())) {
101            inquiryHref.setHref(new StringBuffer(inquiryHref.getHref()).append("&").append(OLEPropertyConstants.COMPONENT_CLASS).append("=").append(((DataMappingFieldDefinition) bo).getComponentClass()).append("&").append(OLEPropertyConstants.PROPERTY_NAME).append("=").append(((DataMappingFieldDefinition) bo).getPropertyName()).toString());
102        }
103        return inquiryHref;
104    }
105
106    protected List<String> getSortProperties() {
107        return SORT_PROPERTIES;
108    }
109
110    public void setKfsBusinessObjectMetaDataService(OleBusinessObjectMetaDataService kfsBusinessObjectMetaDataService) {
111        this.kfsBusinessObjectMetaDataService = kfsBusinessObjectMetaDataService;
112    }
113}