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 com.google.common.base.Preconditions;
26  import com.google.common.collect.Lists;
27  import org.apache.commons.beanutils.ConversionException;
28  import org.apache.commons.lang3.StringUtils;
29  import org.nuiton.converter.NuitonConverter;
30  
31  import java.util.List;
32  
33  import static org.nuiton.i18n.I18n.t;
34  
35  /**
36   * To convert to a {@link SampleCategoryModel} from a string representation.
37   *
38   * Each entry is a couple (categoryId,label).
39   *
40   * @author Tony Chemit - chemit@codelutin.com
41   * @since 2.4
42   */
43  public class SampleCategoryModelConverter implements NuitonConverter {
44  
45      @Override
46      public <T> T convert(Class<T> aClass, Object value) {
47          Preconditions.checkNotNull(
48                  value, "Can not convert a null SampleCategoryModel");
49          if (isEnabled(aClass)) {
50              Object result;
51              if (isEnabled(value.getClass())) {
52                  result = value;
53                  return (T) result;
54              }
55              if (value instanceof String) {
56  
57                  List<SampleCategoryModelEntry> entries = Lists.newArrayList();
58  
59                  String strValue = (String) value;
60  
61                  if (StringUtils.isNotEmpty(strValue)) {
62                      String[] entryStrs = strValue.split("\\s*\\|\\s*");
63  
64                      for (String entryStr : entryStrs) {
65                          String[] entryParts = entryStr.split("\\s*\\,\\s*");
66                          SampleCategoryModelEntry entry = new SampleCategoryModelEntry();
67                          entry.setCategoryId(Integer.valueOf(entryParts[0]));
68                          entry.setLabel(entryParts[1]);
69                          if (entryParts.length > 2) {
70                              entry.setCode(entryParts[2]);
71                          } else {
72                              entry.setCode(SampleCategoryModels.getCode(entryParts[1]));
73                          }
74                          entry.setOrder(entries.size());
75                          entries.add(entry);
76                      }
77                  }
78                  result = new SampleCategoryModel(entries);
79                  return (T) result;
80              }
81          }
82          throw new ConversionException(
83                  t("tutti.persistence.error.no.convertor", aClass.getName(), value));
84      }
85  
86      protected boolean isEnabled(Class<?> aClass) {
87          return SampleCategoryModel.class.equals(aClass);
88      }
89  
90      @Override
91      public Class<?> getType() {
92          return SampleCategoryModel.class;
93      }
94  }