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 fr.ifremer.tutti.persistence.entities.TuttiEntity;
28  import org.apache.commons.lang3.StringUtils;
29  import org.nuiton.csv.ImportRuntimeException;
30  import org.nuiton.csv.ValueParserFormatter;
31  
32  import java.text.ParseException;
33  import java.util.ArrayList;
34  import java.util.List;
35  import java.util.Map;
36  
37  import static org.nuiton.i18n.I18n.t;
38  
39  /**
40   * @param <E>
41   * @author Kevin Morin - kmorin@codelutin.com
42   * @since 3.10
43   */
44  public class ForeignKeyListParserFormatter<E extends TuttiEntity> implements ValueParserFormatter<List<E>> {
45  
46      public static final String LIST_SEPARATOR = ",";
47  
48      protected final String propertyName;
49  
50      protected final Class<E> entityType;
51  
52      protected final Map<String, E> universe;
53  
54      public ForeignKeyListParserFormatter(Class<E> entityType, String propertyName, Map<String, E> universe) {
55          this.entityType = entityType;
56          this.propertyName = propertyName;
57          this.universe = universe;
58      }
59  
60      @Override
61      public List<E> parse(String value) throws ParseException {
62          List<E> result = new ArrayList<>();
63          if (StringUtils.isNotBlank(value)) {
64  
65              String[] ids = value.split(LIST_SEPARATOR);
66              for (String id : ids) {
67                  // get entity from universe
68                  E entity = universe.get(id);
69  
70                  if (entity == null) {
71  
72                      // can not find entity this is a big problem for us...
73                      throw new ImportRuntimeException(t("tutti.service.csv.parse.entityNotFound", entityType.getSimpleName(), propertyName, id));
74                  }
75  
76                  result.add(entity);
77              }
78          }
79          return result;
80      }
81  
82      @Override
83      public String format(List<E> e) {
84          List<String> ids = new ArrayList<>();
85          for (E entity : e) {
86              if (entity != null) {
87                  ids.add(entity.getId());
88              }
89          }
90          return StringUtils.join(ids, LIST_SEPARATOR);
91      }
92  }