View Javadoc
1   package net.sumaris.core.extraction.dao.trip;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Core Extraction
6    * %%
7    * Copyright (C) 2018 - 2019 SUMARiS Consortium
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.collect.Lists;
26  import net.sumaris.core.extraction.dao.ExtractionDao;
27  import net.sumaris.core.extraction.vo.ExtractionFilterCriterionVO;
28  import net.sumaris.core.extraction.vo.ExtractionFilterOperatorEnum;
29  import net.sumaris.core.extraction.vo.ExtractionFilterVO;
30  import net.sumaris.core.extraction.vo.trip.ExtractionTripFilterVO;
31  import net.sumaris.core.util.Beans;
32  import net.sumaris.core.util.Dates;
33  import net.sumaris.core.util.StringUtils;
34  import org.apache.commons.collections4.CollectionUtils;
35  
36  import java.util.List;
37  
38  /**
39   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
40   */
41  public interface ExtractionTripDao extends ExtractionDao {
42  
43      String TR_SHEET_NAME = "TR";
44  
45      default ExtractionTripFilterVO toTripFilterVO(ExtractionFilterVO source){
46          ExtractionTripFilterVOtractionTripFilterVO.html#ExtractionTripFilterVO">ExtractionTripFilterVO target = new ExtractionTripFilterVO();
47          if (source == null) return target;
48  
49          Beans.copyProperties(source, target);
50          target.setPreview(source.isPreview());
51  
52          if (CollectionUtils.isNotEmpty(source.getCriteria())) {
53  
54              source.getCriteria().stream()
55                      .filter(criterion ->
56                              org.apache.commons.lang3.StringUtils.isNotBlank(criterion.getValue())
57                                      && "=".equals(criterion.getOperator()))
58                      .forEach(criterion -> {
59                          switch (criterion.getName().toLowerCase()) {
60                              case "project":
61                                  target.setProgramLabel(criterion.getValue());
62                                  break;
63                              case "year":
64                                  int year = Integer.parseInt(criterion.getValue());
65                                  target.setStartDate(Dates.getFirstDayOfYear(year));
66                                  target.setEndDate(Dates.getLastSecondOfYear(year));
67                                  break;
68                          }
69                      });
70          }
71          return target;
72      }
73  
74      default ExtractionFilterVO toExtractionFilterVO(ExtractionTripFilterVO source){
75          ExtractionFilterVOractionFilterVO.html#ExtractionFilterVO">ExtractionFilterVO target = new ExtractionFilterVO();
76          if (source == null) return target;
77  
78          Beans.copyProperties(source, target);
79  
80          List<ExtractionFilterCriterionVO> criteria = Lists.newArrayList();
81          target.setCriteria(criteria);
82  
83          if (StringUtils.isNotBlank(source.getProgramLabel())) {
84              ExtractionFilterCriterionVOrCriterionVO.html#ExtractionFilterCriterionVO">ExtractionFilterCriterionVO criterion = new ExtractionFilterCriterionVO();
85              criterion.setName("project");
86              criterion.setOperator(ExtractionFilterOperatorEnum.EQUALS.getSymbol());
87              criterion.setValue(source.getProgramLabel());
88  
89              criterion.setSheetName(TR_SHEET_NAME);
90  
91              criteria.add(criterion);
92          }
93  
94          if (source.getStartDate() != null && source.getEndDate() != null) {
95              // TODO convert into a criterion 'year', if first/last day of a year
96          }
97  
98          return target;
99      }
100 }