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.NodeIdFactory;
027
028/**
029 * Allows an element to have an optional ID specified
030 *
031 * @version 
032 */
033@XmlType(name = "optionalIdentifiedDefinition")
034@XmlAccessorType(XmlAccessType.PROPERTY)
035// must use XmlAccessType.PROPERTY which is required by camel-spring / camel-blueprint for their namespace parsers
036public abstract class OptionalIdentifiedDefinition<T extends OptionalIdentifiedDefinition<T>> implements NamedNode {
037    private transient String shortName;
038    private String id;
039    private Boolean customId;
040    private DescriptionDefinition description;
041
042    @Override
043    public String getId() {
044        return id;
045    }
046
047    /**
048     * Sets the id of this node
049     */
050    @XmlAttribute
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    public void setDescription(DescriptionDefinition description) {
067        this.description = description;
068    }
069
070    // Fluent API
071    // -------------------------------------------------------------------------
072
073    /**
074     * Sets the description of this node
075     *
076     * @param text  sets the text description, use null to not set a text
077     * @return the builder
078     */
079    @SuppressWarnings("unchecked")
080    public T description(String text) {
081        if (text != null) {
082            if (description == null) {
083                description = new DescriptionDefinition();
084            }
085            description.setText(text);
086        }
087        return (T) this;
088    }
089
090    /**
091     * Sets the description of this node
092     *
093     * @param id  sets the id, use null to not set an id
094     * @param text  sets the text description, use null to not set a text
095     * @param lang  sets the language for the description, use null to not set a language
096     * @return the builder
097     */
098    @SuppressWarnings("unchecked")
099    public T description(String id, String text, String lang) {
100        if (id != null) {
101            setId(id);
102        }
103        if (text != null) {
104            if (description == null) {
105                description = new DescriptionDefinition();
106            }
107            description.setText(text);
108        }
109        if (lang != null) {
110            if (description == null) {
111                description = new DescriptionDefinition();
112            }
113            description.setLang(lang);
114        }
115        return (T) this;
116    }
117
118    /**
119     * Sets the id of this node
120     *
121     * @param id  the id
122     * @return the builder
123     */
124    @SuppressWarnings("unchecked")
125    public T id(String id) {
126        setId(id);
127        return (T) this;
128    }
129
130    /**
131     * Gets the node id, creating one if not already set.
132     */
133    public String idOrCreate(NodeIdFactory factory) {
134        if (id == null) {
135            id = factory.createId(this);
136        }
137        return id;
138    }
139
140    public Boolean getCustomId() {
141        return customId;
142    }
143
144    /**
145     * Whether the node id was explicit set, or was auto generated by Camel.
146     */
147    @XmlAttribute
148    public void setCustomId(Boolean customId) {
149        this.customId = customId;
150    }
151
152    /**
153     * Returns whether a custom id has been assigned
154     */
155    public boolean hasCustomIdAssigned() {
156        return customId != null && customId;
157    }
158
159    /**
160     * Returns the description text or null if there is no description text associated with this node
161     */
162    @Override
163    public String getDescriptionText() {
164        return (description != null) ? description.getText() : null;
165    }
166
167    // Implementation methods
168    // -------------------------------------------------------------------------
169
170}