View Javadoc

1   /*
2    * Copyright 2006-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.rice.kns.lookup;
17  
18  import java.beans.PropertyDescriptor;
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.regex.Pattern;
26  
27  import org.apache.commons.beanutils.PropertyUtils;
28  import org.kuali.rice.kns.authorization.BusinessObjectRestrictions;
29  import org.kuali.rice.kns.bo.BusinessObject;
30  import org.kuali.rice.kns.bo.BusinessObjectAttributeEntry;
31  import org.kuali.rice.kns.datadictionary.control.ControlDefinition;
32  import org.kuali.rice.kns.service.DataDictionaryService;
33  import org.kuali.rice.kns.service.KNSServiceLocator;
34  import org.kuali.rice.kns.util.BeanPropertyComparator;
35  import org.kuali.rice.kns.util.KNSConstants;
36  import org.kuali.rice.kns.web.struts.form.LookupForm;
37  
38  public class DictionaryLookupableHelperServiceImpl extends AbstractLookupableHelperServiceImpl {
39      private static final long serialVersionUID = 970484069493741447L;
40      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DictionaryLookupableHelperServiceImpl.class);
41      private static final List IGNORED_FIELDS = Arrays.asList(new String[] { "class", "validationNumber" });
42  
43  
44      
45      @Override
46      public List getSearchResults(Map<String, String> fieldValues) {
47          throw new UnsupportedOperationException("getSearchResults not supported for DictionaryLookupableHelperServiceImpl");
48      }
49  
50  
51      /**
52       * Overrides the default lookupable search to provide a search against the BusinessObjectDictionaryService to retrieve attribute
53       * definitions.
54       */
55      public List getSearchResults(Map fieldValues, Map fieldConversions) {
56          setBackLocation((String) fieldValues.get(KNSConstants.BACK_LOCATION));
57          setDocFormKey((String) fieldValues.get(KNSConstants.DOC_FORM_KEY));
58  
59          List searchResults = new ArrayList();
60          DataDictionaryService dataDictionaryService = KNSServiceLocator.getDataDictionaryService();
61          try {
62              String boClassName = (String) fieldValues.get(KNSConstants.DICTIONARY_BO_NAME);
63  
64              // get bo class to query on
65              Class boClass = Class.forName(boClassName);
66  
67              // use reflection to get the properties for the bo
68              // TODO: Get list of attributes from dictionary service
69              PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(boClass);
70              for (int i = 0; i < descriptors.length; ++i) {
71                  PropertyDescriptor propertyDescriptor = descriptors[i];
72  
73                  // skip fields in IGNORED_FIELDS
74                  if (IGNORED_FIELDS.contains(propertyDescriptor.getName())) {
75                      continue;
76                  }
77  
78  
79                  // ignore collection attributes for now
80                  // Set BusinessObjectAttributeEntry by querying dictionary service
81                  if (!Collection.class.isAssignableFrom(propertyDescriptor.getPropertyType())) {
82                      String propertyName = propertyDescriptor.getName();
83  
84                      BusinessObjectAttributeEntry attributeEntry = new BusinessObjectAttributeEntry();
85                      attributeEntry.setAttributeName(propertyName);
86                      attributeEntry.setAttributeLabel(dataDictionaryService.getAttributeLabel(boClass, propertyName));
87                      attributeEntry.setAttributeSummary(dataDictionaryService.getAttributeSummary(boClass, propertyName));
88  
89                      Integer maxLength = dataDictionaryService.getAttributeMaxLength(boClass, propertyName);
90                      if (maxLength != null) {
91                          attributeEntry.setAttributeMaxLength(maxLength.toString());
92                      }
93  
94                      Pattern validationExpression = dataDictionaryService.getAttributeValidatingExpression(boClass, propertyName);
95                      if (validationExpression != null) {
96                          attributeEntry.setAttributeValidatingExpression(validationExpression.pattern());
97                      }
98                      ControlDefinition controlDef = dataDictionaryService.getAttributeControlDefinition(boClass, propertyName);
99                      if (controlDef != null) {
100                         attributeEntry.setAttributeControlType(controlDef.toString());
101                     }
102                     Class formatterClass = dataDictionaryService.getAttributeFormatter(boClass, propertyName);
103                     if (formatterClass != null) {
104                         attributeEntry.setAttributeFormatterClassName(formatterClass.getName());
105                     }
106 
107                     // add to result list
108                     searchResults.add(attributeEntry);
109                 }
110 
111             }
112 
113             // sort list if default sort column given
114             List defaultSortColumns = getDefaultSortColumns();
115             if (defaultSortColumns.size() > 0) {
116                 Collections.sort(searchResults, new BeanPropertyComparator(getDefaultSortColumns(), true));
117             }
118         }
119         catch (ClassNotFoundException e) {
120             LOG.error("Class not found for bo search name" + e.getMessage());
121             throw new RuntimeException("Class not found for bo search name" + e.getMessage());
122         }
123 
124         return searchResults;
125     }
126 
127 
128     /**
129      * @see org.kuali.rice.kns.lookup.Lookupable#getReturnUrl(java.lang.Object, java.util.Map, java.lang.String)
130      */
131     @Override
132     public HtmlData getReturnUrl(BusinessObject bo, LookupForm lookupForm, List returnKeys, BusinessObjectRestrictions businessObjectRestrictions) {
133         return getEmptyAnchorHtmlData();
134     }
135 
136 	/***
137      * TODO: Revisit whether this method is needed here at all
138 	 * 
139 	 * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getActionUrlHref(org.kuali.rice.kns.bo.BusinessObject, java.lang.String, java.util.List)
140 	 */
141     @Override
142     protected String getActionUrlHref(BusinessObject businessObject, String methodToCall, List pkNames){
143         return KNSConstants.EMPTY_STRING;
144     }
145 
146     /**
147      * @see org.kuali.rice.kns.lookup.Lookupable#getDefaultReturnType()
148      */
149     public List getDefaultReturnType() {
150         return new ArrayList();
151     }
152 
153     /**
154      * @see org.kuali.rice.kns.lookup.KualiLookupableImpl#getReturnKeys()
155      */
156     @Override
157     public List getReturnKeys() {
158         return new ArrayList();
159     }
160 }