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.util.common.exception.PersistenceException; 020import com.echothree.util.common.exception.PersistenceReadOnlyException; 021import com.echothree.util.common.persistence.BasePK; 022 023public abstract class BaseEntity { 024 025 static private final String INVALID_CONSTRUCTOR_EXCEPTION = "Invalid Constructor"; 026 static private final String READ_ONLY_EXCEPTION = "Entity is Read-Only"; 027 028 protected EntityPermission _entityPermission; 029 030 /** Creates a new instance of BaseEntity */ 031 protected BaseEntity() { 032 throw new PersistenceException(INVALID_CONSTRUCTOR_EXCEPTION); 033 } 034 035 /** Creates a new instance of BaseEntity */ 036 protected BaseEntity(EntityPermission entityPermission) { 037 _entityPermission = entityPermission; 038 } 039 040 public EntityPermission getEntityPermission() { 041 return _entityPermission; 042 } 043 044 protected void checkReadWrite() 045 throws PersistenceReadOnlyException { 046 if(_entityPermission.equals(EntityPermission.READ_ONLY)) 047 throw new PersistenceReadOnlyException(READ_ONLY_EXCEPTION); 048 } 049 050 public void store() { 051 store(ThreadSession.currentSession()); 052 } 053 054 public abstract void store(Session session); 055 056 public void remove() { 057 remove(ThreadSession.currentSession()); 058 } 059 060 public abstract void remove(Session session); 061 062 public abstract BasePK getPrimaryKey(); 063 064 public abstract BaseFactory getBaseFactoryInstance(); 065 066 public abstract boolean hasBeenModified(); 067 068}