guestbook.Entity.java Source code

Java tutorial

Introduction

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

import java.util.Set;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 *
 * @author ify
 */
public class Entity {
    private String id, label;
    private final JSONObject jobj;
    private JSONObject statements;

    public Entity(String q, JSONObject obj) {
        id = q;
        jobj = obj;
        setLabel();
    }

    protected String getId() {
        return id;
    }

    protected String getLabel() {
        return label;
    }

    protected String getItem(String property) {

        return null;
    }

    protected JSONObject getStatements() {
        return statements;
    }

    protected Object retrieveValue(String p, String type) {
        Set s = statements.keySet();
        if (s.contains(p)) {
            JSONArray statement = (JSONArray) statements.get(p);
            JSONObject x = (JSONObject) statement.get(0);
            JSONObject mainsnak = (JSONObject) x.get("mainsnak");
            JSONObject datavalue = (JSONObject) mainsnak.get("datavalue");
            JSONObject value = (JSONObject) datavalue.get("value");
            Object object = value.get(type);
            return object;
        }
        return null;
    }

    protected Object retrieveValue(String p) {
        return retrieveValue(p, "numeric-id");
    }

    public String getValue(String p) {
        if (statements == null)
            return null;
        Set s = statements.keySet();
        if (s.contains(p)) {
            String str = "";
            JSONArray statement = (JSONArray) statements.get(p);
            for (int i = 0; i < statement.size(); i++) {
                JSONObject x = (JSONObject) statement.get(i);
                JSONObject mainsnak = (JSONObject) x.get("mainsnak");
                Set g = mainsnak.keySet();
                if (!g.contains("datatype")) {
                    System.out.println(getId() + " :No datatype");
                    return null;
                }
                String snaktype = (String) mainsnak.get("snaktype");
                if (snaktype.equals("somevalue"))
                    return "Unknown value" + "001";
                String type = (String) mainsnak.get("datatype");
                JSONObject datavalue = (JSONObject) mainsnak.get("datavalue");
                switch (type) {
                case "wikibase-item":
                    JSONObject value = (JSONObject) datavalue.get("value");
                    str = str + (Long) value.get("numeric-id") + "xxx";
                    break;
                case "string":
                    str = str + (String) datavalue.get("value") + "001";
                    break;
                case "quantity":
                    JSONObject values = (JSONObject) datavalue.get("value");
                    String ss = (String) values.get("amount");
                    str = str + ss.substring(1) + "001";
                    break;
                case "commonsMedia":
                    str = str + (String) datavalue.get("value") + "001";
                    break;
                case "globe-coordinate":
                    final String d = "\u00b0";
                    JSONObject value2 = (JSONObject) datavalue.get("value");
                    String lat = value2.get("latitude").toString();
                    String lon = value2.get("longitude").toString();
                    str = str + lat + d + "N " + lon + d + "E " + "001";
                    break;
                case "time":
                    JSONObject value3 = (JSONObject) datavalue.get("value");
                    //GregorianCalendar gc = (GregorianCalendar) value3.get("time");
                    String gc = (String) value3.get("time");
                    //String gcs = gc.c
                    str = str + gc + "001";
                    break;
                case "url":
                    str = str + (String) datavalue.get("value") + "001";
                    break;
                default:
                    break;
                }
            }
            if (str.length() > 0) {
                return str.trim();
            }
        }
        return null;
    }

    private void setLabel() {
        Set s = jobj.keySet();
        if (s.contains("labels")) {
            JSONObject labelobj;
            labelobj = (JSONObject) jobj.get("labels");

            JSONObject en = (JSONObject) labelobj.get("en");
            String value = (String) en.get("value");
            label = value;
            statements = (JSONObject) jobj.get("claims");
        } else {
            label = "Label Missing";
            statements = (JSONObject) jobj.get("claims");
        }
    }
}