001: /*
002: * Copyright 2005-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.service.impl;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.apache.commons.lang.StringUtils;
025: import org.kuali.RiceConstants;
026: import org.kuali.core.bo.PersistableBusinessObject;
027: import org.kuali.core.dao.LookupDao;
028: import org.kuali.core.service.AuthorizationService;
029: import org.kuali.core.service.DataDictionaryService;
030: import org.kuali.core.service.KualiConfigurationService;
031: import org.kuali.core.service.LookupService;
032: import org.kuali.core.service.PersistenceStructureService;
033: import org.springframework.transaction.annotation.Transactional;
034:
035: /**
036: * This class is the service implementation for the Lookup structure. It Provides a generic search mechanism against Business
037: * Objects. This is the default implementation, that is delivered with Kuali.
038: */
039: @Transactional
040: public class LookupServiceImpl implements LookupService {
041: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
042: .getLogger(LookupServiceImpl.class);
043: private static final Collection EMPTY_COLLECTION = new ArrayList(0);
044:
045: private LookupDao lookupDao;
046: private KualiConfigurationService kualiConfigurationService;
047: private AuthorizationService authorizationService;
048: private DataDictionaryService dataDictionaryService;
049: private PersistenceStructureService persistenceStructureService;
050:
051: public Collection findCollectionBySearchUnbounded(Class example,
052: Map formProps) {
053: return findCollectionBySearchHelper(example, formProps, true);
054: }
055:
056: /**
057: * Returns a collection of objects based on the given search parameters.
058: *
059: * @return Collection returned from the search
060: */
061: public Collection findCollectionBySearch(Class example,
062: Map formProps) {
063: return findCollectionBySearchHelper(example, formProps, false);
064: }
065:
066: public Collection findCollectionBySearchHelper(Class example,
067: Map formProps, boolean unbounded) {
068: return lookupDao.findCollectionBySearchHelper(example,
069: formProps, unbounded,
070: allPrimaryKeyValuesPresentAndNotWildcard(example,
071: formProps));
072: }
073:
074: /**
075: * Retrieves a Object based on the search criteria, which should uniquely identify a record.
076: *
077: * @return Object returned from the search
078: */
079: public Object findObjectBySearch(Class example, Map formProps) {
080: if (example == null || formProps == null) {
081: throw new IllegalArgumentException(
082: "Object and Map must not be null");
083: }
084:
085: PersistableBusinessObject obj = null;
086: try {
087: obj = (PersistableBusinessObject) example.newInstance();
088: } catch (IllegalAccessException e) {
089: throw new RuntimeException("Cannot get new instance of "
090: + example.getName(), e);
091: } catch (InstantiationException e) {
092: throw new RuntimeException("Cannot instantiate "
093: + example.getName(), e);
094: }
095:
096: return lookupDao.findObjectByMap(obj, formProps);
097: }
098:
099: public boolean allPrimaryKeyValuesPresentAndNotWildcard(
100: Class boClass, Map formProps) {
101: List pkFields = persistenceStructureService
102: .listPrimaryKeyFieldNames(boClass);
103: Iterator pkIter = pkFields.iterator();
104: boolean returnVal = true;
105: while (returnVal && pkIter.hasNext()) {
106: String pkName = (String) pkIter.next();
107: String pkValue = (String) formProps.get(pkName);
108:
109: if (StringUtils.isBlank(pkValue)) {
110: returnVal = false;
111: } else if (StringUtils.indexOfAny(pkValue,
112: RiceConstants.QUERY_CHARACTERS) != -1) {
113: returnVal = false;
114: }
115: }
116: return returnVal;
117: }
118:
119: /**
120: * @return Returns the lookupDao.
121: */
122: public LookupDao getLookupDao() {
123: return lookupDao;
124: }
125:
126: /**
127: * @param lookupDao The lookupDao to set.
128: */
129: public void setLookupDao(LookupDao lookupDao) {
130: this .lookupDao = lookupDao;
131: }
132:
133: public KualiConfigurationService getKualiConfigurationService() {
134: return kualiConfigurationService;
135: }
136:
137: public void setKualiConfigurationService(
138: KualiConfigurationService kualiConfigurationService) {
139: this .kualiConfigurationService = kualiConfigurationService;
140: }
141:
142: public void setAuthorizationService(
143: AuthorizationService authorizationService) {
144: this .authorizationService = authorizationService;
145: }
146:
147: public void setDataDictionaryService(
148: DataDictionaryService dataDictionaryService) {
149: this .dataDictionaryService = dataDictionaryService;
150: }
151:
152: public void setPersistenceStructureService(
153: PersistenceStructureService persistenceStructureService) {
154: this.persistenceStructureService = persistenceStructureService;
155: }
156: }
|