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