1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.scripting.thymeleaf;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.thymeleaf.context.IContext;
24 import org.thymeleaf.engine.IterationStatusVar;
25
26
27
28
29
30
31
32
33 public class MyBatisBindingContext {
34
35 static final String CONTEXT_VARIABLE_NAME = "_" + MyBatisBindingContext.class.getName();
36
37 private final Map<String, Object> customBindVariables = new HashMap<>();
38 private final IterationStatusManager iterationStatusManager = new IterationStatusManager();
39 private final boolean fallbackParameterObject;
40
41
42
43
44
45
46
47
48
49 public static MyBatisBindingContext load(IContext context) {
50 return (MyBatisBindingContext) context.getVariable(CONTEXT_VARIABLE_NAME);
51 }
52
53
54
55
56
57
58
59 MyBatisBindingContext(boolean fallbackParameterObject) {
60 this.fallbackParameterObject = fallbackParameterObject;
61 }
62
63
64
65
66
67
68 Map<String, Object> getCustomBindVariables() {
69 return customBindVariables;
70 }
71
72
73
74
75
76
77
78
79
80 public void setCustomBindVariable(String name, Object value) {
81 customBindVariables.put(name, value);
82 }
83
84
85
86
87
88
89
90
91
92 public boolean containsCustomBindVariable(String name) {
93 return customBindVariables.containsKey(name);
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107 public String generateUniqueName(String objectName, IterationStatusVar status) {
108 return iterationStatusManager.generateUniqueName(objectName, status);
109 }
110
111
112
113
114
115
116 boolean isFallbackParameterObject() {
117 return fallbackParameterObject;
118 }
119
120 private static class IterationStatusManager {
121
122 private final Map<String, List<IterationStatusVar>> statusListMapping = new HashMap<>();
123
124 private String generateUniqueName(String objectName, IterationStatusVar status) {
125 List<IterationStatusVar> statusList = statusListMapping.computeIfAbsent(objectName, k -> new ArrayList<>());
126 int index;
127 if (!statusList.contains(status)) {
128 index = statusList.size();
129 statusList.add(status);
130 } else {
131 index = statusList.indexOf(status);
132 }
133 return objectName + "_" + index + "_" + status.getIndex();
134 }
135
136 }
137
138 }