001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.kew.engine.node.service.impl; 017 018import org.apache.log4j.Logger; 019import org.kuali.rice.kew.engine.node.Branch; 020import org.kuali.rice.kew.engine.node.BranchState; 021import org.kuali.rice.kew.engine.node.service.BranchService; 022import org.kuali.rice.krad.data.DataObjectService; 023import org.springframework.beans.factory.annotation.Required; 024 025import java.util.List; 026 027public class BranchServiceImpl implements BranchService { 028 private static final Logger LOG = Logger.getLogger(BranchServiceImpl.class); 029 030 private DataObjectService dataObjectService; 031 032 public void save(Branch branch){ 033 dataObjectService.save(branch); 034 } 035 036 public void deleteBranchStates(List statesToBeDeleted){ 037 for(BranchState bs : (List<BranchState>)statesToBeDeleted){ 038 dataObjectService.delete(bs); 039 } 040 } 041 042 /** 043 * Walks up the Branch/scope hierarchy trying to find a variable with the specified name 044 * @param branch the lowermost branch at which to start the search 045 * @param name name of the variable to search for 046 * @return a BranchState object in the first Branch/scope in which the variable was found 047 */ 048 private BranchState resolveScopedVariable(Branch branch, String name) { 049 Branch b = branch; 050 while (b != null) { 051 for (BranchState bs: b.getBranchState()) { 052 LOG.debug(bs); 053 } 054 LOG.debug("Resolving variable: '" + name + "' in scope (branch): '" + branch.getName() + "' (" + branch.getBranchId() + ")"); 055 BranchState bs = b.getBranchState(name); 056 if (bs != null) { 057 return bs; 058 } 059 b = b.getParentBranch(); 060 } 061 return null; 062 } 063 064 public String getScopedVariableValue(Branch branch, String name) { 065 BranchState bs = resolveScopedVariable(branch, name); 066 if (bs != null) return bs.getValue(); 067 return null; 068 } 069 070 public String setScopedVariableValue(Branch branch, String name, String value) { 071 LOG.debug("Setting scoped variable value: " + name + " " + value); 072 BranchState bs = resolveScopedVariable(branch, name); 073 String oldValue = null; 074 if (bs == null) { 075 LOG.debug("Defining new variable named '" + name + "' at scope '" + branch + "'"); 076 // create new variable at initial search scope 077 bs = new BranchState(); 078 bs.setKey(name); 079 bs.setValue(value); 080 bs.setBranch(branch); 081 branch.addBranchState(bs); 082 } else { 083 oldValue = bs.getValue(); 084 LOG.debug("Replacing old value of variable '" + name + "' (" + oldValue + ") at scope '" + branch + "' with new value: " + value); 085 bs.setValue(value); 086 } 087 // now save the Branch whose state we just modified 088 save(bs.getBranch()); 089 return oldValue; 090 } 091 092 093 public DataObjectService getDataObjectService() { 094 return dataObjectService; 095 } 096 097 @Required 098 public void setDataObjectService(DataObjectService dataObjectService) { 099 this.dataObjectService = dataObjectService; 100 } 101 102}