tfg.mongoaccessservice.MongoResource.java Source code

Java tutorial

Introduction

Here is the source code for tfg.mongoaccessservice.MongoResource.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 tfg.mongoaccessservice;

import Modelo.AlarmContainer;
import Modelo.DaoAlarms;
import Modelo.DaoMailContext;
import Modelo.DaoUser;
import Modelo.DaoUserTwitterTokens;
import Modelo.DaoWorkflow;
import Modelo.MailContext;
import Modelo.UserTwitterTokens;
import SpecificData.UserData;
import SpecificData.WorkFlowInfo;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

/**
 * REST Web Service
 *
 * @author estevo
 */
@Path("mongo")
public class MongoResource implements ApplicationCodes {

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of MongoResource
     */
    public MongoResource() {
    }

    @GET
    @Path("/getworkflows")
    @Produces(MediaType.APPLICATION_JSON)

    public Response getWork() {
        DBCursor obj = null;
        try {
            DB db = ConnectionFactory.getInstance().getConnection();
            DBCollection coll = db.getCollection("mail");
            obj = coll.find();
            System.out.println(obj);
        } catch (UnknownHostException ex) {
            Logger.getLogger(MongoResource.class.getName()).log(Level.SEVERE, null, ex);
        }

        return Response.status(Response.Status.OK).entity(obj.toArray()).build();

    }

    @POST
    @Path("/storeMail")
    @Consumes("application/x-www-form-urlencoded")
    @Produces("text/plain")
    public Response storeMail(MultivaluedMap<String, String> formParams) {

        MailContext mail = new MailContext(formParams.getFirst("AsociatedWorflowId"),
                formParams.getFirst("mailSender"), formParams.getFirst("passWord"),
                formParams.getFirst("SMTPServer"), formParams.getFirst("SendPort"),
                formParams.getFirst("protocol"));

        DaoMailContext daoMail = new DaoMailContext();

        if (daoMail.storeMailContext(mail) == DATA_SUCCESSFULLY_STORED) {
            return Response.status(DATA_SUCCESSFULLY_STORED).entity(MONGO_SUCCESS_MSG).build();
        }
        return Response.status(ERROR_STORING_DATA).entity(MONGO_ERROR_MSG).build();
    }

    @POST
    @Path("/getMail")
    @Consumes("application/x-www-form-urlencoded")
    @Produces(MediaType.APPLICATION_JSON)

    public Response getMail(MultivaluedMap<String, String> formParams) {

        DaoMailContext daoMail = new DaoMailContext();
        MailContext mail = null;
        mail = daoMail.getMailContext(formParams.getFirst("AsociatedWorflowId"));

        if (mail != null) {
            return Response.status(DATA_FOUND).entity(mail).build();
        }
        return Response.status(NOT_FOUND_IN_DATABASE).entity(MONGO_NOTFOUND_ID).build();
    }

    @POST
    @Path("/deleteMail")
    @Consumes("application/x-www-form-urlencoded")
    @Produces(MediaType.APPLICATION_JSON)

    public Response deleteMail(MultivaluedMap<String, String> formParams) {
        DaoMailContext daoMail = new DaoMailContext();
        int code = daoMail.deleteMailContext(formParams.getFirst("AsociatedWorflowId"));
        if (code == NOT_FOUND_IN_DATABASE) {
            return Response.status(NOT_FOUND_IN_DATABASE).entity(MONGO_NOTFOUND_ID).build();
        } else if (code == DATA_SUCCESSFULLY_DELETED) {
            return Response.status(DATA_SUCCESSFULLY_DELETED).entity(MONGO_SUCCESS_MSG).build();
        } else {
            return Response.status(ERROR_DELETING_DATA).entity(MONGO_ERROR_MSG).build();
        }
    }

    @POST
    @Path("/storeTwitter")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("text/plain")
    public Response storeTwitter(UserTwitterTokens tokens) {
        DaoUserTwitterTokens daoTwitter = new DaoUserTwitterTokens();
        if (daoTwitter.storeTwitterTokens(tokens) == DATA_SUCCESSFULLY_STORED) {
            return Response.status(Response.Status.OK).entity(MONGO_SUCCESS_MSG).build();
        }
        return Response.status(ERROR_STORING_DATA).entity(MONGO_ERROR_MSG).build();
    }

    @POST
    @Path("/getTwitter")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public Response getTwitter(MultivaluedMap<String, String> formParams) {

        DaoUserTwitterTokens daoTwitter = new DaoUserTwitterTokens();
        UserTwitterTokens tokens = null;
        tokens = daoTwitter.getTwitterTokens(formParams.getFirst("AsociatedWorflowId"));

        if (tokens != null) {
            return Response.status(Response.Status.OK).entity(tokens).build();
        }

        return Response.status(NOT_FOUND_IN_DATABASE).entity(MONGO_NOTFOUND_ID).build();

    }

    @POST
    @Path("/deleteTwitter")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public Response deleteTwitter(MultivaluedMap<String, String> formParams) {
        DaoUserTwitterTokens daoTwitter = new DaoUserTwitterTokens();
        int code = daoTwitter.deleteTwitterTokens(formParams.getFirst("AsociatedWorflowId"));
        if (code == NOT_FOUND_IN_DATABASE) {
            return Response.status(NOT_FOUND_IN_DATABASE).entity(MONGO_NOTFOUND_ID).build();
        } else if (code == DATA_SUCCESSFULLY_DELETED) {
            return Response.status(DATA_SUCCESSFULLY_DELETED).entity(MONGO_SUCCESS_MSG).build();
        } else {
            return Response.status(ERROR_DELETING_DATA).entity(MONGO_ERROR_MSG).build();
        }
    }

    @POST
    @Path("/getAlarm")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)

    public Response getAlarm(MultivaluedMap<String, String> formParams) {
        ArrayList<AlarmContainer> Container = new ArrayList<AlarmContainer>();
        DaoAlarms al = new DaoAlarms();
        Container = al.getAlarmsbyPrioritiy(formParams.getFirst("priority"), formParams.getFirst("time"));
        return Response.status(Response.Status.OK).entity(Container).build();
    }

    @POST
    @Path("/setLog")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)

    public Response setLog(MultivaluedMap<String, String> formParams) {
        DaoAlarms al = new DaoAlarms();
        al.GenerateLog(formParams.getFirst("alarmId"));
        return Response.status(Response.Status.OK).build();

    }

    @POST
    @Path("/getUserInfo")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)

    public Response getUserInfo(MultivaluedMap<String, String> formParams) {
        DaoUser du = new DaoUser();
        UserData data = du.retrieveUserData(formParams.getFirst("userId"));
        return Response.status(Response.Status.OK).entity(data).build();

    }

    @POST
    @Path("/getWorkflowInfo")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)

    public Response getWorkflowInfo(MultivaluedMap<String, String> formParams) {
        DaoWorkflow dw = new DaoWorkflow();
        WorkFlowInfo data = dw.getWorkflowInfo((formParams.getFirst("workflowId")));
        return Response.status(Response.Status.OK).entity(data).build();
    }

}