com.opencarte.db.VotesParser.java Source code

Java tutorial

Introduction

Here is the source code for com.opencarte.db.VotesParser.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opencarte.db;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.opencarte.model.DataSet;
import com.opencarte.model.ParisTaxi;
import com.opencarte.model.VotePoints;
import com.opencarte.model.VotePoints.VotePointsAux;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URL;
import org.apache.commons.io.IOUtils;

/**
 *
 * @author Yami
 */
public class VotesParser extends Parser {

    private String myFileContent = null;
    private GsonBuilder myBuilder;
    private Gson myyGson;

    public VotesParser(String fileName) {
        super(fileName);
        try {
            this.myFileContent = IOUtils.toString(new URL(fileName).openStream());
            myBuilder = new GsonBuilder();
        } catch (IOException ex) {
            System.out.println(ex);
            //Logger.getLogger(Parser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public VotePoints[] getDataSet() {

        //GsonBuilder myBuilder;
        //Gson myGson; 
        DataSet[] dataset = super.getDataSet();
        VotePoints[] votePoints = new VotePoints[dataset.length];
        VotePointsAux[] votepointsaux = new VotePointsAux[dataset.length];

        JsonDeserializer<VotePointsAux> votepointsauxDes = new JsonDeserializer<VotePointsAux>() {

            @Override
            public VotePointsAux deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
                    throws JsonParseException {
                JsonObject deserialize = je.getAsJsonObject();
                return new VotePointsAux(
                        deserialize.get("fields").getAsJsonObject().get("equipement").getAsString(),
                        deserialize.get("fields").getAsJsonObject().get("adresse").getAsString());
            }
        };
        myBuilder.registerTypeAdapter(VotePointsAux.class, votepointsauxDes);
        myyGson = myBuilder.create();

        try {
            votepointsaux = myyGson.fromJson(myFileContent, VotePointsAux[].class);
        } catch (JsonParseException ex) {
            System.out.println("Invalid JSON: " + ex.getMessage());
        }

        for (int i = 0; i < dataset.length; i++) {
            //parisTaxis[i].setDatasetid(dataset[i].getDatasetid());
            votePoints[i] = new VotePoints(dataset[i].getDatasetid(), dataset[i].getRecordid(),
                    dataset[i].getGeometry(), votepointsaux[i]);// TaxietGeometry(dataset[i].getGeometry());
            //parisTaxis[i].setFields();
        }

        return votePoints;
    }

}