001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.war.beans.cms;
034:
035: import com.flexive.faces.FxJsfUtils;
036: import com.flexive.shared.EJBLookup;
037: import com.flexive.shared.interfaces.TreeEngine;
038: import static com.flexive.shared.tree.FxTreeMode.Edit;
039: import com.flexive.shared.tree.FxTreeNode;
040: import com.flexive.war.FxRequest;
041:
042: import java.util.*;
043:
044: /**
045: * Request based beans, providing access to the tree.
046: *
047: * @author Gregor Schober (gregor.schober@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
048: */
049: public class TreeProviderBean extends
050: Hashtable<String, List<FxTreeNode>> {
051:
052: private TreeEngine tree;
053: private Map<Long, List<FxTreeNode>> cachedNodes = new HashMap<Long, List<FxTreeNode>>(
054: 25); // cache nodes during request
055: private Map<Object, List<FxTreeNode>> cachedResults = new HashMap<Object, List<FxTreeNode>>(
056: 25); // cache results during request
057:
058: public TreeProviderBean() {
059: tree = EJBLookup.getTreeEngine();
060: }
061:
062: @SuppressWarnings("unchecked")
063: public List<FxTreeNode> get(Object key) {
064:
065: String path = String.valueOf(key);
066: if (cachedResults.containsKey(key)) {
067: return cachedResults.get(key);
068: }
069:
070: FxRequest request = FxJsfUtils.getRequest();
071: try {
072: long nodeId = -1;
073: if (path.indexOf(":+") > 0) {
074: String split[] = path.split(":\\+");
075: nodeId = Long.valueOf(split[0]);
076: int level = Integer.valueOf(split[1]);
077: int idx = 0;
078: for (long tmp : request.getIdChain()) {
079: if (tmp == nodeId) {
080: break;
081: }
082: idx++;
083: }
084: idx += level;
085: nodeId = request.getIdChain()[idx];
086: } else if (path.equalsIgnoreCase("..")) {
087: nodeId = request.getTemplateInfo().getParentNode();
088: } else if (path.equalsIgnoreCase(".")) {
089: nodeId = request.getNodeId();
090: } else if (path.equalsIgnoreCase("/")) {
091: nodeId = FxTreeNode.ROOT_NODE;
092: } else {
093: try {
094: nodeId = Long.parseLong(path);
095: } catch (Throwable t) {/*ignore*/
096: }
097: if (nodeId == -1)
098: nodeId = tree.getIdByPath(Edit, path);
099: }
100: List<FxTreeNode> result = null;
101: if (!cachedNodes.containsKey(nodeId)) {
102: result = tree.getTree(Edit, nodeId, 3).getChildren();
103: for (FxTreeNode node : result)
104: processActiveFlag(request, node);
105: cachedNodes.put(nodeId, result);
106: }
107: if (result != null)
108: cachedResults.put(key, result);
109: return result;
110: } catch (Throwable t) {
111: List<FxTreeNode> errorResult = new ArrayList<FxTreeNode>(1);
112: String msg = t.getLocalizedMessage() == null ? "Null Exception"
113: : t.getLocalizedMessage();
114: errorResult.add(FxTreeNode.createErrorNode(request
115: .getNodeId(), msg));
116: return errorResult;
117: }
118: }
119:
120: /**
121: * Set the active flag of the given node and all its children.
122: *
123: * @param request the request
124: * @param node the node
125: */
126: private void processActiveFlag(final FxRequest request,
127: final FxTreeNode node) {
128: node.setActivate(request.treeNodeIsActive(node.getId()));
129: if (node.getChildren() != null) {
130: for (FxTreeNode child : node.getChildren()) {
131: processActiveFlag(request, child);
132: }
133: }
134: }
135:
136: }
|