View Javadoc

1   /**
2    * Copyright 2005-2015 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 org.apache.commons.collections.CollectionUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
21  import org.kuali.rice.core.api.mo.ModelObjectUtils;
22  import org.kuali.rice.krad.service.BusinessObjectService;
23  import org.kuali.rice.krms.api.repository.function.FunctionDefinition;
24  import org.kuali.rice.krms.api.repository.function.FunctionRepositoryService;
25  
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * Default implementation of the {@link FunctionRepositoryService}.
35   * 
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   *
38   */
39  public class FunctionBoServiceImpl implements FunctionRepositoryService, FunctionBoService {
40  		
41      private BusinessObjectService businessObjectService;
42  
43      // used for converting lists of BOs to model objects
44      private static final ModelObjectUtils.Transformer<FunctionBo, FunctionDefinition> toFunctionDefinition =
45              new ModelObjectUtils.Transformer<FunctionBo, FunctionDefinition>() {
46                  public FunctionDefinition transform(FunctionBo input) {
47                      return FunctionBo.to(input);
48                  };
49              };
50      
51  	@Override
52  	public FunctionDefinition getFunction(String functionId) {
53  		return getFunctionById(functionId);
54  	}
55  	
56  	@Override
57  	public List<FunctionDefinition> getFunctions(List<String> functionIds) {
58  
59          if (functionIds == null) throw new RiceIllegalArgumentException();
60  
61  		List<FunctionDefinition> functionDefinitions = new ArrayList<FunctionDefinition>();
62  		for (String functionId : functionIds){
63              if (!StringUtils.isBlank(functionId)) {
64                  FunctionDefinition functionDefinition = getFunctionById(functionId);
65                  if (functionDefinition != null) {
66                      functionDefinitions.add(functionDefinition);
67                  }
68              }
69  		}
70          return Collections.unmodifiableList(functionDefinitions);
71  	}
72  	
73  	/**
74  	 * This method will create a {@link FunctionDefintion} as described
75  	 * by the function passed in.
76  	 * 
77  	 * @see org.kuali.rice.krms.impl.repository.FunctionBoService#createFunction(org.kuali.rice.krms.api.repository.function.FunctionDefinition)
78  	 */
79  	@Override
80  	public FunctionDefinition createFunction(FunctionDefinition function) {
81  		if (function == null){
82  	        throw new IllegalArgumentException("function is null");
83  		}
84  		
85  		final String nameKey = function.getName();
86  		final String namespaceKey = function.getNamespace();
87  		final FunctionDefinition existing = getFunctionByNameAndNamespace(nameKey, namespaceKey);
88  		if (existing != null && existing.getName().equals(nameKey) && existing.getNamespace().equals(namespaceKey)){
89              throw new IllegalStateException("the function to create already exists: " + function);			
90  		}
91  		
92  		FunctionBo functionBo = FunctionBo.from(function);
93  		businessObjectService.save(functionBo);
94  		return FunctionBo.to(functionBo);
95  	}
96  
97  	/**
98  	 * This overridden method updates an existing Function in the repository
99  	 * 
100 	 * @see org.kuali.rice.krms.impl.repository.FunctionBoService#updateFunction(org.kuali.rice.krms.api.repository.function.FunctionDefinition)
101 	 */
102 	@Override
103 	public void updateFunction(FunctionDefinition function) {
104 		if (function == null){
105 			throw new IllegalArgumentException("function is null");
106 		}
107 		
108 		final String functionIdKey = function.getId();
109 		final FunctionDefinition existing = getFunctionById(functionIdKey);
110 		if (existing == null) {
111 			throw new IllegalStateException("the function does not exist: " + function);
112 		}
113 		final FunctionDefinition toUpdate;
114 		if (!existing.getId().equals(function.getId())){
115 			final FunctionDefinition.Builder builder = FunctionDefinition.Builder.create(function);
116 			builder.setId(existing.getId());
117 			toUpdate = builder.build();
118 		} else {
119 			toUpdate = function;
120 		}
121 
122 		businessObjectService.save(FunctionBo.from(toUpdate));
123 	}
124 	
125     
126 	/**
127 	 * This overridden method retrieves a function by the given function id.
128 	 * 
129 	 * @see org.kuali.rice.krms.impl.repository.FunctionBoService#getFunctionById(java.lang.String)
130 	 */
131 	@Override
132 	public FunctionDefinition getFunctionById(String functionId) {
133 		if (StringUtils.isBlank(functionId)){
134             throw new RiceIllegalArgumentException("functionId is null or blank");
135 		}
136 		FunctionBo functionBo = businessObjectService.findBySinglePrimaryKey(FunctionBo.class, functionId);
137 		return FunctionBo.to(functionBo);
138 	}
139 	
140 	/**
141 	 * 
142 	 * This overridden method retrieves a function by the given name and namespace.
143 	 * 
144 	 * @see org.kuali.rice.krms.impl.repository.FunctionBoService#getFunctionByNameAndNamespace(java.lang.String, java.lang.String)
145 	 */
146 	public FunctionDefinition getFunctionByNameAndNamespace( String name, String namespace ){
147 		if (StringUtils.isBlank(name)){
148 			throw new IllegalArgumentException("name is null or blank");
149 		}
150 		if (StringUtils.isBlank(namespace)){
151 			throw new IllegalArgumentException("namespace is null or blank");
152 		}
153 				
154         final Map<String, Object> map = new HashMap<String, Object>();
155         map.put("name", name);
156         map.put("namespace", namespace);
157 		FunctionBo functionBo = businessObjectService.findByPrimaryKey(FunctionBo.class, Collections.unmodifiableMap(map));
158 		return FunctionBo.to(functionBo);
159 	}
160 
161     /**
162      * Gets all of the {@link FunctionDefinition}s within the given namespace
163      *
164      * @param namespace the namespace in which to get the functions
165      * @return the list of function definitions, or if none are found, an empty list
166      */
167     public List<FunctionDefinition> getFunctionsByNamespace(String namespace) {
168         if (StringUtils.isBlank(namespace)){
169             throw new IllegalArgumentException("namespace is null or blank");
170         }
171 
172         final Map<String, Object> map = new HashMap<String, Object>();
173         map.put("namespace", namespace);
174         Collection<FunctionBo> functionBos = businessObjectService.findMatching(FunctionBo.class, map);
175 
176         return convertFunctionBosToImmutables(functionBos);
177     }
178 
179     /**
180      * Converts a Collection of FunctionBos to an Unmodifiable List of Agendas
181      *
182      * @param functionBos a mutable List of FunctionBos to made completely immutable.
183      * @return An unmodifiable List of FunctionDefinitions
184      */
185     private List<FunctionDefinition> convertFunctionBosToImmutables(final Collection<FunctionBo> functionBos) {
186         if (CollectionUtils.isEmpty(functionBos)) {
187             return Collections.emptyList();
188         }
189         return Collections.unmodifiableList(ModelObjectUtils.transform(functionBos, toFunctionDefinition));
190     }
191 
192     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
193         this.businessObjectService = businessObjectService;
194     }
195 }