View Javadoc
1   /**
2    *    Copyright 2006-2016 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.generator.config;
17  
18  import static org.mybatis.generator.internal.util.StringUtility.stringContainsSpace;
19  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
20  import static org.mybatis.generator.internal.util.messages.Messages.getString;
21  
22  import java.util.List;
23  
24  import org.mybatis.generator.api.dom.xml.Attribute;
25  import org.mybatis.generator.api.dom.xml.XmlElement;
26  
27  /**
28   * The Class ColumnOverride.
29   *
30   * @author Jeff Butler
31   */
32  public class ColumnOverride extends PropertyHolder {
33  
34      /** The column name. */
35      private String columnName;
36  
37      /** The java property. */
38      private String javaProperty;
39  
40      /** The jdbc type. */
41      private String jdbcType;
42  
43      /** The java type. */
44      private String javaType;
45  
46      /** The type handler. */
47      private String typeHandler;
48  
49      /** The is column name delimited. */
50      private boolean isColumnNameDelimited;
51  
52      /** The configured delimited column name. */
53      private String configuredDelimitedColumnName;
54  
55      /**
56       * Instantiates a new column override.
57       *
58       * @param columnName
59       *            the column name
60       */
61      public ColumnOverride(String columnName) {
62          super();
63  
64          this.columnName = columnName;
65          isColumnNameDelimited = stringContainsSpace(columnName);
66      }
67  
68      /**
69       * Gets the column name.
70       *
71       * @return the column name
72       */
73      public String getColumnName() {
74          return columnName;
75      }
76  
77      /**
78       * Gets the java property.
79       *
80       * @return the java property
81       */
82      public String getJavaProperty() {
83          return javaProperty;
84      }
85  
86      /**
87       * Sets the java property.
88       *
89       * @param javaProperty
90       *            the new java property
91       */
92      public void setJavaProperty(String javaProperty) {
93          this.javaProperty = javaProperty;
94      }
95  
96      /**
97       * Gets the java type.
98       *
99       * @return the java type
100      */
101     public String getJavaType() {
102         return javaType;
103     }
104 
105     /**
106      * Sets the java type.
107      *
108      * @param javaType
109      *            the new java type
110      */
111     public void setJavaType(String javaType) {
112         this.javaType = javaType;
113     }
114 
115     /**
116      * Gets the jdbc type.
117      *
118      * @return the jdbc type
119      */
120     public String getJdbcType() {
121         return jdbcType;
122     }
123 
124     /**
125      * Sets the jdbc type.
126      *
127      * @param jdbcType
128      *            the new jdbc type
129      */
130     public void setJdbcType(String jdbcType) {
131         this.jdbcType = jdbcType;
132     }
133 
134     /**
135      * Gets the type handler.
136      *
137      * @return the type handler
138      */
139     public String getTypeHandler() {
140         return typeHandler;
141     }
142 
143     /**
144      * Sets the type handler.
145      *
146      * @param typeHandler
147      *            the new type handler
148      */
149     public void setTypeHandler(String typeHandler) {
150         this.typeHandler = typeHandler;
151     }
152 
153     /**
154      * To xml element.
155      *
156      * @return the xml element
157      */
158     public XmlElement toXmlElement() {
159         XmlElement xmlElement = new XmlElement("columnOverride"); //$NON-NLS-1$
160         xmlElement.addAttribute(new Attribute("column", columnName)); //$NON-NLS-1$
161 
162         if (stringHasValue(javaProperty)) {
163             xmlElement.addAttribute(new Attribute("property", javaProperty)); //$NON-NLS-1$
164         }
165 
166         if (stringHasValue(javaType)) {
167             xmlElement.addAttribute(new Attribute("javaType", javaType)); //$NON-NLS-1$
168         }
169 
170         if (stringHasValue(jdbcType)) {
171             xmlElement.addAttribute(new Attribute("jdbcType", jdbcType)); //$NON-NLS-1$
172         }
173 
174         if (stringHasValue(typeHandler)) {
175             xmlElement.addAttribute(new Attribute("typeHandler", typeHandler)); //$NON-NLS-1$
176         }
177 
178         if (stringHasValue(configuredDelimitedColumnName)) {
179             xmlElement.addAttribute(new Attribute(
180                     "delimitedColumnName", configuredDelimitedColumnName)); //$NON-NLS-1$
181         }
182 
183         addPropertyXmlElements(xmlElement);
184 
185         return xmlElement;
186     }
187 
188     /**
189      * Checks if is column name delimited.
190      *
191      * @return true, if is column name delimited
192      */
193     public boolean isColumnNameDelimited() {
194         return isColumnNameDelimited;
195     }
196 
197     /**
198      * Sets the column name delimited.
199      *
200      * @param isColumnNameDelimited
201      *            the new column name delimited
202      */
203     public void setColumnNameDelimited(boolean isColumnNameDelimited) {
204         this.isColumnNameDelimited = isColumnNameDelimited;
205 
206         configuredDelimitedColumnName = isColumnNameDelimited ? "true" : "false"; //$NON-NLS-1$ //$NON-NLS-2$
207     }
208 
209     /**
210      * Validate.
211      *
212      * @param errors
213      *            the errors
214      * @param tableName
215      *            the table name
216      */
217     public void validate(List<String> errors, String tableName) {
218         if (!stringHasValue(columnName)) {
219             errors.add(getString("ValidationError.22", //$NON-NLS-1$
220                     tableName));
221         }
222     }
223 }