Search in sources :

Example 1 with ListResult

use of io.syndesis.common.model.ListResult in project syndesis by syndesisio.

the class DataManager method fetchAll.

@SafeVarargs
@SuppressWarnings({ "unchecked", "varargs" })
public final <T extends WithId<T>> ListResult<T> fetchAll(Class<T> model, Function<ListResult<T>, ListResult<T>>... operators) {
    ListResult<T> result;
    if (getDataAccessObject(model) != null) {
        return doWithDataAccessObject(model, d -> d.fetchAll(operators));
    } else {
        Kind kind = Kind.from(model);
        Map<String, T> cache = caches.getCache(kind.getModelName());
        result = ListResult.of(cache.values());
        if (operators == null) {
            return result;
        }
        for (Function<ListResult<T>, ListResult<T>> operator : operators) {
            result = operator.apply(result);
        }
        return result;
    }
}
Also used : ListResult(io.syndesis.common.model.ListResult) Kind(io.syndesis.common.model.Kind)

Example 2 with ListResult

use of io.syndesis.common.model.ListResult in project syndesis by syndesisio.

the class JsonDbDao method fetchAll.

@Override
@SuppressWarnings({ "unchecked", "PMD.CyclomaticComplexity" })
public ListResult<T> fetchAll(Function<ListResult<T>, ListResult<T>>... operators) {
    try {
        GetOptions options = new GetOptions();
        // Try to convert operators to equivalent DB queries.
        if (operators != null) {
            for (int i = 0; i < operators.length; i++) {
                Function<ListResult<T>, ListResult<T>> operator = operators[i];
                if (operator.getClass() == IdPrefixFilter.class) {
                    IdPrefixFilter<T> filter = (IdPrefixFilter<T>) operator;
                    options.startAt(":" + filter.getPrefix());
                    options.endAt(":" + filter.getPrefix());
                    // Take it out of the list.
                    operators[i] = null;
                }
            }
        }
        // get the data out..
        byte[] json = jsondb.getAsByteArray(getCollectionPath(), options);
        ListResult<T> result;
        if (json != null && json.length > 0) {
            // Lets use jackson to parse the map of keys to our model instances
            ObjectReader reader = Json.reader();
            TypeFactory typeFactory = reader.getTypeFactory();
            MapType mapType = typeFactory.constructMapType(LinkedHashMap.class, String.class, getType());
            LinkedHashMap<String, T> map = reader.forType(mapType).readValue(json);
            result = ListResult.of(map.values());
        } else {
            result = ListResult.of(Collections.<T>emptyList());
        }
        if (operators == null) {
            return result;
        }
        for (Function<ListResult<T>, ListResult<T>> operator : operators) {
            if (operator != null) {
                result = operator.apply(result);
            }
        }
        return result;
    } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") RuntimeException | IOException e) {
        throw SyndesisServerException.launderThrowable(e);
    }
}
Also used : IOException(java.io.IOException) GetOptions(io.syndesis.server.jsondb.GetOptions) MapType(com.fasterxml.jackson.databind.type.MapType) ListResult(io.syndesis.common.model.ListResult) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) IdPrefixFilter(io.syndesis.server.dao.manager.operators.IdPrefixFilter)

Example 3 with ListResult

use of io.syndesis.common.model.ListResult in project syndesis-qe by syndesisio.

the class AbstractEndpoint method list.

public List<T> list(String id) {
    final ObjectMapper mapper = new ObjectMapper().registerModules(new Jdk8Module());
    mapper.configure(Feature.AUTO_CLOSE_SOURCE, true);
    final ObjectWriter ow = mapper.writer();
    final Class<ListResult<T>> listtype = (Class) ListResult.class;
    log.debug("GET : {}", getEndpointUrl(Optional.ofNullable(id)));
    final Invocation.Builder invocation = this.createInvocation(id);
    final JsonNode response = invocation.get(JsonNode.class);
    ListResult<T> result = null;
    try {
        result = Json.reader().forType(listtype).readValue(response.toString());
    } catch (IOException ex) {
        log.error("" + ex);
    }
    final List<T> ts = new ArrayList<>();
    for (int i = 0; i < result.getTotalCount(); i++) {
        T con = null;
        try {
            final String json = ow.writeValueAsString(result.getItems().get(i));
            con = Json.reader().forType(type).readValue(json);
        } catch (IOException ex) {
            log.error(ex.toString());
        }
        ts.add(con);
    }
    return ts;
}
Also used : Invocation(javax.ws.rs.client.Invocation) ArrayList(java.util.ArrayList) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ListResult(io.syndesis.common.model.ListResult) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with ListResult

use of io.syndesis.common.model.ListResult in project syndesis-qe by syndesisio.

the class AbstractEndpoint method list.

public List<T> list(String id) {
    final ObjectMapper mapper = new ObjectMapper().registerModules(new Jdk8Module());
    mapper.configure(Feature.AUTO_CLOSE_SOURCE, true);
    final ObjectWriter ow = mapper.writer();
    final Class<ListResult<T>> listtype = (Class) ListResult.class;
    log.debug("GET : {}", getEndpointUrl(Optional.ofNullable(id)));
    JsonNode response = this.createInvocation(id).get(JsonNode.class);
    ListResult<T> result = null;
    try {
        result = JsonUtils.reader().forType(listtype).readValue(response.toString());
    } catch (IOException ex) {
        log.error("" + ex);
    }
    final List<T> ts = new ArrayList<>();
    for (int i = 0; i < result.getTotalCount(); i++) {
        T con = null;
        try {
            final String json = ow.writeValueAsString(result.getItems().get(i));
            con = JsonUtils.reader().forType(type).readValue(json);
        } catch (IOException ex) {
            log.error(ex.toString());
        }
        ts.add(con);
    }
    return ts;
}
Also used : ArrayList(java.util.ArrayList) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ListResult(io.syndesis.common.model.ListResult) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ListResult (io.syndesis.common.model.ListResult)4 IOException (java.io.IOException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)2 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)2 ArrayList (java.util.ArrayList)2 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 MapType (com.fasterxml.jackson.databind.type.MapType)1 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)1 Kind (io.syndesis.common.model.Kind)1 IdPrefixFilter (io.syndesis.server.dao.manager.operators.IdPrefixFilter)1 GetOptions (io.syndesis.server.jsondb.GetOptions)1 Invocation (javax.ws.rs.client.Invocation)1