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.XmlType;
024    
025    import org.apache.camel.NamedNode;
026    import 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    public abstract class OptionalIdentifiedDefinition<T extends OptionalIdentifiedDefinition<T>> implements NamedNode {
036        private String id;
037        private Boolean customId;
038        private DescriptionDefinition description;
039    
040        /**
041         * Gets the value of the id property.
042         */
043        @Override
044        public String getId() {
045            return id;
046        }
047    
048        /**
049         * Sets the value of the id property.
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        @XmlElement
062        public void setDescription(DescriptionDefinition description) {
063            this.description = description;
064        }
065    
066        /**
067         * Returns a short name for this node which can be useful for ID generation or referring to related resources like images
068         *
069         * @return defaults to "node" but derived nodes should overload this to provide a unique name
070         */
071        @Override
072        public String getShortName() {
073            return "node";
074        }
075    
076        // Fluent API
077        // -------------------------------------------------------------------------
078    
079        /**
080         * Sets the description of this node
081         *
082         * @param id  sets the id, use null to not set an id
083         * @param text  sets the text description, use null to not set a text
084         * @param lang  sets the language for the description, use null to not set a language
085         * @return the builder
086         */
087        @SuppressWarnings("unchecked")
088        public T description(String id, String text, String lang) {
089            if (id != null) {
090                setId(id);
091            }
092            if (text != null) {
093                if (description == null) {
094                    description = new DescriptionDefinition();
095                }
096                description.setText(text);
097            }
098            if (lang != null) {
099                if (description == null) {
100                    description = new DescriptionDefinition();
101                }
102                description.setLang(lang);
103            }
104            return (T) this;
105        }
106    
107        /**
108         * Sets the id of this node
109         *
110         * @param id  the id
111         * @return the builder
112         */
113        @SuppressWarnings("unchecked")
114        public T id(String id) {
115            setId(id);
116            return (T) this;
117        }
118    
119        /**
120         * Gets the node id, creating one if not already set.
121         */
122        public String idOrCreate(NodeIdFactory factory) {
123            if (id == null) {
124                id = factory.createId(this);
125            }
126            return getId();
127        }
128    
129        public Boolean isCustomId() {
130            return customId;
131        }
132    
133        @XmlAttribute
134        public void setCustomId(Boolean customId) {
135            this.customId = customId;
136        }
137    
138        /**
139         * Returns whether a custom id has been assigned
140         */
141        public boolean hasCustomIdAssigned() {
142            return customId != null && customId;
143        }
144    
145        /**
146         * Returns the description text or null if there is no description text associated with this node
147         */
148        @Override
149        public String getDescriptionText() {
150            return (description != null) ? description.getText() : null;
151        }
152    
153        // Implementation methods
154        // -------------------------------------------------------------------------
155    
156    }