OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.ui.light;

import org.openconcerto.utils.io.JSONConverter;
import org.openconcerto.utils.io.Transferable;

import java.util.ArrayList;
import java.util.List;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONAware;
import net.minidev.json.JSONObject;

public class UserSearch implements Transferable, JSONAware {
    private String tableId;
    private List<UserSearchItem> content = new ArrayList<>();

    public UserSearch() {
        // Serialization
    }

    public UserSearch(final String tableId) {
        this.tableId = tableId;
    }

    public UserSearch(final JSONObject json) {
        this.fromJSON(json);
    }

    public final String getTableId() {
        return this.tableId;
    }

    public final List<UserSearchItem> getContent() {
        return this.content;
    }

    @Override
    public JSONObject toJSON() {
        final JSONObject json = new JSONObject();
        json.put("table-id", this.tableId);
        json.put("content", JSONConverter.getJSON(this.content));
        return json;
    }

    @Override
    public void fromJSON(JSONObject json) {
        this.tableId = JSONConverter.getParameterFromJSON(json, "table-id", String.class);
        final JSONArray jsonContent = JSONConverter.getParameterFromJSON(json, "content", JSONArray.class);
        if (jsonContent != null) {
            this.content = new ArrayList<>();
            for (final Object o : jsonContent) {
                this.content.add(new UserSearchItem((JSONObject) JSONConverter.getObjectFromJSON(o, JSONObject.class)));
            }
        }
    }

    /*
     * JSONAware pour que le JSONArray.toString() fonctionne quand cet objet est dans un JSONArray
     */
    @Override
    public String toJSONString() {
        return toJSON().toJSONString();
    }

    @Override
    public String toString() {
        return "UserSearch on " + this.tableId + " " + this.content.size() + " items";
    }

}