1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
32  
33  
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  
64  
65  
66  
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  
88  
89  
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 
117 
118 
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 
132 
133 
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 }