View Javadoc
1   package fr.ifremer.reefdb.service;
2   
3   /*
4    * #%L
5    * Reef DB :: 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.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   * Data context of ui.
43   * <p/>
44   * All shared data must be there to avoid reloading some stuff.
45   *
46   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
47   */
48  public class ReefDbDataContext extends AbstractBean implements Closeable, SecurityContext {
49  
50      /** Constant <code>PROPERTY_RECORDER_PERSON_ID="recorderPersonId"</code> */
51      public static final String PROPERTY_RECORDER_PERSON_ID = "recorderPersonId";
52      /** Constant <code>PROPERTY_RECORDER_DEPARTMENT_ID="recorderDepartmentId"</code> */
53      public static final String PROPERTY_RECORDER_DEPARTMENT_ID = "recorderDepartmentId";
54      /** Constant <code>PROPERTY_CONTEXT="context"</code> */
55      public static final String PROPERTY_CONTEXT = "context";
56      /**
57       * Logger.
58       */
59      private static final Log LOG = LogFactory.getLog(ReefDbDataContext.class);
60      // Singleton
61      private static final ReefDbDataContext INSTANCE = new ReefDbDataContext();
62      /**
63       * the current recorder user
64       */
65      private Integer recorderPersonId;
66      /**
67       * the current recorder department
68       */
69      private Integer recorderDepartmentId;
70      /**
71       * the current context
72       */
73      private ContextDTO context;
74  
75      /**
76       * <p>Constructor for ReefDbDataContext.</p>
77       */
78      protected ReefDbDataContext() {
79          // protected constructor, for singleton
80      }
81  
82      /**
83       * <p>instance.</p>
84       *
85       * @return a {@link fr.ifremer.reefdb.service.ReefDbDataContext} object.
86       */
87      public static ReefDbDataContext instance() {
88          return INSTANCE;
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public void close() {
94          clearContext();
95      }
96  
97      /**
98       * <p>clearContext.</p>
99       */
100     public void clearContext() {
101         clearContext(false);
102     }
103 
104     /**
105      * <p>clearContextKeepRecorderPerson.</p>
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         // reset local caches
117         resetLocalCache();
118     }
119 
120     /**
121      * <p>resetLocalCache.</p>
122      */
123     public void resetLocalCache() {
124 
125     }
126 
127     /*
128      * recorder person Id
129      */
130     /**
131      * <p>isRecorderPersonFilled.</p>
132      *
133      * @return a boolean.
134      */
135     public boolean isRecorderPersonFilled() {
136         return recorderPersonId != null;
137     }
138 
139     /**
140      * <p>Getter for the field <code>recorderPersonId</code>.</p>
141      *
142      * @return a {@link java.lang.Integer} object.
143      */
144     public Integer getRecorderPersonId() {
145         return recorderPersonId;
146     }
147 
148     /**
149      * <p>Setter for the field <code>recorderPersonId</code>.</p>
150      *
151      * @param recorderPersonId a {@link java.lang.Integer} object.
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     /** {@inheritDoc} */
160     @Override
161     public int getPrincipalUserId() {
162         return getRecorderPersonId();
163     }
164 
165     /*
166      * recorder department Id
167      */
168     /**
169      * <p>isRecorderDepartmentFilled.</p>
170      *
171      * @return a boolean.
172      */
173     public boolean isRecorderDepartmentFilled() {
174         return recorderDepartmentId != null;
175     }
176 
177     /**
178      * <p>Getter for the field <code>recorderDepartmentId</code>.</p>
179      *
180      * @return a {@link java.lang.Integer} object.
181      */
182     public Integer getRecorderDepartmentId() {
183         return recorderDepartmentId;
184     }
185 
186     /**
187      * <p>Setter for the field <code>recorderDepartmentId</code>.</p>
188      *
189      * @param recorderDepartmentId a {@link java.lang.Integer} object.
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      * <p>isRecorderValidator.</p>
199      *
200      * @return a boolean.
201      */
202     public boolean isRecorderValidator() {
203         return SecurityContextHelper.hasAuthority(QuadrigeUserAuthority.VALIDATOR);
204     }
205 
206     /**
207      * <p>Getter for the field <code>context</code>.</p>
208      *
209      * @return a {@link fr.ifremer.reefdb.dto.configuration.context.ContextDTO} object.
210      */
211     public ContextDTO getContext() {
212         return context;
213     }
214 
215     /**
216      * <p>Setter for the field <code>context</code>.</p>
217      *
218      * @param context a {@link fr.ifremer.reefdb.dto.configuration.context.ContextDTO} object.
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      * <p>getContextId.</p>
228      *
229      * @return a int.
230      */
231     public int getContextId() {
232         Assert.notNull(getContext());
233         return getContext().getId();
234     }
235 
236     /**
237      * <p>isContextFiltered.</p>
238      *
239      * @param contextFilter a {@link FilterTypeValues} object.
240      * @return a boolean.
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 }