All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tools.dynamia.domain.AbstractEntity Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2023 Dynamia Soluciones IT S.A.S - NIT 900302344-1
 * Colombia / South America
 *
 * 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 tools.dynamia.domain;


import tools.dynamia.commons.Identifiable;
import tools.dynamia.commons.Jsonable;
import tools.dynamia.commons.Mappable;
import tools.dynamia.commons.Nameable;
import tools.dynamia.commons.PropertyChangeListener;
import tools.dynamia.commons.PropertyChangeListenerContainer;
import tools.dynamia.commons.PropertyChangeSupport;
import tools.dynamia.commons.Xmlable;

import java.io.Serializable;
import java.util.Objects;

import static tools.dynamia.domain.util.DomainUtils.lookupCrudService;

/**
 * Represent a data Entity class with ID and properties
 *
 * @param  the generic type
 * @author Ing. Mario Serrano Leones
 */
public abstract class AbstractEntity implements Serializable, Identifiable,
        PropertyChangeListenerContainer, Referenceable, Jsonable, Xmlable, Mappable, Nameable {


    private static final long serialVersionUID = 1L;

    private transient PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    /**
     * Gets the id.
     *
     * @return the id
     */
    @Override
    public abstract ID getId();

    /**
     * Sets the id.
     *
     * @param id the new id
     */
    @Override
    public abstract void setId(ID id);

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final AbstractEntity other = (AbstractEntity) obj;
        if (this.getId() == null && this != other) {
            return false;
        }

        return this.getId() == other.getId() || (this.getId() != null && this.getId().equals(other.getId()));
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        int hash = 7;
        hash = 83 * hash + (this.getId() != null ? this.getId().hashCode() : 0);
        return hash;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return getClass().getName() + "[" + getId() + "]";
    }

    /**
     * Add a PropertyChangeListener to get object change, subclasses must invoke
     * notifyChange to fire listeners
     */
    @Override
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        if (propertyChangeSupport == null) {
            propertyChangeSupport = new PropertyChangeSupport(this);
        }
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    /**
     * Remove PropertyChangeListener
     */
    @Override
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }

    /**
     * Remove all property change listeners
     */
    public void removePropertyChangeListeners() {
        propertyChangeSupport.clearListeners();
    }


    /**
     * Notify PropertyChangeListeners change, this method automatically check if the
     * oldValue and newValue are different to fire the listeners.
     */
    protected void notifyChange(String propertyName, Object oldValue, Object newValue) {
        if (oldValue == null || !Objects.equals(oldValue, newValue)) {
            propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
        }
    }

    @Override
    public EntityReference toEntityReference() {
        EntityReference entityReference = new EntityReference<>(getId(), getClass().getName());
        entityReference.setName(toString());
        entityReference.getAttributes().putAll(toMap());
        return entityReference;
    }

    /**
     * Save this instance
     */
    public void save() {
        lookupCrudService().save(this);
    }

    /**
     * Delete this instance
     */
    public void delete() {
        lookupCrudService().delete(this);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy