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.referential.Caracteristic;
28  import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue;
29  import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValues;
30  import fr.ifremer.tutti.persistence.entities.referential.Caracteristics;
31  import org.nuiton.csv.Common;
32  import org.nuiton.csv.ValueParserFormatter;
33  
34  import java.io.Serializable;
35  import java.text.ParseException;
36  
37  import static org.nuiton.i18n.I18n.t;
38  
39  /**
40   * Created on 2/14/15.
41   *
42   * @author Tony Chemit - chemit@codelutin.com
43   * @since 3.14
44   */
45  public class CaracteristicValueParserFormatter implements ValueParserFormatter<Serializable> {
46  
47      public static CaracteristicValueParserFormatter newFormatter() {
48          return new CaracteristicValueParserFormatter(false, null);
49      }
50  
51      public static CaracteristicValueParserFormatter newTechnicalFormatter() {
52          return new CaracteristicValueParserFormatter(true, null);
53      }
54  
55      public static CaracteristicValueParserFormatter newParser(Caracteristic caracteristic) {
56          return new CaracteristicValueParserFormatter(true, caracteristic);
57      }
58  
59      private final boolean technical;
60  
61      private final Caracteristic caracteristic;
62  
63      private final ValueParserFormatter<Integer> integerDelegate;
64  
65      private final ValueParserFormatter<Float> floatDelegate;
66  
67      private final ValueParserFormatter<String> textDelegate;
68  
69      protected CaracteristicValueParserFormatter(boolean technical, Caracteristic caracteristic) {
70          this.technical = technical;
71          this.caracteristic = caracteristic;
72          this.integerDelegate = Common.INTEGER;
73          this.floatDelegate = Common.FLOAT;
74          this.textDelegate = Common.STRING;
75      }
76  
77      @Override
78      public Serializable parse(String value) throws ParseException {
79  
80          Serializable result;
81  
82          if ("NA".equals(value)) {
83  
84              result = null;
85  
86          } else if (Caracteristics.isNumberCaracteristic(caracteristic)) {
87  
88              result = floatDelegate.parse(value);
89  
90          } else if (Caracteristics.isTextCaracteristic(caracteristic)) {
91  
92              result = value;
93  
94          } else {
95  
96              result = CaracteristicQualitativeValues.getQualitativeValue(caracteristic, value);
97              if (result == null) {
98                  throw new ParseException(t("tutti.service.csv.caracteristic.qualitativeValue.notFound", caracteristic.getId(), value), 0);
99              }
100 
101         }
102 
103         return result;
104 
105     }
106 
107     @Override
108     public String format(Serializable e) {
109 
110         String value;
111 
112         if (e == null) {
113 
114             value = "NA";
115 
116         } else {
117 
118             if (e instanceof Float) {
119 
120                 value = floatDelegate.format((Float) e);
121 
122             } else if (e instanceof Integer) {
123 
124                 value = integerDelegate.format((Integer) e);
125 
126             } else if (e instanceof String) {
127 
128                 value = textDelegate.format((String) e);
129 
130             } else {
131 
132                 CaracteristicQualitativeValue qv = (CaracteristicQualitativeValue) e;
133                 value = technical ? qv.getId() : qv.getDescription();
134 
135             }
136 
137         }
138 
139         return value;
140 
141     }
142 
143 }