View Javadoc
1   package fr.ifremer.tutti.service.csv;
2   
3   /*
4    * #%L
5    * Tutti :: Service
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 com.google.common.base.Preconditions;
26  import fr.ifremer.tutti.persistence.entities.data.Program;
27  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
28  import fr.ifremer.tutti.persistence.entities.referential.Gear;
29  import fr.ifremer.tutti.persistence.entities.referential.Person;
30  import fr.ifremer.tutti.persistence.entities.referential.Species;
31  import fr.ifremer.tutti.persistence.entities.referential.TuttiLocation;
32  import fr.ifremer.tutti.persistence.entities.referential.Vessel;
33  import fr.ifremer.tutti.type.WeightUnit;
34  import org.nuiton.csv.Common;
35  import org.nuiton.csv.ValueFormatter;
36  import org.nuiton.csv.ValueParserFormatter;
37  
38  import java.io.Serializable;
39  import java.util.List;
40  import java.util.Set;
41  
42  import static org.nuiton.i18n.I18n.t;
43  
44  /**
45   * Helper around csv import / export in Tutti.
46   *
47   * @author Tony Chemit - chemit@codelutin.com
48   * @since 1.0
49   */
50  public class TuttiCsvUtil extends Common {
51  
52      /**
53       * To parse / format possible nullable weight.
54       *
55       * @since 3.3.2
56       */
57      public static final ValueParserFormatter<Float> WEIGHT_NULL_TO_9 =
58              new FloatParserFormatter(-9f, true) {
59                  @Override
60                  public String format(Float value) {
61                      if (value == null) {
62                          // if value is null then use the default value
63                          value = defaultValue;
64                      }
65                      return WeightUnit.KG.renderWeight(value);
66                  }
67  
68                  @Override
69                  protected Float parseNoneEmptyValue(String value) {
70                      Float aFloat = super.parseNoneEmptyValue(value);
71                      if (defaultValue.equals(aFloat)) {
72                          // if having -9, then value is null
73                          aFloat = null;
74                      }
75                      return aFloat;
76                  }
77              };
78  
79      public static final ValueParserFormatter<Integer> INTEGER_NULL_TO_9 =
80              new IntegerParserFormatter(-9, true) {
81                  @Override
82                  public String format(Integer value) {
83                      if (value == null) {
84                          // if value is null then use the default value
85                          value = defaultValue;
86                      }
87                      return super.format(value);
88                  }
89  
90                  @Override
91                  protected Integer parseNoneEmptyValue(String value) {
92                      Integer aFloat = super.parseNoneEmptyValue(value);
93                      if (defaultValue.equals(aFloat)) {
94                          // if having -9, then value is null
95                          aFloat = null;
96                      }
97                      return aFloat;
98                  }
99              };
100 
101 //    public static final ValueParserFormatter<String> COMMENT_PARSER_FORMATTER = new ValueParserFormatter<String>() {
102 //
103 //        @Override
104 //        public String parse(String value) throws ParseException {
105 //            return value == null ? "" : value.replaceAll("@@", "\n");
106 //        }
107 //
108 //        @Override
109 //        public String format(String value) {
110 //            return value == null ? "" : value.replaceAll("\n", "@@");
111 //        }
112 //    };
113 
114 //    public static final ValueFormatter<Species> SPECIES_NAME_FORMATTER = new ValueFormatter<Species>() {
115 //        @Override
116 //        public String format(Species s) {
117 //            Preconditions.checkNotNull(s, t("tutti.service.error.species.null"));
118 //            return s.getName();
119 //        }
120 //    };
121 
122     public static final ValueFormatter<Species> SPECIES_SURVEY_CODE_FORMATTER = s -> {
123         Preconditions.checkNotNull(s, t("tutti.service.error.species.null"));
124         String surveyCode = s.getSurveyCode();
125         return surveyCode == null ? "" : surveyCode;
126     };
127 
128     public static final ValueFormatter<Species> SPECIES_REF_TAX_CODE_FORMATTER = s -> {
129         Preconditions.checkNotNull(s, t("tutti.service.error.species.null"));
130         String surveyCode = s.getRefTaxCode();
131         return surveyCode == null ? "" : surveyCode;
132     };
133 
134     public static ValueParserFormatter<Float> WEIGHT_PARSER_FORMATTER = new FloatParserFormatter(null, true) {
135 
136         @Override
137         public String format(Float value) {
138             if (value != null) {
139                 value = WeightUnit.KG.round(value);
140             }
141             return super.format(value);
142         }
143 
144         @Override
145         protected Float parseNoneEmptyValue(String value) {
146             Float aFloat = super.parseNoneEmptyValue(value);
147             return WeightUnit.KG.round(aFloat);
148         }
149     };
150 
151     public static final CommentParserFormatter COMMENT_PARSER_FORMATTER = new CommentParserFormatter();
152 
153 //    public static final ValueParserFormatter<List<String>> COMMENT_LIST_PARSER_FORMATTER = new CommentListParserFormatter(COMMENT_PARSER_FORMATTER);
154 
155     public static final ValueFormatter<Caracteristic> CARACTERISTIC_FORMATTER = CaracteristicParserFormatter.newFormatter();
156 
157     public static final ValueFormatter<Caracteristic> CARACTERISTIC_TECHNICAL_FORMATTER = CaracteristicParserFormatter.newTechnicalFormatter();
158 
159     public static final ValueFormatter<Serializable> CARACTERISTIC_VALUE_FORMATTER = CaracteristicValueParserFormatter.newFormatter();
160 
161     public static ValueFormatter<Serializable> CARACTERISTIC_VALUE_TECHNICAL_FORMATTER = CaracteristicValueParserFormatter.newTechnicalFormatter();
162 
163     public static final ValueFormatter<Program> PROGRAM_FORMATTER = ProgramParserFormatter.newFormatter();
164 
165     public static final ValueFormatter<Program> PROGRAM_TECHNICAL_FORMATTER = ProgramParserFormatter.newTechnicalFormatter();
166 
167     public static final ValueFormatter<Gear> GEAR_FORMATTER = GearParserFormatter.newFormatter();
168 
169     public static final ValueFormatter<Gear> GEAR_TECHNICAL_FORMATTER = GearParserFormatter.newTechnicalFormatter();
170 
171 //    public static final ValueFormatter<List<Gear>> GEAR_LIST_FORMATTER = GearListParserFormatter.newFormatter(GearParserFormatter.newFormatter());
172 
173     public static final ValueFormatter<List<Gear>> GEAR_LIST_TECHNICAL_FORMATTER = GearListParserFormatter.newFormatter(GearParserFormatter.newTechnicalFormatter());
174 
175     public static final ValueFormatter<TuttiLocation> COUNTRY_FORMATTER = new CountryFormatter();
176 
177     public static final ValueFormatter<TuttiLocation> PROGRAM_ZONE_FORMATTER = new ProgramZoneFormatter();
178 
179     public static final ValueFormatter<TuttiLocation> HARBOUR_FORMATTER = HarbourParserFormatter.newFormatter();
180 
181     public static final ValueFormatter<TuttiLocation> HARBOUR_TECHNICAL_FORMATTER = HarbourParserFormatter.newTechnicalFormatter();
182 
183     public static final ValueFormatter<TuttiLocation> FISHING_OPERATION_STRATA_FORMATTER = FishingOperationStrataParserFormatter.newFormatter();
184 
185     public static final ValueFormatter<TuttiLocation> FISHING_OPERATION_STRATA_TECHNICAL_FORMATTER = FishingOperationStrataParserFormatter.newTechnicalFormatter();
186 
187     public static final ValueFormatter<TuttiLocation> FISHING_OPERATION_SUB_STRATA_FORMATTER = FishingOperationSubStrataParserFormatter.newFormatter();
188 
189     public static final ValueFormatter<TuttiLocation> FISHING_OPERATION_SUB_STRATA_TECHNICAL_FORMATTER = FishingOperationSubStrataParserFormatter.newTechnicalFormatter();
190 
191     public static final ValueFormatter<TuttiLocation> FISHING_OPERATION_LOCATION_FORMATTER = FishingOperationLocationParserFormatter.newFormatter();
192 
193     public static final ValueFormatter<TuttiLocation> FISHING_OPERATION_LOCATION_TECHNICAL_FORMATTER = FishingOperationLocationParserFormatter.newTechnicalFormatter();
194 
195     public static final ValueFormatter<Vessel> VESSEL_FORMATTER = VesselParserFormatter.newFormatter();
196 
197     public static final ValueFormatter<Vessel> VESSEL_TECHNICAL_FORMATTER = VesselParserFormatter.newTechnicalFormatter();
198 
199     public static final ValueFormatter<Species> SPECIES_FORMATTER = SpeciesParserFormatter.newFormatter();
200 
201     public static final ValueFormatter<Species> SPECIES_TECHNICAL_FORMATTER = SpeciesParserFormatter.newTechnicalFormatter();
202 
203     public static final ValueFormatter<List<Person>> PERSON_LIST_FORMATTER = PersonListParserFormatter.newFormatter(PersonParserFormatter.newFormatter());
204 
205     public static final ValueFormatter<List<Person>> PERSON_LIST_TECHNICAL_FORMATTER = PersonListParserFormatter.newFormatter(PersonParserFormatter.newTechnicalFormatter());
206 
207     public static final ValueFormatter<List<Vessel>> VESSEL_LIST_FORMATTER = VesselListParserFormatter.newFormatter(VesselParserFormatter.newFormatter());
208 
209     public static final ValueFormatter<List<Vessel>> VESSEL_LIST_TECHNICAL_FORMATTER = VesselListParserFormatter.newFormatter(VesselParserFormatter.newTechnicalFormatter());
210 
211     public static final ValueParserFormatter<List<Integer>> INTEGER_LIST_PARSER_FORMATTER = new IntegerListParserFormatter();
212 
213     public static final ValueParserFormatter<Set<String>> STRING_SET_PARSER_FORMATTER = new StringSetParserFormatter();
214 
215     public static <E extends Enum<E>> ValueParserFormatter<E> newEnumByNameParserFormatter(Class<E> enumType, boolean mandatory) {
216         return new fr.ifremer.tutti.service.csv.EnumByNameParserFormatter<>(enumType, mandatory);
217     }
218 
219 
220     protected TuttiCsvUtil() {
221         // no instance
222     }
223 
224 }