com.open.shift.support.controller.SupportResource.java Source code

Java tutorial

Introduction

Here is the source code for com.open.shift.support.controller.SupportResource.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.open.shift.support.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.open.shift.support.dao.ISupportDAO;
import com.open.shift.support.dataObjects.Client;
import com.open.shift.support.dataObjects.LogDetails;
import com.open.shift.support.dataObjects.User;
import java.io.File;
import java.io.FileReader;
import java.math.BigDecimal;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonReader;
import javax.json.JsonValue;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.apache.commons.codec.digest.DigestUtils;

/**
 * REST Web Service
 *
 * @author Family
 */
@Path("support")
@ApplicationScoped
public class SupportResource {

    @Context
    private UriInfo context;

    @Inject
    private ISupportDAO supportDAO;

    @Context
    private HttpServletRequest request;

    @Context
    private ServletContext ctx;

    private static String OS = System.getProperty("os.name").toLowerCase();

    /**
     * Creates a new instance of SupportResource
     */
    public SupportResource() {

        System.out.println("The " + this);

    }

    @POST
    @Path("auth")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void authenticateUser(@FormParam("user") String user, @FormParam("pass") String pass) throws Exception {
        String hexPass = DigestUtils.sha256Hex(pass);
        String r = supportDAO.authenticateUser(user, hexPass);
        if (r.equalsIgnoreCase("fail")) {
            request.setAttribute("isAuthenticated", false);
        } else {
            request.setAttribute("isAuthenticated", true);
        }

    }

    @POST
    @Produces(MediaType.TEXT_PLAIN)
    public String addUsers(@FormParam("user") String user, @FormParam("pass") String pass) {
        try {
            String hexPass = DigestUtils.sha256Hex(pass);
            supportDAO.addUsers(user, hexPass);
            return "Done";
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Inside supp ex");
            return "Fail";
        }

    }

    @GET
    @Path("client")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Client> getClientDetails() throws Exception {
        return supportDAO.getClientDetails();
    }

    @GET
    @Path("user")
    @Produces(MediaType.APPLICATION_JSON)
    public List<User> getUserDetails(@QueryParam("clientId") String clientId) throws Exception {
        return supportDAO.getUserDetails(clientId);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("logs")
    public List<LogDetails> getLogDetails(@QueryParam("userId") String userId) throws Exception {
        return supportDAO.getLogDetails(userId);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("areachart")
    public JsonObject generateAreaChart() throws Exception {
        System.out.println("The injected hashcode is " + this.supportDAO);
        int usa[] = { 0, 0, 0, 0, 0, 6, 11, 32, 110, 235, 369, 640, 1005, 1436, 2063, 3057, 4618, 6444, 9822, 15468,
                20434, 24126, 27387, 29459, 31056, 31982, 32040, 31233, 29224, 27342, 26662, 26956, 27912, 28999,
                28965, 27826, 25579, 25722, 24826, 24605, 24304, 23464, 23708, 24099, 24357, 24237, 24401, 24344,
                23586, 22380, 21004, 17287, 14747, 13076, 12555, 12144, 11009, 10950, 10871, 10824, 10577, 10527,
                10475, 10421, 10358, 10295, 10104 };

        int ussr[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 25, 50, 120, 150, 200, 426, 660, 869, 1060, 1605, 2471,
                3322, 4238, 5221, 6129, 7089, 8339, 9399, 10538, 11643, 13092, 14478, 15915, 17385, 19055, 21205,
                23044, 25393, 27935, 30062, 32049, 33952, 35804, 37431, 39197, 45000, 43000, 41000, 39000, 37000,
                35000, 33000, 31000, 29000, 27000, 25000, 24000, 23000, 22000, 21000, 20000, 19000, 18000, 18000,
                17000, 16000 };

        URL u = ctx.getResource("/WEB-INF/classes/templates/area.json");
        String areaJson = null;
        if (OS.indexOf("win") >= 0) {
            areaJson = u.toString().substring(6);
        } else if (OS.indexOf("nux") >= 0) {
            areaJson = u.toString().substring(5);
        }
        System.out.println("The areaJson is " + areaJson);

        JsonReader reader = Json.createReader(new FileReader(areaJson));
        JsonObject main = reader.readObject();

        JsonObject yaxis = main.getJsonObject("yAxis");

        JsonObjectBuilder title = Json.createObjectBuilder().add("text", "US and USSR Deposits");
        JsonObjectBuilder ytitle = Json.createObjectBuilder().add("text", "Amount Deposisted");
        JsonArrayBuilder usaData = Json.createArrayBuilder();
        for (Integer usa1 : usa) {
            usaData.add(usa1);
        }

        JsonArrayBuilder ussrData = Json.createArrayBuilder();
        for (Integer uss1 : ussr) {
            ussrData.add(uss1);
        }

        JsonObjectBuilder usaSeries = Json.createObjectBuilder().add("name", "USA").add("data", usaData);
        JsonObjectBuilder ussrSeries = Json.createObjectBuilder().add("name", "USSR").add("data", ussrData);
        JsonArrayBuilder series = Json.createArrayBuilder().add(usaSeries).add(ussrSeries);

        //main json object builder
        JsonObjectBuilder mainJsonObj = Json.createObjectBuilder();
        for (Map.Entry<String, JsonValue> mainObj : main.entrySet()) {
            String key = mainObj.getKey();
            JsonValue value = mainObj.getValue();

            if (key.equalsIgnoreCase("yAxis")) {
                String yaxisLabel = "labels";
                mainJsonObj.add(key, Json.createObjectBuilder().add(yaxisLabel, yaxis.getJsonObject(yaxisLabel))
                        .add("title", ytitle.build()));
            } else {
                mainJsonObj.add(key, value);
            }

        }
        mainJsonObj.add("title", title.build()).add("series", series.build());

        return mainJsonObj.build();

    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("piechart")
    public JsonObject generatePieChart() throws Exception {
        URL u = ctx.getResource("/WEB-INF/classes/templates/pie.json");
        String pieJson = null;
        if (OS.indexOf("win") >= 0) {
            pieJson = u.toString().substring(6);
        } else if (OS.indexOf("nux") >= 0) {
            pieJson = u.toString().substring(5);
        }

        JsonReader reader = Json.createReader(new FileReader(pieJson));
        JsonObject main = reader.readObject();
        JsonObjectBuilder title = Json.createObjectBuilder().add("text", "Companies Contribution in Deposits");
        JsonArrayBuilder data = Json.createArrayBuilder().add(Json.createArrayBuilder().add("Infosys").add(45.0))
                .add(Json.createArrayBuilder().add("TCS").add(26.8))
                .add(Json.createArrayBuilder().add("Oracle").add(8.5))
                .add(Json.createArrayBuilder().add("Accenture").add(6.2)).add(Json.createObjectBuilder()
                        .add("name", "IBM").add("y", 12.8).add("sliced", true).add("selected", true));
        JsonArrayBuilder series = Json.createArrayBuilder().add(
                Json.createObjectBuilder().add("type", "pie").add("name", "Deposit Contributed").add("data", data));

        JsonObjectBuilder mainJsonObj = Json.createObjectBuilder();
        for (Map.Entry<String, JsonValue> entrySet : main.entrySet()) {
            String key = entrySet.getKey();
            JsonValue value = entrySet.getValue();
            mainJsonObj.add(key, value);
        }
        mainJsonObj.add("title", title.build()).add("series", series.build());

        return mainJsonObj.build();
    }

}