Add possibility to specifiy multiple uuids as command parameters
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / handler / UuidsParameterTypeConverter.java
1 package eu.etaxonomy.taxeditor.handler;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.UUID;
6
7 import org.eclipse.core.commands.AbstractParameterValueConverter;
8 import org.eclipse.core.commands.ParameterValueConversionException;
9
10 /**
11 * Converts either a single {@link UUID} or a list of UUIDSs to a string representation
12 * and vica versa.
13 *
14 * @author pplitzner
15 * @date Sep 15, 2015
16 *
17 */
18 public class UuidsParameterTypeConverter extends AbstractParameterValueConverter {
19
20 private static final String SEPARATOR = ",";
21
22 public UuidsParameterTypeConverter() {
23 }
24
25 @Override
26 public Object convertToObject(String parameterValue) throws ParameterValueConversionException {
27 if(parameterValue.endsWith(SEPARATOR)){
28 List<UUID> uuids = new ArrayList<UUID>();
29 String[] split = parameterValue.split(SEPARATOR);
30 for (String string : split) {
31 uuids.add(UUID.fromString(string));
32 }
33 return uuids;
34 }
35 else{
36 return UUID.fromString(parameterValue);
37 }
38 }
39
40 @Override
41 public String convertToString(Object parameterValue) throws ParameterValueConversionException {
42 if(parameterValue instanceof List){
43 List list = (List)parameterValue;
44 String stringList = "";
45 for (Object object : list) {
46 stringList += parameterValue.toString()+SEPARATOR;
47 }
48 }
49 else if(parameterValue instanceof UUID){
50 return parameterValue.toString();
51 }
52 else{
53 throw new ParameterValueConversionException("Parameter is of wrong type: "+parameterValue.getClass().toString());
54 }
55 return null;
56 }
57
58 }