workflowalfrescoactivitialfresco-enterpriseactiviti-cloud

Alfresco Process Services override DbSqlSessionFactory?


I am using Alfresco Process Services 24.2 Spring version - 6.1.5

I have overridden DbSqlSessionFactory in my custom code using below code CustomDbSqlSessionFactory

package com.activiti.extension.bean.extension.db;

import lombok.RequiredArgsConstructor;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.impl.db.DbSqlSession;
import org.activiti.engine.impl.db.DbSqlSessionFactory;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.interceptor.Session;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

@Component
@RequiredArgsConstructor
public class CustomDbSqlSessionFactory extends DbSqlSessionFactory {

    private final ObjectFactory<CustomDbSqlSession> dbSqlSession;

    @Override
    public Session openSession(CommandContext commandContext) {
        return super.openSession(commandContext);
    }

}

And CustomDbSqlSession

package com.activiti.extension.bean.extension.db;

import org.activiti.engine.impl.db.DbSqlSession;
import org.activiti.engine.impl.persistence.entity.Entity;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;

import static java.util.stream.Collectors.toList;
import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;

@Slf4j
@Component
@Scope(SCOPE_PROTOTYPE)
public class CustomDbSqlSession extends DbSqlSession {

    public CustomDbSqlSession(CustomDbSqlSessionFactory dbSqlSessionFactory, CustomEntityCache entityCache) {
        super(dbSqlSessionFactory, entityCache);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public List selectListWithRawParameter(String statement, Object parameter, int firstResult, int maxResults) {
        return super.selectListWithRawParameter(statement, parameter, firstResult, maxResults);

    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public List selectListWithRawParameterWithoutFilter(String statement, Object parameter, int firstResult, int maxResults) {
       return super.selectListWithRawParameterWithoutFilter(statement, parameter, firstResult, maxResults);

    }

    @Override
    protected void flushRegularInsert(Entity persistentObject, Class<? extends Entity> clazz) {
        super.flushRegularInsert(persistentObject, clazz);
    }

    @Override
    protected void flushBulkInsert(Collection<Entity> persistentObjectList, Class<? extends Entity> clazz) {
        super.flushBulkInsert(persistentObjectList, clazz);
    }

    @Override
    protected void flushUpdates() {
        super.flushUpdates();
    }
}

I am able to compile and create build, After deploying changes to APS I am able to create and execute process . But in CustomDbSqlSession class, I have overridden few DbSqlSession's methods and that are not getting executed. I tried by adding logs and debuggers in Intellij Idea it seems flow is not executing in custom overridden method, it's calling the default DbSqlSession's method.

I wanted to execute the CustomDbSqlSession's overridden methods.

Can anyone please help here?


Solution

  • I fixed it using CustomDbSqlSessionFactory class

    package com.activiti.extension.bean.extension.db;
    
    import com.activiti.extension.bean.service.encryption.VariableEncryptionService;
    import lombok.RequiredArgsConstructor;
    import org.activiti.engine.impl.db.DbSqlSessionFactory;
    import org.activiti.engine.impl.interceptor.CommandContext;
    import org.activiti.engine.impl.interceptor.Session;
    import org.springframework.stereotype.Component;
    
    @Component
    @RequiredArgsConstructor
    public class CustomDbSqlSessionFactory extends DbSqlSessionFactory {
    
        private final VariableEncryptionService variableEncryptionService;
    
        @Override
        public Session openSession(CommandContext commandContext) {
           return new CustomDbSqlSession(this, commandContext, variableEncryptionService);
        }
    
    }
    

    Custom CustomDbSqlSession class

    package com.activiti.extension.bean.extension.db;
    
    import com.activiti.extension.bean.service.encryption.VariableEncryptionService;
    import lombok.extern.slf4j.Slf4j;
    import org.activiti.engine.ActivitiException;
    import org.activiti.engine.ActivitiOptimisticLockingException;
    import org.activiti.engine.impl.db.DbSqlSession;
    import org.activiti.engine.impl.db.HasRevision;
    import org.activiti.engine.impl.interceptor.CommandContext;
    import org.activiti.engine.impl.persistence.entity.Entity;
    import org.springframework.context.annotation.Scope;
    
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.List;
    
    import static java.util.stream.Collectors.toList;
    import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;
    
    @Slf4j
    //@Component
    @Scope(SCOPE_PROTOTYPE)
    public class CustomDbSqlSession extends DbSqlSession {
    
        private final VariableEncryptionService variableEncryptionService;
    
        public CustomDbSqlSession(CustomDbSqlSessionFactory dbSqlSessionFactory, CommandContext commandContext,
                                  VariableEncryptionService variableEncryptionService) {
            super(dbSqlSessionFactory, commandContext.getEntityCache());
            this.variableEncryptionService = variableEncryptionService;
        }
    
        private <T> void encrypt(T value) {
            variableEncryptionService.encrypt(value);
        }
    
        private <T> T decrypt(T value) {
            return variableEncryptionService.decrypt(value);
        }
    
        @SuppressWarnings({"rawtypes", "unchecked"})
        @Override
        public List selectListWithRawParameter(String statement, Object parameter, int firstResult, int maxResults) {
            List<Object> result = super.selectListWithRawParameter(statement, parameter, firstResult, maxResults);
    
            return super.cacheLoadOrStore(result.stream()
                    .map(this::decrypt)
                    .collect(toList()));
        }
    
        @SuppressWarnings({"rawtypes", "unchecked"})
        @Override
        public List selectListWithRawParameterWithoutFilter(String statement, Object parameter, int firstResult, int maxResults) {
            List<Object> result = super.selectListWithRawParameterWithoutFilter(statement, parameter, firstResult, maxResults);
            return result.stream()
                    .map(this::decrypt)
                    .collect(toList());
        }
    
        @SuppressWarnings({"rawtypes", "unchecked"})
        @Override
        public List selectListWithRawParameter(String statement, Object parameter, int firstResult, int maxResults, boolean useCache) {
            List<Object> result = super.selectListWithRawParameter(statement, parameter, firstResult, maxResults, useCache);
    
            return super.cacheLoadOrStore(result.stream()
                    .map(this::decrypt)
                    .collect(toList()));
        }
    
        @Override
        protected void flushRegularInsert(Entity persistentObject, Class<? extends Entity> clazz) {
            encrypt(persistentObject);
            super.flushRegularInsert(persistentObject, clazz);
        }
    
        @Override
        protected void flushBulkInsert(Collection<Entity> persistentObjectList, Class<? extends Entity> clazz) {
            persistentObjectList.forEach(this::encrypt);
            super.flushBulkInsert(persistentObjectList, clazz);
        }
    
        @Override
        protected void flushUpdates() {
            super.updatedObjects.forEach(this::encrypt);
            super.flushUpdates();
          }
    
    }