Project

General

Profile

Download (2.46 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2019 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.remote.converter;
10

    
11
import java.io.IOException;
12
import java.util.ArrayList;
13
import java.util.List;
14
import java.util.UUID;
15

    
16
import org.springframework.core.convert.converter.Converter;
17

    
18
import com.fasterxml.jackson.core.JsonParseException;
19
import com.fasterxml.jackson.databind.JsonMappingException;
20
import com.fasterxml.jackson.databind.ObjectMapper;
21

    
22
import eu.etaxonomy.cdm.persistence.dao.common.Restriction;
23

    
24
/**
25
 * Converter implementation to read a {@link Restriction} from its
26
 * json serialization.
27
 *
28
 * @author a.kohlbecker
29
 * @since Jun 3, 2019
30
 *
31
 */
32
public class RestrictionConverter implements Converter<String, Restriction<?>>  {
33

    
34
    private ObjectMapper objectMapper;
35

    
36
    public RestrictionConverter (ObjectMapper objectMapper) {
37
        this.objectMapper = objectMapper;
38
    }
39

    
40
    @Override
41
    public Restriction<?> convert(String source) {
42

    
43
            try {
44
                Restriction restriction = objectMapper.readValue(source, Restriction.class);
45
                // the below loop is detects UUID string representations and converts them to UUIDs
46
                // such conversion is needed for all user types, we are only handling UUIDs here quickly and dirty
47
                // TODO think about the best solution, handle the string to object conversion in
48
                // CdmEntityDaoBase.createRestriction(String propertyName, Object value, MatchMode matchMode) ?
49
                List<Object> convertedValues = new ArrayList<>(restriction.getValues().size());
50
                for(Object val : restriction.getValues()){
51
                    if(val.toString().matches("([a-f\\d]{8}(-[a-f\\d]{4}){3}-[a-f\\d]{12}?)")){
52
                        convertedValues.add(UUID.fromString(val.toString()));
53
                    } else {
54
                        convertedValues.add(val);
55
                    }
56
                }
57
                restriction.setValues(convertedValues);
58
                return restriction ;
59
            } catch (JsonParseException | JsonMappingException e) {
60
                throw new IllegalArgumentException(e);
61
            }catch (IOException e) {
62
                // TODO more specific unchecked exception type?
63
                throw new RuntimeException(e);
64
            }
65

    
66
    }
67

    
68
}
    (1-1/1)