View Javadoc

1   /*
2    * Copyright 2006-2011 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  
17  package org.kuali.rice.krms.impl.repository;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.commons.collections.CollectionUtils;
27  import org.apache.commons.lang.StringUtils;
28  import org.kuali.rice.krad.service.BusinessObjectService;
29  import org.kuali.rice.krad.service.KRADServiceLocator;
30  import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
31  
32  
33  public final class KrmsAttributeDefinitionServiceImpl implements KrmsAttributeDefinitionService {
34  
35      private BusinessObjectService businessObjectService;
36      private final Map<String,KrmsAttributeDefinitionBo> krmsAttributeDefinitionIdCache = Collections.synchronizedMap( new HashMap<String,KrmsAttributeDefinitionBo>() );
37  
38  	@Override
39  	/**
40  	* @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#convertAttributeKeys()
41  	*/
42  	public Map<String,String> convertAttributeKeys(Map<String,String> attributesByName, String namespace) {
43  		Map<String,String> attributesById = new HashMap<String,String>();
44  		if(attributesByName != null) {
45  			for(Map.Entry<String, String> attr : attributesByName.entrySet()) {
46  				String newKey = getKrmsAttributeId(attr.getKey(), namespace);
47  				if(StringUtils.isNotEmpty(newKey)) {
48  					attributesById.put(newKey, attr.getValue());
49  				}
50  			}
51  		}
52  		return attributesById;
53  	}
54     
55  	@Override
56  	/**
57  	* @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#getKrmsAttributeId()
58  	*/
59  	public String getKrmsAttributeId( String attributeName, String namespace) {
60  		String returnId = null;
61  		KrmsAttributeDefinitionBo bo = getKrmsAttributeBo(attributeName, namespace);
62  		if (bo != null){
63  			returnId = bo.getId();
64  		}
65  		return returnId;
66  	}
67      
68  	@Override
69  	/**
70  	* @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#getKrmsAttributeBo()
71  	*/
72  	public KrmsAttributeDefinitionBo getKrmsAttributeBo( String attributeName, String namespace) {
73  		String key = createKey(namespace, attributeName);
74  		if (krmsAttributeDefinitionIdCache.containsKey(key))
75  			return krmsAttributeDefinitionIdCache.get(key);		
76  
77  		KrmsAttributeDefinitionBo result = null;
78  		Map<String,Object> criteria = new HashMap<String,Object>( 3 );
79  		criteria.put( "name", attributeName );
80  		criteria.put( "namespace", namespace );
81          criteria.put( "active", Boolean.TRUE );
82  		Collection<KrmsAttributeDefinitionBo> defs = getBusinessObjectService().findMatching( KrmsAttributeDefinitionBo.class, criteria );
83  		if(CollectionUtils.isNotEmpty(defs)) {
84  			if (defs.size() > 1){
85  				throw new IllegalStateException("Multiple KrmsAttributeDefinitions found with same name and namespace");
86  			}
87  			result = defs.iterator().next();
88  			krmsAttributeDefinitionIdCache.put(key, result);
89  		}
90  		return result;
91  	}
92      
93  	@Override
94  	/**
95  	* @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#createAttributeDefinition()
96  	*/
97  	public KrmsAttributeDefinition createAttributeDefinition(KrmsAttributeDefinition attributeDefinition) {
98  		if (attributeDefinition == null){
99  	        throw new IllegalArgumentException("attributeDefinition is null");
100 		}
101 		final String nameKey = attributeDefinition.getName();
102 		final String namespaceKey = attributeDefinition.getNamespace();
103 		final KrmsAttributeDefinition existing = getAttributeDefinitionByNameAndNamespace(nameKey, namespaceKey);
104 		if (existing != null && existing.getName().equals(nameKey) && existing.getNamespace().equals(namespaceKey)){
105             throw new IllegalStateException("the krms attribute definition to create already exists: " + attributeDefinition);			
106 		}
107 		
108 		KrmsAttributeDefinitionBo bo = KrmsAttributeDefinitionBo.from(attributeDefinition);
109 		getBusinessObjectService().save(bo);
110 		updateCache(bo);
111 		return KrmsAttributeDefinitionBo.to(bo);
112 	}
113 
114 	@Override
115 	/**
116 	* @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#updateAttributeDefinition()
117 	*/
118 	public void updateAttributeDefinition(KrmsAttributeDefinition attributeDefinition) {
119 		if (attributeDefinition == null){
120 	        throw new IllegalArgumentException("attributeDefinition is null");
121 		}
122 		final String idKey = attributeDefinition.getId();
123 		final KrmsAttributeDefinitionBo existing = getBusinessObjectService().findBySinglePrimaryKey(KrmsAttributeDefinitionBo.class, idKey);
124 		if (existing == null){
125             throw new IllegalStateException("the krms attribute definition does not exist: " + attributeDefinition);			
126 		}
127 		final KrmsAttributeDefinition toUpdate;
128 		if (!existing.getId().equals(attributeDefinition.getId())){
129 			final KrmsAttributeDefinition.Builder builder = KrmsAttributeDefinition.Builder.create(attributeDefinition);
130 			builder.setId(existing.getId());
131 			toUpdate = builder.build();
132 		} else {
133 			toUpdate = attributeDefinition;
134 		}
135 		KrmsAttributeDefinitionBo bo = KrmsAttributeDefinitionBo.from(toUpdate);
136 		getBusinessObjectService().save(bo);
137 		updateCache(bo);
138 	}
139 
140 	@Override
141 	/**
142 	* @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#getAttributeDefinitionById()
143 	*/
144     public KrmsAttributeDefinition getAttributeDefinitionById(final String id) {
145         if (StringUtils.isBlank(id)) {
146             throw new IllegalArgumentException("id is blank");
147         }
148 		if (krmsAttributeDefinitionIdCache.containsKey(id)){
149 			return KrmsAttributeDefinitionBo.to(krmsAttributeDefinitionIdCache.get(id));	
150 		} 
151         KrmsAttributeDefinitionBo bo = getBusinessObjectService().findBySinglePrimaryKey(KrmsAttributeDefinitionBo.class, id);
152         return KrmsAttributeDefinitionBo.to(bo);
153     }
154 
155     @Override
156 	/**
157 	* @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#getAttributeDefinitionByNameAndNamespace()
158 	*/
159     public KrmsAttributeDefinition getAttributeDefinitionByNameAndNamespace(final String name, final String namespace) {
160         if (StringUtils.isBlank(name)) {
161             throw new IllegalArgumentException("name is blank");
162         }
163         if (StringUtils.isBlank(namespace)) {
164             throw new IllegalArgumentException("namespace is blank");
165         }
166         KrmsAttributeDefinitionBo bo = getKrmsAttributeBo(name, namespace);
167         if (bo == null) 
168         	return null;
169        	return KrmsAttributeDefinitionBo.to(bo);
170     }
171 
172     @Override
173 	/**
174 	* @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#findAttributeDefinitionsByNamespace()
175 	*/
176     public List<KrmsAttributeDefinition> findAttributeDefinitionsByNamespace(final String namespace) {
177         final Map<String, Object> map = new HashMap<String, Object>();
178         map.put("namespace", namespace);
179         map.put("active", Boolean.TRUE);
180         Collection<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos = getBusinessObjectService().findMatching(KrmsAttributeDefinitionBo.class, Collections.unmodifiableMap(map));
181         return convertListOfBosToImmutables(krmsAttributeDefinitionBos);
182     }
183 
184     @Override
185 	/**
186 	* @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#findAllAttributeDefinitions()
187 	*/
188     public List<KrmsAttributeDefinition> findAllAttributeDefinitions() {
189         final Map<String, Object> map = new HashMap<String, Object>();
190         map.put("active", Boolean.TRUE);
191         
192         Collection<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos = getBusinessObjectService().findMatching(KrmsAttributeDefinitionBo.class, Collections.unmodifiableMap(map));
193         return convertListOfBosToImmutables(krmsAttributeDefinitionBos);
194     }
195 
196     /**
197      * Sets the businessObjectService attribute value.
198      *
199      * @param businessObjectService The businessObjectService to set.
200      */
201     public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
202         this.businessObjectService = businessObjectService;
203     }
204 
205     /**
206      * This method returns a reference to the businessObjectService.
207      * If the businessObjectService is not set, get it from the KRADServiceLocator.
208      * @return businessObjectService
209      */
210     protected BusinessObjectService getBusinessObjectService() {
211 		if ( businessObjectService == null ) {
212 			businessObjectService = KRADServiceLocator.getBusinessObjectService();
213 		}
214 		return businessObjectService;
215 	}
216 
217     /**
218      * Converts a List<KrmsAttributeDefinitionBo> to an Unmodifiable List<KrmsAttributeDefinition>
219      *
220      * @param krmsAttributeDefinitionBos a mutable List<KrmsAttributeDefinitionBo> to made completely immutable.
221      * @return An unmodifiable List<KrmsAttributeDefinition>
222      */
223     public List<KrmsAttributeDefinition> convertListOfBosToImmutables(final Collection<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos) {
224         ArrayList<KrmsAttributeDefinition> krmsAttributeDefinitions = new ArrayList<KrmsAttributeDefinition>();
225         for (KrmsAttributeDefinitionBo bo : krmsAttributeDefinitionBos) {
226             KrmsAttributeDefinition krmsAttributeDefinition = KrmsAttributeDefinitionBo.to(bo);
227             krmsAttributeDefinitions.add(krmsAttributeDefinition);
228         }
229         return Collections.unmodifiableList(krmsAttributeDefinitions);
230     }
231 
232     // Cache related methods
233     
234     public void clearCache(){
235     	krmsAttributeDefinitionIdCache.clear();
236     }
237     
238     private String createKey(String namespace, String name){
239     	String key = namespace + ":" + name;
240     	return key;
241     }
242     
243     private void updateCache(KrmsAttributeDefinitionBo bo){
244 		String key = createKey(bo.getNamespace(), bo.getName());
245 		if (bo.isActive()){
246 			krmsAttributeDefinitionIdCache.put(key, bo);
247 		} else {
248 			krmsAttributeDefinitionIdCache.remove(key);
249 		}
250     }
251 }