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

win.doyto.query.service.AbstractDynamicService Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
// Generated by delombok at Wed Nov 06 09:48:14 UTC 2024
/*
 * Copyright © 2019-2024 Forb Yuan
 *
 * 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 win.doyto.query.service;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.SimpleTransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionOperations;
import org.springframework.transaction.support.TransactionTemplate;
import win.doyto.query.annotation.Entity;
import win.doyto.query.annotation.EntityType;
import win.doyto.query.core.DataAccess;
import win.doyto.query.core.DoytoQuery;
import win.doyto.query.core.IdWrapper;
import win.doyto.query.entity.EntityAspect;
import win.doyto.query.entity.Persistable;
import win.doyto.query.entity.UserIdProvider;
import win.doyto.query.util.BeanUtil;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * AbstractDynamicService
 *
 * @author f0rb on 2019-05-28
 */
@SuppressWarnings("java:S6813")
public abstract class AbstractDynamicService, I extends Serializable, Q extends DoytoQuery> implements DynamicService, InitializingBean {
    protected DataAccess dataAccess;
    protected final Class entityClass;
    @Autowired(required = false)
    private UserIdProvider userIdProvider;
    @Autowired(required = false)
    private CacheManager cacheManager;
    @Lazy
    @Autowired(required = false)
    protected List> entityAspects = new LinkedList<>();
    protected TransactionOperations transactionOperations = NoneTransactionOperations.instance;
    private List cacheList;
    @Resource
    private BeanFactory beanFactory;

    @SuppressWarnings("unchecked")
    protected AbstractDynamicService() {
        entityClass = (Class) BeanUtil.getActualTypeArguments(getConcreteClass())[0];
        dataAccess = new MemoryDataAccess<>(entityClass);
    }

    protected Class getConcreteClass() {
        return getClass();
    }

    @Autowired(required = false)
    public void setTransactionManager(PlatformTransactionManager transactionManager) {
        transactionOperations = new TransactionTemplate(transactionManager);
    }

    @Value("${doyto.query.caches:}")
    public void setCacheList(String caches) {
        cacheList = Arrays.stream(caches.split("[,\\s]")).filter(s -> !s.isEmpty()).toList();
    }

    protected String getCacheName() {
        return entityClass.getSimpleName().intern();
    }

    private EntityType getEntityType() {
        Entity entity = entityClass.getAnnotation(Entity.class);
        return entity != null ? entity.type() : EntityType.RELATIONAL;
    }

    @SuppressWarnings({"java:S4973", "StringEquality"})
    @Override
    public void afterPropertiesSet() {
        try {
            if (beanFactory != null) {
                EntityType entityType = getEntityType();
                dataAccess = DataAccessManager.create(entityType, beanFactory, entityClass);
            }
            if (!entityAspects.isEmpty()) {
                dataAccess = new AspectDataAccess<>(dataAccess, entityAspects, transactionOperations);
            }
            if (userIdProvider != null) {
                dataAccess = new UserIdDataAccess<>(dataAccess, userIdProvider);
            }
            if (cacheManager != null) {
                String cacheName = getCacheName();
                if (cacheList.contains(cacheName) || cacheName != entityClass.getSimpleName().intern()) {
                    dataAccess = new CachedDataAccess<>(dataAccess, cacheManager, cacheName);
                }
            }
        } catch (Exception e) {
            throw new BeanInitializationException("Failed to create DataAccess for " + entityClass.getName(), e);
        }
    }

    @Override
    public int create(Collection entities, String... columns) {
        return dataAccess.batchInsert(entities, columns);
    }

    @Override
    public E fetch(IdWrapper w) {
        if (dataAccess instanceof CachedDataAccess) {
            return ((CachedDataAccess) dataAccess).delegate.get(w);
        }
        return dataAccess.get(w);
    }

    @Override
    public E remove(IdWrapper w) {
        E e = get(w);
        if (e != null) {
            dataAccess.delete(w);
        }
        return e;
    }


    private static class NoneTransactionOperations implements TransactionOperations {
        private static final TransactionOperations instance = new NoneTransactionOperations();
        private static final TransactionStatus TRANSACTION_STATUS = new SimpleTransactionStatus();

        @Override
        public  T execute(TransactionCallback transactionCallback) {
            return transactionCallback.doInTransaction(TRANSACTION_STATUS);
        }
    }


    @SuppressWarnings({"unused", "java:S1610"})
    abstract class ExcludedDataAccess {
        public abstract void get(I id);

        public abstract void delete(I id);
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public void setCacheManager(final CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public java.util.List query(final Q query) {
        return this.dataAccess.query(query);
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public long count(final Q query) {
        return this.dataAccess.count(query);
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public win.doyto.query.core.PageList page(final Q query) {
        return this.dataAccess.page(query);
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public  java.util.List queryColumns(final Q q, final java.lang.Class clazz, final java.lang.String... columns) {
        return this.dataAccess.queryColumns(q, clazz, columns);
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public E get(final win.doyto.query.core.IdWrapper w) {
        return this.dataAccess.get(w);
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public int delete(final win.doyto.query.core.IdWrapper w) {
        return this.dataAccess.delete(w);
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public int delete(final Q query) {
        return this.dataAccess.delete(query);
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public void create(final E e) {
        this.dataAccess.create(e);
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public int batchInsert(final java.lang.Iterable entities, final java.lang.String... columns) {
        return this.dataAccess.batchInsert(entities, columns);
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public int update(final E e) {
        return this.dataAccess.update(e);
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public int patch(final E e) {
        return this.dataAccess.patch(e);
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public int patch(final E e, final Q q) {
        return this.dataAccess.patch(e, q);
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public java.util.List queryIds(final Q query) {
        return this.dataAccess.queryIds(query);
    }
}