converter.PollutionConverter.java Source code

Java tutorial

Introduction

Here is the source code for converter.PollutionConverter.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.
 */

/**
 *
 * @author Franois
 */
package converter;

import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBObject;
import model.Pollution;

/**
 *
 * @author Franois
 */
public class PollutionConverter {

    // convert Square Object to MongoDB DBObject
    // take special note of converting id String to ObjectId
    public static DBObject toDBObject(Pollution p) {

        BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().append("name", p.getName())
                .append("rate", p.getRate()).append("lat", p.getLatitude()).append("long", p.getLongitude())
                .append("distance", p.getDistance());

        return builder.get();
    }

    // convert DBObject Object to Square
    // take special note of converting ObjectId to String
    public static Pollution toPollution(DBObject doc) {
        Pollution p = new Pollution((String) doc.get("name"), (Double) doc.get("rate"), (Double) doc.get("lat"),
                (Double) doc.get("long"), (Double) doc.get("distance"));

        return p;

    }

}