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