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.model;
018
019import javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlElement;
023import javax.xml.bind.annotation.XmlType;
024
025import org.apache.camel.NamedNode;
026import org.apache.camel.spi.Metadata;
027import org.apache.camel.spi.NodeIdFactory;
028
029/**
030 * Allows an element to have an optional ID specified
031 */
032@XmlType(name = "optionalIdentifiedDefinition")
033@XmlAccessorType(XmlAccessType.PROPERTY)
034// must use XmlAccessType.PROPERTY which is required by camel-spring / camel-blueprint for their namespace parsers
035public abstract class OptionalIdentifiedDefinition<T extends OptionalIdentifiedDefinition<T>> implements NamedNode {
036
037    private String id;
038    private Boolean customId;
039    private DescriptionDefinition description;
040
041    @Override
042    public String getId() {
043        return id;
044    }
045
046    /**
047     * Sets the id of this node
048     */
049    @XmlAttribute
050    @Metadata(description = "The id of this node")
051    public void setId(String value) {
052        this.id = value;
053        customId = true;
054    }
055
056    public DescriptionDefinition getDescription() {
057        return description;
058    }
059
060    /**
061     * Sets the description of this node
062     *
063     * @param description sets the text description, use null to not set a text
064     */
065    @XmlElement
066    @Metadata(description = "The description for this node")
067    public void setDescription(DescriptionDefinition description) {
068        this.description = description;
069    }
070
071    @Override
072    public NamedNode getParent() {
073        return null;
074    }
075
076    // Fluent API
077    // -------------------------------------------------------------------------
078
079    /**
080     * Sets the description of this node
081     *
082     * @param text sets the text description, use null to not set a text
083     * @return the builder
084     */
085    @SuppressWarnings("unchecked")
086    public T description(String text) {
087        if (text != null) {
088            if (description == null) {
089                description = new DescriptionDefinition();
090            }
091            description.setText(text);
092        }
093        return (T)this;
094    }
095
096    /**
097     * Sets the description of this node
098     *
099     * @param id sets the id, use null to not set an id
100     * @param text sets the text description, use null to not set a text
101     * @param lang sets the language for the description, use null to not set a
102     *            language
103     * @return the builder
104     */
105    @SuppressWarnings("unchecked")
106    public T description(String id, String text, String lang) {
107        if (id != null) {
108            setId(id);
109        }
110        if (text != null) {
111            if (description == null) {
112                description = new DescriptionDefinition();
113            }
114            description.setText(text);
115        }
116        if (lang != null) {
117            if (description == null) {
118                description = new DescriptionDefinition();
119            }
120            description.setLang(lang);
121        }
122        return (T)this;
123    }
124
125    /**
126     * Sets the id of this node.
127     * <p/>
128     * <b>Important:</b> If you want to set the id of the route, then you
129     * <b>must</b> use <tt>routeId(String)</tt> instead.
130     *
131     * @param id the id
132     * @return the builder
133     */
134    @SuppressWarnings("unchecked")
135    public T id(String id) {
136        setId(id);
137        return (T)this;
138    }
139
140    /**
141     * Gets the node id, creating one if not already set.
142     */
143    public String idOrCreate(NodeIdFactory factory) {
144        if (id == null) {
145            id = factory.createId(this);
146        }
147        return id;
148    }
149
150    public Boolean getCustomId() {
151        return customId;
152    }
153
154    /**
155     * Whether the node id was explicit set, or was auto generated by Camel.
156     */
157    @XmlAttribute
158    public void setCustomId(Boolean customId) {
159        this.customId = customId;
160    }
161
162    /**
163     * Returns whether a custom id has been assigned
164     */
165    public boolean hasCustomIdAssigned() {
166        return customId != null && customId;
167    }
168
169    /**
170     * Returns the description text or null if there is no description text
171     * associated with this node
172     */
173    @Override
174    public String getDescriptionText() {
175        return (description != null) ? description.getText() : null;
176    }
177
178    // Implementation methods
179    // -------------------------------------------------------------------------
180
181}