001/** 002 * Copyright 2005-2016 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 */ 016package org.kuali.rice.krad.service.impl; 017 018import org.apache.commons.lang.StringUtils; 019import org.kuali.rice.core.api.config.property.ConfigurationService; 020import org.kuali.rice.core.api.search.SearchOperator; 021import org.kuali.rice.kns.service.KNSServiceLocator; 022import org.kuali.rice.krad.dao.LookupDao; 023import org.kuali.rice.krad.service.LookupService; 024 025import java.util.Collection; 026import java.util.Iterator; 027import java.util.List; 028import java.util.Map; 029 030/** 031 * Service implementation for the Lookup structure. It Provides a generic search 032 * mechanism against Business Objects. This is the default implementation, that 033 * is delivered with Kuali. 034 * 035 * @author Kuali Rice Team (rice.collab@kuali.org) 036 */ 037public class LookupServiceImpl implements LookupService { 038 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupServiceImpl.class); 039 040 private LookupDao lookupDao; 041 private ConfigurationService kualiConfigurationService; 042 043 public <T extends Object> Collection<T> findCollectionBySearchUnbounded(Class<T> example, 044 Map<String, String> formProps) { 045 return findCollectionBySearchHelper(example, formProps, true); 046 } 047 048 /** 049 * Returns a collection of objects based on the given search parameters. 050 * 051 * @return Collection returned from the search 052 */ 053 public <T extends Object> Collection<T> findCollectionBySearch(Class<T> example, Map<String, String> formProps) { 054 return findCollectionBySearchHelper(example, formProps, false); 055 } 056 057 public <T extends Object> Collection<T> findCollectionBySearchHelper(Class<T> example, 058 Map<String, String> formProps, boolean unbounded) { 059 return lookupDao.findCollectionBySearchHelper(example, formProps, unbounded, 060 allPrimaryKeyValuesPresentAndNotWildcard(example, formProps)); 061 } 062 063 /** 064 * Retrieves a Object based on the search criteria, which should uniquely 065 * identify a record. 066 * 067 * @return Object returned from the search 068 */ 069 public <T extends Object> T findObjectBySearch(Class<T> example, Map<String, String> formProps) { 070 if (example == null || formProps == null) { 071 throw new IllegalArgumentException("Object and Map must not be null"); 072 } 073 074 T obj = null; 075 try { 076 obj = example.newInstance(); 077 } catch (IllegalAccessException e) { 078 throw new RuntimeException("Cannot get new instance of " + example.getName(), e); 079 } catch (InstantiationException e) { 080 throw new RuntimeException("Cannot instantiate " + example.getName(), e); 081 } 082 083 return lookupDao.findObjectByMap(obj, formProps); 084 } 085 086 public boolean allPrimaryKeyValuesPresentAndNotWildcard(Class<?> boClass, Map<String, String> formProps) { 087 List<String> pkFields = KNSServiceLocator.getBusinessObjectMetaDataService().listPrimaryKeyFieldNames( 088 boClass); 089 Iterator<String> pkIter = pkFields.iterator(); 090 boolean returnVal = true; 091 while (returnVal && pkIter.hasNext()) { 092 String pkName = pkIter.next(); 093 String pkValue = formProps.get(pkName); 094 095 if (StringUtils.isBlank(pkValue)) { 096 returnVal = false; 097 } else { 098 for (SearchOperator op : SearchOperator.QUERY_CHARACTERS) { 099 if (pkValue.contains(op.op())) { 100 returnVal = false; 101 break; 102 } 103 } 104 } 105 } 106 return returnVal; 107 } 108 109 /** 110 * @return Returns the lookupDao. 111 */ 112 public LookupDao getLookupDao() { 113 return lookupDao; 114 } 115 116 /** 117 * @param lookupDao The lookupDao to set. 118 */ 119 public void setLookupDao(LookupDao lookupDao) { 120 this.lookupDao = lookupDao; 121 } 122 123 public ConfigurationService getKualiConfigurationService() { 124 return kualiConfigurationService; 125 } 126 127 public void setKualiConfigurationService(ConfigurationService kualiConfigurationService) { 128 this.kualiConfigurationService = kualiConfigurationService; 129 } 130}