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