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