View Javadoc

1   /**
2    * Copyright 2005-2013 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 java.util.List;
19  
20  import org.apache.log4j.Logger;
21  import org.kuali.rice.kew.engine.node.Branch;
22  import org.kuali.rice.kew.engine.node.BranchState;
23  import org.kuali.rice.kew.engine.node.dao.BranchDAO;
24  import org.kuali.rice.kew.engine.node.service.BranchService;
25  
26  
27  
28  public class BranchServiceImpl implements BranchService {
29      private static final Logger LOG = Logger.getLogger(BranchServiceImpl.class);
30  
31      private BranchDAO branchDAO;
32      
33      public void save(Branch branch){
34          getBranchDAO().save(branch);
35      }
36      
37       public BranchDAO getBranchDAO() {
38          return branchDAO;
39      }
40      public void setBranchDAO(BranchDAO branchDAO) {
41          this.branchDAO = branchDAO;
42      }
43      
44      public void deleteBranchStates(List statesToBeDeleted){
45          getBranchDAO().deleteBranchStates(statesToBeDeleted);
46      }
47  
48      /**
49       * Walks up the Branch/scope hierarchy trying to find a variable with the specified name
50       * @param branch the lowermost branch at which to start the search
51       * @param name name of the variable to search for
52       * @return a BranchState object in the first Branch/scope in which the variable was found
53       */
54      private BranchState resolveScopedVariable(Branch branch, String name) {
55          Branch b = branch;
56          while (b != null) {
57              for (BranchState bs: b.getBranchState()) {
58                  LOG.debug(bs);
59              }
60              LOG.debug("Resolving variable: '" + name + "' in scope (branch): '" + branch.getName() + "' (" + branch.getBranchId() + ")");
61              BranchState bs = b.getBranchState(name);
62              if (bs != null) {
63                  return bs;    
64              }
65              b = b.getParentBranch();
66          }
67          return null;
68      }
69  
70      public String getScopedVariableValue(Branch branch, String name) {
71          BranchState bs = resolveScopedVariable(branch, name);
72          if (bs != null) return bs.getValue();
73          return null;
74      }
75  
76      public String setScopedVariableValue(Branch branch, String name, String value) {
77          LOG.debug("Setting scoped variable value: " + name + " " + value);
78          BranchState bs = resolveScopedVariable(branch, name);
79          String oldValue = null;
80          if (bs == null) {
81              LOG.debug("Defining new variable named '" + name + "' at scope '" + branch + "'");
82              // create new variable at initial search scope
83              bs = new BranchState();
84              bs.setKey(name);
85              bs.setValue(value);
86              bs.setBranch(branch);
87              branch.addBranchState(bs);
88          } else {
89              oldValue = bs.getValue();
90              LOG.debug("Replacing old value of variable '" + name + "' (" + oldValue + ") at scope '" + branch + "' with new value: " + value);
91              bs.setValue(value);
92          }
93          // now save the Branch whose state we just modified
94          save(bs.getBranch());
95          return oldValue;
96      }
97      
98  }