cz.mgn.mediservice.rest.Loader.java Source code

Java tutorial

Introduction

Here is the source code for cz.mgn.mediservice.rest.Loader.java

Source

/*
 * The MIT License
 *
 * Copyright 2014 indy.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package cz.mgn.mediservice.rest;

import cz.mgn.mediservice.model.Drug;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

/**
 *
 * @author indy
 */
public class Loader {

    protected static final String SERVER_URL = "http://localhost/school/mediservice/api.php";

    public static ArrayList<Drug> loadDrugs(int pharmacyId) {
        ArrayList<Drug> drugs = new ArrayList<Drug>();

        JSONArray json = (JSONArray) loadJson();
        for (Object item : json) {
            JSONObject drugJson = (JSONObject) item;

            int id = Integer.parseInt((String) drugJson.get("id"));
            String name = (String) drugJson.get("name");
            String description = (String) drugJson.get("description");
            float price = Float.parseFloat((String) drugJson.get("price"));

            Drug drug = new Drug(id, name, description, price);
            drugs.add(drug);
        }

        return drugs;
    }

    protected static Object loadJson() {
        String request = createRequestUrl();
        String json = load(request);
        return parse(json);
    }

    protected static String createRequestUrl() {
        //TODO
        return "request=instock&q={\"token\":%20\"d1ap8r2gtpm6ag0121elrzzj97uqy2aekat82k0gyya09y5msi\",%20\"pharmacy\":%201}";
    }

    protected static String load(String request) {
        String result = null;

        try {
            URL url = new URL(SERVER_URL + "?" + request);

            URLConnection con = url.openConnection();
            result = IOUtils.toString(con.getInputStream(), "UTF-8");
        } catch (IOException ex) {
            Logger.getLogger(Loader.class.getName()).log(Level.SEVERE, null, ex);
        }

        return result;
    }

    protected static Object parse(String source) {
        return JSONValue.parse(source);
    }
}