1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
32
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
46
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
62 TemplateModel value = beanModel.get(key);
63
64
65 if (value == null && additionalParams != null && additionalParams.containsKey(key)) {
66 return additionalParams.get(key);
67 }
68
69
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 }