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.plugins;
17  
18  import java.util.List;
19  
20  import org.mybatis.generator.api.IntrospectedTable;
21  import org.mybatis.generator.api.PluginAdapter;
22  import org.mybatis.generator.api.dom.xml.Attribute;
23  import org.mybatis.generator.api.dom.xml.Document;
24  import org.mybatis.generator.api.dom.xml.XmlElement;
25  import org.mybatis.generator.internal.util.StringUtility;
26  
27  /**
28   * This plugin adds a cache element to generated sqlMaps.  This plugin
29   * is for MyBatis3 targeted runtimes only.  The plugin accepts the
30   * following properties (all are optional):
31   * 
32   * cache_eviction
33   * cache_flushInterval
34   * cache_size
35   * cache_readOnly
36   * cache_type
37   * 
38   * All properties correspond to properties of the MyBatis cache element and
39   * are passed "as is" to the corresponding properties of the generated cache
40   * element.  All properties can be specified at the table level, or on the
41   * plugin element.  The property on the table element will override any
42   * property on the plugin element.
43   * 
44   * @author Jason Bennett
45   * @author Jeff Butler
46   */
47  public class CachePlugin extends PluginAdapter {
48      public enum CacheProperty {
49          EVICTION("cache_eviction", "eviction"), //$NON-NLS-1$ //$NON-NLS-2$
50          FLUSH_INTERVAL("cache_flushInterval", "flushInterval"), //$NON-NLS-1$ //$NON-NLS-2$
51          READ_ONLY("cache_readOnly", "readOnly"), //$NON-NLS-1$ //$NON-NLS-2$
52          SIZE("cache_size", "size"), //$NON-NLS-1$ //$NON-NLS-2$
53          TYPE("cache_type", "type"); //$NON-NLS-1$ //$NON-NLS-2$
54          
55          private String propertyName;
56          private String attributeName;
57          
58          CacheProperty(String propertyName, String attributeName) {
59              this.propertyName = propertyName;
60              this.attributeName = attributeName;
61          }
62  
63          public String getPropertyName() {
64              return propertyName;
65          }
66  
67          public String getAttributeName() {
68              return attributeName;
69          }
70      }
71      
72      public CachePlugin() {
73          super();
74      }
75  
76      public boolean validate(List<String> warnings) {
77          return true;
78      }
79  
80      @Override
81      public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
82  
83          XmlElement element = new XmlElement("cache"); //$NON-NLS-1$
84          context.getCommentGenerator().addComment(element);
85  
86          for (CacheProperty cacheProperty : CacheProperty.values()) {
87              addAttributeIfExists(element, introspectedTable, cacheProperty);
88          }
89          
90          document.getRootElement().addElement(element);
91  
92          return true;
93      }
94      
95      private void addAttributeIfExists(XmlElement element, IntrospectedTable introspectedTable,
96              CacheProperty cacheProperty) {
97          String property = introspectedTable.getTableConfigurationProperty(cacheProperty.getPropertyName());
98          if (property == null) {
99              property = properties.getProperty(cacheProperty.getPropertyName());
100         }
101         
102         if (StringUtility.stringHasValue(property)) {
103             element.addAttribute(new Attribute(cacheProperty.getAttributeName(), property));
104         }
105     }
106 }