• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / freemarker-scripting / #281

pending completion
#281

Pull #150

github

web-flow
Merge 3411c6de0 into 9185d6a44
Pull Request #150: Update dependency org.mybatis:mybatis-parent to v38

243 of 260 relevant lines covered (93.46%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.5
/src/main/java/org/mybatis/scripting/freemarker/ParamObjectAdapter.java
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) {
1✔
40
    beanModel = new BeanModel(paramObject, new BeansWrapperBuilder(incompatibleImprovementsVersion).build());
1✔
41
    this.generatedParams = generatedParams;
1✔
42
  }
1✔
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) {
1✔
50
      additionalParams = new HashMap<>();
1✔
51
    }
52
    additionalParams.put(key, value);
1✔
53
  }
1✔
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);
1✔
63

64
    // If no value retrieved, trying to find the key in additional params
65
    if (value == null && additionalParams != null && additionalParams.containsKey(key)) {
1✔
66
      return additionalParams.get(key);
1✔
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)) {
1✔
71
      return new GeneratedParamsTemplateModel(generatedParams);
1✔
72
    }
73

74
    return value;
1✔
75
  }
76

77
  @Override
78
  public boolean isEmpty() throws TemplateModelException {
79
    return beanModel.isEmpty();
×
80
  }
81
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc