View Javadoc
1   package fr.ifremer.tutti.service.csv;
2   
3   /*
4    * #%L
5    * Tutti :: Service
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (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 General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import com.google.common.collect.Lists;
28  import fr.ifremer.tutti.persistence.entities.TuttiEntities;
29  import fr.ifremer.tutti.persistence.entities.TuttiEntity;
30  import fr.ifremer.tutti.persistence.entities.referential.Species;
31  import fr.ifremer.tutti.persistence.entities.referential.Speciess;
32  import org.nuiton.csv.ExportableColumn;
33  import org.nuiton.csv.ValueFormatter;
34  import org.nuiton.csv.ext.AbstractImportExportModel;
35  
36  import java.util.List;
37  import java.util.Map;
38  
39  /**
40   * Created on 2/5/15.
41   *
42   * @author Tony Chemit - chemit@codelutin.com
43   * @since 3.13
44   */
45  public abstract class AbstractTuttiImportExportModel<M> extends AbstractImportExportModel<M> {
46  
47      public AbstractTuttiImportExportModel(char separator) {
48          super(separator);
49      }
50  
51      public <E extends TuttiEntity> void newForeignKeyColumn(String propertyName, Class<E> entityType, String foreignKeyName, Map<String, E> universe) {
52          newMandatoryColumn(propertyName, propertyName, new ForeignKeyParserFormatter<>(entityType, foreignKeyName, universe));
53      }
54  
55      public <E extends TuttiEntity> void newForeignKeyColumn(String headerName, String propertyName, Class<E> entityType, String foreignKeyName, Map<String, E> universe) {
56          newMandatoryColumn(headerName, propertyName, new ForeignKeyParserFormatter<>(entityType, foreignKeyName, universe));
57      }
58  
59      public void newSpeciesForeignKeyColumn(String propertyName, List<Species> species) {
60          if (species == null) {
61              species = Lists.newArrayList();
62          }
63          Map<String, Species> universe = Speciess.splitReferenceSpeciesByReferenceTaxonId(species);
64          newMandatoryColumn(propertyName, propertyName, new ForeignKeyParserFormatter<>(Species.class, Species.PROPERTY_REFERENCE_TAXON_ID, universe));
65      }
66  
67      public <E extends TuttiEntity> void newForeignKeyColumn(String propertyName, Class<E> entityType, List<E> list) {
68          if (list == null) {
69              list = Lists.newArrayList();
70          }
71          Map<String, E> universe = TuttiEntities.splitById(list);
72          newMandatoryColumn(propertyName, propertyName, new ForeignKeyParserFormatter<>(entityType, TuttiEntity.PROPERTY_ID, universe));
73      }
74  
75      public <T> ExportableColumn<M, T> newNullableColumnForExport(String headerName, String propertyName, ValueFormatter<T> valueFormatter) {
76          return modelBuilder.newColumnForExport(headerName, new BeanNullableGetter<>(propertyName), valueFormatter);
77      }
78  
79      public ExportableColumn<M, String> newNullableColumnForExport(String headerName, String propertyName) {
80          return newNullableColumnForExport(headerName, propertyName, TuttiCsvUtil.STRING);
81      }
82  
83      public <T> ExportableColumn<M, T> newIndexNullableColumnForExport(String headerName, String collectionName, int order, String propertyName, ValueFormatter<T> valueFormatter) {
84          return modelBuilder.newColumnForExport(headerName, new BeanIndexNullableGetter<>(collectionName, order, propertyName), valueFormatter);
85      }
86  
87      public ExportableColumn<M, String> newIndexNullableColumnForExport(String headerName, String collectionName, int order, String propertyName) {
88          return newIndexNullableColumnForExport(headerName, collectionName, order, propertyName, TuttiCsvUtil.STRING);
89      }
90  
91  }