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

sirius.biz.model.TraceData Maven / Gradle / Ivy

There is a newer version: 9.6
Show newest version
/*
 * Made with all the love in the world
 * by scireum in Remshalden, Germany
 *
 * Copyright by scireum GmbH
 * http://www.scireum.de - [email protected]
 */

package sirius.biz.model;

import sirius.biz.protocol.NoJournal;
import sirius.db.mixing.Column;
import sirius.db.mixing.Composite;
import sirius.db.mixing.annotations.BeforeSave;
import sirius.db.mixing.annotations.Length;
import sirius.db.mixing.annotations.NullAllowed;
import sirius.db.mixing.annotations.Transient;
import sirius.kernel.async.TaskContext;
import sirius.web.security.UserContext;

import java.time.LocalDateTime;

/**
 * Provides tracing information which can be embedded into other entities or mixins.
 */
public class TraceData extends Composite {

    /**
     * Stores the username of the user which created the assoicated entity.
     */
    public static final Column CREATED_BY = Column.named("createdBy");
    @NoJournal
    @NullAllowed
    @Length(50)
    private String createdBy;

    /**
     * Stores the timstamp when the associated entity was created.
     */
    public static final Column CREATED_AT = Column.named("createdAt");
    @NoJournal
    @NullAllowed
    private LocalDateTime createdAt;

    /**
     * Stores the system string ({@link TaskContext#getSystemString()} where the associated entity was created.
     */
    public static final Column CREATED_IN = Column.named("createdIn");
    @NoJournal
    @NullAllowed
    @Length(150)
    private String createdIn;

    /**
     * Stores the username of the user which last changed the associated entity.
     */
    public static final Column CHANGED_BY = Column.named("changedBy");
    @NoJournal
    @NullAllowed
    @Length(50)
    private String changedBy;

    /**
     * Stores the timestamp when the associated entity was last changed.
     */
    public static final Column CHANGED_AT = Column.named("changedAt");
    @NoJournal
    @NullAllowed
    private LocalDateTime changedAt;

    /**
     * Stores the system string ({@link TaskContext#getSystemString()} where the associated entity was last changed.
     */
    public static final Column CHANGED_IN = Column.named("changedIn");
    @NoJournal
    @NullAllowed
    @Length(150)
    private String changedIn;

    @Transient
    private boolean silent;

    @BeforeSave
    protected void update() {
        if (!silent) {
            if (createdAt == null) {
                createdBy = UserContext.getCurrentUser().getUserName();
                createdAt = LocalDateTime.now();
                createdIn = TaskContext.get().getSystemString();
            }
            changedBy = UserContext.getCurrentUser().getUserName();
            changedAt = LocalDateTime.now();
            changedIn = TaskContext.get().getSystemString();
        }
    }

    /**
     * Determines if change tracking is currently disabled.
     *
     * @return true, if change tracking is currently disabled, false otherwise
     */
    public boolean isSilent() {
        return silent;
    }

    /**
     * Can be used to disable or re-enable change tracking.
     * 

* Some changes, performed by the system, should not update the tracing information. A login of a user * (which might increment its login counter) would be an example, where change tracking should be disabled. * * @param silent true if change tracking for the current entity instance should be disabled or * false to re-enable */ public void setSilent(boolean silent) { this.silent = silent; } public String getCreatedBy() { return createdBy; } public LocalDateTime getCreatedAt() { return createdAt; } public String getCreatedIn() { return createdIn; } public String getChangedBy() { return changedBy; } public LocalDateTime getChangedAt() { return changedAt; } public String getChangedIn() { return changedIn; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy