View Javadoc
1   package fr.ifremer.dali.ui.swing.content.extraction.list;
2   
3   /*
4    * #%L
5    * Dali :: UI
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.dto.DaliBeanFactory;
27  import fr.ifremer.dali.dto.ErrorDTO;
28  import fr.ifremer.dali.dto.configuration.filter.FilterDTO;
29  import fr.ifremer.dali.dto.enums.ExtractionFilterTypeValues;
30  import fr.ifremer.dali.dto.referential.GroupingTypeDTO;
31  import fr.ifremer.dali.dto.referential.PersonDTO;
32  import fr.ifremer.dali.dto.system.extraction.ExtractionDTO;
33  import fr.ifremer.dali.dto.system.extraction.ExtractionParameterDTO;
34  import fr.ifremer.dali.ui.swing.util.table.AbstractDaliRowUIModel;
35  import fr.ifremer.quadrige3.core.dao.technical.Assert;
36  import fr.ifremer.quadrige3.ui.core.dto.referential.StatusDTO;
37  import org.apache.commons.collections4.CollectionUtils;
38  import org.nuiton.util.beans.Binder;
39  import org.nuiton.util.beans.BinderFactory;
40  
41  import java.util.Collection;
42  import java.util.Collections;
43  import java.util.Date;
44  
45  /**
46   * extractions table row model
47   */
48  public class ExtractionsRowModel extends AbstractDaliRowUIModel<ExtractionDTO, ExtractionsRowModel> implements ExtractionDTO {
49  
50      private static final Binder<ExtractionDTO, ExtractionsRowModel> FROM_BEAN_BINDER =
51              BinderFactory.newBinder(ExtractionDTO.class, ExtractionsRowModel.class);
52  
53      private static final Binder<ExtractionsRowModel, ExtractionDTO> TO_BEAN_BINDER =
54              BinderFactory.newBinder(ExtractionsRowModel.class, ExtractionDTO.class);
55  
56      private boolean filtersValid;
57  
58      /**
59       * Constant <code>PROPERTY_GROUPING_TYPE="groupingType"</code>
60       */
61      public static final String PROPERTY_GROUPING_TYPE = "groupingType";
62  
63      /**
64       * <p>Constructor for ExtractionsRowModel.</p>
65       */
66      public ExtractionsRowModel() {
67          super(FROM_BEAN_BINDER, TO_BEAN_BINDER);
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      protected ExtractionDTO newBean() {
75          return DaliBeanFactory.newExtractionDTO();
76      }
77  
78      /**
79       * <p>getFilterOfType.</p>
80       *
81       * @param filterTypeId a int.
82       * @return a {@link fr.ifremer.dali.dto.configuration.filter.FilterDTO} object.
83       */
84      public FilterDTO getFilterOfType(int filterTypeId) {
85          if (!isFiltersEmpty()) {
86              for (FilterDTO filter : getFilters()) {
87                  if (filter.getFilterTypeId() == filterTypeId) {
88                      return filter;
89                  }
90              }
91          }
92          return null;
93      }
94  
95      /**
96       * <p>isFiltersValid.</p>
97       *
98       * @return a boolean.
99       */
100     public boolean isFiltersValid() {
101         return filtersValid;
102     }
103 
104     /**
105      * <p>Setter for the field <code>filtersValid</code>.</p>
106      *
107      * @param filtersValid a boolean.
108      */
109     public void setFiltersValid(boolean filtersValid) {
110         this.filtersValid = filtersValid;
111     }
112 
113     /**
114      * <p>replaceFilter.</p>
115      *
116      * @param filter a {@link fr.ifremer.dali.dto.configuration.filter.FilterDTO} object.
117      * @return true if filter has been replaced, false if filter has been only added
118      */
119     public boolean replaceFilter(FilterDTO filter) {
120         Assert.notNull(filter);
121         Assert.notNull(filter.getFilterTypeId());
122 
123         boolean removed = false;
124         if (!isFiltersEmpty()) {
125             removed = removeFilters(getFilterOfType(filter.getFilterTypeId()));
126         }
127         if (CollectionUtils.isNotEmpty(filter.getElements())) {
128             addFilters(filter);
129         }
130         setDirty(true);
131         setModify(true);
132         return removed;
133     }
134 
135     /**
136      * <p>getGroupingType.</p>
137      *
138      * @return a {@link fr.ifremer.dali.dto.referential.GroupingTypeDTO} object.
139      */
140     public GroupingTypeDTO getGroupingType() {
141 
142         FilterDTO filter = getFilterOfType(ExtractionFilterTypeValues.ORDER_ITEM_TYPE.getFilterTypeId());
143         if (filter != null && CollectionUtils.isNotEmpty(filter.getElements())) {
144             return (GroupingTypeDTO) filter.getElements().get(0);
145 
146         }
147         return null;
148     }
149 
150     /**
151      * <p>setGroupingType.</p>
152      *
153      * @param groupingType a {@link fr.ifremer.dali.dto.referential.GroupingTypeDTO} object.
154      */
155     public void setGroupingType(GroupingTypeDTO groupingType) {
156 
157         // update the specific filter
158         FilterDTO filter = getFilterOfType(ExtractionFilterTypeValues.ORDER_ITEM_TYPE.getFilterTypeId());
159         if (filter == null) {
160             filter = DaliBeanFactory.newFilterDTO();
161             filter.setFilterTypeId(ExtractionFilterTypeValues.ORDER_ITEM_TYPE.getFilterTypeId());
162         }
163         filter.setElements(Collections.singletonList(groupingType));
164         filter.setFilterLoaded(true);
165         filter.setDirty(true);
166         replaceFilter(filter);
167     }
168 
169     /* DELEGATE METHODS */
170 
171     /**
172      * <p>getName.</p>
173      *
174      * @return a {@link java.lang.String} object.
175      */
176     public String getName() {
177         return delegateObject.getName();
178     }
179 
180     /**
181      * {@inheritDoc}
182      */
183     public void setName(String name) {
184         delegateObject.setName(name);
185     }
186 
187     /**
188      * <p>isDirty.</p>
189      *
190      * @return a boolean.
191      */
192     public boolean isDirty() {
193         return delegateObject.isDirty();
194     }
195 
196     /**
197      * {@inheritDoc}
198      */
199     public void setDirty(boolean dirty) {
200         delegateObject.setDirty(dirty);
201     }
202 
203     @Override
204     public boolean isReadOnly() {
205         return delegateObject.isReadOnly();
206     }
207 
208     @Override
209     public void setReadOnly(boolean readOnly) {
210         delegateObject.setReadOnly(readOnly);
211     }
212 
213     @Override
214     public Date getCreationDate() {
215         return delegateObject.getCreationDate();
216     }
217 
218     @Override
219     public void setCreationDate(Date date) {
220         delegateObject.setCreationDate(date);
221     }
222 
223     @Override
224     public Date getUpdateDate() {
225         return delegateObject.getUpdateDate();
226     }
227 
228     @Override
229     public void setUpdateDate(Date date) {
230         delegateObject.setUpdateDate(date);
231     }
232 
233     @Override
234     public StatusDTO getStatus() {
235         return null;
236     }
237 
238     @Override
239     public void setStatus(StatusDTO status) {
240 
241     }
242 
243     /**
244      * <p>getUser.</p>
245      *
246      * @return a {@link fr.ifremer.dali.dto.referential.PersonDTO} object.
247      */
248     public PersonDTO getUser() {
249         return delegateObject.getUser();
250     }
251 
252     /**
253      * {@inheritDoc}
254      */
255     public void setUser(PersonDTO user) {
256         delegateObject.setUser(user);
257     }
258 
259     /**
260      * {@inheritDoc}
261      */
262     public FilterDTO getFilters(int index) {
263         return delegateObject.getFilters(index);
264     }
265 
266     /**
267      * <p>isFiltersEmpty.</p>
268      *
269      * @return a boolean.
270      */
271     public boolean isFiltersEmpty() {
272         return delegateObject.isFiltersEmpty();
273     }
274 
275     /**
276      * <p>sizeFilters.</p>
277      *
278      * @return a int.
279      */
280     public int sizeFilters() {
281         return delegateObject.sizeFilters();
282     }
283 
284     /**
285      * {@inheritDoc}
286      */
287     public void addFilters(FilterDTO filters) {
288         delegateObject.addFilters(filters);
289     }
290 
291     /**
292      * {@inheritDoc}
293      */
294     public void addAllFilters(Collection<FilterDTO> filters) {
295         delegateObject.addAllFilters(filters);
296     }
297 
298     /**
299      * {@inheritDoc}
300      */
301     public boolean removeFilters(FilterDTO filters) {
302         return delegateObject.removeFilters(filters);
303     }
304 
305     /**
306      * {@inheritDoc}
307      */
308     public boolean removeAllFilters(Collection<FilterDTO> filters) {
309         return delegateObject.removeAllFilters(filters);
310     }
311 
312     /**
313      * {@inheritDoc}
314      */
315     public boolean containsFilters(FilterDTO filters) {
316         return delegateObject.containsFilters(filters);
317     }
318 
319     /**
320      * {@inheritDoc}
321      */
322     public boolean containsAllFilters(Collection<FilterDTO> filters) {
323         return delegateObject.containsAllFilters(filters);
324     }
325 
326     /**
327      * <p>getFilters.</p>
328      *
329      * @return a {@link java.util.Collection} object.
330      */
331     public Collection<FilterDTO> getFilters() {
332         return delegateObject.getFilters();
333     }
334 
335     /**
336      * {@inheritDoc}
337      */
338     public void setFilters(Collection<FilterDTO> filters) {
339         delegateObject.setFilters(filters);
340     }
341 
342     /**
343      * {@inheritDoc}
344      */
345     public ErrorDTO getErrors(int index) {
346         return delegateObject.getErrors(index);
347     }
348 
349     /**
350      * <p>isErrorsEmpty.</p>
351      *
352      * @return a boolean.
353      */
354     public boolean isErrorsEmpty() {
355         return delegateObject.isErrorsEmpty();
356     }
357 
358     /**
359      * <p>sizeErrors.</p>
360      *
361      * @return a int.
362      */
363     public int sizeErrors() {
364         return delegateObject.sizeErrors();
365     }
366 
367     /**
368      * {@inheritDoc}
369      */
370     public void addErrors(ErrorDTO errors) {
371         delegateObject.addErrors(errors);
372     }
373 
374     /**
375      * {@inheritDoc}
376      */
377     public void addAllErrors(Collection<ErrorDTO> errors) {
378         delegateObject.addAllErrors(errors);
379     }
380 
381     /**
382      * {@inheritDoc}
383      */
384     public boolean removeErrors(ErrorDTO errors) {
385         return delegateObject.removeErrors(errors);
386     }
387 
388     /**
389      * {@inheritDoc}
390      */
391     public boolean removeAllErrors(Collection<ErrorDTO> errors) {
392         return delegateObject.removeAllErrors(errors);
393     }
394 
395     /**
396      * {@inheritDoc}
397      */
398     public boolean containsErrors(ErrorDTO errors) {
399         return delegateObject.containsErrors(errors);
400     }
401 
402     /**
403      * {@inheritDoc}
404      */
405     public boolean containsAllErrors(Collection<ErrorDTO> errors) {
406         return delegateObject.containsAllErrors(errors);
407     }
408 
409     /**
410      * <p>getErrors.</p>
411      *
412      * @return a {@link java.util.Collection} object.
413      */
414     public Collection<ErrorDTO> getErrors() {
415         return delegateObject.getErrors();
416     }
417 
418     /**
419      * {@inheritDoc}
420      */
421     public void setErrors(Collection<ErrorDTO> errors) {
422         delegateObject.setErrors(errors);
423     }
424 
425     @Override
426     public ExtractionParameterDTO getParameter() {
427         return delegateObject.getParameter();
428     }
429 
430     @Override
431     public void setParameter(ExtractionParameterDTO parameter) {
432         delegateObject.setParameter(parameter);
433     }
434 
435 }