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