package ubic.gemma.model.common.auditAndSecurity.curation;

import org.apache.commons.lang3.StringEscapeUtils;
import ubic.gemma.model.common.auditAndSecurity.AuditEvent;

import java.util.Date;

/**
 * Created by tesarst on 07/03/17.
 * Abstract curatable value object that provides variables and methods for data stored in CurationDetails objects on
 * curatable objects.
 */
public abstract class AbstractCuratableValueObject {

    private static final String TROUBLE_DETAILS_NONE = "Not troubled";

    protected Long id;

    protected Date lastUpdated;

    protected Boolean troubled;

    protected AuditEvent lastTroubledEvent;

    protected Boolean needsAttention;

    protected AuditEvent lastNeedsAttentionEvent;

    protected String curationNote;

    protected AuditEvent lastNoteUpdateEvent;

    public AbstractCuratableValueObject() {
    }

    public AbstractCuratableValueObject( Long id, Date lastUpdated, Boolean troubled, AuditEvent lastTroubledEvent,
            Boolean needsAttention, AuditEvent lastNeedsAttentionEvent, String curationNote,
            AuditEvent lastNoteUpdateEvent ) {
        this.id = id;
        this.lastUpdated = lastUpdated;
        this.troubled = troubled;
        this.lastTroubledEvent = lastTroubledEvent;
        this.needsAttention = needsAttention;
        this.lastNeedsAttentionEvent = lastNeedsAttentionEvent;
        this.curationNote = curationNote;
        this.lastNoteUpdateEvent = lastNoteUpdateEvent;
    }

    public Long getId() {
        return this.id;
    }

    public void setId( Long id ) {
        this.id = id;
    }

    public Date getLastUpdated() {
        return lastUpdated;
    }

    public void setLastUpdated( Date lastUpdated ) {
        this.lastUpdated = lastUpdated;
    }

    public Boolean getTroubled() {
        return troubled;
    }

    public void setTroubled( Boolean troubled ) {
        this.troubled = troubled;
    }

    public AuditEvent getLastTroubledEvent() {
        return lastTroubledEvent;
    }

    public void setLastTroubledEvent( AuditEvent lastTroubledEvent ) {
        this.lastTroubledEvent = lastTroubledEvent;
    }

    public Boolean getNeedsAttention() {
        return needsAttention;
    }

    public void setNeedsAttention( Boolean needsAttention ) {
        this.needsAttention = needsAttention;
    }

    public AuditEvent getLastNeedsAttentionEvent() {
        return lastNeedsAttentionEvent;
    }

    public void setLastNeedsAttentionEvent( AuditEvent lastNeedsAttentionEvent ) {
        this.lastNeedsAttentionEvent = lastNeedsAttentionEvent;
    }

    public String getCurationNote() {
        return curationNote;
    }

    public void setCurationNote( String curationNote ) {
        this.curationNote = curationNote;
    }

    public AuditEvent getLastNoteUpdateEvent() {
        return lastNoteUpdateEvent;
    }

    public void setLastNoteUpdateEvent( AuditEvent lastNoteUpdateEvent ) {
        this.lastNoteUpdateEvent = lastNoteUpdateEvent;
    }

    /**
     * @return a string describing the current trouble of this object. In this case, only the trouble of the Expression
     * Experiment are described. If you also need to include the Array Design trouble info, use
     * {@link ubic.gemma.model.expression.experiment.ExpressionExperimentDetailsValueObject}
     */
    public String getTroubleDetails() {
        return this.getTroubleDetails( true );
    }

    public String getTroubleDetails( boolean htmlEscape ) {
        String details = this.getTroubled() ? this.getLastTroubledEvent().toString() : TROUBLE_DETAILS_NONE;

        return htmlEscape ? StringEscapeUtils.escapeHtml4( details ) : details;
    }
}

@@ -104,16 +104,55 @@
 eUtils;%0A
+import org.openjena.atlas.logging.Log;%0A
 import u
@@ -623,16 +623,24 @@
 troubled
+ = false
 ;%0A%0A    p
@@ -715,16 +715,24 @@
 ttention
+ = false
 ;%0A%0A    p
@@ -3563,91 +3563,332 @@
 s = 
-this.getTroubled() ? this.getLastTroubledEvent().toString() : TROUBLE_DETAILS_NONE;
+TROUBLE_DETAILS_NONE;%0A        if(this.getTroubled())%7B%0A            if(this.getLastTroubledEvent() == null)%7B%0A                Log.warn(this, %22Curatable object is troubled, but has no trouble event! Id: %22+this.getId());%0A            %7Delse%7B%0A                details = this.getLastTroubledEvent().toString();%0A            %7D%0A        %7D
 %0A%0A  

Work on git Issue #1 - Model - Fix problems in curatable value object