View Javadoc

1   /**
2    * Copyright 2005-2013 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.krms.impl.repository;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.commons.collections.CollectionUtils;
26  import org.apache.commons.lang.StringUtils;
27  import org.kuali.rice.core.api.criteria.CriteriaLookupService;
28  import org.kuali.rice.core.api.criteria.GenericQueryResults;
29  import org.kuali.rice.core.api.criteria.Predicate;
30  import org.kuali.rice.core.api.criteria.QueryByCriteria;
31  import org.kuali.rice.krad.service.BusinessObjectService;
32  import org.kuali.rice.krad.service.KRADServiceLocator;
33  import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
34  import org.kuali.rice.krms.impl.util.KrmsImplConstants;
35  
36  import static org.kuali.rice.core.api.criteria.PredicateFactory.*;
37  
38  public final class KrmsAttributeDefinitionServiceImpl implements KrmsAttributeDefinitionService {
39  
40      private BusinessObjectService businessObjectService;
41      private CriteriaLookupService criteriaLookupService;
42  
43  	@Override
44  	/**
45  	* @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#convertAttributeKeys()
46  	*/
47  	public Map<String,String> convertAttributeKeys(Map<String,String> attributesByName, String namespace) {
48  		Map<String,String> attributesById = new HashMap<String,String>();
49  		if(attributesByName != null) {
50  			for(Map.Entry<String, String> attr : attributesByName.entrySet()) {
51  				String newKey = getKrmsAttributeId(attr.getKey(), namespace);
52  				if(StringUtils.isNotEmpty(newKey)) {
53  					attributesById.put(newKey, attr.getValue());
54  				}
55  			}
56  		}
57  		return attributesById;
58  	}
59     
60  	@Override
61  	/**
62  	* @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#getKrmsAttributeId()
63  	*/
64  	public String getKrmsAttributeId( String attributeName, String namespace) {
65  		String returnId = null;
66  		KrmsAttributeDefinitionBo bo = getKrmsAttributeBo(attributeName, namespace);
67  		if (bo != null){
68  			returnId = bo.getId();
69  		}
70  		return returnId;
71  	}
72      
73  	@Override
74  	/**
75  	* @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#getKrmsAttributeBo()
76  	*/
77  	public KrmsAttributeDefinitionBo getKrmsAttributeBo( String attributeName, String namespace) {
78  		KrmsAttributeDefinitionBo result = null;
79  		Map<String,Object> criteria = new HashMap<String,Object>( 3 );
80  		criteria.put( "name", attributeName );
81  		criteria.put( "namespace", namespace );
82          criteria.put( "active", Boolean.TRUE );
83  		Collection<KrmsAttributeDefinitionBo> defs = getBusinessObjectService().findMatching( KrmsAttributeDefinitionBo.class, criteria );
84  		if(CollectionUtils.isNotEmpty(defs)) {
85  			if (defs.size() > 1){
86  				throw new IllegalStateException("Multiple KrmsAttributeDefinitions found with same name and namespace");
87  			}
88  			result = defs.iterator().next();
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 		return KrmsAttributeDefinitionBo.to(bo);
111 	}
112 
113 	@Override
114 	/**
115 	* @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#updateAttributeDefinition()
116 	*/
117 	public void updateAttributeDefinition(KrmsAttributeDefinition attributeDefinition) {
118 		if (attributeDefinition == null){
119 	        throw new IllegalArgumentException("attributeDefinition is null");
120 		}
121 		final String idKey = attributeDefinition.getId();
122 		final KrmsAttributeDefinitionBo existing = getBusinessObjectService().findBySinglePrimaryKey(KrmsAttributeDefinitionBo.class, idKey);
123 		if (existing == null){
124             throw new IllegalStateException("the krms attribute definition does not exist: " + attributeDefinition);			
125 		}
126 		final KrmsAttributeDefinition toUpdate;
127 		if (!existing.getId().equals(attributeDefinition.getId())){
128 			final KrmsAttributeDefinition.Builder builder = KrmsAttributeDefinition.Builder.create(attributeDefinition);
129 			builder.setId(existing.getId());
130 			toUpdate = builder.build();
131 		} else {
132 			toUpdate = attributeDefinition;
133 		}
134 		KrmsAttributeDefinitionBo bo = KrmsAttributeDefinitionBo.from(toUpdate);
135 		getBusinessObjectService().save(bo);
136 	}
137 
138 	@Override
139 	/**
140 	* @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#getAttributeDefinitionById()
141 	*/
142     public KrmsAttributeDefinition getAttributeDefinitionById(final String id) {
143         if (StringUtils.isBlank(id)) {
144             throw new IllegalArgumentException("id is blank");
145         }
146         KrmsAttributeDefinitionBo bo = getBusinessObjectService().findBySinglePrimaryKey(KrmsAttributeDefinitionBo.class, id);
147         return KrmsAttributeDefinitionBo.to(bo);
148     }
149 
150     @Override
151 	/**
152 	* @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#getAttributeDefinitionByNameAndNamespace()
153 	*/
154     public KrmsAttributeDefinition getAttributeDefinitionByNameAndNamespace(final String name, final String namespace) {
155         if (StringUtils.isBlank(name)) {
156             throw new IllegalArgumentException("name is blank");
157         }
158         if (StringUtils.isBlank(namespace)) {
159             throw new IllegalArgumentException("namespace is blank");
160         }
161         KrmsAttributeDefinitionBo bo = getKrmsAttributeBo(name, namespace);
162         if (bo == null) 
163         	return null;
164        	return KrmsAttributeDefinitionBo.to(bo);
165     }
166 
167     @Override
168 	/**
169 	* @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#findAttributeDefinitionsByNamespace()
170 	*/
171     public List<KrmsAttributeDefinition> findAttributeDefinitionsByNamespace(final String namespace) {
172         final Map<String, Object> map = new HashMap<String, Object>();
173         map.put("namespace", namespace);
174         map.put("active", Boolean.TRUE);
175         Collection<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos = getBusinessObjectService().findMatching(KrmsAttributeDefinitionBo.class, Collections.unmodifiableMap(map));
176         return convertListOfBosToImmutables(krmsAttributeDefinitionBos);
177     }
178 
179     @Override
180 	/**
181 	* @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#findAttributeDefinitionsByType()
182 	*/
183     public List<KrmsAttributeDefinition> findAttributeDefinitionsByType(final String typeId) {
184 
185         List<KrmsAttributeDefinition> results = Collections.emptyList();
186 
187         final Map<String, Object> map = new HashMap<String, Object>();
188         map.put("typeId", typeId);
189         map.put("active", Boolean.TRUE);
190         Collection<KrmsTypeAttributeBo> krmsTypeAttributeBos = getBusinessObjectService().findMatching(KrmsTypeAttributeBo.class, Collections.unmodifiableMap(map));
191 
192         if (!CollectionUtils.isEmpty(krmsTypeAttributeBos)) {
193             String [] inList = new String[krmsTypeAttributeBos.size()];
194             int inListIdex = 0;
195             for (KrmsTypeAttributeBo krmsTypeAttributeBo : krmsTypeAttributeBos) {
196                 inList[inListIdex] = krmsTypeAttributeBo.getAttributeDefinitionId();
197                 ++inListIdex; // don't forget to increment our index
198             }
199 
200             QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create();
201             qBuilder.setPredicates(in("id", inList));
202             results = convertListOfBosToImmutables(getCriteriaLookupService().lookup(KrmsAttributeDefinitionBo.class, qBuilder.build()).getResults());
203         }
204 
205         return results;
206     }
207 
208 
209     @Override
210 	/**
211 	* @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#findAllAttributeDefinitions()
212 	*/
213     public List<KrmsAttributeDefinition> findAllAttributeDefinitions() {
214         final Map<String, Object> map = new HashMap<String, Object>();
215         map.put("active", Boolean.TRUE);
216         
217         Collection<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos = getBusinessObjectService().findMatching(KrmsAttributeDefinitionBo.class, Collections.unmodifiableMap(map));
218         return convertListOfBosToImmutables(krmsAttributeDefinitionBos);
219     }
220 
221     /**
222      * Sets the businessObjectService attribute value.
223      *
224      * @param businessObjectService The businessObjectService to set.
225      */
226     public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
227         this.businessObjectService = businessObjectService;
228     }
229 
230     /**
231      * This method returns a reference to the businessObjectService.
232      * If the businessObjectService is not set, get it from the KRADServiceLocator.
233      * @return businessObjectService
234      */
235     protected BusinessObjectService getBusinessObjectService() {
236 		if ( businessObjectService == null ) {
237 			businessObjectService = KRADServiceLocator.getBusinessObjectService();
238 		}
239 		return businessObjectService;
240 	}
241 
242     /**
243      * Converts a List<KrmsAttributeDefinitionBo> to an Unmodifiable List<KrmsAttributeDefinition>
244      *
245      * @param krmsAttributeDefinitionBos a mutable List<KrmsAttributeDefinitionBo> to made completely immutable.
246      * @return An unmodifiable List<KrmsAttributeDefinition>
247      */
248     public List<KrmsAttributeDefinition> convertListOfBosToImmutables(final Collection<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos) {
249         ArrayList<KrmsAttributeDefinition> krmsAttributeDefinitions = new ArrayList<KrmsAttributeDefinition>();
250         for (KrmsAttributeDefinitionBo bo : krmsAttributeDefinitionBos) {
251             KrmsAttributeDefinition krmsAttributeDefinition = KrmsAttributeDefinitionBo.to(bo);
252             krmsAttributeDefinitions.add(krmsAttributeDefinition);
253         }
254         return Collections.unmodifiableList(krmsAttributeDefinitions);
255     }
256 
257         /**
258      * Sets the criteriaLookupService attribute value.
259      *
260      * @param criteriaLookupService The criteriaLookupService to set.
261      */
262     public void setCriteriaLookupService(final CriteriaLookupService criteriaLookupService) {
263         this.criteriaLookupService = criteriaLookupService;
264     }
265 
266     protected CriteriaLookupService getCriteriaLookupService() {
267         return criteriaLookupService;
268     }
269 
270 }