View Javadoc
1   package fr.ifremer.dali.service;
2   
3   /*
4    * #%L
5    * Dali :: Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import fr.ifremer.dali.decorator.DecoratorService;
27  import fr.ifremer.dali.dto.configuration.context.ContextDTO;
28  import fr.ifremer.dali.dto.configuration.filter.FilterDTO;
29  import fr.ifremer.dali.dto.enums.FilterTypeValues;
30  import fr.ifremer.dali.service.administration.campaign.CampaignService;
31  import fr.ifremer.dali.service.administration.context.ContextService;
32  import fr.ifremer.dali.service.administration.program.ProgramStrategyService;
33  import fr.ifremer.dali.service.administration.user.UserService;
34  import fr.ifremer.dali.service.control.ControlRuleService;
35  import fr.ifremer.dali.service.control.RuleListService;
36  import fr.ifremer.dali.service.extraction.ExtractionPerformService;
37  import fr.ifremer.dali.service.extraction.ExtractionService;
38  import fr.ifremer.dali.service.observation.ObservationService;
39  import fr.ifremer.dali.service.persistence.PersistenceService;
40  import fr.ifremer.dali.service.referential.ReferentialService;
41  import fr.ifremer.dali.service.system.SystemService;
42  import fr.ifremer.quadrige3.core.dao.technical.Assert;
43  import fr.ifremer.quadrige3.core.security.QuadrigeUserAuthority;
44  import fr.ifremer.quadrige3.core.security.SecurityContext;
45  import fr.ifremer.quadrige3.core.security.SecurityContextHelper;
46  import fr.ifremer.quadrige3.synchro.service.client.SynchroHistoryService;
47  import org.apache.commons.collections4.CollectionUtils;
48  import org.jdesktop.beans.AbstractBean;
49  
50  import java.io.Closeable;
51  import java.util.Objects;
52  
53  /**
54   * Data context of ui.
55   * <p/>
56   * All shared data must be there to avoid reloading some stuff.
57   *
58   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
59   */
60  public class DaliDataContext extends AbstractBean implements Closeable, SecurityContext {
61  
62      /** Constant <code>PROPERTY_CONTEXT="context"</code> */
63      public static final String PROPERTY_CONTEXT = "context";
64  
65      // Singleton
66      private static final DaliDataContext INSTANCE = new DaliDataContext();
67      /**
68       * the current recorder user
69       */
70      private Integer recorderPersonId;
71      /**
72       * the current recorder department
73       */
74      private Integer recorderDepartmentId;
75      /**
76       * the current context
77       */
78      private ContextDTO context;
79  
80      /**
81       * <p>Constructor for DaliDataContext.</p>
82       */
83      private DaliDataContext() {
84          // protected constructor, for singleton
85      }
86  
87      /**
88       * <p>instance.</p>
89       *
90       * @return a {@link DaliDataContext} object.
91       */
92      public static DaliDataContext instance() {
93          return INSTANCE;
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public void close() {
99          clearContext();
100     }
101 
102     /**
103      * <p>clearContext.</p>
104      */
105     public void clearContext() {
106         clearContext(false);
107     }
108 
109     /**
110      * <p>clearContextKeepRecorderPerson.</p>
111      */
112     public void clearContextKeepRecorderPerson() {
113         clearContext(true);
114     }
115 
116     private void clearContext(boolean keepRecorderPerson) {
117         if (!keepRecorderPerson) {
118             setRecorderPersonId(null);
119         }
120 
121         // reset local caches
122         resetLocalCache();
123     }
124 
125     /**
126      * <p>resetLocalCache.</p>
127      */
128     public void resetLocalCache() {
129 
130     }
131 
132     /*
133      * recorder person Id
134      */
135     /**
136      * <p>isRecorderPersonFilled.</p>
137      *
138      * @return a boolean.
139      */
140     public boolean isRecorderPersonFilled() {
141         return recorderPersonId != null;
142     }
143 
144     /**
145      * <p>Getter for the field <code>recorderPersonId</code>.</p>
146      *
147      * @return a {@link java.lang.Integer} object.
148      */
149     public Integer getRecorderPersonId() {
150         return recorderPersonId;
151     }
152 
153     /**
154      * <p>Setter for the field <code>recorderPersonId</code>.</p>
155      *
156      * @param recorderPersonId a {@link java.lang.Integer} object.
157      */
158     public void setRecorderPersonId(Integer recorderPersonId) {
159         this.recorderPersonId = recorderPersonId;
160         this.recorderDepartmentId = null;
161     }
162 
163     /** {@inheritDoc} */
164     @Override
165     public int getPrincipalUserId() {
166         return getRecorderPersonId();
167     }
168 
169     /*
170      * recorder department Id
171      */
172     /**
173      * <p>isRecorderDepartmentFilled.</p>
174      *
175      * @return a boolean.
176      */
177     public boolean isRecorderDepartmentFilled() {
178         return recorderDepartmentId != null;
179     }
180 
181     /**
182      * <p>Getter for the field <code>recorderDepartmentId</code>.</p>
183      *
184      * @return a {@link java.lang.Integer} object.
185      */
186     public Integer getRecorderDepartmentId() {
187         if (recorderDepartmentId == null && recorderPersonId != null) {
188             recorderDepartmentId = getUserService().getDepartmentIdByUserId(recorderPersonId);
189         }
190         return recorderDepartmentId;
191     }
192 
193     /**
194      * <p>Setter for the field <code>recorderDepartmentId</code>.</p>
195      *
196      * @param recorderDepartmentId a {@link java.lang.Integer} object.
197      */
198     public void setRecorderDepartmentId(Integer recorderDepartmentId) {
199 //        Integer oldId = getRecorderDepartmentId();
200         this.recorderDepartmentId = recorderDepartmentId;
201 //        firePropertyChange(PROPERTY_RECORDER_DEPARTMENT_ID, oldId, recorderDepartmentId);
202     }
203 
204     /**
205      * <p>isRecorderValidator.</p>
206      *
207      * @return a boolean.
208      */
209     public boolean isRecorderValidator() {
210         return SecurityContextHelper.hasAuthority(QuadrigeUserAuthority.VALIDATOR);
211     }
212 
213     /**
214      * <p>isRecorderQualifier.</p>
215      *
216      * @return a boolean.
217      */
218     public boolean isRecorderQualifier() {
219         return SecurityContextHelper.hasAuthority(QuadrigeUserAuthority.QUALIFIER);
220     }
221 
222     /**
223      * <p>Getter for the field <code>context</code>.</p>
224      *
225      * @return a {@link fr.ifremer.dali.dto.configuration.context.ContextDTO} object.
226      */
227     public ContextDTO getContext() {
228         return context;
229     }
230 
231     /**
232      * <p>Setter for the field <code>context</code>.</p>
233      *
234      * @param context a {@link fr.ifremer.dali.dto.configuration.context.ContextDTO} object.
235      */
236     public void setContext(ContextDTO context) {
237         ContextDTO oldContext = getContext();
238         this.context = context;
239         firePropertyChange(PROPERTY_CONTEXT, oldContext, context);
240     }
241 
242     /**
243      * <p>getContextId.</p>
244      *
245      * @return a int.
246      */
247     public int getContextId() {
248         Assert.notNull(getContext());
249         return getContext().getId();
250     }
251 
252     /**
253      * <p>isContextFiltered.</p>
254      *
255      * @param contextFilter a {@link FilterTypeValues} object.
256      * @return a boolean.
257      */
258     public boolean isContextFiltered(FilterTypeValues contextFilter) {
259         Assert.notNull(contextFilter);
260         if (getContext() == null || CollectionUtils.isEmpty(getContext().getFilters())) {
261             return false;
262         }
263         for (FilterDTO filter : getContext().getFilters()) {
264             if (Objects.equals(filter.getFilterTypeId(), contextFilter.getFilterTypeId())) {
265                 return true;
266             }
267         }
268         return false;
269     }
270 
271 
272     // ------------------------------------------------------------------------//
273     // -- Services methods --//
274     // ------------------------------------------------------------------------//
275 
276     /**
277      * <p>Getter for the field <code>decoratorService</code>.</p>
278      *
279      * @return a {@link fr.ifremer.dali.decorator.DecoratorService} object.
280      */
281     public DecoratorService getDecoratorService() {
282         return DaliServiceLocator.instance().getDecoratorService();
283     }
284 
285     /**
286      * <p>getSystemService.</p>
287      *
288      * @return a {@link fr.ifremer.dali.service.system.SystemService} object.
289      */
290     public SystemService getSystemService() {
291         return DaliServiceLocator.instance().getSystemService();
292     }
293 
294     /**
295      * <p>getReferentialService.</p>
296      *
297      * @return a {@link fr.ifremer.dali.service.referential.ReferentialService} object.
298      */
299     public ReferentialService getReferentialService() {
300         return DaliServiceLocator.instance().getReferentialService();
301     }
302 
303     /**
304      * <p>getObservationService.</p>
305      *
306      * @return a {@link fr.ifremer.dali.service.observation.ObservationService} object.
307      */
308     public ObservationService getObservationService() {
309         return DaliServiceLocator.instance().getObservationService();
310     }
311 
312     /**
313      * <p>getExtractionService.</p>
314      *
315      * @return a {@link fr.ifremer.dali.service.extraction.ExtractionService} object.
316      */
317     public ExtractionService getExtractionService() {
318         return DaliServiceLocator.instance().getExtractionService();
319     }
320 
321     /**
322      * <p>getExtractionPerformService.</p>
323      *
324      * @return a {@link fr.ifremer.dali.service.extraction.ExtractionPerformService} object.
325      */
326     public ExtractionPerformService getExtractionPerformService() {
327         return DaliServiceLocator.instance().getExtractionPerformService();
328     }
329 
330     /**
331      * <p>getPersistenceService.</p>
332      *
333      * @return a {@link fr.ifremer.dali.service.persistence.PersistenceService} object.
334      */
335     public PersistenceService getPersistenceService() {
336         return DaliServiceLocator.instance().getPersistenceService();
337     }
338 
339     /**
340      * <p>getUserService.</p>
341      *
342      * @return a {@link fr.ifremer.dali.service.administration.user.UserService} object.
343      */
344     public UserService getUserService() {
345         return DaliServiceLocator.instance().getUserService();
346     }
347 
348     /**
349      * <p>getSynchroHistoryService.</p>
350      *
351      * @return a {@link fr.ifremer.quadrige3.synchro.service.client.SynchroHistoryService} object.
352      */
353     public SynchroHistoryService getSynchroHistoryService() {
354         return DaliServiceLocator.instance().getSynchroHistoryService();
355     }
356 
357     /**
358      * <p>getProgramStrategyService.</p>
359      *
360      * @return a {@link fr.ifremer.dali.service.administration.program.ProgramStrategyService} object.
361      */
362     public ProgramStrategyService getProgramStrategyService() {
363         return DaliServiceLocator.instance().getProgramStrategyService();
364     }
365 
366     /**
367      * <p>getContextService.</p>
368      *
369      * @return a {@link fr.ifremer.dali.service.administration.context.ContextService} object.
370      */
371     public ContextService getContextService() {
372         return DaliServiceLocator.instance().getContextService();
373     }
374 
375     /**
376      * <p>getRulesControlService.</p>
377      *
378      * @return a {@link ControlRuleService} object.
379      */
380     public ControlRuleService getRulesControlService() {
381         return DaliServiceLocator.instance().getRulesControlService();
382     }
383 
384     public RuleListService getRuleListService() {
385         return DaliServiceLocator.instance().getRuleListService();
386     }
387 
388     public CampaignService getCampaignService() {
389         return DaliServiceLocator.instance().getCampaignService();
390     }
391 }