All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jporm.rx.session.SessionImpl Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright 2015 Francesco Cina'
 *
 * 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 com.jporm.rx.session;

import java.util.concurrent.CompletableFuture;

import com.jporm.annotation.mapper.clazz.ClassDescriptor;
import com.jporm.commons.core.connection.AsyncConnectionProvider;
import com.jporm.commons.core.exception.JpoException;
import com.jporm.commons.core.inject.ClassTool;
import com.jporm.commons.core.inject.ClassToolMap;
import com.jporm.commons.core.inject.ServiceCatalog;
import com.jporm.commons.core.query.SqlFactory;
import com.jporm.commons.core.query.cache.SqlCache;
import com.jporm.persistor.Persistor;
import com.jporm.rx.query.delete.CustomDeleteQuery;
import com.jporm.rx.query.delete.CustomDeleteQueryImpl;
import com.jporm.rx.query.delete.DeleteQueryImpl;
import com.jporm.rx.query.delete.DeleteResult;
import com.jporm.rx.query.find.CustomFindQuery;
import com.jporm.rx.query.find.CustomFindQueryImpl;
import com.jporm.rx.query.find.CustomResultFindQueryBuilder;
import com.jporm.rx.query.find.CustomResultFindQueryBuilderImpl;
import com.jporm.rx.query.find.FindQuery;
import com.jporm.rx.query.find.FindQueryImpl;
import com.jporm.rx.query.save.CustomSaveQuery;
import com.jporm.rx.query.save.CustomSaveQueryImpl;
import com.jporm.rx.query.save.SaveQueryImpl;
import com.jporm.rx.query.update.CustomUpdateQuery;
import com.jporm.rx.query.update.CustomUpdateQueryImpl;
import com.jporm.rx.query.update.UpdateQueryImpl;
import com.jporm.sql.dialect.DBProfile;

public class SessionImpl implements Session {

    private final ServiceCatalog serviceCatalog;
    private final ClassToolMap classToolMap;
    private final SqlFactory sqlFactory;
    private final DBProfile dbType;
    private final SqlCache sqlCache;
    private final SqlSession sqlSession;

    public SessionImpl(final ServiceCatalog serviceCatalog, final AsyncConnectionProvider connectionProvider, final boolean autoCommit, SqlCache sqlCache,
            SqlFactory sqlFactory) {
        this.serviceCatalog = serviceCatalog;
        this.sqlCache = sqlCache;
        this.sqlFactory = sqlFactory;
        classToolMap = serviceCatalog.getClassToolMap();
        dbType = connectionProvider.getDBProfile();
        sqlSession = new SqlSessionImpl(new SqlExecutorImpl(serviceCatalog.getTypeFactory(), connectionProvider, autoCommit), sqlFactory.getSqlDsl());
    }

    @Override
    public  CompletableFuture delete(final BEAN bean) {
        Class typedClass = (Class) bean.getClass();
        return new DeleteQueryImpl<>(bean, typedClass, serviceCatalog.getClassToolMap().get(typedClass), sqlCache, sql().executor()).execute();
    }

    @Override
    public  CustomDeleteQuery delete(final Class clazz) throws JpoException {
        return new CustomDeleteQueryImpl(sqlFactory.deleteFrom(clazz), sql().executor());
    }

    /**
     * Returns whether a bean has to be saved. Otherwise it has to be updated
     * because it already exists.
     *
     * @return
     */
    private  CompletableFuture exist(final BEAN bean, final Persistor persistor) {
        if (persistor.hasGenerator()) {
            return CompletableFuture.completedFuture(!persistor.useGenerators(bean));
        } else {
            return findByModelId(bean).exist();
        }
    }

    @Override
    public final  CustomFindQuery find(final Class clazz) throws JpoException {
        return find(clazz, clazz.getSimpleName());
    }

    private final  FindQuery find(final Class clazz, final Object... pkFieldValues) throws JpoException {
        return new FindQueryImpl<>(clazz, pkFieldValues, serviceCatalog.getClassToolMap().get(clazz), sql().executor(), sqlFactory, sqlCache);
    }

    @Override
    public final  CustomFindQuery find(final Class clazz, final String alias) throws JpoException {
        return new CustomFindQueryImpl<>(clazz, alias, serviceCatalog.getClassToolMap().get(clazz), sql().executor(), sqlFactory);
    }

    @Override
    public  CustomResultFindQueryBuilder find(final String... selectFields) {
        return new CustomResultFindQueryBuilderImpl(selectFields, sql().executor(), sqlFactory);
    }

    @Override
    public final  FindQuery findById(final Class clazz, final Object value) throws JpoException {
        return this.find(clazz, value);
    }

    @Override
    public final  FindQuery findByModelId(final BEAN model) throws JpoException {
        Class modelClass = (Class) model.getClass();
        ClassTool ormClassTool = classToolMap.get(modelClass);
        ClassDescriptor descriptor = ormClassTool.getDescriptor();
        String[] pks = descriptor.getPrimaryKeyColumnJavaNames();
        Object[] values = ormClassTool.getPersistor().getPropertyValues(pks, model);
        return find(modelClass, values);
    }

    @Override
    public  CompletableFuture save(final BEAN bean) {
        try {
            serviceCatalog.getValidatorService().validateThrowException(bean);
        } catch (Exception e) {
            CompletableFuture validate = new CompletableFuture();
            validate.completeExceptionally(e);
            return validate;
        }
        Class typedClass = (Class) bean.getClass();
        return new SaveQueryImpl<>(bean, typedClass, serviceCatalog.getClassToolMap().get(typedClass), sqlCache, sql().executor(), sqlFactory, dbType).execute();
    }

    @Override
    public  CustomSaveQuery save(final Class clazz, final String... fields) throws JpoException {
        return new CustomSaveQueryImpl<>(sqlFactory.insertInto(clazz, fields), sql().executor());
    }

    @Override
    public  CompletableFuture saveOrUpdate(final BEAN bean) {
        try {
            serviceCatalog.getValidatorService().validateThrowException(bean);
        } catch (Exception e) {
            CompletableFuture validate = new CompletableFuture();
            validate.completeExceptionally(e);
            return validate;
        }
        Persistor persistor = (Persistor) serviceCatalog.getClassToolMap().get(bean.getClass()).getPersistor();
        return exist(bean, persistor).thenCompose(exists -> {
            if (exists) {
                return update(bean);
            }
            return save(bean);
        });
    }

    @Override
    public SqlSession sql() {
        return sqlSession;
    }

    @Override
    public  CompletableFuture update(final BEAN bean) {
        try {
            serviceCatalog.getValidatorService().validateThrowException(bean);
        } catch (Exception e) {
            CompletableFuture validate = new CompletableFuture();
            validate.completeExceptionally(e);
            return validate;
        }
        Class typedClass = (Class) bean.getClass();
        return new UpdateQueryImpl<>(bean, typedClass, serviceCatalog.getClassToolMap().get(typedClass), sqlCache, sql().executor()).execute();
    }

    @Override
    public  CustomUpdateQuery update(final Class clazz) throws JpoException {
        return new CustomUpdateQueryImpl(sqlFactory.update(clazz), sql().executor());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy