| 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 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 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | 17 | public class FunctionBoServiceImpl implements FunctionRepositoryService, FunctionBoService { |
| 36 | |
|
| 37 | |
private BusinessObjectService businessObjectService; |
| 38 | |
|
| 39 | |
@Override |
| 40 | |
public FunctionDefinition getFunction(String functionId) { |
| 41 | 1 | return getFunctionById(functionId); |
| 42 | |
} |
| 43 | |
|
| 44 | |
@Override |
| 45 | |
public List<FunctionDefinition> getFunctions(List<String> functionIds) { |
| 46 | |
|
| 47 | 1 | List<FunctionDefinition> functionDefinitions = new ArrayList<FunctionDefinition>(); |
| 48 | 1 | for (String functionId : functionIds){ |
| 49 | 2 | functionDefinitions.add( getFunctionById(functionId) ); |
| 50 | |
} |
| 51 | 1 | return Collections.unmodifiableList(functionDefinitions); |
| 52 | |
} |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
@Override |
| 61 | |
public FunctionDefinition createFunction(FunctionDefinition function) { |
| 62 | 3 | if (function == null){ |
| 63 | 1 | throw new IllegalArgumentException("function is null"); |
| 64 | |
} |
| 65 | |
|
| 66 | 2 | final String nameKey = function.getName(); |
| 67 | 2 | final String namespaceKey = function.getNamespace(); |
| 68 | 2 | final FunctionDefinition existing = getFunctionByNameAndNamespace(nameKey, namespaceKey); |
| 69 | 2 | if (existing != null && existing.getName().equals(nameKey) && existing.getNamespace().equals(namespaceKey)){ |
| 70 | 1 | throw new IllegalStateException("the function to create already exists: " + function); |
| 71 | |
} |
| 72 | |
|
| 73 | 1 | FunctionBo functionBo = FunctionBo.from(function); |
| 74 | 1 | businessObjectService.save(functionBo); |
| 75 | 1 | return FunctionBo.to(functionBo); |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
@Override |
| 84 | |
public void updateFunction(FunctionDefinition function) { |
| 85 | 3 | if (function == null){ |
| 86 | 1 | throw new IllegalArgumentException("function is null"); |
| 87 | |
} |
| 88 | |
|
| 89 | 2 | final String functionIdKey = function.getId(); |
| 90 | 2 | final FunctionDefinition existing = getFunctionById(functionIdKey); |
| 91 | 2 | if (existing == null) { |
| 92 | 1 | throw new IllegalStateException("the function does not exist: " + function); |
| 93 | |
} |
| 94 | |
final FunctionDefinition toUpdate; |
| 95 | 1 | if (!existing.getId().equals(function.getId())){ |
| 96 | 0 | final FunctionDefinition.Builder builder = FunctionDefinition.Builder.create(function); |
| 97 | 0 | builder.setId(existing.getId()); |
| 98 | 0 | toUpdate = builder.build(); |
| 99 | 0 | } else { |
| 100 | 1 | toUpdate = function; |
| 101 | |
} |
| 102 | |
|
| 103 | 1 | businessObjectService.save(FunctionBo.from(toUpdate)); |
| 104 | 1 | } |
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
@Override |
| 113 | |
public FunctionDefinition getFunctionById(String functionId) { |
| 114 | 10 | if (StringUtils.isBlank(functionId)){ |
| 115 | 1 | throw new IllegalArgumentException("functionId is null or blank"); |
| 116 | |
} |
| 117 | 9 | FunctionBo functionBo = businessObjectService.findBySinglePrimaryKey(FunctionBo.class, functionId); |
| 118 | 9 | return FunctionBo.to(functionBo); |
| 119 | |
} |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
public FunctionDefinition getFunctionByNameAndNamespace( String name, String namespace ){ |
| 128 | 8 | if (StringUtils.isBlank(name)){ |
| 129 | 2 | throw new IllegalArgumentException("name is null or blank"); |
| 130 | |
} |
| 131 | 6 | if (StringUtils.isBlank(namespace)){ |
| 132 | 2 | throw new IllegalArgumentException("namespace is null or blank"); |
| 133 | |
} |
| 134 | |
|
| 135 | 4 | final Map<String, Object> map = new HashMap<String, Object>(); |
| 136 | 4 | map.put("name", name); |
| 137 | 4 | map.put("namespace", namespace); |
| 138 | 4 | FunctionBo functionBo = businessObjectService.findByPrimaryKey(FunctionBo.class, Collections.unmodifiableMap(map)); |
| 139 | 4 | return FunctionBo.to(functionBo); |
| 140 | |
} |
| 141 | |
|
| 142 | |
public void setBusinessObjectService(BusinessObjectService businessObjectService) { |
| 143 | 17 | this.businessObjectService = businessObjectService; |
| 144 | 17 | } |
| 145 | |
} |