Example usage for org.json JSONArray getJSONObject

List of usage examples for org.json JSONArray getJSONObject

Introduction

In this page you can find the example usage for org.json JSONArray getJSONObject.

Prototype

public JSONObject getJSONObject(int index) throws JSONException 

Source Link

Document

Get the JSONObject associated with an index.

Usage

From source file:com.eventattend.portal.bl.FaceBookBL.java

public List getFriendsList(JSONArray jsonArray) {

    List friendsList = null;//w  w  w. ja v  a2s  . c  o m
    JSONObject jsonObject = null;
    ProfileDTO profileDTO = null;

    friendsList = new ArrayList();

    for (int i = 0; i < jsonArray.length(); i++) {

        try {
            jsonObject = jsonArray.getJSONObject(i);
            profileDTO = getUserData(jsonObject);
            friendsList.add(profileDTO);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    return friendsList;

}

From source file:GUI.FormUsuarios.java

/**
 * Creates new form FormUsuarios/*from   www .ja v  a 2 s .  com*/
 */
public FormUsuarios() {
    initComponents();

    HttpResponse response;
    response = JSON.request(Config.URL + "usuarios/listartipos.json");
    JSONObject jObject = JSON.JSON(response);
    try {
        System.out.println(jObject.get("code"));
        JSONArray jsonArr = jObject.getJSONArray("data");

        for (int i = 0; i < jsonArr.length(); i++) {
            JSONObject data = jsonArr.getJSONObject(i);
            cb_tipo.addItem(new listaTipo(data.get("idTipo").toString(), data.get("descripcion").toString()));
            ValoresCombobox.put(data.get("descripcion").toString(),
                    Integer.parseInt(data.get("idTipo").toString()));
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    initializeComponent();
}

From source file:net.homelinux.penecoptero.android.citybikes.app.AccountHelper.java

/**
 * @return Returns a List of cities. Each city is described in a Map of Strings.
 * Each Map contains a "code" value for the city code and a "name" value containing
 * the name of the city.//from  www  . ja  v a 2 s .  c om
 */
public List<Map<String, String>> getCities() {
    try {
        String citiesString = getUrl(SERVER_URL + "/contracts/full");
        JSONArray cities = new JSONArray(citiesString);
        ArrayList<Map<String, String>> citiesList = new ArrayList<Map<String, String>>();
        for (int i = 0; i < cities.length(); i++) {
            JSONObject city = cities.getJSONObject(i);
            Map<String, String> cityMap = new HashMap<String, String>(2);
            cityMap.put("code", city.getString("code"));
            cityMap.put("name", city.getString("name"));
            citiesList.add(cityMap);
        }
        return citiesList;
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return new ArrayList<Map<String, String>>();
    }
}

From source file:com.flurry.samples.tumblrsharing.Photo.java

/**
 * Deserializes JSON into Photo objects/*from   w  w w  .  ja v a 2s. co m*/
 *
 * @param photoArray     JSONArray for photo list
 */
public static List<Photo> fromJson(JSONArray photoArray) {
    List<Photo> photoList = new ArrayList<Photo>();
    JSONObject photoObject = null;
    try {
        // Deserialize json into object fields
        for (int i = 0; i < photoArray.length(); i++) {
            Photo p = new Photo();
            photoObject = photoArray.getJSONObject(i);

            p.owner = photoObject.getString("owner");
            p.photoId = photoObject.getString("id");
            p.secret = photoObject.getString("secret");
            p.server = photoObject.getInt("server");
            p.farm = photoObject.getInt("farm");
            p.title = photoObject.getString("title");
            p.photoUrl = "https://farm" + p.farm + ".staticflickr.com/" + p.server + "/" + p.photoId + "_"
                    + p.secret + "_c.jpg";
            p.photoUrl = p.photoUrl.replace(" ", "");

            photoList.add(p);
        }

    } catch (JSONException e) {
        AnalyticsHelper.logError(LOG_TAG, "Deserialize Photo Feed JSONArray Error.", e);
        return null;
    }
    // Return new object
    return photoList;
}

From source file:bhaskarandroidnannodegree.popularmoviesstage1.asynctasks.FetchTrailReview.java

/**
 * Take the String representing the complete movie list in JSON Format and
 * pull out the data we need to construct the Strings needed for the wireframes.
 * <p>/*from  w ww.j a v a 2  s .  c  om*/
 * Fortunately parsing is easy:  constructor takes the JSON string and converts it
 * into an Object hierarchy for us.
 */
private void getMovieDataFromJson(String movieJsonStr) throws JSONException {

    // These are the names of the JSON objects that need to be extracted.
    final String NAME = "name";
    final String SIZE = "size";
    final String SOURCE = "source";
    final String TYPE = "type";
    final String REVIEW_ID = "id";
    final String GENRE_ID = "id";
    final String AUTHOR = "author";
    final String CONTENT = "content";
    final String URL = "url";
    final String TOTAL_PAGES_REVIEWS = "total_pages";
    final String TOTAL_RESULTS_REVIEWS = "total_results";
    final String PAGE = "page";
    final String MOVIE_ID = "id";

    final String RESULT1 = "trailers";
    final String RESULT2 = "reviews";
    final String RESULT3 = "genres";
    final String YOUTUBE = "youtube";

    try {
        JSONObject movieJson = new JSONObject(movieJsonStr);
        JSONArray movieArrayTrailer = movieJson.getJSONObject(RESULT1).getJSONArray(YOUTUBE);
        JSONObject movieArrayReviews = movieJson.getJSONObject(RESULT2);
        JSONArray movieArrayGenres = movieJson.getJSONArray(RESULT3);

        String movie_id = movieJson.getString(MOVIE_ID);

        for (int i = 0; i < movieArrayTrailer.length(); i++) {

            JSONObject trailerInfo = movieArrayTrailer.getJSONObject(i);

            String name;
            String size;
            String source;
            String type;
            name = trailerInfo.getString(NAME);
            size = trailerInfo.getString(SIZE);
            source = trailerInfo.getString(SOURCE);
            type = trailerInfo.getString(TYPE);

            TrailerInfo trailer = new TrailerInfo();
            trailer.setName(name);
            trailer.setSize(size);
            trailer.setSource(source);
            trailer.setType(type);

            listTrailers.add(trailer);

        }

        String page = movieArrayReviews.getString(PAGE);
        String total_page = movieArrayReviews.getString(TOTAL_PAGES_REVIEWS);
        String total_results = movieArrayReviews.getString(TOTAL_RESULTS_REVIEWS);

        JSONArray reviews = movieArrayReviews.getJSONArray("results");

        for (int j = 0; j < reviews.length(); j++) {

            JSONObject reviewsInfo = reviews.getJSONObject(j);

            String id;
            String author;
            String content;
            String url;
            id = reviewsInfo.getString(REVIEW_ID);
            author = reviewsInfo.getString(AUTHOR);
            content = reviewsInfo.getString(CONTENT);
            url = reviewsInfo.getString(URL);

            MovieReview review = new MovieReview();
            review.setId(id);
            review.setAuthor(author);
            review.setContent(content);
            review.setUrl(url);

            listMovieReviews.add(review);
        }

        for (int i = 0; i < movieArrayGenres.length(); i++) {

            JSONObject gen = movieArrayGenres.getJSONObject(i);
            String genre_id = gen.getString(GENRE_ID);
            String name = gen.getString(NAME);

        }

    } catch (JSONException e) {
        //Log.e(LOG_TAG, e.getMessage(), e);
        e.printStackTrace();
    }
}

From source file:org.eclipse.orion.server.cf.handlers.v1.AppsHandlerV1.java

private IStatus getApps(Target target) throws Exception {
    String appsUrl = target.getSpace().getCFJSON().getJSONObject("entity").getString("apps_url");
    //      appsUrl = appsUrl.replaceAll("apps", "summary");
    URI appsURI = URIUtil.toURI(target.getUrl()).resolve(appsUrl);

    GetMethod getAppsMethod = new GetMethod(appsURI.toString());
    HttpUtil.configureHttpMethod(getAppsMethod, target);
    getAppsMethod.setQueryString("inline-relations-depth=2"); //$NON-NLS-1$

    ServerStatus getAppsStatus = HttpUtil.executeMethod(getAppsMethod);
    if (!getAppsStatus.isOK())
        return getAppsStatus;

    /* extract available apps */
    JSONObject appsJSON = getAppsStatus.getJsonData();

    if (appsJSON.getInt(CFProtocolConstants.V2_KEY_TOTAL_RESULTS) < 1) {
        return new ServerStatus(IStatus.OK, HttpServletResponse.SC_OK, null, null);
    }// w  ww.j a va2 s . co  m

    JSONObject result = new JSONObject();
    JSONArray resources = appsJSON.getJSONArray(CFProtocolConstants.V2_KEY_RESOURCES);
    for (int k = 0; k < resources.length(); ++k) {
        JSONObject appJSON = resources.getJSONObject(k);
        App2 app = new App2();
        app.setCFJSON(appJSON);
        result.append("Apps", app.toJSON());
    }

    return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result);
}

From source file:com.parking.billing.Security.java

/**
 * Verifies that the data was signed with the given signature, and returns
 * the list of verified purchases. The data is in JSON format and contains
 * a nonce (number used once) that we generated and that was signed
 * (as part of the whole data string) with a private key. The data also
 * contains the {@link PurchaseState} and product ID of the purchase.
 * In the general case, there can be an array of purchase transactions
 * because there may be delays in processing the purchase on the backend
 * and then several purchases can be batched together.
 * @param signedData the signed JSON string (signed, not encrypted)
 * @param signature the signature for the data, signed with the private key
 *///from  ww  w.j av a  2  s . c o  m
public static ArrayList<VerifiedPurchase> verifyPurchase(String signedData, String signature) {
    if (signedData == null) {
        Log.e(TAG, "data is null");
        return null;
    }
    if (BillingConstants.DEBUG) {
        Log.i(TAG, "signedData: " + signedData);
    }

    boolean verified = false;

    if (!TextUtils.isEmpty(signature)) {
        /**
         * Compute your public key (that you got from the Android Market publisher site).
         *
         * Instead of just storing the entire literal string here embedded in the
         * program,  construct the key at runtime from pieces or
         * use bit manipulation (for example, XOR with some other string) to hide
         * the actual key.  The key itself is not secret information, but we don't
         * want to make it easy for an adversary to replace the public key with one
         * of their own and then fake messages from the server.
         *
         * Generally, encryption keys / passwords should only be kept in memory
         * long enough to perform the operation they need to perform.
         */
        //Chintan's Key
        String base64EncodedPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuRIrk/6nAPzZo5HKe261/ZMfoe63mtY5QUlc0A0/77RTicrS9Nk1VjtVniRpHmjasQOGsQrBpBGJUYp0ixsjJpgfjLv7OvpF8Hp/ucth2T/Bm7kl/odRDT3urAp3snvqZEzfOg1wtDU7DAnDW1zNSqVNCVczXRnNrEmGxEjamKkTTQwz37ui7AhjKXCXAJY4n5ANj1oymnjGN5FHfzcMb07wR/ucz39ZX+Raf6qBsbnYkmuDH6pJ/4ZI9+vjbgWzXCx07DefQW4dtNMQZlVlKgKnJUkafePUYJVIO4sRgeWL1e5b8dbIYMO7gB9oopyfVhZifi+pDGr5+YAxi6D3PwIDAQAB";
        //Mandar's Key:
        //String base64EncodedPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAj7zgTswMi1ePyAK7rCnCOkpmviHZzoSn2cxtyQ5ZQRFifNGkKq3Gli3VbeIeeJR8GHzlfOPUSgtMrd17WtGJoo29rsw6UuXov+imQutGKZglcM/cIrlIdZCsse3dGYyDcKFhEHrC/nPdwlYxgIGBaZKAcbbhitkdgYaVHQvGFTagCytxDq9NDJAY7exSKKm2HimfjlcBZjhHeImZ+cRCPux+9uoBQ4mTRYjrXfcpi/OPKTKsq2AHXf/y60qsZJlgGl3tBgRQo6lOEr7UbbHKESTKvOQ4t3J1wjNz8Z3+T0PZHb5JkeTsdAE7cG7jmz2HmMxfdXcT5mBTTDei6DPDGwIDAQAB";
        PublicKey key = Security.generatePublicKey(base64EncodedPublicKey);
        verified = Security.verify(key, signedData, signature);
        if (!verified) {
            Log.w(TAG, "signature does not match data.");
            return null;
        }
    }

    JSONObject jObject;
    JSONArray jTransactionsArray = null;
    int numTransactions = 0;
    long nonce = 0L;
    try {
        jObject = new JSONObject(signedData);

        // The nonce might be null if the user backed out of the buy page.
        nonce = jObject.optLong("nonce");
        jTransactionsArray = jObject.optJSONArray("orders");
        if (jTransactionsArray != null) {
            numTransactions = jTransactionsArray.length();
        }
    } catch (JSONException e) {
        return null;
    }

    if (!Security.isNonceKnown(nonce)) {
        Log.w(TAG, "Nonce not found: " + nonce);
        return null;
    }

    ArrayList<VerifiedPurchase> purchases = new ArrayList<VerifiedPurchase>();
    try {
        for (int i = 0; i < numTransactions; i++) {
            JSONObject jElement = jTransactionsArray.getJSONObject(i);
            int response = jElement.getInt("purchaseState");
            PurchaseState purchaseState = PurchaseState.valueOf(response);
            String productId = jElement.getString("productId");
            String packageName = jElement.getString("packageName");
            long purchaseTime = jElement.getLong("purchaseTime");
            String orderId = jElement.optString("orderId", "");
            String notifyId = null;
            if (jElement.has("notificationId")) {
                notifyId = jElement.getString("notificationId");
            }
            String developerPayload = jElement.optString("developerPayload", null);

            // If the purchase state is PURCHASED, then we require a
            // verified nonce.

            /**
             *   mandarm - We are ok with no signature for our test code! 
             */
            //TODO - Take care for signed purchases.

            if (purchaseState == PurchaseState.PURCHASED && !verified) {
                continue;
            }

            purchases.add(new VerifiedPurchase(purchaseState, notifyId, productId, orderId, purchaseTime,
                    developerPayload));
        }
    } catch (JSONException e) {
        Log.e(TAG, "JSON exception: ", e);
        return null;
    }
    removeNonce(nonce);
    return purchases;
}

From source file:org.apache.giraph.graph.BspServiceWorker.java

/**
 * Get values of aggregators aggregated by master in previous superstep.
 *
 * @param superstep Superstep to get the aggregated values from
 *//*from ww  w. j a va  2 s .com*/
private void getAggregatorValues(long superstep) {
    String mergedAggregatorPath = getMergedAggregatorPath(getApplicationAttempt(), superstep - 1);
    JSONArray aggregatorArray = null;
    try {
        byte[] zkData = getZkExt().getData(mergedAggregatorPath, false, null);
        aggregatorArray = new JSONArray(new String(zkData));
    } catch (KeeperException.NoNodeException e) {
        LOG.info("getAggregatorValues: no aggregators in " + mergedAggregatorPath + " on superstep "
                + superstep);
        return;
    } catch (KeeperException e) {
        throw new IllegalStateException(
                "Failed to get data for " + mergedAggregatorPath + " with KeeperException", e);
    } catch (InterruptedException e) {
        throw new IllegalStateException(
                "Failed to get data for " + mergedAggregatorPath + " with InterruptedException", e);
    } catch (JSONException e) {
        throw new IllegalStateException(
                "Failed to get data for " + mergedAggregatorPath + " with JSONException", e);
    }
    for (int i = 0; i < aggregatorArray.length(); ++i) {
        try {
            if (LOG.isDebugEnabled()) {
                LOG.debug("getAggregatorValues: " + "Getting aggregators from "
                        + aggregatorArray.getJSONObject(i));
            }
            String aggregatorName = aggregatorArray.getJSONObject(i).getString(AGGREGATOR_NAME_KEY);
            Aggregator<Writable> aggregator = getAggregatorMap().get(aggregatorName);
            if (aggregator == null) {
                continue;
            }
            Writable aggregatorValue = aggregator.getAggregatedValue();
            InputStream input = new ByteArrayInputStream(
                    Base64.decode(aggregatorArray.getJSONObject(i).getString(AGGREGATOR_VALUE_KEY)));
            aggregatorValue.readFields(new DataInputStream(input));
            aggregator.setAggregatedValue(aggregatorValue);
            if (LOG.isDebugEnabled()) {
                LOG.debug("getAggregatorValues: " + "Got aggregator=" + aggregatorName + " value="
                        + aggregatorValue);
            }
        } catch (JSONException e) {
            throw new IllegalStateException("Failed to decode data for index " + i + " with KeeperException",
                    e);
        } catch (IOException e) {
            throw new IllegalStateException("Failed to decode data for index " + i + " with KeeperException",
                    e);
        }
    }
    if (LOG.isInfoEnabled()) {
        LOG.info("getAggregatorValues: Finished loading " + mergedAggregatorPath + " with aggregator values "
                + aggregatorArray);
    }
}

From source file:com.linkedpipes.plugin.loader.dcatAp11ToCkanBatch.DcatAp11ToCkanBatch.java

@Override
public void execute() throws LpException {

    apiURI = configuration.getApiUri();/*from www.j  a  v  a2  s  . c o m*/

    if (apiURI == null || apiURI.isEmpty() || configuration.getApiKey() == null
            || configuration.getApiKey().isEmpty()) {
        throw exceptionFactory.failure("Missing required settings.");
    }

    Map<String, String> organizations = getOrganizations();

    LOG.debug("Querying metadata for datasets");

    LinkedList<String> datasets = new LinkedList<>();
    for (Map<String, Value> map : executeSelectQuery(
            "SELECT ?d WHERE {?d a <" + DcatAp11ToCkanBatchVocabulary.DCAT_DATASET_CLASS + ">}")) {
        datasets.add(map.get("d").stringValue());
    }

    int current = 0;
    int total = datasets.size();

    LOG.info("Found " + total + " datasets");

    progressReport.start(total);

    for (String datasetURI : datasets) {
        current++;

        CloseableHttpResponse queryResponse = null;

        LOG.info("Processing dataset " + current + "/" + total + ": " + datasetURI);

        String datasetID = executeSimpleSelectQuery("SELECT ?did WHERE {<" + datasetURI + "> <"
                + DcatAp11ToCkanBatchVocabulary.LODCZCKAN_DATASET_ID + "> ?did }", "did");
        if (datasetID.isEmpty()) {
            LOG.warn("Dataset " + datasetURI + " has missing CKAN ID");
            continue;
        }

        boolean datasetExists = false;

        Map<String, String> resUrlIdMap = new HashMap<>();
        Map<String, String> resDistroIdMap = new HashMap<>();
        Map<String, JSONObject> resourceList = new HashMap<>();

        LOG.debug("Querying for the dataset " + datasetID + " in CKAN");
        HttpGet httpGet = new HttpGet(apiURI + "/package_show?id=" + datasetID);
        try {
            queryResponse = queryClient.execute(httpGet);
            if (queryResponse.getStatusLine().getStatusCode() == 200) {
                LOG.debug("Dataset found");
                datasetExists = true;

                JSONObject response = new JSONObject(EntityUtils.toString(queryResponse.getEntity()))
                        .getJSONObject("result");
                JSONArray resourcesArray = response.getJSONArray("resources");
                for (int i = 0; i < resourcesArray.length(); i++) {
                    String id = resourcesArray.getJSONObject(i).getString("id");
                    resourceList.put(id, resourcesArray.getJSONObject(i));

                    String url = resourcesArray.getJSONObject(i).getString("url");
                    resUrlIdMap.put(url, id);

                    if (resourcesArray.getJSONObject(i).has("distro_url")) {
                        String distro = resourcesArray.getJSONObject(i).getString("distro_url");
                        resDistroIdMap.put(distro, id);
                    }
                }
            } else {
                String ent = EntityUtils.toString(queryResponse.getEntity());
                LOG.debug("Dataset not found: " + ent);
            }
        } catch (Exception e) {
            LOG.error(e.getLocalizedMessage(), e);
        } finally {
            if (queryResponse != null) {
                try {
                    queryResponse.close();
                } catch (IOException e) {
                    LOG.error(e.getLocalizedMessage(), e);
                }
            }
        }

        LinkedList<String> keywords = new LinkedList<>();
        for (Map<String, Value> map : executeSelectQuery(
                "SELECT ?keyword WHERE {<" + datasetURI + "> <" + DcatAp11ToCkanBatchVocabulary.DCAT_KEYWORD
                        + "> ?keyword FILTER(LANGMATCHES(LANG(?keyword), \"" + configuration.getLoadLanguage()
                        + "\"))}")) {
            keywords.add(map.get("keyword").stringValue());
        }

        String publisher_uri = executeSimpleSelectQuery("SELECT ?publisher_uri WHERE {<" + datasetURI + "> <"
                + DCTERMS.PUBLISHER + "> ?publisher_uri }", "publisher_uri");
        String publisher_name = executeSimpleSelectQuery(
                "SELECT ?publisher_name WHERE {<" + datasetURI + "> <" + DCTERMS.PUBLISHER + ">/<" + FOAF.NAME
                        + "> ?publisher_name FILTER(LANGMATCHES(LANG(?publisher_name), \""
                        + configuration.getLoadLanguage() + "\"))}",
                "publisher_name");

        if (!organizations.containsKey(publisher_uri)) {
            LOG.debug("Creating organization " + publisher_uri);
            JSONObject root = new JSONObject();

            if (publisher_name == null || publisher_name.isEmpty()) {
                throw exceptionFactory.failure("Organization has no name: " + publisher_uri);
            }

            root.put("title", publisher_name);
            String orgname = Normalizer.normalize(publisher_name, Normalizer.Form.NFD)
                    .replaceAll("\\P{InBasic_Latin}", "").replace(' ', '-').replace('.', '-').toLowerCase();
            root.put("name", orgname);
            JSONArray org_extras = new JSONArray();
            org_extras.put(new JSONObject().put("key", "uri").put("value", publisher_uri));
            root.put("extras", org_extras);

            HttpPost httpPost = new HttpPost(apiURI + "/organization_create");
            httpPost.addHeader(new BasicHeader("Authorization", configuration.getApiKey()));

            String json = root.toString();

            httpPost.setEntity(new StringEntity(json, Charset.forName("utf-8")));

            CloseableHttpResponse response = null;

            try {
                response = postClient.execute(httpPost);
                if (response.getStatusLine().getStatusCode() == 200) {
                    LOG.debug("Organization created OK");
                    //LOG.info("Response: " + EntityUtils.toString(response.getEntity()));
                    organizations.put(publisher_uri, orgname);
                } else if (response.getStatusLine().getStatusCode() == 409) {
                    String ent = EntityUtils.toString(response.getEntity());
                    LOG.error("Organization conflict: " + ent);
                    throw exceptionFactory.failure("Organization conflict: " + ent);
                } else {
                    String ent = EntityUtils.toString(response.getEntity());
                    LOG.error("Response:" + ent);
                    throw exceptionFactory.failure("Error creating organization: " + ent);
                }
            } catch (Exception e) {
                LOG.error(e.getLocalizedMessage(), e);
            } finally {
                if (response != null) {
                    try {
                        response.close();
                    } catch (IOException e) {
                        LOG.error(e.getLocalizedMessage(), e);
                        throw exceptionFactory.failure("Error creating dataset");
                    }
                }
            }
        }

        LOG.debug("Creating JSON");

        JSONObject root = new JSONObject();

        JSONArray tags = new JSONArray();
        for (String keyword : keywords) {
            String safekeyword = fixKeyword(keyword);
            if (safekeyword.length() >= 2) {
                tags.put(new JSONObject().put("name", safekeyword));
            }
        }
        root.put("tags", tags);

        JSONArray resources = new JSONArray();

        if (!datasetID.isEmpty()) {
            root.put("name", datasetID);
        }

        String title = executeSimpleSelectQuery("SELECT ?title WHERE {<" + datasetURI + "> <" + DCTERMS.TITLE
                + "> ?title FILTER(LANGMATCHES(LANG(?title), \"" + configuration.getLoadLanguage() + "\"))}",
                "title");
        if (!title.isEmpty()) {
            root.put("title", title);
        }
        String description = executeSimpleSelectQuery("SELECT ?description WHERE {<" + datasetURI + "> <"
                + DCTERMS.DESCRIPTION + "> ?description FILTER(LANGMATCHES(LANG(?description), \""
                + configuration.getLoadLanguage() + "\"))}", "description");
        if (!description.isEmpty()) {
            root.put("notes", description);
        }
        String contactPoint = executeSimpleSelectQuery("SELECT ?contact WHERE {<" + datasetURI + "> <"
                + DcatAp11ToCkanBatchVocabulary.DCAT_CONTACT_POINT + ">/<"
                + DcatAp11ToCkanBatchVocabulary.VCARD_HAS_EMAIL + "> ?contact }", "contact");
        if (!contactPoint.isEmpty()) {
            root.put("maintainer_email", contactPoint);
        }
        String curatorName = executeSimpleSelectQuery(
                "SELECT ?name WHERE {<" + datasetURI + "> <" + DcatAp11ToCkanBatchVocabulary.DCAT_CONTACT_POINT
                        + ">/<" + DcatAp11ToCkanBatchVocabulary.VCARD_FN + "> ?name }",
                "name");
        if (!curatorName.isEmpty()) {
            root.put("maintainer", curatorName);
        }
        String issued = executeSimpleSelectQuery(
                "SELECT ?issued WHERE {<" + datasetURI + "> <" + DCTERMS.ISSUED + "> ?issued }", "issued");
        if (!issued.isEmpty()) {
            root.put("metadata_created", issued);
        }
        String modified = executeSimpleSelectQuery(
                "SELECT ?modified WHERE {<" + datasetURI + "> <" + DCTERMS.MODIFIED + "> ?modified }",
                "modified");
        if (!modified.isEmpty()) {
            root.put("metadata_modified", modified);
        }

        if (configuration.getProfile().equals(DcatAp11ToCkanBatchVocabulary.PROFILES_NKOD.stringValue())) {
            if (!publisher_uri.isEmpty()) {
                root.put("publisher_uri", publisher_uri);
            }
            if (!publisher_name.isEmpty()) {
                root.put("publisher_name", publisher_name);
            }

            String periodicity = executeSimpleSelectQuery("SELECT ?periodicity WHERE {<" + datasetURI + "> <"
                    + DCTERMS.ACCRUAL_PERIODICITY + "> ?periodicity }", "periodicity");
            if (!periodicity.isEmpty()) {
                root.put("frequency", periodicity);
            }
            String temporalStart = executeSimpleSelectQuery(
                    "SELECT ?temporalStart WHERE {<" + datasetURI + "> <" + DCTERMS.TEMPORAL + ">/<"
                            + DcatAp11ToCkanBatchVocabulary.SCHEMA_STARTDATE + "> ?temporalStart }",
                    "temporalStart");
            if (!temporalStart.isEmpty()) {
                root.put("temporal_start", temporalStart);
            }
            String temporalEnd = executeSimpleSelectQuery(
                    "SELECT ?temporalEnd WHERE {<" + datasetURI + "> <" + DCTERMS.TEMPORAL + ">/<"
                            + DcatAp11ToCkanBatchVocabulary.SCHEMA_ENDDATE + "> ?temporalEnd }",
                    "temporalEnd");
            if (!temporalEnd.isEmpty()) {
                root.put("temporal_end", temporalEnd);
            }
            String schemaURL = executeSimpleSelectQuery(
                    "SELECT ?schema WHERE {<" + datasetURI + "> <" + FOAF.PAGE + "> ?schema }", "schema");
            if (!schemaURL.isEmpty()) {
                root.put("schema", schemaURL);
            }
            String spatial = executeSimpleSelectQuery(
                    "SELECT ?spatial WHERE {<" + datasetURI + "> <" + DCTERMS.SPATIAL + "> ?spatial }",
                    "spatial");
            if (!spatial.isEmpty()) {
                root.put("spatial_uri", spatial);
            }
            LinkedList<String> themes = new LinkedList<>();
            for (Map<String, Value> map : executeSelectQuery("SELECT ?theme WHERE {<" + datasetURI + "> <"
                    + DcatAp11ToCkanBatchVocabulary.DCAT_THEME + "> ?theme }")) {
                themes.add(map.get("theme").stringValue());
            }
            String concatThemes = "";
            for (String theme : themes) {
                concatThemes += theme + " ";
            }
            if (!concatThemes.isEmpty())
                root.put("theme", concatThemes);

        }

        //Distributions

        LinkedList<String> distributions = new LinkedList<>();
        for (Map<String, Value> map : executeSelectQuery("SELECT ?distribution WHERE {<" + datasetURI + "> <"
                + DcatAp11ToCkanBatchVocabulary.DCAT_DISTRIBUTION + "> ?distribution }")) {
            distributions.add(map.get("distribution").stringValue());
        }

        for (String distribution : distributions) {
            JSONObject distro = new JSONObject();

            String dtitle = executeSimpleSelectQuery("SELECT ?title WHERE {<" + distribution + "> <"
                    + DCTERMS.TITLE + "> ?title FILTER(LANGMATCHES(LANG(?title), \""
                    + configuration.getLoadLanguage() + "\"))}", "title");
            if (!dtitle.isEmpty()) {
                distro.put("name", dtitle);
            }
            String ddescription = executeSimpleSelectQuery("SELECT ?description WHERE {<" + distribution + "> <"
                    + DCTERMS.DESCRIPTION + "> ?description FILTER(LANGMATCHES(LANG(?description), \""
                    + configuration.getLoadLanguage() + "\"))}", "description");
            if (!ddescription.isEmpty()) {
                distro.put("description", ddescription);
            }
            //DCAT-AP v1.1: has to be am IRI from http://publications.europa.eu/mdr/authority/file-type/index.html
            String dformat = executeSimpleSelectQuery(
                    "SELECT ?format WHERE {<" + distribution + "> <" + DCTERMS.FORMAT + "> ?format }",
                    "format");
            if (!dformat.isEmpty() && codelists != null) {
                String formatlabel = executeSimpleCodelistSelectQuery(
                        "SELECT ?formatlabel WHERE {<" + dformat + "> <" + SKOS.PREF_LABEL
                                + "> ?formatlabel FILTER(LANGMATCHES(LANG(?formatlabel), \"en\"))}",
                        "formatlabel");
                if (!formatlabel.isEmpty()) {
                    distro.put("format", formatlabel);
                }
            }

            String dwnld = executeSimpleSelectQuery("SELECT ?dwnld WHERE {<" + distribution + "> <"
                    + DcatAp11ToCkanBatchVocabulary.DCAT_DOWNLOADURL + "> ?dwnld }", "dwnld");
            String access = executeSimpleSelectQuery("SELECT ?acc WHERE {<" + distribution + "> <"
                    + DcatAp11ToCkanBatchVocabulary.DCAT_ACCESSURL + "> ?acc }", "acc");

            //we prefer downloadURL, but only accessURL is mandatory
            if (dwnld == null || dwnld.isEmpty()) {
                dwnld = access;
                if (dwnld == null || dwnld.isEmpty()) {
                    LOG.warn("Empty download and access URLs: " + datasetURI);
                    continue;
                }
            }

            if (!dwnld.isEmpty()) {
                distro.put("url", dwnld);
            }
            if (!distribution.isEmpty()) {
                distro.put("distro_url", distribution);
            }

            distro.put("resource_type", "file");

            if (resDistroIdMap.containsKey(distribution)) {
                String id = resDistroIdMap.get(distribution);
                distro.put("id", id);
                resourceList.remove(id);
            } else if (resUrlIdMap.containsKey(dwnld)) {
                String id = resUrlIdMap.get(dwnld);
                distro.put("id", id);
                resourceList.remove(id);
            }

            String dissued = executeSimpleSelectQuery(
                    "SELECT ?issued WHERE {<" + distribution + "> <" + DCTERMS.ISSUED + "> ?issued }",
                    "issued");
            if (!dissued.isEmpty()) {
                distro.put("created", dissued);
            }
            String dmodified = executeSimpleSelectQuery(
                    "SELECT ?modified WHERE {<" + distribution + "> <" + DCTERMS.MODIFIED + "> ?modified }",
                    "modified");
            if (!dmodified.isEmpty()) {
                distro.put("last_modified", dmodified);
            }

            if (configuration.getProfile().equals(DcatAp11ToCkanBatchVocabulary.PROFILES_NKOD.stringValue())) {
                String dtemporalStart = executeSimpleSelectQuery(
                        "SELECT ?temporalStart WHERE {<" + distribution + "> <" + DCTERMS.TEMPORAL + ">/<"
                                + DcatAp11ToCkanBatchVocabulary.SCHEMA_STARTDATE + "> ?temporalStart }",
                        "temporalStart");
                if (!dtemporalStart.isEmpty()) {
                    distro.put("temporal_start", dtemporalStart);
                }
                String dtemporalEnd = executeSimpleSelectQuery(
                        "SELECT ?temporalEnd WHERE {<" + distribution + "> <" + DCTERMS.TEMPORAL + ">/<"
                                + DcatAp11ToCkanBatchVocabulary.SCHEMA_ENDDATE + "> ?temporalEnd }",
                        "temporalEnd");
                if (!dtemporalEnd.isEmpty()) {
                    distro.put("temporal_end", dtemporalEnd);
                }
                String dspatial = executeSimpleSelectQuery(
                        "SELECT ?spatial WHERE {<" + distribution + "> <" + DCTERMS.SPATIAL + "> ?spatial }",
                        "spatial");
                if (!dspatial.isEmpty()) {
                    root.put("spatial_uri", dspatial);
                }
                String dschemaURL = executeSimpleSelectQuery(
                        "SELECT ?schema WHERE {<" + distribution + "> <" + DCTERMS.CONFORMS_TO + "> ?schema }",
                        "schema");
                if (!dschemaURL.isEmpty()) {
                    distro.put("describedBy", dschemaURL);
                }
                String dlicense = executeSimpleSelectQuery(
                        "SELECT ?license WHERE {<" + distribution + "> <" + DCTERMS.LICENSE + "> ?license }",
                        "license");
                if (!dlicense.isEmpty()) {
                    distro.put("license_link", dlicense);
                }
                String dmimetype = executeSimpleSelectQuery("SELECT ?format WHERE {<" + distribution + "> <"
                        + DcatAp11ToCkanBatchVocabulary.DCAT_MEDIATYPE + "> ?format }", "format");
                if (!dmimetype.isEmpty()) {
                    distro.put("mimetype", dmimetype.replaceAll(".*\\/([^\\/]+\\/[^\\/]+)", "$1"));
                }
            }

            resources.put(distro);
        }

        //Add the remaining distributions that were not updated but existed in the original dataset
        for (Entry<String, JSONObject> resource : resourceList.entrySet()) {
            resources.put(resource.getValue());
        }

        root.put("resources", resources);

        //Create new dataset
        if (!datasetExists) {
            JSONObject createRoot = new JSONObject();
            CloseableHttpResponse response = null;

            createRoot.put("name", datasetID);
            createRoot.put("title", title);
            createRoot.put("owner_org", organizations.get(publisher_uri));

            LOG.debug("Creating dataset in CKAN");
            HttpPost httpPost = new HttpPost(apiURI + "/package_create?id=" + datasetID);
            httpPost.addHeader(new BasicHeader("Authorization", configuration.getApiKey()));

            String json = createRoot.toString();

            LOG.debug("Creating dataset with: " + json);

            httpPost.setEntity(new StringEntity(json, Charset.forName("utf-8")));

            try {
                response = createClient.execute(httpPost);
                if (response.getStatusLine().getStatusCode() == 200) {
                    LOG.debug("Dataset created OK");
                    //LOG.info("Response: " + EntityUtils.toString(response.getEntity()));
                } else if (response.getStatusLine().getStatusCode() == 409) {
                    String ent = EntityUtils.toString(response.getEntity());
                    LOG.error("Dataset already exists: " + ent);
                    throw exceptionFactory.failure("Dataset already exists");
                } else {
                    String ent = EntityUtils.toString(response.getEntity());
                    LOG.error("Response:" + ent);
                    throw exceptionFactory.failure("Error creating dataset");
                }
            } catch (Exception e) {
                LOG.error(e.getLocalizedMessage(), e);
            } finally {
                if (response != null) {
                    try {
                        response.close();
                    } catch (IOException e) {
                        LOG.error(e.getLocalizedMessage(), e);
                        throw exceptionFactory.failure("Error creating dataset");
                    }
                }
            }
        }

        //Update existing dataset
        String json = root.toString();
        LOG.debug("Posting to CKAN");
        HttpPost httpPost = new HttpPost(apiURI + "/package_update?id=" + datasetID);
        httpPost.addHeader(new BasicHeader("Authorization", configuration.getApiKey()));

        LOG.debug(json);

        httpPost.setEntity(new StringEntity(json, Charset.forName("utf-8")));
        CloseableHttpResponse response = null;

        try {
            response = postClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                //LOG.info("Response:" + EntityUtils.toString(response.getEntity()));
            } else {
                String ent = EntityUtils.toString(response.getEntity());
                LOG.error("Response:" + ent);
                throw exceptionFactory.failure("Error updating dataset");
            }
        } catch (Exception e) {
            LOG.error(e.getLocalizedMessage(), e);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    LOG.error(e.getLocalizedMessage(), e);
                    throw exceptionFactory.failure("Error updating dataset");
                }
            }
        }

        progressReport.entryProcessed();
    }

    try {
        queryClient.close();
        createClient.close();
        postClient.close();
    } catch (IOException e) {
        LOG.error(e.getLocalizedMessage(), e);
    }

    progressReport.done();

}

From source file:com.liferay.tasks.callback.GetTasksCallback.java

@Override
public List<TaskModel> transform(Object obj) throws Exception {
    List<TaskModel> tasks = new ArrayList<TaskModel>();

    JSONArray jsonArray = (JSONArray) obj;

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject json = jsonArray.getJSONObject(i);

        tasks.add(new TaskModel(json));
    }//from   w ww .  j av  a 2s  . co  m

    return tasks;
}