functionnality.LikerLineST2.java Source code

Java tutorial

Introduction

Here is the source code for functionnality.LikerLineST2.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 functionnality;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import javax.swing.JOptionPane;
import model.LineStatus;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import webrequester.CouchDBWebRequest;
import webrequester.TisseoWebRequest;
import webrequester.XMLParser;

/**
 *
 * @author loic
 */
public class LikerLineST2 {
    private LineStatus lineS;
    private String message;

    public LikerLineST2(LineStatus _lS) {
        lineS = _lS;
        message = "";
    }

    public LineStatus rateLigne(String login, String value) {
        CouchDBWebRequest couchDB = new CouchDBWebRequest();
        message = "Consultation";
        JSONObject response;
        couchDB.addPath("/id_ligne_" + lineS.getIdentifiant());
        response = couchDB.parseResultJSON(couchDB.requestWithGet());
        //Requete des valeurs d'une ligne

        if (response.get("error") != null) {
            createTable(lineS.getIdentifiant());
            response = couchDB.parseResultJSON(couchDB.requestWithGet());
        }

        if (response.get("Voted") == null) {
            response.put("Voted", new JSONArray());
        }

        //((JSONObject)((JSONArray)response.get("Voted")).get(0)).get("login")
        int i = 0;
        boolean trouver = false;
        while (i < ((JSONArray) response.get("Voted")).size() && !trouver) {
            if (((JSONObject) ((JSONArray) response.get("Voted")).get(i)).get("login").equals(login)) {
                trouver = true;
            }
            i++;
        }
        if (trouver) {
            message = "You have already voted";
        } else {
            try {
                JSONObject jsonObj = new JSONObject();
                jsonObj.put("login", new String(login.getBytes("ISO-8859-1"), Charset.forName("UTF-8")));
                jsonObj.put("value", value);
                if (value.equals("like")) {
                    ((JSONObject) response).put("like", (long) response.get("like") + 1);
                    message = "You have voted like with " + login;
                } else {
                    ((JSONObject) response).put("unlike", (long) response.get("unlike") + 1);
                    message = "You have voted unlike with " + login;
                }
                ((JSONArray) response.get("Voted")).add(jsonObj);
                couchDB.requestWithPut(response.toString());

            } catch (UnsupportedEncodingException ex) {
                return null;
            }
        }
        lineS.setLike(Integer.parseInt(("" + response.get("like"))));
        lineS.setUnlike(Integer.parseInt(("" + response.get("unlike"))));
        return lineS;
    }

    public boolean createTable(String id) {
        CouchDBWebRequest couchDB = new CouchDBWebRequest();
        Document d;
        //Crer le doc apres avoir vrifier qu'il existe bien dans Tisseo
        TisseoWebRequest tisseoR = new TisseoWebRequest("/linesList");
        tisseoR.addParameterGet("lineId", id);

        d = tisseoR.parseResultXML(tisseoR.requestWithGet());
        String shortName = "", name = "", type = "";

        couchDB.addPath("/id_ligne_" + id);
        if (d.getChildNodes().item(0).getChildNodes().getLength() < 4) {
            try {
                Node n = d.getChildNodes().item(0).getChildNodes().item(1);
                shortName = n.getAttributes().getNamedItem("shortName").getNodeValue();
                name = n.getAttributes().getNamedItem("name").getNodeValue();
                type = n.getChildNodes().item(1).getAttributes().getNamedItem("name").getNodeValue();
                JSONObject tableCreate = new JSONObject();
                tableCreate.put("ShortName",
                        new String(shortName.getBytes("ISO-8859-1"), Charset.forName("UTF-8")));
                tableCreate.put("Name", new String(name.getBytes("ISO-8859-1"), Charset.forName("UTF-8")));
                tableCreate.put("Type", new String(type.getBytes("ISO-8859-1"), Charset.forName("UTF-8")));
                tableCreate.put("like", 0);
                tableCreate.put("unlike", 0);
                tableCreate.put("Voted", new JSONArray());

                couchDB.requestWithPut(new String(tableCreate.toString().getBytes("UTF-8"), "UTF-8"));
            } catch (UnsupportedEncodingException ex) {
                return false;
            }

        } else {
            return false;
        }
        return true;
    }

    public LineStatus request() {
        CouchDBWebRequest couchDB = new CouchDBWebRequest();
        message = "Consultation";
        JSONObject response;
        couchDB.addPath("/id_ligne_" + lineS.getIdentifiant());
        response = couchDB.parseResultJSON(couchDB.requestWithGet());
        if (response.get("error") != null) {
            createTable(lineS.getIdentifiant());
            response = couchDB.parseResultJSON(couchDB.requestWithGet());
        }

        lineS.setLike(Integer.parseInt(("" + response.get("like"))));
        lineS.setUnlike(Integer.parseInt(("" + response.get("unlike"))));
        return lineS;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}