View Javadoc
1   /**
2    * Copyright 2005-2015 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.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       * Walks up the Branch/scope hierarchy trying to find a variable with the specified name
44       * @param branch the lowermost branch at which to start the search
45       * @param name name of the variable to search for
46       * @return a BranchState object in the first Branch/scope in which the variable was found
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              // create new variable at initial search scope
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          // now save the Branch whose state we just modified
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 }