info.archinnov.achilles.internals.runtime.AbstractManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of achilles-core Show documentation
Show all versions of achilles-core Show documentation
CQL implementation for Achilles using Datastax Java driver
/*
* Copyright (C) 2012-2016 DuyHai DOAN
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package info.archinnov.achilles.internals.runtime;
import static info.archinnov.achilles.internals.runtime.BeanInternalValidator.validateColumnsForInsertStatic;
import static info.archinnov.achilles.internals.runtime.BeanInternalValidator.validatePrimaryKey;
import static info.archinnov.achilles.internals.statement.StatementHelper.isSelectStatement;
import static info.archinnov.achilles.validation.Validator.*;
import static java.lang.String.format;
import java.util.Optional;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.datastax.driver.core.*;
import info.archinnov.achilles.internals.dsl.crud.InsertJSONWithOptions;
import info.archinnov.achilles.internals.metamodel.AbstractEntityProperty;
import info.archinnov.achilles.internals.dsl.crud.DeleteWithOptions;
import info.archinnov.achilles.internals.dsl.crud.InsertWithOptions;
import info.archinnov.achilles.internals.dsl.raw.NativeQuery;
import info.archinnov.achilles.internals.dsl.raw.TypedQuery;
import info.archinnov.achilles.type.tuples.Tuple2;
public abstract class AbstractManager {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractManager.class);
protected final Class entityClass;
protected final RuntimeEngine rte;
protected final AbstractEntityProperty meta_internal;
public AbstractManager(Class entityClass, AbstractEntityProperty meta_internal, RuntimeEngine rte) {
this.entityClass = entityClass;
this.meta_internal = meta_internal;
this.rte = rte;
}
/**
* Map a given row back to an entity instance.
* This method provides a raw object mapping facility. Advanced features like interceptors are not available.
* User codecs are taken into account though.
*
* @param row given Cassandra row
* @return entity instance
*/
public ENTITY mapFromRow(Row row) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(format("Map row %s back to entity of type %s", row, entityClass.getCanonicalName()));
}
validateNotNull(row, "Row object should not be null");
final String tableName = row.getColumnDefinitions().asList().get(0).getTable();
final String entityTableName = meta_internal.getTableOrViewName();
validateTableTrue(entityTableName.equals(tableName),
"CQL row is from table '%s', it cannot be mapped to entity '%s' associated to table '%s'",
tableName, entityClass.getCanonicalName(), entityTableName);
return meta_internal.createEntityFrom(row);
}
/**
* Return the native Session object used by this Manager
*
* @return {@link com.datastax.driver.core.Session} instance used by this Manager
*/
public Session getNativeSession() {
return rte.session;
}
/**
* Return the native Cluster object used by this Manager
*
* @return {@link com.datastax.driver.core.Cluster} instance used by this Manager
*/
public Cluster getNativeCluster() {
return rte.getCluster();
}
protected InsertWithOptions insertInternal(ENTITY instance, boolean insertStatic) {
validateNotNull(instance, "Entity to be inserted should not be null");
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(format("Create insert CRUD for entity %s", instance));
}
if (insertStatic) {
validateColumnsForInsertStatic(instance, meta_internal);
} else {
validatePrimaryKey(instance, meta_internal);
}
return new InsertWithOptions<>(meta_internal, rte, instance, insertStatic);
}
protected InsertJSONWithOptions insertJSONInternal(String json) {
validateNotBlank(json, "The JSON string to be used for INSERT JSON should not be blank");
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(format("Create insert json CRUD for with JSON %s", json));
}
return new InsertJSONWithOptions(meta_internal, rte, json);
}
protected DeleteWithOptions deleteInternal(ENTITY instance) {
validateNotNull(instance, "Entity to be deleted should not be null");
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(format("Create delete CRUD for entity %s", instance));
}
validatePrimaryKey(instance, meta_internal);
final Tuple2
© 2015 - 2024 Weber Informatics LLC | Privacy Policy