Search in sources :

Example 1 with ManagedSiteException

use of org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException in project stanbol by apache.

the class YardSite method store.

/**
 * Stores the parsed representation to the Yard and also applies the
 * configured {@link #getFieldMapper() FieldMappings}.
 * @param The representation to store
 */
@Override
public void store(Representation representation) throws ManagedSiteException {
    try {
        Yard yard = getYard();
        fieldMapper.applyMappings(representation, representation, yard.getValueFactory());
        yard.store(representation);
    } catch (YardException e) {
        throw new ManagedSiteException(e.getMessage(), e);
    }
}
Also used : Yard(org.apache.stanbol.entityhub.servicesapi.yard.Yard) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException)

Example 2 with ManagedSiteException

use of org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException in project stanbol by apache.

the class YardSite method getEntity.

@Override
public Entity getEntity(String id) throws ManagedSiteException {
    Representation rep;
    try {
        rep = getYard().getRepresentation(id);
    } catch (YardException e) {
        throw new ManagedSiteException(e.getMessage(), e);
    }
    if (rep != null) {
        Entity entity = new EntityImpl(config.getId(), rep, null);
        SiteUtils.initEntityMetadata(entity, siteMetadata, null);
        return entity;
    } else {
        return null;
    }
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) EntityImpl(org.apache.stanbol.entityhub.core.model.EntityImpl) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation)

Example 3 with ManagedSiteException

use of org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException in project stanbol by apache.

the class YardSite method store.

/**
 * Stores the parsed representations to the Yard and also applies the
 * configured {@link #getFieldMapper() FieldMappings}.
 * @param The representations to store
 */
@Override
public void store(final Iterable<Representation> representations) throws ManagedSiteException {
    try {
        Yard yard = getYard();
        final ValueFactory vf = yard.getValueFactory();
        yard.store(new Iterable<Representation>() {

            @Override
            public Iterator<Representation> iterator() {
                return new Iterator<Representation>() {

                    Iterator<Representation> it = representations.iterator();

                    @Override
                    public boolean hasNext() {
                        return it.hasNext();
                    }

                    @Override
                    public Representation next() {
                        Representation next = it.next();
                        fieldMapper.applyMappings(next, next, vf);
                        return next;
                    }

                    @Override
                    public void remove() {
                        it.remove();
                    }
                };
            }
        });
    } catch (YardException e) {
        throw new ManagedSiteException(e.getMessage(), e);
    }
}
Also used : Yard(org.apache.stanbol.entityhub.servicesapi.yard.Yard) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) AdaptingIterator(org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator) Iterator(java.util.Iterator) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) InMemoryValueFactory(org.apache.stanbol.entityhub.core.model.InMemoryValueFactory) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)

Example 4 with ManagedSiteException

use of org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException in project stanbol by apache.

the class YardSite method findEntities.

@Override
public QueryResultList<Entity> findEntities(FieldQuery query) throws ManagedSiteException {
    QueryResultList<Representation> results;
    try {
        results = getYard().findRepresentation(query);
    } catch (YardException e) {
        throw new ManagedSiteException(e.getMessage(), e);
    }
    return new QueryResultListImpl<Entity>(results.getQuery(), new AdaptingIterator<Representation, Entity>(results.iterator(), new AdaptingIterator.Adapter<Representation, Entity>() {

        private final String siteId = config.getId();

        @Override
        public Entity adapt(Representation value, Class<Entity> type) {
            Entity entity = new EntityImpl(siteId, value, null);
            SiteUtils.initEntityMetadata(entity, siteMetadata, null);
            return entity;
        }
    }, Entity.class), Entity.class);
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) EntityImpl(org.apache.stanbol.entityhub.core.model.EntityImpl) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation)

Example 5 with ManagedSiteException

use of org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException in project stanbol by apache.

the class ReferencedSiteRootResource method updateOrCreateEntity.

private Response updateOrCreateEntity(Site site, String id, Map<String, Representation> parsed, String requestMethod, boolean create, boolean update, HttpHeaders headers) {
    long start = System.currentTimeMillis();
    MediaType accepted = getAcceptableMediaType(headers, JerseyUtils.ENTITY_SUPPORTED_MEDIA_TYPES, MediaType.APPLICATION_JSON_TYPE);
    ManagedSite managedSite;
    if (site instanceof ManagedSite) {
        managedSite = (ManagedSite) site;
    } else {
        ResponseBuilder builder = Response.status(Status.FORBIDDEN).entity(String.format("The Site '%s' is not managed and does not support " + "create/update nor delete operations", site.getId())).header(HttpHeaders.ACCEPT, accepted);
        // addCORSOrigin(servletContext, builder, headers);
        return builder.build();
    }
    // (1) if an id is parsed we need to ignore all other representations
    if (id != null && !"*".equals(id)) {
        Representation r = parsed.get(id);
        if (r == null) {
            ResponseBuilder builder = Response.status(Status.BAD_REQUEST).entity(String.format("Parsed RDF data do not contain any " + "Information about the parsed id '%s'", id)).header(HttpHeaders.ACCEPT, accepted);
            // addCORSOrigin(servletContext, builder, headers);
            return builder.build();
        } else {
            parsed = Collections.singletonMap(id, r);
        }
    }
    // First check if all parsed Representation can be created/updated
    if (!(create && update)) {
        // if both create and update are enabled skip this
        log.debug("   ... validate parsed Representation state (create: {}| update: {})", create, update);
        for (Entry<String, Representation> entry : parsed.entrySet()) {
            boolean exists;
            try {
                exists = managedSite.getEntity(entry.getKey()) != null;
            } catch (SiteException e) {
                log.error(String.format("Exception while checking the existance " + "of an Entity with id  %s in the Entityhub.", entry.getKey()), e);
                ResponseBuilder builder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Unable to process Entity %s because of" + "an Error while checking the current version of that" + "Entity within the Entityhub (Message: %s)", entry.getKey(), e.getMessage())).header(HttpHeaders.ACCEPT, accepted);
                // addCORSOrigin(servletContext, builder, headers);
                return builder.build();
            }
            if ((exists && !update) || (!exists && !create)) {
                ResponseBuilder builder = Response.status(Status.BAD_REQUEST).entity(String.format("Unable to %s an Entity %s becuase it %s and %s is deactivated. " + " You might want to set the '%s' parameter to TRUE in your Request", exists ? "update" : "create", entry.getKey(), exists ? "does already exists " : "does not", exists ? "updateing existing" : "creating new", exists ? "does already" : "does not exists", exists ? "update" : "create")).header(HttpHeaders.ACCEPT, accepted);
                // addCORSOrigin(servletContext, builder, headers);
                return builder.build();
            }
        }
    }
    long validateCompleted = System.currentTimeMillis();
    log.info("   ... validate request data {}ms", validateCompleted - start);
    try {
        managedSite.store(parsed.values());
    } catch (ManagedSiteException e) {
        log.error(String.format("Exception while storing parsed Representations " + "in the ManagedSite %s", managedSite.getId()), e);
        ResponseBuilder builder = Response.status(Status.INTERNAL_SERVER_ERROR).entity("Unable to store parsed Entities to ManagedSite " + managedSite.getId() + " because of an error (Message: " + e.getMessage() + ")").header(HttpHeaders.ACCEPT, accepted);
        // addCORSOrigin(servletContext, builder, headers);
        return builder.build();
    }
    ResponseBuilder builder;
    if (create && parsed.size() == 1) {
        String createdId = parsed.keySet().iterator().next();
        URI created = uriInfo.getRequestUriBuilder().queryParam("id", createdId).build();
        builder = Response.created(created);
        builder.header(HttpHeaders.ACCEPT, accepted);
    } else {
        builder = Response.noContent();
    }
    log.info("   ... create/update {} entities in {}ms", parsed.size(), System.currentTimeMillis() - validateCompleted);
    // addCORSOrigin(servletContext, builder, headers);
    return builder.build();
}
Also used : ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) MediaType(javax.ws.rs.core.MediaType) MediaTypeUtil.getAcceptableMediaType(org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) ManagedSite(org.apache.stanbol.entityhub.servicesapi.site.ManagedSite) URI(java.net.URI)

Aggregations

ManagedSiteException (org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException)5 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)4 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)4 EntityImpl (org.apache.stanbol.entityhub.core.model.EntityImpl)2 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)2 Yard (org.apache.stanbol.entityhub.servicesapi.yard.Yard)2 URI (java.net.URI)1 Iterator (java.util.Iterator)1 MediaType (javax.ws.rs.core.MediaType)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)1 InMemoryValueFactory (org.apache.stanbol.entityhub.core.model.InMemoryValueFactory)1 QueryResultListImpl (org.apache.stanbol.entityhub.core.query.QueryResultListImpl)1 RdfRepresentation (org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation)1 ValueFactory (org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)1 ManagedSite (org.apache.stanbol.entityhub.servicesapi.site.ManagedSite)1 SiteException (org.apache.stanbol.entityhub.servicesapi.site.SiteException)1 AdaptingIterator (org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator)1