View Javadoc
1   /*
2    * Copyright 2007-2008 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.ole.sys.businessobject.lookup;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
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.apache.log4j.Logger;
26  import org.kuali.ole.sys.OLEPropertyConstants;
27  import org.kuali.ole.sys.businessobject.BusinessObjectProperty;
28  import org.kuali.ole.sys.businessobject.DataMappingFieldDefinition;
29  import org.kuali.ole.sys.businessobject.FunctionalFieldDescription;
30  import org.kuali.ole.sys.service.OleBusinessObjectMetaDataService;
31  import org.kuali.rice.kns.lookup.HtmlData;
32  import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
33  import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
34  import org.kuali.rice.kns.lookup.LookupUtils;
35  import org.kuali.rice.krad.bo.BusinessObject;
36  import org.kuali.rice.krad.lookup.CollectionIncomplete;
37  import org.kuali.rice.krad.util.BeanPropertyComparator;
38  
39  public class DataMappingFieldDefinitionLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
40      private Logger LOG = Logger.getLogger(DataMappingFieldDefinitionLookupableHelperServiceImpl.class);
41      private static final List<String> SORT_PROPERTIES = new ArrayList<String>();
42      static {
43          SORT_PROPERTIES.add(OLEPropertyConstants.NAMESPACE_CODE);
44          SORT_PROPERTIES.add(OLEPropertyConstants.FUNCTIONAL_FIELD_DESCRIPTION_BUSINESS_OBJECT_PROPERTY_COMPONENT_LABEL);
45          SORT_PROPERTIES.add(OLEPropertyConstants.FUNCTIONAL_FIELD_DESCRIPTION_BUSINESS_OBJECT_PROPERTY_LABEL);
46      }
47      protected OleBusinessObjectMetaDataService kfsBusinessObjectMetaDataService;
48  
49      @Override
50      public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
51          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");
52          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));
53          Map<String, BusinessObject> matches = new HashMap<String, BusinessObject>();
54          for (FunctionalFieldDescription functionalFieldDescription : functionalFieldDescriptions) {
55              if (kfsBusinessObjectMetaDataService.isMatch(functionalFieldDescription.getComponentClass(), functionalFieldDescription.getPropertyName(), fieldValues.get(OLEPropertyConstants.TABLE_NAME), fieldValues.get(OLEPropertyConstants.FIELD_NAME))) {
56                  matches.put(functionalFieldDescription.getComponentClass() + functionalFieldDescription.getPropertyName(), functionalFieldDescription);
57              }
58          }
59          if (StringUtils.isBlank(fieldValues.get(OLEPropertyConstants.FUNCTIONAL_FIELD_DESCRIPTION_DESCRIPTION))) {
60              for (BusinessObjectProperty businessObjectProperty : businessObjectProperties) {
61                  if (!matches.containsKey(businessObjectProperty.getComponentClass() + businessObjectProperty.getPropertyName()) && kfsBusinessObjectMetaDataService.isMatch(businessObjectProperty.getComponentClass(), businessObjectProperty.getPropertyName(), fieldValues.get(OLEPropertyConstants.TABLE_NAME), fieldValues.get(OLEPropertyConstants.FIELD_NAME))) {
62                      matches.put(businessObjectProperty.getComponentClass() + businessObjectProperty.getPropertyName(), businessObjectProperty);
63                  }
64              }
65          }
66  
67          Map<String, DataMappingFieldDefinition> dataMappingFieldDefinitions = new HashMap<String, DataMappingFieldDefinition>();
68          int searchResultsLimit = LookupUtils.getSearchResultsLimit(DataMappingFieldDefinition.class);
69          int iterationCount = 0;
70          for (BusinessObject businessObject : matches.values()) {
71              if (++iterationCount <= searchResultsLimit) {
72                  if (businessObject instanceof FunctionalFieldDescription) {
73                      FunctionalFieldDescription functionalFieldDescription = (FunctionalFieldDescription) businessObject;
74                      dataMappingFieldDefinitions.put(functionalFieldDescription.getComponentClass() + functionalFieldDescription.getPropertyName(), kfsBusinessObjectMetaDataService.getDataMappingFieldDefinition(functionalFieldDescription));
75                  }
76                  else {
77                      BusinessObjectProperty businessObjectProperty = (BusinessObjectProperty) businessObject;
78                      dataMappingFieldDefinitions.put(businessObjectProperty.getComponentClass() + businessObjectProperty.getPropertyName(), kfsBusinessObjectMetaDataService.getDataMappingFieldDefinition(businessObjectProperty.getComponentClass(), businessObjectProperty.getPropertyName()));
79                  }
80              }
81              else {
82                  break;
83              }
84          }
85  
86          List<DataMappingFieldDefinition> searchResults = null;
87          if (matches.size() > searchResultsLimit) {
88              searchResults = new CollectionIncomplete(dataMappingFieldDefinitions.values(), Long.valueOf(matches.size()));
89          }
90          else {
91              searchResults = new CollectionIncomplete(dataMappingFieldDefinitions.values(), 0L);
92          }
93          Collections.sort(searchResults, new BeanPropertyComparator(getSortProperties(), true));
94          return searchResults;
95      }
96  
97      @Override
98      public HtmlData getInquiryUrl(BusinessObject bo, String propertyName) {
99          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 }