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 IgnoredColumn.
29   *
30   * @author Jeff Butler
31   */
32  public class IgnoredColumn {
33  
34      /** The column name. */
35      private String columnName;
36  
37      /** The is column name delimited. */
38      private boolean isColumnNameDelimited;
39  
40      /** The configured delimited column name. */
41      private String configuredDelimitedColumnName;
42  
43      /**
44       * Instantiates a new ignored column.
45       *
46       * @param columnName
47       *            the column name
48       */
49      public IgnoredColumn(String columnName) {
50          super();
51          this.columnName = columnName;
52          isColumnNameDelimited = stringContainsSpace(columnName);
53      }
54  
55      /**
56       * Gets the column name.
57       *
58       * @return the column name
59       */
60      public String getColumnName() {
61          return columnName;
62      }
63  
64      /**
65       * Checks if is column name delimited.
66       *
67       * @return true, if is column name delimited
68       */
69      public boolean isColumnNameDelimited() {
70          return isColumnNameDelimited;
71      }
72  
73      /**
74       * Sets the column name delimited.
75       *
76       * @param isColumnNameDelimited
77       *            the new column name delimited
78       */
79      public void setColumnNameDelimited(boolean isColumnNameDelimited) {
80          this.isColumnNameDelimited = isColumnNameDelimited;
81          configuredDelimitedColumnName = isColumnNameDelimited ? "true" : "false"; //$NON-NLS-1$ //$NON-NLS-2$
82      }
83  
84      /* (non-Javadoc)
85       * @see java.lang.Object#equals(java.lang.Object)
86       */
87      public boolean equals(Object obj) {
88          if (obj == null || !(obj instanceof IgnoredColumn)) {
89              return false;
90          }
91  
92          return columnName.equals(((IgnoredColumn) obj).getColumnName());
93      }
94  
95      /* (non-Javadoc)
96       * @see java.lang.Object#hashCode()
97       */
98      public int hashCode() {
99          return columnName.hashCode();
100     }
101 
102     /**
103      * To xml element.
104      *
105      * @return the xml element
106      */
107     public XmlElement toXmlElement() {
108         XmlElement xmlElement = new XmlElement("ignoreColumn"); //$NON-NLS-1$
109         xmlElement.addAttribute(new Attribute("column", columnName)); //$NON-NLS-1$
110 
111         if (stringHasValue(configuredDelimitedColumnName)) {
112             xmlElement.addAttribute(new Attribute(
113                     "delimitedColumnName", configuredDelimitedColumnName)); //$NON-NLS-1$
114         }
115 
116         return xmlElement;
117     }
118 
119     /**
120      * Validate.
121      *
122      * @param errors
123      *            the errors
124      * @param tableName
125      *            the table name
126      */
127     public void validate(List<String> errors, String tableName) {
128         if (!stringHasValue(columnName)) {
129             errors.add(getString("ValidationError.21", //$NON-NLS-1$
130                     tableName));
131         }
132     }
133 }