summaryrefslogtreecommitdiffstats
path: root/verigraph/src/it/polito/verigraph/deserializer/GraphCustomDeserializer.java
blob: 19245c2e955c84f35e2e3e3b0dea1f437f70bc2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*******************************************************************************
 * Copyright (c) 2017 Politecnico di Torino and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License, Version 2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 *******************************************************************************/
package it.polito.verigraph.deserializer;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

import it.polito.verigraph.exception.BadRequestException;
import it.polito.verigraph.exception.InternalServerErrorException;
import it.polito.verigraph.model.Graph;
import it.polito.verigraph.model.Node;


/**
 * The Class GraphCustomDeserializer is a custom deserializer for a Graph object
 */
public class GraphCustomDeserializer extends JsonDeserializer<Graph>{

    /* (non-Javadoc)
     * @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
     */
    @Override
    public Graph deserialize(JsonParser jp, DeserializationContext context){
        JsonNode root = null;
        try {
            root = jp.getCodec().readTree(jp);
        }
        catch (JsonProcessingException e) {
            throw new InternalServerErrorException("Error parsing a graph: " + e.getMessage());
        }
        catch (IOException e) {
            throw new InternalServerErrorException("I/O error parsing a graph: " + e.getMessage());
        }

        JsonNode nodesJson = root.get("nodes");

        if(nodesJson == null)
            throw new BadRequestException("Invalid graph");

        List<Node> nodeList = null;
        try {
            nodeList = new ObjectMapper().readValue(nodesJson.toString(), TypeFactory.defaultInstance().constructCollectionType(List.class, Node.class));
        }
        catch (JsonParseException e) {
            throw new BadRequestException("Invalid content for a graph: " + e.getMessage());
        }
        catch (JsonMappingException e) {
            throw new BadRequestException("Invalid input json structure for a graph: " + e.getMessage());
        }
        catch (IOException e) {
            throw new InternalServerErrorException("I/O error parsing a graph: " + e.getMessage());
        }

        Graph graph = new Graph();
        if(root.get("id") != null){
            long graphId = root.get("id").asLong();
            graph.setId(graphId);
        }
        Map<Long, Node> nodes = graph.getNodes();

        long numberOfNodes = 0;
        for (Node node : nodeList){
            nodes.put(++numberOfNodes, node);
        }
        return graph;

    }

}