001 /*
002 * Copyright 2011 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.kfs.sys.businessobject.lookup;
017
018 import java.util.ArrayList;
019 import java.util.Collections;
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.apache.log4j.Logger;
026 import org.kuali.kfs.sys.KFSPropertyConstants;
027 import org.kuali.kfs.sys.businessobject.BusinessObjectProperty;
028 import org.kuali.kfs.sys.businessobject.DataMappingFieldDefinition;
029 import org.kuali.kfs.sys.businessobject.FunctionalFieldDescription;
030 import org.kuali.kfs.sys.service.KfsBusinessObjectMetaDataService;
031 import org.kuali.rice.kns.bo.BusinessObject;
032 import org.kuali.rice.kns.lookup.CollectionIncomplete;
033 import org.kuali.rice.kns.lookup.HtmlData;
034 import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
035 import org.kuali.rice.kns.lookup.LookupUtils;
036 import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
037 import org.kuali.rice.kns.util.BeanPropertyComparator;
038
039 public 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(KFSPropertyConstants.NAMESPACE_CODE);
044 SORT_PROPERTIES.add(KFSPropertyConstants.FUNCTIONAL_FIELD_DESCRIPTION_BUSINESS_OBJECT_PROPERTY_COMPONENT_LABEL);
045 SORT_PROPERTIES.add(KFSPropertyConstants.FUNCTIONAL_FIELD_DESCRIPTION_BUSINESS_OBJECT_PROPERTY_LABEL);
046 }
047 protected KfsBusinessObjectMetaDataService kfsBusinessObjectMetaDataService;
048
049 @Override
050 public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
051 List<FunctionalFieldDescription> functionalFieldDescriptions = (List<FunctionalFieldDescription>) kfsBusinessObjectMetaDataService.findFunctionalFieldDescriptions(fieldValues.get(KFSPropertyConstants.NAMESPACE_CODE), fieldValues.get(KFSPropertyConstants.FUNCTIONAL_FIELD_DESCRIPTION_BUSINESS_OBJECT_PROPERTY_COMPONENT_LABEL), fieldValues.get(KFSPropertyConstants.FUNCTIONAL_FIELD_DESCRIPTION_BUSINESS_OBJECT_PROPERTY_LABEL), fieldValues.get(KFSPropertyConstants.FUNCTIONAL_FIELD_DESCRIPTION_DESCRIPTION), "Y");
052 List<BusinessObjectProperty> businessObjectProperties = kfsBusinessObjectMetaDataService.findBusinessObjectProperties(fieldValues.get(KFSPropertyConstants.NAMESPACE_CODE), fieldValues.get(KFSPropertyConstants.FUNCTIONAL_FIELD_DESCRIPTION_BUSINESS_OBJECT_PROPERTY_COMPONENT_LABEL), fieldValues.get(KFSPropertyConstants.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(KFSPropertyConstants.TABLE_NAME), fieldValues.get(KFSPropertyConstants.FIELD_NAME))) {
056 matches.put(functionalFieldDescription.getComponentClass() + functionalFieldDescription.getPropertyName(), functionalFieldDescription);
057 }
058 }
059 if (StringUtils.isBlank(fieldValues.get(KFSPropertyConstants.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(KFSPropertyConstants.TABLE_NAME), fieldValues.get(KFSPropertyConstants.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(KFSPropertyConstants.COMPONENT_CLASS).append("=").append(((DataMappingFieldDefinition) bo).getComponentClass()).append("&").append(KFSPropertyConstants.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(KfsBusinessObjectMetaDataService kfsBusinessObjectMetaDataService) {
111 this.kfsBusinessObjectMetaDataService = kfsBusinessObjectMetaDataService;
112 }
113 }