Search in sources :

Example 1 with Provider

use of de.ipk_gatersleben.bit.bi.bridge.brapicomp.dbentities.Provider in project IPK-BrAPI-Validator by plantbreeding.

the class AdminResource method updateProviders.

/**
 * Update database information using public json document.
 *
 * @return Empty response with status code
 */
@POST
@Path("/updateproviders")
public Response updateProviders(@Context HttpHeaders headers) {
    if (Config.get("advancedMode") == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    LOGGER.debug("New GET /admin/testallpublic call.");
    int resourcesUpdated = 0;
    try {
        // Check auth header
        if (!auth(headers)) {
            String e = JsonMessageManager.jsonMessage(401, "unauthorized", 4002);
            return Response.status(Status.UNAUTHORIZED).entity(e).build();
        }
        ObjectMapper mapper = new ObjectMapper();
        // Required because some resource properties are arrays and some objects.
        mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        // Download Json
        URL url = new URL(Config.get("resourceJsonUrl"));
        URLConnection connection;
        if (!Config.get("http.proxyHost").equals("")) {
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(Config.get("http.proxyHost"), Integer.parseInt(Config.get("http.proxyPort"))));
            connection = url.openConnection(proxy);
        } else {
            connection = url.openConnection();
        }
        connection.connect();
        InputStream is = connection.getInputStream();
        JsonNode node = mapper.readTree(is).at("/brapi-providers/category");
        Dao<Provider, UUID> providerDao = DataSourceManager.getDao(Provider.class);
        Dao<Resource, UUID> resourceDao = DataSourceManager.getDao(Resource.class);
        // Iterate through providers.
        for (int i = 0; i < node.size(); i++) {
            Provider providerJson = mapper.treeToValue(node.get(i), Provider.class);
            // Check if provider existed (by name)
            Provider providerDb = providerDao.queryBuilder().where().eq(Provider.NAME_FIELD_NAME, providerJson.getName()).queryForFirst();
            if (providerDb == null) {
                // Not found in DB, generate
                providerDao.create(providerJson);
                providerDao.refresh(providerJson);
                Iterator<Resource> it = providerJson.getResources().iterator();
                while (it.hasNext()) {
                    Resource res = it.next();
                    res.setProvider(providerJson);
                    res.setPublic(true);
                    resourceDao.create(res);
                    resourcesUpdated++;
                }
            } else {
                // Found in DB, update.
                providerDb.setDescription(providerJson.getDescription());
                providerDb.setLogo(providerJson.getLogo());
                Iterator<Resource> it = providerJson.getResources().iterator();
                while (it.hasNext()) {
                    Resource res = it.next();
                    // Check resources in DB
                    Resource resDb = resourceDao.queryBuilder().where().eq(Resource.URL_FIELD_NAME, res.getUrl()).and().eq(Resource.PROVIDER_FIELD_NAME, providerDb.getId()).queryForFirst();
                    if (resDb == null) {
                        // Not found, create
                        res.setProvider(providerDb);
                        res.setPublic(true);
                        resourceDao.create(res);
                        resourcesUpdated++;
                    } else {
                        // Found, generate
                        resDb.setCertificate(res.getCertificate());
                        resDb.setName(res.getName());
                        resDb.setLogo(res.getLogo());
                        resDb.setPublic(true);
                        resourceDao.update(resDb);
                        resourcesUpdated++;
                    }
                }
                providerDao.update(providerDb);
            }
        }
        String response = JsonMessageManager.jsonMessage(200, "" + resourcesUpdated + " resources updated.", 2000);
        return Response.ok().entity(response).build();
    } catch (IOException | SQLException e) {
        e.printStackTrace();
        String e1 = JsonMessageManager.jsonMessage(500, "internal server error", 5003);
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e1).build();
    }
}
Also used : SQLException(java.sql.SQLException) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) Resource(de.ipk_gatersleben.bit.bi.bridge.brapicomp.dbentities.Resource) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection) Provider(de.ipk_gatersleben.bit.bi.bridge.brapicomp.dbentities.Provider) Proxy(java.net.Proxy) UUID(java.util.UUID) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Provider (de.ipk_gatersleben.bit.bi.bridge.brapicomp.dbentities.Provider)1 Resource (de.ipk_gatersleben.bit.bi.bridge.brapicomp.dbentities.Resource)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InetSocketAddress (java.net.InetSocketAddress)1 Proxy (java.net.Proxy)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 SQLException (java.sql.SQLException)1 UUID (java.util.UUID)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1