1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.engine.node.service.impl;
17
18 import org.apache.log4j.Logger;
19 import org.kuali.rice.kew.engine.node.Branch;
20 import org.kuali.rice.kew.engine.node.BranchState;
21 import org.kuali.rice.kew.engine.node.service.BranchService;
22 import org.kuali.rice.krad.data.DataObjectService;
23 import org.springframework.beans.factory.annotation.Required;
24
25 import java.util.List;
26
27 public class BranchServiceImpl implements BranchService {
28 private static final Logger LOG = Logger.getLogger(BranchServiceImpl.class);
29
30 private DataObjectService dataObjectService;
31
32 public void save(Branch branch){
33 dataObjectService.save(branch);
34 }
35
36 public void deleteBranchStates(List statesToBeDeleted){
37 for(BranchState bs : (List<BranchState>)statesToBeDeleted){
38 dataObjectService.delete(bs);
39 }
40 }
41
42
43
44
45
46
47
48 private BranchState resolveScopedVariable(Branch branch, String name) {
49 Branch b = branch;
50 while (b != null) {
51 for (BranchState bs: b.getBranchState()) {
52 LOG.debug(bs);
53 }
54 LOG.debug("Resolving variable: '" + name + "' in scope (branch): '" + branch.getName() + "' (" + branch.getBranchId() + ")");
55 BranchState bs = b.getBranchState(name);
56 if (bs != null) {
57 return bs;
58 }
59 b = b.getParentBranch();
60 }
61 return null;
62 }
63
64 public String getScopedVariableValue(Branch branch, String name) {
65 BranchState bs = resolveScopedVariable(branch, name);
66 if (bs != null) return bs.getValue();
67 return null;
68 }
69
70 public String setScopedVariableValue(Branch branch, String name, String value) {
71 LOG.debug("Setting scoped variable value: " + name + " " + value);
72 BranchState bs = resolveScopedVariable(branch, name);
73 String oldValue = null;
74 if (bs == null) {
75 LOG.debug("Defining new variable named '" + name + "' at scope '" + branch + "'");
76
77 bs = new BranchState();
78 bs.setKey(name);
79 bs.setValue(value);
80 bs.setBranch(branch);
81 branch.addBranchState(bs);
82 } else {
83 oldValue = bs.getValue();
84 LOG.debug("Replacing old value of variable '" + name + "' (" + oldValue + ") at scope '" + branch + "' with new value: " + value);
85 bs.setValue(value);
86 }
87
88 save(bs.getBranch());
89 return oldValue;
90 }
91
92
93 public DataObjectService getDataObjectService() {
94 return dataObjectService;
95 }
96
97 @Required
98 public void setDataObjectService(DataObjectService dataObjectService) {
99 this.dataObjectService = dataObjectService;
100 }
101
102 }