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     */
017    package org.apache.camel.model;
018    
019    import javax.xml.bind.annotation.XmlAccessType;
020    import javax.xml.bind.annotation.XmlAccessorType;
021    import javax.xml.bind.annotation.XmlAttribute;
022    import javax.xml.bind.annotation.XmlElement;
023    import javax.xml.bind.annotation.XmlID;
024    import javax.xml.bind.annotation.XmlTransient;
025    import javax.xml.bind.annotation.XmlType;
026    import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
027    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
028    
029    import org.apache.camel.spi.NodeIdFactory;
030    
031    /**
032     * Allows an element to have an optional ID specified
033     *
034     * @version $Revision: 20802 $
035     */
036    @XmlType(name = "optionalIdentifiedDefinition")
037    @XmlAccessorType(XmlAccessType.FIELD)
038    public abstract class OptionalIdentifiedDefinition<T extends OptionalIdentifiedDefinition<T>> {
039        @XmlAttribute(required = false)
040        @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
041        @XmlID
042        private String id;
043        @XmlTransient
044        private boolean customId;
045        @XmlElement(required = false)
046        private DescriptionDefinition description;
047    
048        /**
049         * Gets the value of the id property.
050         */
051        public String getId() {
052            return id;
053        }
054    
055        /**
056         * Sets the value of the id property.
057         */
058        public void setId(String value) {
059            this.id = value;
060            customId = true;
061        }
062    
063        public DescriptionDefinition getDescription() {
064            return description;
065        }
066    
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        public String getShortName() {
077            return "node";
078        }
079    
080        // Fluent API
081        // -------------------------------------------------------------------------
082    
083        /**
084         * Sets the description of this node
085         *
086         * @param id  sets the id, use null to not set an id
087         * @param text  sets the text description, use null to not set a text
088         * @param lang  sets the language for the description, use null to not set a language
089         * @return the builder
090         */
091        @SuppressWarnings("unchecked")
092        public T description(String id, String text, String lang) {
093            if (id != null) {
094                setId(id);
095            }
096            if (text != null) {
097                if (description == null) {
098                    description = new DescriptionDefinition();
099                }
100                description.setText(text);
101            }
102            if (lang != null) {
103                if (description == null) {
104                    description = new DescriptionDefinition();
105                }
106                description.setLang(lang);
107            }
108            return (T) this;
109        }
110    
111        /**
112         * Sets the id of this node
113         *
114         * @param id  the id
115         * @return the builder
116         */
117        @SuppressWarnings("unchecked")
118        public T id(String id) {
119            setId(id);
120            return (T) this;
121        }
122    
123        /**
124         * Gets the node id, creating one if not already set.
125         */
126        public String idOrCreate(NodeIdFactory factory) {
127            if (id == null) {
128                id = factory.createId(this);
129            }
130            return getId();
131        }
132    
133        /**
134         * Returns whether a custom id has been assigned
135         */
136        public boolean hasCustomIdAssigned() {
137            return customId;
138        }
139    
140        /**
141         * Returns the description text or null if there is no description text associated with this node
142         */
143        public String getDescriptionText() {
144            return (description != null) ? description.getText() : null;
145        }
146    
147        // Implementation methods
148        // -------------------------------------------------------------------------
149    
150    }