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.krad.data.DataObjectService;
20 import org.kuali.rice.krms.api.repository.context.ContextDefinition;
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import static org.kuali.rice.krms.impl.repository.BusinessObjectServiceMigrationUtils.deleteMatching;
26 import static org.kuali.rice.krms.impl.repository.BusinessObjectServiceMigrationUtils.findSingleMatching;
27
28
29
30
31
32
33
34
35 public final class ContextBoServiceImpl implements ContextBoService {
36
37 private DataObjectService dataObjectService;
38
39
40
41
42
43
44
45 @Override
46 public ContextDefinition createContext(ContextDefinition context) {
47 if (context == null) {
48 throw new IllegalArgumentException("context is null");
49 }
50
51 final String contextIdKey = context.getId();
52 final ContextDefinition existing = getContextByContextId(contextIdKey);
53
54 if (existing != null) {
55 throw new IllegalStateException("the context to create already exists: " + context);
56 }
57
58 ContextBo bo = dataObjectService.save(ContextBo.from(context));
59 return ContextBo.to(bo);
60 }
61
62
63
64
65 @Override
66 public void updateContext(ContextDefinition context) {
67 if (context == null){
68 throw new IllegalArgumentException("context is null");
69 }
70
71
72 final String contextIdKey = context.getId();
73 final ContextBo existing = dataObjectService.find(ContextBo.class, contextIdKey);
74
75 if (existing == null) {
76 throw new IllegalStateException("the context does not exist: " + context);
77 }
78
79 final ContextDefinition toUpdate;
80
81 if (!existing.getId().equals(context.getId())){
82
83 final ContextDefinition.Builder builder = ContextDefinition.Builder.create(context);
84 builder.setId(existing.getId());
85 toUpdate = builder.build();
86 } else {
87 toUpdate = context;
88 }
89
90
91 ContextBo boToUpdate = ContextBo.from(toUpdate);
92
93
94 Map<String,String> fields = new HashMap<String,String>(1);
95 fields.put("context.id", toUpdate.getId());
96 deleteMatching(dataObjectService, ContextAttributeBo.class, fields);
97
98
99 dataObjectService.save(boToUpdate);
100 }
101
102 @Override
103 public ContextDefinition getContextByContextId(String contextId) {
104 if (StringUtils.isBlank(contextId)){
105 return null;
106 }
107 ContextBo bo = dataObjectService.find(ContextBo.class, contextId);
108 return ContextBo.to(bo);
109 }
110
111 @Override
112 public ContextDefinition getContextByNameAndNamespace( String name, String namespace ){
113 if (StringUtils.isBlank(name)){
114 throw new IllegalArgumentException("name is null or blank");
115 }
116 if (StringUtils.isBlank(namespace)){
117 throw new IllegalArgumentException("namespace is null or blank");
118 }
119
120 final Map<String, Object> map = new HashMap<String, Object>();
121 map.put("name", name);
122 map.put("namespace", namespace);
123 ContextBo bo = findSingleMatching(dataObjectService, ContextBo.class, map);
124
125 return ContextBo.to(bo);
126 }
127
128
129
130
131
132
133 public void setDataObjectService(final DataObjectService dataObjectService) {
134 this.dataObjectService = dataObjectService;
135 }
136 }