001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.util.server.transfer;
018
019import com.echothree.util.common.string.StringUtils;
020import com.echothree.util.common.form.TransferProperties;
021import com.echothree.util.common.transfer.BaseTransfer;
022import de.odysseus.el.ExpressionFactoryImpl;
023import de.odysseus.el.util.SimpleContext;
024import java.util.Collection;
025import java.util.HashSet;
026import java.util.Set;
027import javax.el.ExpressionFactory;
028
029public abstract class BaseWrapperBuilder {
030    
031    protected <E> Class getContainedClass(Collection<E> collection) {
032        Class listContains = null;
033
034        if(collection.size() > 0) {
035            Object firstObject = collection.iterator().next();
036
037            if(firstObject instanceof BaseTransfer) {
038                listContains = firstObject.getClass();
039            }
040        }
041        
042        return listContains;
043    }
044    
045    protected <E> Set<E> getObjectsToRemove(TransferProperties transferProperties, Collection<E> collection) {
046        Set<E> objectsToRemove = null;
047        
048        if(transferProperties != null) {
049            Class listContains = getContainedClass(collection);
050            
051            if(listContains != null) {
052                String expression = transferProperties.getExpression(listContains);
053                
054                if(expression != null) {
055                    ExpressionFactory expressionFactory = new ExpressionFactoryImpl();
056                    String simpleName = StringUtils.getInstance().lowerCaseFirstCharacter(listContains.getSimpleName());
057                    String variableName = simpleName.substring(0, simpleName.length() - 8);
058                    
059                    objectsToRemove = new HashSet<>();
060
061                    for(var object : collection) {
062                        SimpleContext simpleContext = new SimpleContext();
063
064                        simpleContext.setVariable(variableName, expressionFactory.createValueExpression(object, object.getClass()));
065
066                        if(!(Boolean)expressionFactory.createValueExpression(simpleContext, expression, Boolean.class).getValue(simpleContext)) {
067                            objectsToRemove.add(object);
068                        }
069                    }
070                }
071            }
072        }
073        
074        return objectsToRemove;
075    }
076    
077    protected <E> void filterCollection(TransferProperties transferProperties, Collection<E> collection) {
078        Set<E> objectsToRemove = getObjectsToRemove(transferProperties, collection);
079        
080        if(objectsToRemove != null) {
081            collection.removeAll(objectsToRemove);
082        }
083    }
084    
085}