View Javadoc
1   /*
2    *    Copyright 2015-2022 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    *       https://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.scripting.freemarker;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  
21  import freemarker.ext.beans.BeanModel;
22  import freemarker.ext.beans.BeansWrapperBuilder;
23  import freemarker.template.TemplateHashModel;
24  import freemarker.template.TemplateModel;
25  import freemarker.template.TemplateModelException;
26  import freemarker.template.Version;
27  
28  /**
29   * Important: if you are using some object that already has property "p", then MyBatisParamDirective will be unavailable
30   * from script.
31   *
32   * @author elwood
33   */
34  public class ParamObjectAdapter implements TemplateHashModel {
35    private final BeanModel beanModel;
36    private final ArrayList generatedParams;
37    private HashMap<String, TemplateModel> additionalParams;
38  
39    public ParamObjectAdapter(Object paramObject, ArrayList generatedParams, Version incompatibleImprovementsVersion) {
40      beanModel = new BeanModel(paramObject, new BeansWrapperBuilder(incompatibleImprovementsVersion).build());
41      this.generatedParams = generatedParams;
42    }
43  
44    /**
45     * Puts the additional parameter into adapter, it will be available if no existing property with same key exists. For
46     * example, it is suitable to add custom objects and directives into dataContext.
47     */
48    public void putAdditionalParam(String key, TemplateModel value) {
49      if (additionalParams == null) {
50        additionalParams = new HashMap<>();
51      }
52      additionalParams.put(key, value);
53    }
54  
55    public ArrayList getGeneratedParams() {
56      return generatedParams;
57    }
58  
59    @Override
60    public TemplateModel get(String key) throws TemplateModelException {
61      // Trying to get bean property
62      TemplateModel value = beanModel.get(key);
63  
64      // If no value retrieved, trying to find the key in additional params
65      if (value == null && additionalParams != null && additionalParams.containsKey(key)) {
66        return additionalParams.get(key);
67      }
68  
69      // If it is GENERATED_PARAMS_KEY, returning wrapper of generated params list
70      if (value == null && FreeMarkerSqlSource.GENERATED_PARAMS_KEY.equals(key)) {
71        return new GeneratedParamsTemplateModel(generatedParams);
72      }
73  
74      return value;
75    }
76  
77    @Override
78    public boolean isEmpty() throws TemplateModelException {
79      return beanModel.isEmpty();
80    }
81  }