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 java.util.List;
019
020import org.apache.log4j.Logger;
021import org.kuali.rice.kew.engine.node.Branch;
022import org.kuali.rice.kew.engine.node.BranchState;
023import org.kuali.rice.kew.engine.node.dao.BranchDAO;
024import org.kuali.rice.kew.engine.node.service.BranchService;
025
026
027
028public class BranchServiceImpl implements BranchService {
029    private static final Logger LOG = Logger.getLogger(BranchServiceImpl.class);
030
031    private BranchDAO branchDAO;
032    
033    public void save(Branch branch){
034        getBranchDAO().save(branch);
035    }
036    
037     public BranchDAO getBranchDAO() {
038        return branchDAO;
039    }
040    public void setBranchDAO(BranchDAO branchDAO) {
041        this.branchDAO = branchDAO;
042    }
043    
044    public void deleteBranchStates(List statesToBeDeleted){
045        getBranchDAO().deleteBranchStates(statesToBeDeleted);
046    }
047
048    /**
049     * Walks up the Branch/scope hierarchy trying to find a variable with the specified name
050     * @param branch the lowermost branch at which to start the search
051     * @param name name of the variable to search for
052     * @return a BranchState object in the first Branch/scope in which the variable was found
053     */
054    private BranchState resolveScopedVariable(Branch branch, String name) {
055        Branch b = branch;
056        while (b != null) {
057            for (BranchState bs: b.getBranchState()) {
058                LOG.debug(bs);
059            }
060            LOG.debug("Resolving variable: '" + name + "' in scope (branch): '" + branch.getName() + "' (" + branch.getBranchId() + ")");
061            BranchState bs = b.getBranchState(name);
062            if (bs != null) {
063                return bs;    
064            }
065            b = b.getParentBranch();
066        }
067        return null;
068    }
069
070    public String getScopedVariableValue(Branch branch, String name) {
071        BranchState bs = resolveScopedVariable(branch, name);
072        if (bs != null) return bs.getValue();
073        return null;
074    }
075
076    public String setScopedVariableValue(Branch branch, String name, String value) {
077        LOG.debug("Setting scoped variable value: " + name + " " + value);
078        BranchState bs = resolveScopedVariable(branch, name);
079        String oldValue = null;
080        if (bs == null) {
081            LOG.debug("Defining new variable named '" + name + "' at scope '" + branch + "'");
082            // create new variable at initial search scope
083            bs = new BranchState();
084            bs.setKey(name);
085            bs.setValue(value);
086            bs.setBranch(branch);
087            branch.addBranchState(bs);
088        } else {
089            oldValue = bs.getValue();
090            LOG.debug("Replacing old value of variable '" + name + "' (" + oldValue + ") at scope '" + branch + "' with new value: " + value);
091            bs.setValue(value);
092        }
093        // now save the Branch whose state we just modified
094        save(bs.getBranch());
095        return oldValue;
096    }
097    
098}