1 package fr.ifremer.tutti.persistence.entities.data;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
37
38
39
40
41
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 }