001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.persistence;
018
019import com.echothree.model.data.core.server.entity.EntityInstance;
020import com.echothree.util.common.exception.PersistenceDatabaseException;
021import com.echothree.util.common.persistence.BasePK;
022import java.sql.SQLException;
023
024public class PersistenceUtils {
025    
026    private PersistenceUtils() {
027        super();
028    }
029    
030    private static class PersistenceUtilsHolder {
031        static PersistenceUtils instance = new PersistenceUtils();
032    }
033    
034    public static PersistenceUtils getInstance() {
035        return PersistenceUtilsHolder.instance;
036    }
037
038    private final static String SqlState_IntegrityConstraintViolation = "23000";
039    
040    public boolean isIntegrityConstraintViolation(PersistenceDatabaseException pde) {
041        var result = false;
042        var cause = pde.getCause();
043
044        if(cause instanceof SQLException se) {
045            if(se.getSQLState().equals(SqlState_IntegrityConstraintViolation)) {
046                result = true;
047            }
048        }
049        
050        return result;
051    }
052    
053    public BasePK getBasePKFromEntityInstance(EntityInstance entityInstance) {
054        var entityTypeDetail = entityInstance.getEntityType().getLastDetail();
055
056        return new BasePK(entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(),
057                entityTypeDetail.getEntityTypeName(), entityInstance.getEntityUniqueId());
058    }
059    
060}