Example usage for javax.json JsonObjectBuilder add

List of usage examples for javax.json JsonObjectBuilder add

Introduction

In this page you can find the example usage for javax.json JsonObjectBuilder add.

Prototype

JsonObjectBuilder add(String name, JsonArrayBuilder builder);

Source Link

Document

Adds a name/ JsonArray pair to the JSON object associated with this object builder.

Usage

From source file:au.org.ands.vocabs.toolkit.db.PopulateAccessPoints.java

/**
 * Main program.//from   www. j a  v a 2  s .  co  m
 * @param args Command-line arguments
 */
public static void main(final String[] args) {
    // Create prefixes that both end with a slash, so that
    // they can be substituted for each other.
    String sparqlPrefix = sparqlPrefixProperty;
    if (!sparqlPrefix.endsWith("/")) {
        sparqlPrefix += "/";
    }
    String sesamePrefix = sesamePrefixProperty;
    if (!sesamePrefix.endsWith("/")) {
        sesamePrefix += "/";
    }
    sesamePrefix += "repositories/";
    System.out.println("sparqlPrefix: " + sparqlPrefix);
    System.out.println("sesamePrefix: " + sesamePrefix);
    List<Version> versions = VersionUtils.getAllVersions();
    for (Version version : versions) {
        System.out.println(version.getId());
        System.out.println(version.getTitle());
        String data = version.getData();
        System.out.println(data);
        JsonNode dataJson = TaskUtils.jsonStringToTree(data);
        JsonNode accessPoints = dataJson.get("access_points");
        if (accessPoints != null) {
            System.out.println(accessPoints);
            System.out.println(accessPoints.size());
            for (JsonNode accessPoint : accessPoints) {
                System.out.println(accessPoint);
                AccessPoint ap = new AccessPoint();
                ap.setVersionId(version.getId());
                String type = accessPoint.get("type").asText();
                JsonObjectBuilder jobPortal = Json.createObjectBuilder();
                JsonObjectBuilder jobToolkit = Json.createObjectBuilder();
                String uri;
                switch (type) {
                case AccessPoint.FILE_TYPE:
                    ap.setType(type);
                    // Get the path from the original access point.
                    String filePath = accessPoint.get("uri").asText();
                    // Save the last component of the path to use
                    // in the portal URI.
                    String downloadFilename = Paths.get(filePath).getFileName().toString();
                    if (!filePath.startsWith("/")) {
                        // Relative path that we need to fix up manually.
                        filePath = "FIXME " + filePath;
                    }
                    jobToolkit.add("path", filePath);
                    ap.setPortalData("");
                    ap.setToolkitData(jobToolkit.build().toString());
                    // Persist what we have ...
                    AccessPointUtils.saveAccessPoint(ap);
                    // ... so that now we can get access to the
                    // ID of the persisted object with ap2.getId().
                    String format;
                    if (downloadFilename.endsWith(".trig")) {
                        // Force TriG. This is needed for some legacy
                        // cases where the filename is ".trig" but
                        // the format has been incorrectly recorded
                        // as RDF/XML.
                        format = "TriG";
                    } else {
                        format = accessPoint.get("format").asText();
                    }
                    jobPortal.add("format", format);
                    jobPortal.add("uri", downloadPrefixProperty + ap.getId() + "/" + downloadFilename);
                    ap.setPortalData(jobPortal.build().toString());
                    AccessPointUtils.updateAccessPoint(ap);
                    break;
                case AccessPoint.API_SPARQL_TYPE:
                    ap.setType(type);
                    uri = accessPoint.get("uri").asText();
                    jobPortal.add("uri", uri);
                    if (uri.startsWith(sparqlPrefix)) {
                        // One of ours, so also add a sesameDownload
                        // endpoint.
                        AccessPoint ap2 = new AccessPoint();
                        ap2.setVersionId(version.getId());
                        ap2.setType(AccessPoint.SESAME_DOWNLOAD_TYPE);
                        ap2.setPortalData("");
                        ap2.setToolkitData("");
                        // Persist what we have ...
                        AccessPointUtils.saveAccessPoint(ap2);
                        // ... so that now we can get access to the
                        // ID of the persisted object with ap2.getId().
                        JsonObjectBuilder job2Portal = Json.createObjectBuilder();
                        JsonObjectBuilder job2Toolkit = Json.createObjectBuilder();
                        job2Portal.add("uri", downloadPrefixProperty + ap2.getId() + "/"
                                + Download.downloadFilename(ap2, ""));
                        job2Toolkit.add("uri", uri.replaceFirst(sparqlPrefix, sesamePrefix));
                        ap2.setPortalData(job2Portal.build().toString());
                        ap2.setToolkitData(job2Toolkit.build().toString());
                        AccessPointUtils.updateAccessPoint(ap2);
                        jobPortal.add("source", AccessPoint.SYSTEM_SOURCE);
                    } else {
                        jobPortal.add("source", AccessPoint.USER_SOURCE);
                    }
                    ap.setPortalData(jobPortal.build().toString());
                    ap.setToolkitData(jobToolkit.build().toString());
                    AccessPointUtils.saveAccessPoint(ap);
                    break;
                case AccessPoint.WEBPAGE_TYPE:
                    uri = accessPoint.get("uri").asText();
                    if (uri.endsWith("concept/topConcepts")) {
                        ap.setType(AccessPoint.SISSVOC_TYPE);
                        jobPortal.add("source", AccessPoint.SYSTEM_SOURCE);
                        jobPortal.add("uri", uri.replaceFirst("/concept/topConcepts$", ""));
                    } else {
                        ap.setType(type);
                        jobPortal.add("uri", uri);
                    }
                    ap.setPortalData(jobPortal.build().toString());
                    ap.setToolkitData(jobToolkit.build().toString());
                    AccessPointUtils.saveAccessPoint(ap);
                    break;
                default:
                }
                System.out.println("type is: " + ap.getType());
                System.out.println("portal_data: " + ap.getPortalData());
                System.out.println("toolkit_data: " + ap.getToolkitData());
            }
        }
    }
}

From source file:org.jboss.set.aphrodite.stream.services.json.StreamComponentJsonParser.java

public static JsonObject encodeStreamComponent(StreamComponent c) {
    final JsonObjectBuilder object = Json.createObjectBuilder();
    object.add(JSON_NAME, c.getName());
    object.add(JSON_CONTACTS, encodeContacts(c.getContacts()));
    object.add(JSON_REPOSITORY_TYPE, c.getRepositoryType().toString());
    object.add(JSON_REPOSITORY_URL, c.getRepositoryURL() == null ? "" : c.getRepositoryURL().toString());
    object.add(JSON_CODEBASE, c.getCodebase().getName());
    object.add(JSON_TAG, c.getTag());/*from   ww w .j a v a2s  .c om*/
    object.add(JSON_VERSION, c.getVersion());
    object.add(JSON_GAV, c.getGAV());
    object.add(JSON_COMMENT, c.getComment());
    return object.build();
}

From source file:org.jboss.set.aphrodite.stream.services.json.StreamsJsonParser.java

private static JsonObjectBuilder encodeStream(Stream stream) {
    final JsonObjectBuilder object = Json.createObjectBuilder();
    object.add(JSON_NAME, stream.getName());
    object.add(JSON_UPSTREAM, stream.getUpstream() == null ? JSON_VALUE_NULL : stream.getUpstream().getName());
    object.add(JSON_CODEBASES, encodeStreamComponents(stream.getAllComponents()));
    return object;
}

From source file:org.jboss.set.aphrodite.stream.services.json.StreamsJsonParser.java

public static JsonObject encode(Collection<Stream> toEncode) {
    //TODO, come up with some check or do we trust us?
    //assert toEncode instanceof LinkedHashMap;// no hanky pankies, we need ordered list, we had, otherwise it will screw us.
    final JsonObjectBuilder rootBuilder = Json.createObjectBuilder();
    final JsonArrayBuilder array = encodeStreams(toEncode);
    rootBuilder.add(JSON_STREAMS, array);
    return rootBuilder.build();
}

From source file:org.apache.nifi.reporting.SiteToSiteProvenanceReportingTask.java

private static void addField(final JsonObjectBuilder builder, final String key, final Long value) {
    if (value != null) {
        builder.add(key, value.longValue());
    }/*  w  w  w. ja  v a2  s . c  om*/
}

From source file:org.apache.nifi.reporting.SiteToSiteProvenanceReportingTask.java

private static void addField(final JsonObjectBuilder builder, final String key, final String value) {
    if (value == null) {
        return;// ww  w.  ja  v  a2s.c  om
    }

    builder.add(key, value);
}

From source file:au.org.ands.vocabs.toolkit.db.AccessPointUtils.java

/** Create an access point for a version, for a Sesame download.
 * Don't duplicate it, if it already exists.
 * @param version The version for which the access point is to be created.
 * @param toolkitUri The URI to put into the toolkitData.
 *///ww  w .  j  a  va 2  s  .  co m
public static void createSesameDownloadAccessPoint(final Version version, final String toolkitUri) {
    List<AccessPoint> aps = getAccessPointsForVersionAndType(version, AccessPoint.SESAME_DOWNLOAD_TYPE);
    for (AccessPoint ap : aps) {
        if (toolkitUri.equals(getToolkitUri(ap))) {
            // Already exists.
            return;
        }
    }
    // No existing access point for this file, so create a new one.
    AccessPoint ap = new AccessPoint();
    ap.setVersionId(version.getId());
    ap.setType(AccessPoint.SESAME_DOWNLOAD_TYPE);
    ap.setPortalData("");
    JsonObjectBuilder jobToolkit = Json.createObjectBuilder();
    jobToolkit.add("uri", toolkitUri);
    ap.setToolkitData(jobToolkit.build().toString());
    // Persist what we have ...
    AccessPointUtils.saveAccessPoint(ap);
    // ... so that now we can get access to the
    // ID of the persisted object with ap.getId().
    JsonObjectBuilder jobPortal = Json.createObjectBuilder();
    jobPortal.add("uri", downloadPrefixProperty + ap.getId() + "/" + Download.downloadFilename(ap, ""));
    ap.setPortalData(jobPortal.build().toString());
    AccessPointUtils.updateAccessPoint(ap);
}

From source file:org.apache.nifi.reporting.SiteToSiteProvenanceReportingTask.java

private static void addField(final JsonObjectBuilder builder, final JsonBuilderFactory factory,
        final String key, final Collection<String> values) {
    if (values == null) {
        return;//w  w w  . java  2  s. c o m
    }

    builder.add(key, createJsonArray(factory, values));
}

From source file:au.org.ands.vocabs.toolkit.db.AccessPointUtils.java

/** Create an access point for a version, for a file. Don't duplicate it,
 * if it already exists./*from w  w  w  .  j  a  v a2s. c  om*/
 * @param version The version for which the access point is to be created.
 * @param format The format of the access point. If null, attempt
 * to deduce a format from the filename.
 * @param targetPath The path to the existing file.
 */
public static void createFileAccessPoint(final Version version, final String format, final Path targetPath) {
    String targetPathString;
    try {
        targetPathString = targetPath.toRealPath().toString();
    } catch (IOException e) {
        LOGGER.error("createFileAccessPoint failed calling " + "toRealPath() on file: " + targetPath.toString(),
                e);
        // Try toAbsolutePath() instead.
        targetPathString = targetPath.toAbsolutePath().toString();
    }
    List<AccessPoint> aps = getAccessPointsForVersionAndType(version, AccessPoint.FILE_TYPE);
    for (AccessPoint ap : aps) {
        if (targetPathString.equals(getToolkitPath(ap))) {
            // Already exists. Check the format.
            if (format != null && !format.equals(getFormat(ap))) {
                // Format changed.
                updateFormat(ap, format);
            }
            return;
        }
    }
    // No existing access point for this file, so create a new one.
    AccessPoint ap = new AccessPoint();
    ap.setVersionId(version.getId());
    ap.setType(AccessPoint.FILE_TYPE);
    JsonObjectBuilder jobPortal = Json.createObjectBuilder();
    JsonObjectBuilder jobToolkit = Json.createObjectBuilder();
    jobToolkit.add("path", targetPathString);
    // toolkitData is now done.
    ap.setToolkitData(jobToolkit.build().toString());
    ap.setPortalData("");
    // Persist what we have ...
    AccessPointUtils.saveAccessPoint(ap);
    // ... so that now we can get access to the
    // ID of the persisted object with ap.getId().
    String baseFilename = targetPath.getFileName().toString();
    jobPortal.add("uri", downloadPrefixProperty + ap.getId() + "/" + baseFilename);
    // Now work on the format. The following is messy. It's really very
    // much for the best if the portal provides the format.
    String deducedFormat;
    if (format == null) {
        // The format was not provided to us, so try to deduce it.
        // First, try the extension.
        String extension = FilenameUtils.getExtension(baseFilename);
        deducedFormat = EXTENSION_TO_FILE_FORMAT_MAP.get(extension);
        if (deducedFormat == null) {
            // No luck with the extension, so try probing.
            try {
                String mimeType = Files.probeContentType(targetPath);
                if (mimeType == null) {
                    // Give up.
                    deducedFormat = "Unknown";
                } else {
                    deducedFormat = MIMETYPE_TO_FILE_FORMAT_MAP.get(mimeType);
                    if (deducedFormat == null) {
                        // Give up.
                        deducedFormat = "Unknown";
                    }
                }
            } catch (IOException e) {
                LOGGER.error("createFileAccessPoint failed to get " + "MIME type of file: " + targetPathString,
                        e);
                // Give up.
                deducedFormat = "Unknown";
            }
        }
    } else {
        // The format was provided to us, so use that. Much easier.
        deducedFormat = format;
    }
    jobPortal.add("format", deducedFormat);
    // portalData is now complete.
    ap.setPortalData(jobPortal.build().toString());
    AccessPointUtils.updateAccessPoint(ap);
}

From source file:au.org.ands.vocabs.toolkit.db.AccessPointUtils.java

/** Create an access point for a version, for a SPARQL endpoint.
 * Don't duplicate it, if it already exists.
 * @param version The version for which the access point is to be created.
 * @param portalUri The URI to put into the portalData.
 * @param source The source of the endpoint, either SYSTEM_SOURCE
 * or USER_SOURCE.//  w  w  w .  jav  a2 s. com
 */
public static void createApiSparqlAccessPoint(final Version version, final String portalUri,
        final String source) {
    List<AccessPoint> aps = getAccessPointsForVersionAndType(version, AccessPoint.API_SPARQL_TYPE);
    for (AccessPoint ap : aps) {
        if (portalUri.equals(getPortalUri(ap))) {
            // Already exists. Don't bother checking the source.
            return;
        }
    }
    // No existing access point for this file, so create a new one.
    AccessPoint ap = new AccessPoint();
    ap.setVersionId(version.getId());
    ap.setType(AccessPoint.API_SPARQL_TYPE);
    JsonObjectBuilder jobPortal = Json.createObjectBuilder();
    JsonObjectBuilder jobToolkit = Json.createObjectBuilder();
    jobPortal.add("uri", portalUri);
    jobPortal.add("source", source);
    ap.setPortalData(jobPortal.build().toString());
    ap.setToolkitData(jobToolkit.build().toString());
    AccessPointUtils.saveAccessPoint(ap);
}