<commit_before>/*
 * Copyright (c) 2017 Comvai, s.r.o. All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.ctoolkit.services.storage.appengine.datastore;

import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.OnLoad;
import com.googlecode.objectify.annotation.OnSave;
import org.ctoolkit.services.storage.EntityIdentity;

import java.io.Serializable;
import java.util.Date;

/**
 * The entity with common properties to all its children:
 * <ul>
 * <li><b>createdDate</b> - the date of entity creation, set only once</li>
 * <li><b>updatedDate</b> - the date of entity update</li>
 * <li><b>version</b> - </li>
 * <li><b>dbModelVersion</b> - the model update time in milliseconds, the date when the model has been first time used
 * in the code. The value is being hardcoded, evaluated by developer.</li>
 * </ul>
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public abstract class BaseEntity<P extends EntityIdentity>
        implements EntityIdentity<P>, Serializable
{
    private static final long serialVersionUID = 1L;

    private Integer version;

    @Index
    private Date createdDate;

    @Index
    private Date updatedDate;

    /**
     * The model version persisted in datastore as a time stamp.
     */
    @Index
    private Date dbModelVersion;

    public Integer getVersion()
    {
        if ( version == null )
        {
            // to be backward compatible
            version = 1;
        }

        return version;
    }

    /**
     * Returns the date of entity creation.
     *
     * @return the date of entity creation
     */
    public Date getCreatedDate()
    {
        return createdDate;
    }

    /**
     * Returns the date of entity update.
     *
     * @return the date of entity update
     */
    public Date getUpdatedDate()
    {
        return updatedDate;
    }

    /**
     * Checks whether instance schema needs to be migrated or not.
     *
     * @return true if instance schema needs to be migrated
     */
    public boolean migrate()
    {
        // check actual model version against persisted modelVersion
        return getModelVersion() > dbModelVersion.getTime();
    }

    /**
     * This method is called before every update.
     * It evaluates <code>createdDate</code>or <code>updatedDate</code>.
     */
    @OnSave
    private void onSave()
    {
        if ( getModelVersion() <= 0 )
        {
            throw new IllegalArgumentException( "dbModelVersion must be specified!!" );
        }

        if ( version == null )
        {
            version = 1;
        }

        if ( createdDate == null )
        {
            dbModelVersion = new Date( getModelVersion() );
            createdDate = new Date();
        }
        else
        {
            updatedDate = new Date();
            version++;
        }
    }

    @OnLoad
    private void onLoad()
    {
        if ( dbModelVersion == null )
        {
            // for backward compatibility, this will mark instance schema needed to be migrated
            dbModelVersion = new Date( 1L );
        }
        else if ( migrate() )
        {
            String message = getToString() + "{ getModelVersion() = " + getModelVersion() + " }";
            System.err.println( "Instance needs to be migrated: " + message );
        }
    }

    /**
     * The model update time in milliseconds, the date when the model has been first time used in the code.
     * The value is being hardcoded, evaluated by developer.
     *
     * @return the model update time in milliseconds
     */
    protected abstract long getModelVersion();

    @Override
    public String toString()
    {
        return getToString();
    }

    private String getToString()
    {
        return "BaseEntity{" +
                "modelVersion=" + dbModelVersion +
                ", version=" + version +
                ", updatedDate=" + updatedDate +
                ", createdDate=" + createdDate +
                '}';
    }
}
<commit_msg>BaseEntity.modificationDate has changed from updatedDate.<commit_after>/*
 * Copyright (c) 2017 Comvai, s.r.o. All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.ctoolkit.services.storage.appengine.datastore;

import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.OnLoad;
import com.googlecode.objectify.annotation.OnSave;
import org.ctoolkit.services.storage.EntityIdentity;

import java.io.Serializable;
import java.util.Date;

/**
 * The entity with common properties to all its children:
 * <ul>
 * <li><b>createdDate</b> - the date of entity creation, set only once</li>
 * <li><b>modificationDate</b> - the date of the last modification of the entity values</li>
 * <li><b>version</b> - </li>
 * <li><b>dbModelVersion</b> - the model update time in milliseconds, the date when the model has been first time used
 * in the code. The value is being hardcoded, evaluated by developer.</li>
 * </ul>
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public abstract class BaseEntity<P extends EntityIdentity>
        implements EntityIdentity<P>, Serializable
{
    private static final long serialVersionUID = 1L;

    private Integer version;

    @Index
    private Date createdDate;

    @Index
    private Date modificationDate;

    /**
     * The model version persisted in datastore as a time stamp.
     */
    @Index
    private Date dbModelVersion;

    public Integer getVersion()
    {
        if ( version == null )
        {
            // to be backward compatible
            version = 1;
        }

        return version;
    }

    /**
     * Returns the date of entity creation.
     *
     * @return the date of entity creation
     */
    public Date getCreatedDate()
    {
        return createdDate;
    }

    /**
     * Returns the date of the last modification of the entity values.
     *
     * @return the date of the last modification
     */
    public Date getModificationDate()
    {
        return modificationDate;
    }

    /**
     * Checks whether instance schema needs to be migrated or not.
     *
     * @return true if instance schema needs to be migrated
     */
    public boolean migrate()
    {
        // check actual model version against persisted modelVersion
        return getModelVersion() > dbModelVersion.getTime();
    }

    /**
     * This method will be called before every update.
     * It evaluates <code>createdDate</code>or <code>modificationDate</code>.
     */
    @OnSave
    private void onSave()
    {
        if ( getModelVersion() <= 0 )
        {
            throw new IllegalArgumentException( "dbModelVersion must be specified!!" );
        }

        if ( version == null )
        {
            version = 1;
        }

        if ( createdDate == null )
        {
            dbModelVersion = new Date( getModelVersion() );
            createdDate = new Date();
        }
        else
        {
            modificationDate = new Date();
            version++;
        }
    }

    @OnLoad
    private void onLoad()
    {
        if ( dbModelVersion == null )
        {
            // for backward compatibility, this will mark instance schema needed to be migrated
            dbModelVersion = new Date( 1L );
        }
        else if ( migrate() )
        {
            String message = getToString() + "{ getModelVersion() = " + getModelVersion() + " }";
            System.err.println( "Instance needs to be migrated: " + message );
        }
    }

    /**
     * The model update time in milliseconds, the date when the model has been first time used in the code.
     * The value is being hardcoded, evaluated by developer.
     *
     * @return the model update time in milliseconds
     */
    protected abstract long getModelVersion();

    @Override
    public String toString()
    {
        return getToString();
    }

    private String getToString()
    {
        return "BaseEntity{" +
                "modelVersion=" + dbModelVersion +
                ", version=" + version +
                ", modificationDate=" + modificationDate +
                ", createdDate=" + createdDate +
                '}';
    }
}

{'fraction_non_alphanumeric': 0.09167446211412535, 'fraction_numerical': 0.004573329175761355, 'mean_word_length': 2.236461486713757, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3755404', 'n_tokens_mistral': 2580, 'n_tokens_neox': 2476, 'n_words': 932}