View Javadoc
1   package fr.ifremer.tutti.persistence.entities.data;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
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 fr.ifremer.adagio.core.dao.technical.synchronization.SynchronizationStatus;
26  import org.apache.commons.lang3.builder.EqualsBuilder;
27  
28  import java.util.Calendar;
29  import java.util.Collections;
30  import java.util.Comparator;
31  import java.util.List;
32  import java.util.Objects;
33  
34  public class Cruises extends AbstractCruises {
35  
36      public static boolean isDirty(Cruise cruise) {
37          String synchronizationStatus = cruise.getSynchronizationStatus();
38          return SynchronizationStatus.DIRTY.getValue().equals(synchronizationStatus);
39      }
40  
41      public static boolean isReadyToSynch(Cruise cruise) {
42          String synchronizationStatus = cruise.getSynchronizationStatus();
43          return SynchronizationStatus.READY_TO_SYNCHRONIZE.getValue().equals(synchronizationStatus);
44      }
45  
46      public static boolean isSynch(Cruise cruise) {
47          String synchronizationStatus = cruise.getSynchronizationStatus();
48          return SynchronizationStatus.SYNCHRONIZED.getValue().equals(synchronizationStatus);
49      }
50  
51      public static boolean equalsNaturalId(Cruise cruise1, String naturalId2) {
52  
53          String naturalId1 = getNaturalId(cruise1);
54          return Objects.equals(naturalId1, naturalId2);
55  
56      }
57  
58      public static boolean equals(Cruise cruise1, Cruise cruise2) {
59  
60          EqualsBuilder equalsBuilder = new EqualsBuilder();
61  
62          Calendar instance = Calendar.getInstance();
63          int year1;
64  
65          if (cruise1.getBeginDate() == null) {
66              year1 = -1;
67          } else {
68              instance.setTime(cruise1.getBeginDate());
69              year1 = instance.get(Calendar.YEAR);
70          }
71  
72          int year2;
73          if (cruise1.getBeginDate() == null) {
74              year2 = -2;
75          } else {
76              instance.setTime(cruise2.getBeginDate());
77              year2 = instance.get(Calendar.YEAR);
78          }
79          equalsBuilder.append(year1, year2);
80          String surveyPart1 = cruise1.getSurveyPart();
81          if (surveyPart1 == null) {
82              surveyPart1 = "";
83          }
84          String surveyPart2 = cruise2.getSurveyPart();
85          if (surveyPart2 == null) {
86              surveyPart2 = "";
87          }
88          equalsBuilder.append(surveyPart1, surveyPart2);
89          equalsBuilder.append(cruise1.getProgram(), cruise2.getProgram());
90  
91          return equalsBuilder.isEquals();
92  
93      }
94  
95      public static String getNaturalId(Cruise cruise) {
96  
97          Calendar instance = Calendar.getInstance();
98          int year1;
99  
100         if (cruise.getBeginDate() == null) {
101             year1 = -1;
102         } else {
103             instance.setTime(cruise.getBeginDate());
104             year1 = instance.get(Calendar.YEAR);
105         }
106         String surveyPart1 = cruise.getSurveyPart();
107         if (surveyPart1 == null) {
108             surveyPart1 = "";
109         }
110         String programId = cruise.getProgram().getId();
111 
112         return year1 + "--" + surveyPart1 + "--" + programId;
113 
114     }
115 
116     public static void sort(List<Cruise> cruises) {
117         Collections.sort(cruises, CRUISE_COMPARATOR);
118     }
119 
120     public static Comparator<Cruise> CRUISE_COMPARATOR = (o1, o2) -> o1.getName().compareTo(o2.getName());
121 
122 }