org.jnosql.artemis.column.AbstractColumnTemplate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of artemis-column Show documentation
Show all versions of artemis-column Show documentation
Eclipse JNoSQL Mapping, Artemis API, to column NoSQL databases
/*
* Copyright (c) 2017 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.jnosql.artemis.column;
import org.jnosql.artemis.Converters;
import org.jnosql.artemis.IdNotFoundException;
import org.jnosql.artemis.column.util.ConverterUtil;
import org.jnosql.artemis.reflection.ClassRepresentation;
import org.jnosql.artemis.reflection.ClassRepresentations;
import org.jnosql.artemis.reflection.FieldRepresentation;
import org.jnosql.diana.api.column.ColumnDeleteQuery;
import org.jnosql.diana.api.column.ColumnEntity;
import org.jnosql.diana.api.column.ColumnFamilyManager;
import org.jnosql.diana.api.column.ColumnQuery;
import org.jnosql.diana.api.column.query.ColumnQueryBuilder;
import java.time.Duration;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import static java.util.Objects.requireNonNull;
/**
* The template method to {@link ColumnTemplate}
*/
public abstract class AbstractColumnTemplate implements ColumnTemplate {
protected abstract ColumnEntityConverter getConverter();
protected abstract ColumnFamilyManager getManager();
protected abstract ColumnWorkflow getFlow();
protected abstract ColumnEventPersistManager getEventManager();
protected abstract ClassRepresentations getClassRepresentations();
protected abstract Converters getConverters();
private final UnaryOperator insert = e -> getManager().insert(e);
private final UnaryOperator update = e -> getManager().update(e);
@Override
public T insert(T entity) {
requireNonNull(entity, "entity is required");
return getFlow().flow(entity, insert);
}
@Override
public T insert(T entity, Duration ttl) {
requireNonNull(entity, "entity is required");
requireNonNull(ttl, "ttl is required");
return getFlow().flow(entity, e -> getManager().insert(e, ttl));
}
@Override
public T update(T entity) {
requireNonNull(entity, "entity is required");
return getFlow().flow(entity, update);
}
@Override
public void delete(ColumnDeleteQuery query) {
requireNonNull(query, "query is required");
getEventManager().firePreDeleteQuery(query);
getManager().delete(query);
}
@Override
public List select(ColumnQuery query) {
requireNonNull(query, "query is required");
getEventManager().firePreQuery(query);
List entities = getManager().select(query);
Function function = e -> getConverter().toEntity(e);
return entities.stream().map(function).collect(Collectors.toList());
}
@Override
public Optional find(Class entityClass, ID id) {
requireNonNull(entityClass, "entityClass is required");
requireNonNull(id, "id is required");
ClassRepresentation classRepresentation = getClassRepresentations().get(entityClass);
FieldRepresentation idField = classRepresentation.getId()
.orElseThrow(() -> IdNotFoundException.newInstance(entityClass));
Object value = ConverterUtil.getValue(id, classRepresentation, idField.getFieldName(), getConverters());
ColumnQuery query = ColumnQueryBuilder.select().from(classRepresentation.getName())
.where(idField.getName()).eq(value).build();
return singleResult(query);
}
@Override
public void delete(Class entityClass, ID id) {
requireNonNull(entityClass, "entityClass is required");
requireNonNull(id, "id is required");
ClassRepresentation classRepresentation = getClassRepresentations().get(entityClass);
FieldRepresentation idField = classRepresentation.getId()
.orElseThrow(() -> IdNotFoundException.newInstance(entityClass));
Object value = ConverterUtil.getValue(id, classRepresentation, idField.getFieldName(), getConverters());
ColumnDeleteQuery query = ColumnQueryBuilder.delete().from(classRepresentation.getName())
.where(idField.getName()).eq(value).build();
getManager().delete(query);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy