model.SearchResponse.java Source code

Java tutorial

Introduction

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

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 *
 * @author roman
 */
public class SearchResponse extends BaseModel {

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public String getBuilt_query() {
        return built_query;
    }

    public void setBuilt_query(String built_query) {
        this.built_query = built_query;
    }

    public Index getUsed_indices() {
        return used_indices;
    }

    public void setUsed_indices(Index used_indices) {
        this.used_indices = used_indices;
    }

    public List<Message> getMessages() {
        return messages;
    }

    public void setMessages(List<Message> messages) {
        this.messages = messages;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getTotal_results() {
        return total_results;
    }

    public void setTotal_results(String total_results) {
        this.total_results = total_results;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    @Override
    public void load(JSONObject object) {
        Iterator it = object.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> pair = (Map.Entry) it.next();
            String key = pair.getKey();
            Object value = pair.getValue();

            if (value == null) {
                continue;
            }

            switch (key) {
            case "used_indices":
                this.used_indices = new Index();
                JSONArray usedIndices = (JSONArray) value;
                if (usedIndices.size() == 1) {
                    this.used_indices.load((JSONObject) usedIndices.get(0));
                }
                break;
            case "messages":
                this.loadMessage((JSONArray) value);
                break;
            default:
                this.setAttribute(key, value.toString());
            }
        }
        System.out.println(this);
    }

    public void loadMessage(JSONArray messages) {
        Iterator it = messages.iterator();
        this.messages = new ArrayList<>();
        while (it.hasNext()) {
            JSONObject object = (JSONObject) it.next();
            Message message = new Message();
            message.load(object);
            this.messages.add(message);
        }
    }

    public String query;
    public String built_query;
    public Index used_indices;
    public List<Message> messages;
    public String time;
    public String total_results;
    public String from;
    public String to;
}