001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
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 */
017package org.apache.camel.view;
018
019import java.util.ArrayList;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.camel.model.ChoiceDefinition;
025import org.apache.camel.model.FromDefinition;
026import org.apache.camel.model.MulticastDefinition;
027import org.apache.camel.model.PipelineDefinition;
028import org.apache.camel.model.ProcessorDefinition;
029import org.apache.camel.model.RouteDefinition;
030import org.apache.camel.model.ToDefinition;
031import org.apache.camel.model.language.ExpressionDefinition;
032import org.apache.camel.util.CollectionStringBuffer;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036/**
037 * A base class for Graph processing code of Camel EIPs containing a number of helper methods
038 *
039 * @version 
040 */
041@Deprecated
042public class GraphSupport {
043    protected final Logger log = LoggerFactory.getLogger(getClass());
044    protected final Map<Object, NodeData> nodeMap = new HashMap<Object, NodeData>();
045    private String imagePrefix = "http://camel.apache.org/images/eip/";
046
047    protected String getLabel(List<ExpressionDefinition> expressions) {
048        CollectionStringBuffer buffer = new CollectionStringBuffer();
049        for (ExpressionDefinition expression : expressions) {
050            buffer.append(getLabel(expression));
051        }
052        return buffer.toString();
053    }
054
055    protected String getLabel(ExpressionDefinition expression) {
056        if (expression != null) {
057            return expression.getLabel();
058        }
059        return "";
060    }
061
062    protected NodeData getNodeData(Object node) {
063        Object key = node;
064        if (node instanceof FromDefinition) {
065            FromDefinition fromType = (FromDefinition) node;
066            key = fromType.getUriOrRef();
067        } else if (node instanceof ToDefinition) {
068            ToDefinition toType = (ToDefinition) node;
069            key = toType.getUriOrRef();
070        }
071        NodeData answer = null;
072        if (key != null) {
073            answer = nodeMap.get(key);
074        }
075        if (answer == null) {
076            String id = "node" + (nodeMap.size() + 1);
077            answer = new NodeData(id, node, imagePrefix);
078            nodeMap.put(key, answer);
079        }
080        return answer;
081    }
082
083    protected Map<String, List<RouteDefinition>> createRouteGroupMap(List<RouteDefinition> routes) {
084        Map<String, List<RouteDefinition>> map = new HashMap<String, List<RouteDefinition>>();
085        for (RouteDefinition route : routes) {
086            addRouteToMap(map, route);
087        }
088        return map;
089    }
090
091    protected void addRouteToMap(Map<String, List<RouteDefinition>> map, RouteDefinition route) {
092        String group = route.getGroup();
093        if (group == null) {
094            group = "Camel Routes";
095        }
096        List<RouteDefinition> list = map.get(group);
097        if (list == null) {
098            list = new ArrayList<RouteDefinition>();
099            map.put(group, list);
100        }
101        list.add(route);
102    }
103
104    protected boolean isMulticastNode(ProcessorDefinition<?> node) {
105        return node instanceof MulticastDefinition || node instanceof ChoiceDefinition;
106    }
107
108    /**
109     * Is the given node a pipeline
110     */
111    protected boolean isPipeline(ProcessorDefinition<?> node) {
112        if (node instanceof MulticastDefinition) {
113            return false;
114        }
115        if (node instanceof PipelineDefinition) {
116            return true;
117        }
118        if (node.getOutputs().size() > 1) {
119            // is pipeline if there is more than 1 output and they are all To types
120            for (Object type : node.getOutputs()) {
121                if (!(type instanceof ToDefinition)) {
122                    return false;
123                }
124            }
125            return true;
126        }
127        return false;
128    }
129
130    public String getImagePrefix() {
131        return imagePrefix;
132    }
133
134    public void setImagePrefix(String imagePrefix) {
135        this.imagePrefix = imagePrefix;
136    }
137}