View Javadoc

1   /**
2    * Copyright 2005-2012 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.Collections;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.kuali.rice.krad.service.BusinessObjectService;
26  import org.kuali.rice.krms.api.repository.function.FunctionDefinition;
27  import org.kuali.rice.krms.api.repository.function.FunctionRepositoryService;
28  
29  /**
30   * Default implementation of the {@link FunctionService}.
31   * 
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   *
34   */
35  public class FunctionBoServiceImpl implements FunctionRepositoryService, FunctionBoService {
36  		
37      private BusinessObjectService businessObjectService;
38      
39  	@Override
40  	public FunctionDefinition getFunction(String functionId) {
41  		return getFunctionById(functionId);
42  	}
43  	
44  	@Override
45  	public List<FunctionDefinition> getFunctions(List<String> functionIds) {
46  		
47  		List<FunctionDefinition> functionDefinitions = new ArrayList<FunctionDefinition>();
48  		for (String functionId : functionIds){
49  			functionDefinitions.add( getFunctionById(functionId) );
50  		}
51          return Collections.unmodifiableList(functionDefinitions);
52  	}
53  	
54  	/**
55  	 * This method will create a {@link FunctionDefintion} as described
56  	 * by the function passed in.
57  	 * 
58  	 * @see org.kuali.rice.krms.impl.repository.FunctionBoService#createFunction(org.kuali.rice.krms.api.repository.function.FunctionDefinition)
59  	 */
60  	@Override
61  	public FunctionDefinition createFunction(FunctionDefinition function) {
62  		if (function == null){
63  	        throw new IllegalArgumentException("function is null");
64  		}
65  		
66  		final String nameKey = function.getName();
67  		final String namespaceKey = function.getNamespace();
68  		final FunctionDefinition existing = getFunctionByNameAndNamespace(nameKey, namespaceKey);
69  		if (existing != null && existing.getName().equals(nameKey) && existing.getNamespace().equals(namespaceKey)){
70              throw new IllegalStateException("the function to create already exists: " + function);			
71  		}
72  		
73  		FunctionBo functionBo = FunctionBo.from(function);
74  		businessObjectService.save(functionBo);
75  		return FunctionBo.to(functionBo);
76  	}
77  
78  	/**
79  	 * This overridden method updates an existing Function in the repository
80  	 * 
81  	 * @see org.kuali.rice.krms.impl.repository.FunctionBoService#updateFunction(org.kuali.rice.krms.api.repository.function.FunctionDefinition)
82  	 */
83  	@Override
84  	public void updateFunction(FunctionDefinition function) {
85  		if (function == null){
86  			throw new IllegalArgumentException("function is null");
87  		}
88  		
89  		final String functionIdKey = function.getId();
90  		final FunctionDefinition existing = getFunctionById(functionIdKey);
91  		if (existing == null) {
92  			throw new IllegalStateException("the function does not exist: " + function);
93  		}
94  		final FunctionDefinition toUpdate;
95  		if (!existing.getId().equals(function.getId())){
96  			final FunctionDefinition.Builder builder = FunctionDefinition.Builder.create(function);
97  			builder.setId(existing.getId());
98  			toUpdate = builder.build();
99  		} else {
100 			toUpdate = function;
101 		}
102 
103 		businessObjectService.save(FunctionBo.from(toUpdate));
104 	}
105 	
106     
107 	/**
108 	 * This overridden method retrieves a function by the given function id.
109 	 * 
110 	 * @see org.kuali.rice.krms.impl.repository.FunctionBoService#getFunctionById(java.lang.String)
111 	 */
112 	@Override
113 	public FunctionDefinition getFunctionById(String functionId) {
114 		if (StringUtils.isBlank(functionId)){
115             throw new IllegalArgumentException("functionId is null or blank");			
116 		}
117 		FunctionBo functionBo = businessObjectService.findBySinglePrimaryKey(FunctionBo.class, functionId);
118 		return FunctionBo.to(functionBo);
119 	}
120 	
121 	/**
122 	 * 
123 	 * This overridden method retrieves a function by the given name and namespace.
124 	 * 
125 	 * @see org.kuali.rice.krms.impl.repository.FunctionBoService#getFunctionByNameAndNamespace(java.lang.String, java.lang.String)
126 	 */
127 	public FunctionDefinition getFunctionByNameAndNamespace( String name, String namespace ){
128 		if (StringUtils.isBlank(name)){
129 			throw new IllegalArgumentException("name is null or blank");
130 		}
131 		if (StringUtils.isBlank(namespace)){
132 			throw new IllegalArgumentException("namespace is null or blank");
133 		}
134 				
135         final Map<String, Object> map = new HashMap<String, Object>();
136         map.put("name", name);
137         map.put("namespace", namespace);
138 		FunctionBo functionBo = businessObjectService.findByPrimaryKey(FunctionBo.class, Collections.unmodifiableMap(map));
139 		return FunctionBo.to(functionBo);
140 	}
141 	
142     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
143         this.businessObjectService = businessObjectService;
144     }
145 }