001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.core.lookup;
017:
018: import java.beans.PropertyDescriptor;
019: import java.util.ArrayList;
020: import java.util.Arrays;
021: import java.util.Collection;
022: import java.util.Collections;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.regex.Pattern;
026:
027: import org.apache.commons.beanutils.PropertyUtils;
028: import org.kuali.RiceConstants;
029: import org.kuali.core.bo.BusinessObject;
030: import org.kuali.core.bo.BusinessObjectAttributeEntry;
031: import org.kuali.core.datadictionary.control.ControlDefinition;
032: import org.kuali.core.service.DataDictionaryService;
033: import org.kuali.core.util.BeanPropertyComparator;
034: import org.kuali.rice.KNSServiceLocator;
035:
036: public class DictionaryLookupableHelperServiceImpl extends
037: AbstractLookupableHelperServiceImpl {
038: private static final long serialVersionUID = 970484069493741447L;
039: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
040: .getLogger(DictionaryLookupableHelperServiceImpl.class);
041: private static final List IGNORED_FIELDS = Arrays
042: .asList(new String[] { "class", "validationNumber" });
043:
044: @Override
045: public List getSearchResults(Map<String, String> fieldValues) {
046: throw new UnsupportedOperationException(
047: "getSearchResults not supported for DictionaryLookupableHelperServiceImpl");
048: }
049:
050: /**
051: * Overrides the default lookupable search to provide a search against the BusinessObjectDictionaryService to retrieve attribute
052: * definitions.
053: */
054: public List getSearchResults(Map fieldValues, Map fieldConversions) {
055: setBackLocation((String) fieldValues
056: .get(RiceConstants.BACK_LOCATION));
057: setDocFormKey((String) fieldValues
058: .get(RiceConstants.DOC_FORM_KEY));
059:
060: List searchResults = new ArrayList();
061: DataDictionaryService dataDictionaryService = KNSServiceLocator
062: .getDataDictionaryService();
063: try {
064: String boClassName = (String) fieldValues
065: .get(RiceConstants.DICTIONARY_BO_NAME);
066:
067: // get bo class to query on
068: Class boClass = Class.forName(boClassName);
069:
070: // use reflection to get the properties for the bo
071: // TODO: Get list of attributes from dictionary service
072: PropertyDescriptor[] descriptors = PropertyUtils
073: .getPropertyDescriptors(boClass);
074: for (int i = 0; i < descriptors.length; ++i) {
075: PropertyDescriptor propertyDescriptor = descriptors[i];
076:
077: // skip fields in IGNORED_FIELDS
078: if (IGNORED_FIELDS.contains(propertyDescriptor
079: .getName())) {
080: continue;
081: }
082:
083: // ignore collection attributes for now
084: // Set BusinessObjectAttributeEntry by querying dictionary service
085: if (!Collection.class
086: .isAssignableFrom(propertyDescriptor
087: .getPropertyType())) {
088: String propertyName = propertyDescriptor.getName();
089:
090: BusinessObjectAttributeEntry attributeEntry = new BusinessObjectAttributeEntry();
091: attributeEntry.setAttributeName(propertyName);
092: attributeEntry
093: .setAttributeLabel(dataDictionaryService
094: .getAttributeLabel(boClass,
095: propertyName));
096: attributeEntry
097: .setAttributeSummary(dataDictionaryService
098: .getAttributeSummary(boClass,
099: propertyName));
100:
101: Integer maxLength = dataDictionaryService
102: .getAttributeMaxLength(boClass,
103: propertyName);
104: if (maxLength != null) {
105: attributeEntry.setAttributeMaxLength(maxLength
106: .toString());
107: }
108:
109: Pattern validationExpression = dataDictionaryService
110: .getAttributeValidatingExpression(boClass,
111: propertyName);
112: if (validationExpression != null) {
113: attributeEntry
114: .setAttributeValidatingExpression(validationExpression
115: .pattern());
116: }
117: ControlDefinition controlDef = dataDictionaryService
118: .getAttributeControlDefinition(boClass,
119: propertyName);
120: if (controlDef != null) {
121: attributeEntry
122: .setAttributeControlType(controlDef
123: .toString());
124: }
125: Class formatterClass = dataDictionaryService
126: .getAttributeFormatter(boClass,
127: propertyName);
128: if (formatterClass != null) {
129: attributeEntry
130: .setAttributeFormatterClassName(formatterClass
131: .getName());
132: }
133:
134: // add to result list
135: searchResults.add(attributeEntry);
136: }
137:
138: }
139:
140: // sort list if default sort column given
141: List defaultSortColumns = getDefaultSortColumns();
142: if (defaultSortColumns.size() > 0) {
143: Collections.sort(searchResults,
144: new BeanPropertyComparator(
145: getDefaultSortColumns(), true));
146: }
147: } catch (ClassNotFoundException e) {
148: LOG.error("Class not found for bo search name"
149: + e.getMessage());
150: throw new RuntimeException(
151: "Class not found for bo search name"
152: + e.getMessage());
153: }
154:
155: return searchResults;
156: }
157:
158: /**
159: * @see org.kuali.core.lookup.Lookupable#getReturnUrl(java.lang.Object, java.util.Map, java.lang.String)
160: */
161: @Override
162: public String getReturnUrl(BusinessObject bo, Map fieldConversions,
163: String lookupImpl) {
164: return RiceConstants.EMPTY_STRING;
165: }
166:
167: /**
168: * @see org.kuali.core.lookup.KualiLookupableImpl#getMaintenanceUrl(java.lang.Object, java.lang.String)
169: */
170: @Override
171: public String getMaintenanceUrl(BusinessObject bo,
172: String methodToCall) {
173: return RiceConstants.EMPTY_STRING;
174: }
175:
176: /**
177: * @see org.kuali.core.lookup.Lookupable#getDefaultReturnType()
178: */
179: public List getDefaultReturnType() {
180: return new ArrayList();
181: }
182:
183: /**
184: * @see org.kuali.core.lookup.KualiLookupableImpl#getReturnKeys()
185: */
186: @Override
187: public List getReturnKeys() {
188: return new ArrayList();
189: }
190: }
|