View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.engine.node.service.impl;
18  
19  import java.util.List;
20  
21  import org.apache.log4j.Logger;
22  import org.kuali.rice.kew.engine.node.Branch;
23  import org.kuali.rice.kew.engine.node.BranchState;
24  import org.kuali.rice.kew.engine.node.dao.BranchDAO;
25  import org.kuali.rice.kew.engine.node.service.BranchService;
26  
27  
28  
29  public class BranchServiceImpl implements BranchService {
30      private static final Logger LOG = Logger.getLogger(BranchServiceImpl.class);
31  
32      private BranchDAO branchDAO;
33      
34      public void save(Branch branch){
35          getBranchDAO().save(branch);
36      }
37      
38       public BranchDAO getBranchDAO() {
39          return branchDAO;
40      }
41      public void setBranchDAO(BranchDAO branchDAO) {
42          this.branchDAO = branchDAO;
43      }
44      
45      public void deleteBranchStates(List statesToBeDeleted){
46          getBranchDAO().deleteBranchStates(statesToBeDeleted);
47      }
48  
49      /**
50       * Walks up the Branch/scope hierarchy trying to find a variable with the specified name
51       * @param branch the lowermost branch at which to start the search
52       * @param name name of the variable to search for
53       * @return a BranchState object in the first Branch/scope in which the variable was found
54       */
55      private BranchState resolveScopedVariable(Branch branch, String name) {
56          Branch b = branch;
57          while (b != null) {
58              for (BranchState bs: b.getBranchState()) {
59                  LOG.debug(bs);
60              }
61              LOG.debug("Resolving variable: '" + name + "' in scope (branch): '" + branch.getName() + "' (" + branch.getBranchId() + ")");
62              BranchState bs = b.getBranchState(name);
63              if (bs != null) {
64                  return bs;    
65              }
66              b = b.getParentBranch();
67          }
68          return null;
69      }
70  
71      public String getScopedVariableValue(Branch branch, String name) {
72          BranchState bs = resolveScopedVariable(branch, name);
73          if (bs != null) return bs.getValue();
74          return null;
75      }
76  
77      public String setScopedVariableValue(Branch branch, String name, String value) {
78          LOG.debug("Setting scoped variable value: " + name + " " + value);
79          BranchState bs = resolveScopedVariable(branch, name);
80          String oldValue = null;
81          if (bs == null) {
82              LOG.debug("Defining new variable named '" + name + "' at scope '" + branch + "'");
83              // create new variable at initial search scope
84              bs = new BranchState(name, value);
85              bs.setBranch(branch);
86              branch.addBranchState(bs);
87          } else {
88              oldValue = bs.getValue();
89              LOG.debug("Replacing old value of variable '" + name + "' (" + oldValue + ") at scope '" + branch + "' with new value: " + value);
90              bs.setValue(value);
91          }
92          // now save the Branch whose state we just modified
93          save(bs.getBranch());
94          return oldValue;
95      }
96      
97  }