1 package fr.ifremer.reefdb.service;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import fr.ifremer.quadrige3.core.dao.technical.Assert;
27 import fr.ifremer.quadrige3.core.security.QuadrigeUserAuthority;
28 import fr.ifremer.quadrige3.core.security.SecurityContext;
29 import fr.ifremer.quadrige3.core.security.SecurityContextHelper;
30 import fr.ifremer.reefdb.dto.configuration.context.ContextDTO;
31 import fr.ifremer.reefdb.dto.configuration.filter.FilterDTO;
32 import fr.ifremer.reefdb.dto.enums.FilterTypeValues;
33 import org.apache.commons.collections4.CollectionUtils;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.jdesktop.beans.AbstractBean;
37
38 import java.io.Closeable;
39 import java.util.Objects;
40
41
42
43
44
45
46
47
48 public class ReefDbDataContext extends AbstractBean implements Closeable, SecurityContext {
49
50
51 public static final String PROPERTY_RECORDER_PERSON_ID = "recorderPersonId";
52
53 public static final String PROPERTY_RECORDER_DEPARTMENT_ID = "recorderDepartmentId";
54
55 public static final String PROPERTY_CONTEXT = "context";
56
57
58
59 private static final Log LOG = LogFactory.getLog(ReefDbDataContext.class);
60
61 private static final ReefDbDataContext INSTANCE = new ReefDbDataContext();
62
63
64
65 private Integer recorderPersonId;
66
67
68
69 private Integer recorderDepartmentId;
70
71
72
73 private ContextDTO context;
74
75
76
77
78 protected ReefDbDataContext() {
79
80 }
81
82
83
84
85
86
87 public static ReefDbDataContext instance() {
88 return INSTANCE;
89 }
90
91
92 @Override
93 public void close() {
94 clearContext();
95 }
96
97
98
99
100 public void clearContext() {
101 clearContext(false);
102 }
103
104
105
106
107 public void clearContextKeepRecorderPerson() {
108 clearContext(true);
109 }
110
111 private void clearContext(boolean keepRecorderPerson) {
112 if (!keepRecorderPerson) {
113 setRecorderPersonId(null);
114 }
115
116
117 resetLocalCache();
118 }
119
120
121
122
123 public void resetLocalCache() {
124
125 }
126
127
128
129
130
131
132
133
134
135 public boolean isRecorderPersonFilled() {
136 return recorderPersonId != null;
137 }
138
139
140
141
142
143
144 public Integer getRecorderPersonId() {
145 return recorderPersonId;
146 }
147
148
149
150
151
152
153 public void setRecorderPersonId(Integer recorderPersonId) {
154 Integer oldId = getRecorderPersonId();
155 this.recorderPersonId = recorderPersonId;
156 firePropertyChange(PROPERTY_RECORDER_PERSON_ID, oldId, recorderPersonId);
157 }
158
159
160 @Override
161 public int getPrincipalUserId() {
162 return getRecorderPersonId();
163 }
164
165
166
167
168
169
170
171
172
173 public boolean isRecorderDepartmentFilled() {
174 return recorderDepartmentId != null;
175 }
176
177
178
179
180
181
182 public Integer getRecorderDepartmentId() {
183 return recorderDepartmentId;
184 }
185
186
187
188
189
190
191 public void setRecorderDepartmentId(Integer recorderDepartmentId) {
192 Integer oldId = getRecorderDepartmentId();
193 this.recorderDepartmentId = recorderDepartmentId;
194 firePropertyChange(PROPERTY_RECORDER_DEPARTMENT_ID, oldId, recorderDepartmentId);
195 }
196
197
198
199
200
201
202 public boolean isRecorderValidator() {
203 return SecurityContextHelper.hasAuthority(QuadrigeUserAuthority.VALIDATOR);
204 }
205
206
207
208
209
210
211 public ContextDTO getContext() {
212 return context;
213 }
214
215
216
217
218
219
220 public void setContext(ContextDTO context) {
221 ContextDTO oldContext = getContext();
222 this.context = context;
223 firePropertyChange(PROPERTY_CONTEXT, oldContext, context);
224 }
225
226
227
228
229
230
231 public int getContextId() {
232 Assert.notNull(getContext());
233 return getContext().getId();
234 }
235
236
237
238
239
240
241
242 public boolean isContextFiltered(FilterTypeValues contextFilter) {
243 Assert.notNull(contextFilter);
244 if (getContext() == null || CollectionUtils.isEmpty(getContext().getFilters())) {
245 return false;
246 }
247 for (FilterDTO filter : getContext().getFilters()) {
248 if (Objects.equals(filter.getFilterTypeId(), contextFilter.getFilterTypeId())) {
249 return true;
250 }
251 }
252 return false;
253 }
254
255 }