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.List;
021import java.util.Locale;
022
023import org.apache.camel.model.AggregateDefinition;
024import org.apache.camel.model.BeanDefinition;
025import org.apache.camel.model.ChoiceDefinition;
026import org.apache.camel.model.FilterDefinition;
027import org.apache.camel.model.FromDefinition;
028import org.apache.camel.model.OtherwiseDefinition;
029import org.apache.camel.model.ProcessorDefinition;
030import org.apache.camel.model.RecipientListDefinition;
031import org.apache.camel.model.ResequenceDefinition;
032import org.apache.camel.model.RoutingSlipDefinition;
033import org.apache.camel.model.SplitDefinition;
034import org.apache.camel.model.ToDefinition;
035import org.apache.camel.model.TransformDefinition;
036import org.apache.camel.model.WhenDefinition;
037
038import static org.apache.camel.util.ObjectHelper.isEmpty;
039import static org.apache.camel.util.ObjectHelper.isNotEmpty;
040
041/**
042 * Represents a node in the EIP diagram tree
043 *
044 * @version 
045 */
046@Deprecated
047public class NodeData {
048    public String id;
049    public String image;
050    public String label;
051    public String shape;
052    public String edgeLabel;
053    public String tooltop;
054    public String nodeType;
055    public boolean nodeWritten;
056    public String url;
057    public List<ProcessorDefinition<?>> outputs;
058    public String association = "property";
059
060    public NodeData(String id, Object node, String imagePrefix) {
061        this.id = id;
062
063        if (node instanceof ProcessorDefinition) {
064            ProcessorDefinition<?> processorType = (ProcessorDefinition<?>)node;
065            this.edgeLabel = processorType.getLabel();
066        }
067        if (node instanceof FromDefinition) {
068            FromDefinition fromType = (FromDefinition)node;
069            this.tooltop = fromType.getLabel();
070            this.label = removeQueryString(this.tooltop);
071            this.url = "http://camel.apache.org/message-endpoint.html";
072        } else if (node instanceof ToDefinition) {
073            ToDefinition toType = (ToDefinition)node;
074            this.tooltop = toType.getLabel();
075            this.label = removeQueryString(this.tooltop);
076            this.edgeLabel = "";
077            this.url = "http://camel.apache.org/message-endpoint.html";
078        } else if (node instanceof FilterDefinition) {
079            this.image = imagePrefix + "MessageFilterIcon.png";
080            this.label = "Filter";
081            this.nodeType = "Message Filter";
082        } else if (node instanceof WhenDefinition) {
083            this.image = imagePrefix + "MessageFilterIcon.png";
084            this.nodeType = "When Filter";
085            this.label = "When";
086            this.url = "http://camel.apache.org/content-based-router.html";
087        } else if (node instanceof OtherwiseDefinition) {
088            this.nodeType = "Otherwise";
089            this.edgeLabel = "";
090            this.url = "http://camel.apache.org/content-based-router.html";
091            this.tooltop = "Otherwise";
092        } else if (node instanceof ChoiceDefinition) {
093            this.image = imagePrefix + "ContentBasedRouterIcon.png";
094            this.nodeType = "Content Based Router";
095            this.label = "Choice";
096            this.edgeLabel = "";
097
098            ChoiceDefinition choice = (ChoiceDefinition)node;
099            List<ProcessorDefinition<?>> outputs = new ArrayList<ProcessorDefinition<?>>(choice.getWhenClauses());
100            if (choice.getOtherwise() != null) {
101                outputs.add(choice.getOtherwise());
102            }
103            this.outputs = outputs;
104        } else if (node instanceof RecipientListDefinition) {
105            this.image = imagePrefix + "RecipientListIcon.png";
106            this.nodeType = "Recipient List";
107        } else if (node instanceof RoutingSlipDefinition) {
108            this.image = imagePrefix + "RoutingTableIcon.png";
109            this.nodeType = "Routing Slip";
110            this.url = "http://camel.apache.org/routing-slip.html";
111        } else if (node instanceof SplitDefinition) {
112            this.image = imagePrefix + "SplitterIcon.png";
113            this.nodeType = "Splitter";
114        } else if (node instanceof AggregateDefinition) {
115            this.image = imagePrefix + "AggregatorIcon.png";
116            this.nodeType = "Aggregator";
117        } else if (node instanceof ResequenceDefinition) {
118            this.image = imagePrefix + "ResequencerIcon.png";
119            this.nodeType = "Resequencer";
120        } else if (node instanceof BeanDefinition) {
121            BeanDefinition beanRef = (BeanDefinition) node;
122            this.nodeType = "Bean Ref";
123            this.label = beanRef.getLabel() + " Bean"; 
124            this.shape = "box";
125        } else if (node instanceof TransformDefinition) {
126            this.nodeType = "Transform";
127            this.url = "http://camel.apache.org/message-translator.html";
128        }
129
130        // lets auto-default as many values as we can
131        if (isEmpty(this.nodeType) && node != null) {
132            String name = node.getClass().getName();
133            int idx = name.lastIndexOf('.');
134            if (idx > 0) {
135                name = name.substring(idx + 1);
136            }
137            if (name.endsWith("Type")) {
138                name = name.substring(0, name.length() - 4);
139            }
140            this.nodeType = insertSpacesBetweenCamelCase(name);
141        }
142        if (this.label == null) {
143            if (isEmpty(this.image)) {
144                this.label = this.nodeType;
145                this.shape = "box";
146            } else if (isNotEmpty(this.edgeLabel)) {
147                this.label = "";
148            } else {
149                this.label = node.toString();
150            }
151        }
152        if (isEmpty(this.tooltop)) {
153            if (isNotEmpty(this.nodeType)) {
154                String description = isNotEmpty(this.edgeLabel) ? this.edgeLabel : this.label;
155                this.tooltop = this.nodeType + ": " + description;
156            } else {
157                this.tooltop = this.label;
158            }
159        }
160        if (isEmpty(this.url) && isNotEmpty(this.nodeType)) {
161            this.url = "http://camel.apache.org/" + this.nodeType.toLowerCase(Locale.ENGLISH).replace(' ', '-') + ".html";
162        }
163        if (node instanceof ProcessorDefinition && this.outputs == null) {
164            ProcessorDefinition<?> processorType = (ProcessorDefinition<?>)node;
165            this.outputs = processorType.getOutputs();
166        }
167    }
168
169    protected String removeQueryString(String text) {
170        int idx = text.indexOf('?');
171        if (idx <= 0) {
172            return text;
173        } else {
174            return text.substring(0, idx);
175        }
176    }
177
178    /**
179     * Inserts a space before each upper case letter after a lowercase
180     */
181    public static String insertSpacesBetweenCamelCase(String name) {
182        boolean lastCharacterLowerCase = false;
183        StringBuilder buffer = new StringBuilder();
184        int i = 0;
185        for (int size = name.length(); i < size; i++) {
186            char ch = name.charAt(i);
187            if (Character.isUpperCase(ch)) {
188                if (lastCharacterLowerCase) {
189                    buffer.append(' ');
190                }
191                lastCharacterLowerCase = false;
192            } else {
193                lastCharacterLowerCase = true;
194            }
195            buffer.append(ch);
196        }
197        return buffer.toString();
198    }
199}