org.smooks.cartridges.persistence.jdbc.ResultSetRowSelector Maven / Gradle / Ivy
/*-
* ========================LICENSE_START=================================
* smooks-persistence-cartridge
* %%
* Copyright (C) 2020 Smooks
* %%
* Licensed under the terms of the Apache License Version 2.0, or
* the GNU Lesser General Public License version 3.0 or later.
*
* SPDX-License-Identifier: Apache-2.0 OR LGPL-3.0-or-later
*
* ======================================================================
*
* 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.
*
* ======================================================================
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* =========================LICENSE_END==================================
*/
package org.smooks.cartridges.persistence.jdbc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smooks.api.ApplicationContext;
import org.smooks.api.ExecutionContext;
import org.smooks.api.SmooksConfigException;
import org.smooks.api.SmooksException;
import org.smooks.api.bean.context.BeanContext;
import org.smooks.api.bean.context.BeanIdStore;
import org.smooks.api.bean.repository.BeanId;
import org.smooks.api.delivery.ordering.Consumer;
import org.smooks.api.delivery.ordering.Producer;
import org.smooks.api.expression.ExpressionEvaluator;
import org.smooks.api.resource.visitor.VisitAfterIf;
import org.smooks.api.resource.visitor.VisitBeforeIf;
import org.smooks.api.resource.visitor.sax.ng.AfterVisitor;
import org.smooks.api.resource.visitor.sax.ng.BeforeVisitor;
import org.smooks.assertion.AssertArgument;
import org.smooks.engine.delivery.fragment.NodeFragment;
import org.smooks.engine.expression.MVELExpressionEvaluator;
import org.smooks.support.FreeMarkerTemplate;
import org.w3c.dom.Element;
import jakarta.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author [email protected]
*/
@VisitBeforeIf(condition = "executeBefore")
@VisitAfterIf(condition = "!executeBefore")
public class ResultSetRowSelector implements BeforeVisitor, AfterVisitor, Producer, Consumer {
private static final Logger LOGGER = LoggerFactory.getLogger(ResultSetRowSelector.class);
@Inject
private String resultSetName;
@Inject
@Named("where")
private ExpressionEvaluator whereEvaluator;
@Inject
private Optional failedSelectError;
@Inject
@Named("beanId")
private String beanId;
@Inject
private Boolean executeBefore = true;
private BeanId resultSetBeanId;
private BeanId beanIdObj;
@Inject
private ApplicationContext appContext;
public ResultSetRowSelector setResultSetName(String resultSetName) {
AssertArgument.isNotNullAndNotEmpty(resultSetName, "resultSetName");
this.resultSetName = resultSetName;
return this;
}
public ResultSetRowSelector setSelector(SQLExecutor executor) {
AssertArgument.isNotNull(executor, "executor");
this.resultSetName = executor.getResultSetName();
if (this.resultSetName == null) {
throw new IllegalArgumentException("Invalid 'executor' argument. Executor must specify a 'resultSetName' in order to be used by a ResultsetRowSelector.");
}
return this;
}
public ResultSetRowSelector setWhereClause(String whereClause) {
AssertArgument.isNotNullAndNotEmpty(whereClause, "whereClause");
this.whereEvaluator = new MVELExpressionEvaluator();
this.whereEvaluator.setExpression(whereClause);
return this;
}
public ResultSetRowSelector setWhereEvaluator(ExpressionEvaluator whereEvaluator) {
AssertArgument.isNotNull(whereEvaluator, "whereEvaluator");
this.whereEvaluator = whereEvaluator;
return this;
}
public ResultSetRowSelector setFailedSelectError(String failedSelectError) {
AssertArgument.isNotNullAndNotEmpty(failedSelectError, "failedSelectError");
this.failedSelectError = Optional.of(new FreeMarkerTemplate(failedSelectError));
return this;
}
public ResultSetRowSelector setBeanId(String beanId) {
AssertArgument.isNotNullAndNotEmpty(beanId, "beanId");
this.beanId = beanId;
return this;
}
public ResultSetRowSelector setExecuteBefore(boolean executeBefore) {
this.executeBefore = executeBefore;
return this;
}
public boolean getExecuteBefore() {
return executeBefore;
}
@PostConstruct
public void postConstruct() throws SmooksConfigException {
BeanIdStore beanIdStore = appContext.getBeanIdStore();
beanIdObj = beanIdStore.register(beanId);
resultSetBeanId = beanIdStore.register(resultSetName);
}
public Set extends Object> getProducts() {
return Stream.of(beanId).collect(Collectors.toSet());
}
public boolean consumes(Object object) {
if (object.equals(resultSetName)) {
return true;
} else if (whereEvaluator != null && whereEvaluator.getExpression().contains(object.toString())) {
return true;
} else {
return failedSelectError != null && failedSelectError.isPresent() && failedSelectError.get().getTemplateText().contains(object.toString());
}
}
@Override
public void visitBefore(Element element, ExecutionContext executionContext) throws SmooksException {
selectRow(executionContext, new NodeFragment(element));
}
@Override
public void visitAfter(Element element, ExecutionContext executionContext) throws SmooksException {
selectRow(executionContext, new NodeFragment(element));
}
private void selectRow(ExecutionContext executionContext, NodeFragment source) throws SmooksException {
BeanContext beanRepository = executionContext.getBeanContext();
Map beanMapClone = new HashMap<>(beanRepository.getBeanMap());
// Lookup the new current value for the bean...
try {
@SuppressWarnings("unchecked")
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy