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.PersistenceNotNullException;
020import com.echothree.util.common.persistence.BasePK;
021
022public abstract class BaseValue<PK extends BasePK> {
023    
024    protected Long entityId;
025    protected PK _primaryKey;
026    
027    /** Creates a new instance of BaseValue */
028    protected BaseValue(PK basePK) {
029        entityId = basePK.getEntityId();
030        _primaryKey = basePK;
031    }
032    
033    /** Creates a new instance of BaseValue */
034    protected BaseValue() {
035        entityId = null;
036        _primaryKey = null;
037    }
038    
039    public abstract BaseFactory getBaseFactoryInstance();
040    
041    protected boolean hasIdentity() {
042        return entityId != null;
043    }
044    
045    public abstract PK getPrimaryKey();
046    
047    public Long getEntityId() {
048        return entityId;
049    }
050    
051    public void setEntityId(Long entityId) {
052        this.entityId = entityId;
053        _primaryKey = null;
054    }
055    
056    protected void checkForNull(Object o)
057            throws PersistenceNotNullException {
058        boolean isNull = false;
059        
060        if(o == null) {
061            isNull = true;
062        } else {
063            if(o instanceof String) {
064                if(((String)o).length() == 0)
065                    isNull = true;
066            }
067        }
068        
069        if(isNull) {
070            throw new PersistenceNotNullException();
071        }
072    }
073    
074    public abstract boolean hasBeenModified();
075    
076    public abstract void clearHasBeenModified();
077    
078}