View Javadoc

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