Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2014-2023 the original author or authors.
*
* 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 org.dbflute.bhv.exception;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.dbflute.bhv.core.context.ConditionBeanContext;
import org.dbflute.bhv.core.context.InternalMapContext;
import org.dbflute.bhv.core.context.InternalMapContext.InvokePathProvider;
import org.dbflute.bhv.core.context.ResourceContext;
import org.dbflute.bhv.core.context.ResourceParameter;
import org.dbflute.bhv.core.context.logmask.BehaviorLogMaskProvider;
import org.dbflute.cbean.ConditionBean;
import org.dbflute.dbway.DBDef;
import org.dbflute.exception.EntityAlreadyExistsException;
import org.dbflute.exception.SQLFailureException;
import org.dbflute.helper.message.ExceptionMessageBuilder;
import org.dbflute.outsidesql.OutsideSqlContext;
import org.dbflute.system.DBFluteSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author jflute
*/
public class SQLExceptionHandler {
// ===================================================================================
// Definition
// ==========
private static final Logger _log = LoggerFactory.getLogger(SQLExceptionHandler.class);
// ===================================================================================
// Attribute
// =========
protected final SQLExceptionAdviser _adviser = createAdviser();
// ===================================================================================
// Handle
// ======
/**
* @param e The instance of SQLException. (NotNull)
* @param resource The resource, item and elements, of SQLException message. (NotNull)
*/
public void handleSQLException(SQLException e, SQLExceptionResource resource) {
if (resource.isUniqueConstraintHandling() && isUniqueConstraintException(e)) {
throwEntityAlreadyExistsException(e, resource);
} else {
throwSQLFailureException(e, resource);
}
}
protected boolean isUniqueConstraintException(SQLException e) {
if (!ResourceContext.isExistResourceContextOnThread()) {
return false;
}
return ResourceContext.isUniqueConstraintException(extractSQLState(e), e.getErrorCode());
}
// ===================================================================================
// Throw
// =====
protected void throwEntityAlreadyExistsException(SQLException e, SQLExceptionResource resource) {
final ExceptionMessageBuilder br = createExceptionMessageBuilder();
br.addNotice("The entity already exists on the database.");
br.addItem("Advice");
br.addElement("Please confirm the primary key whether it already exists on the database.");
br.addElement("And also confirm the unique constraint for other columns.");
setupCommonElement(br, e, resource);
final String msg = br.buildExceptionMessage();
throw new EntityAlreadyExistsException(msg, e);
}
protected void throwSQLFailureException(SQLException e, SQLExceptionResource resource) {
final ExceptionMessageBuilder br = createExceptionMessageBuilder();
final List noticeList = resource.getNoticeList();
if (!noticeList.isEmpty()) {
for (String notice : noticeList) {
br.addNotice(notice);
}
} else {
br.addNotice("The SQL failed to execute."); // as default
}
br.addItem("Advice");
br.addElement("Read the SQLException message.");
final String advice = askAdvice(e, ResourceContext.currentDBDef());
if (advice != null && advice.trim().length() > 0) {
br.addElement("*" + advice);
}
setupCommonElement(br, e, resource);
final String msg = br.buildExceptionMessage();
throw new SQLFailureException(msg, e);
}
protected ExceptionMessageBuilder createExceptionMessageBuilder() {
return new ExceptionMessageBuilder();
}
protected String askAdvice(SQLException e, DBDef dbdef) {
return _adviser.askAdvice(e, dbdef);
}
// ===================================================================================
// Element
// =======
protected void setupCommonElement(ExceptionMessageBuilder br, SQLException e, SQLExceptionResource resource) {
br.addItem("SQLState");
br.addElement(extractSQLState(e));
br.addItem("ErrorCode");
br.addElement(e.getErrorCode());
setupSQLExceptionElement(br, e);
final Map> resourceMap = resource.getResourceMap();
for (Entry> entry : resourceMap.entrySet()) {
br.addItem(entry.getKey());
final List