Skip to content

Commit

Permalink
#191 bugfix saveStatus/validStatus not updated correctly; refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Feb 27, 2017
1 parent 74cbe6b commit 286fdb2
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static final JSONDocument ofDocument(final Document document, final JSONO
final DocumentValidStatus documentValidStatus = document.getValidStatus();
if (documentValidStatus != null)
{
jsonDocument.setValidStatus(documentValidStatus.toJson());
jsonDocument.setValidStatus(documentValidStatus);
}

final DocumentSaveStatus documentSaveStatus = document.getSaveStatus();
Expand Down Expand Up @@ -178,7 +178,7 @@ private static JSONDocument ofEvent(final DocumentChanges documentChangedEvents,
final DocumentValidStatus documentValidStatus = documentChangedEvents.getDocumentValidStatus();
if (documentValidStatus != null)
{
jsonDocument.setValidStatus(documentValidStatus.toJson());
jsonDocument.setValidStatus(documentValidStatus);
}

//
Expand Down Expand Up @@ -274,16 +274,13 @@ public static JSONDocument ofDocumentView(final IDocumentView documentView)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private final String rowId;

@JsonProperty("valid-status")
@JsonProperty("validStatus")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private String validStatus;
private DocumentValidStatus validStatus;

@JsonProperty("saved")
@JsonProperty("saveStatus")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Boolean saved;
@JsonProperty("save-status")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private String saveStatus;
private DocumentSaveStatus saveStatus;

@JsonProperty("staleTabIds")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
Expand Down Expand Up @@ -362,23 +359,22 @@ public String getRowId()
return rowId;
}

private void setValidStatus(final String validStatus)
private void setValidStatus(final DocumentValidStatus validStatus)
{
this.validStatus = validStatus;
}

public String getValidStatus()
public DocumentValidStatus getValidStatus()
{
return validStatus;
}

private void setSaveStatus(final DocumentSaveStatus documentSaveStatus)
{
this.saveStatus = documentSaveStatus.toJson();
this.saved = documentSaveStatus.isSaved();
this.saveStatus = documentSaveStatus;
}

public String getSaveStatus()
public DocumentSaveStatus getSaveStatus()
{
return saveStatus;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,9 @@ public static JSONDocumentField ofDocumentFieldChangedEvent(final DocumentFieldC
@JsonInclude(JsonInclude.Include.NON_NULL)
private String lookupValuesStaleReason;

@JsonProperty("valid")
@JsonProperty("validStatus")
@JsonInclude(JsonInclude.Include.NON_NULL)
private Boolean valid;

@JsonProperty("validReason")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private String validReason;
private DocumentValidStatus validStatus;

/** Other properties */
private final Map<String, Object> otherProperties = new LinkedHashMap<>();
Expand Down Expand Up @@ -226,8 +222,7 @@ public String toString()
.add("displayed-reason", displayedReason)
.add("lookupValuesStale", lookupValuesStale)
.add("lookupValuesStale-reason", lookupValuesStaleReason)
.add("valid", valid)
.add("validReason", validReason)
.add("validStatus", validStatus)
.add("otherProperties", otherProperties)
.toString();
}
Expand Down Expand Up @@ -290,17 +285,7 @@ public JSONDocumentField setDisplayed(final boolean displayed)

/* package */ JSONDocumentField setValidStatus(final DocumentValidStatus validStatus)
{
if (validStatus == null)
{
valid = null;
validReason = null;
}
else
{
valid = validStatus.isValid();
validReason = validStatus.getReason();
}

this.validStatus = validStatus;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -922,11 +922,13 @@ public void markAsNotNew()
private final DocumentValidStatus setValidStatusAndReturn(final DocumentValidStatus valid)
{
Preconditions.checkNotNull(valid, "valid"); // shall not happen
final DocumentValidStatus validOld = _valid;
if (Objects.equals(validOld, valid))
{
return validOld; // no change
}

// Don't check if changed because we want ALWAYS to collect the valid status
// final DocumentValidStatus validOld = _valid;
// if (Objects.equals(validOld, valid))
// {
// return validOld; // no change
// }

_valid = valid;
Execution.getCurrentDocumentChangesCollectorOrNull().collectDocumentValidStatusChanged(getDocumentPath(), valid);
Expand All @@ -945,7 +947,7 @@ private final DocumentSaveStatus setSaveStatusAndReturn(final DocumentSaveStatus
// }

_saveStatus = saveStatus;
Execution.getCurrentDocumentChangesCollector().collectDocumentSaveStatusChanged(getDocumentPath(), saveStatus);
Execution.getCurrentDocumentChangesCollectorOrNull().collectDocumentSaveStatusChanged(getDocumentPath(), saveStatus);
return _saveStatus;
}

Expand Down Expand Up @@ -1418,7 +1420,7 @@ public DocumentValidStatus checkAndGetValidStatus()
if (!validState.isValid())
{
logger.trace("Considering document invalid because {} is not valid: {}", includedDocumentsPerDetailId, validState);
return setValidStatusAndReturn(validState);
return setValidStatusAndReturn(DocumentValidStatus.childInvalid());
}
}

Expand Down Expand Up @@ -1516,6 +1518,7 @@ public DocumentSaveStatus saveIfValidAndHasChanges()
{
// NOTE: usually if we do the right checkings we shall not get to this
logger.warn("Failed saving document, but IGNORED: {}", this, saveEx);
setValidStatusAndReturn(DocumentValidStatus.invalid(saveEx));
return setSaveStatusAndReturn(DocumentSaveStatus.notSaved(saveEx));
}
}
Expand Down Expand Up @@ -1709,6 +1712,18 @@ public Document build()
// Update document's valid status
document.checkAndGetValidStatus();

//
// Update document's save status
if (fieldInitializerMode == FieldInitializationMode.NewDocument)
{
document.setSaveStatusAndReturn(DocumentSaveStatus.notSavedJustCreated());
}
else if (fieldInitializerMode == FieldInitializationMode.Load
|| fieldInitializerMode == FieldInitializationMode.Refresh)
{
document.setSaveStatusAndReturn(DocumentSaveStatus.savedJustLoaded());
}

return document;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;

/*
Expand All @@ -17,15 +21,16 @@
*
* This program 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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
public final class DocumentSaveStatus
{
public static final DocumentSaveStatus unknown()
Expand All @@ -42,30 +47,51 @@ public static final DocumentSaveStatus notSaved(final DocumentValidStatus invali
{
final boolean hasChangesToBeSaved = true;
final boolean error = false;
final String reason = "Invalid status: " + invalidState;
final String reason = invalidState.getReason();
return new DocumentSaveStatus(hasChangesToBeSaved, error, reason);
}

public static final DocumentSaveStatus notSaved(final Exception exception)
{
final boolean hasChangesToBeSaved = true;
final boolean error = true;
final String reason = "Error: " + exception.getLocalizedMessage();
final String reason = exception.getLocalizedMessage();
return new DocumentSaveStatus(hasChangesToBeSaved, error, reason);
}

public static final DocumentSaveStatus notSavedJustCreated()
{
return STATUS_NotSavedJustCreated;
}

public static final DocumentSaveStatus savedJustLoaded()
{
return STATUS_SavedJustLoaded;
}

private static final DocumentSaveStatus STATUS_Unknown = new DocumentSaveStatus(true, false, "not yet checked");
private static final DocumentSaveStatus STATUS_Saved = new DocumentSaveStatus(false, false, null);
private static final DocumentSaveStatus STATUS_NotSavedJustCreated = new DocumentSaveStatus(true, false, "new");
private static final DocumentSaveStatus STATUS_SavedJustLoaded = new DocumentSaveStatus(false, false, "just loaded");

@JsonProperty("saved")
private final boolean saved;
@JsonProperty("hasChanges")
private final boolean hasChangesToBeSaved;
@JsonProperty("error")
private final boolean error;
@JsonProperty("reason")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private final String reason;

private transient Integer _hashcode;

private DocumentSaveStatus(final boolean hasChangesToBeSaved, final boolean error, final String reason)
{
super();

this.saved = !hasChangesToBeSaved && !error;

this.hasChangesToBeSaved = hasChangesToBeSaved;
this.error = error;
this.reason = reason;
Expand All @@ -76,17 +102,13 @@ public String toString()
{
return MoreObjects.toStringHelper(this)
.omitNullValues()
.add("saved", saved)
.add("hasChangesToBeSaved", hasChangesToBeSaved)
.add("error", error)
.add("reason", reason)
.toString();
}

public String toJson()
{
return toString();
}

@Override
public int hashCode()
{
Expand Down Expand Up @@ -115,7 +137,7 @@ public boolean equals(final Object obj)
&& error == other.error
&& Objects.equals(reason, other.reason);
}

public boolean isSaved()
{
return !hasChangesToBeSaved && !error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

/*
* #%L
* metasfresh-webui-api
Expand All @@ -15,15 +20,16 @@
*
* This program 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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
public final class DocumentValidStatus
{
public static final DocumentValidStatus inititalInvalid()
Expand All @@ -41,25 +47,52 @@ public static final DocumentValidStatus valid()
return STATE_Valid;
}

public static final DocumentValidStatus childInvalid()
{
return STATE_ChildInvalid;
}

public static final DocumentValidStatus invalidMandatoryFieldNotFilled(final String fieldName)
{
return new DocumentValidStatus(false, "Mandatory field not filled: " + fieldName);
return new DocumentValidStatus(false, "Mandatory field not filled", fieldName);
}

public static final DocumentValidStatus invalid(final Throwable error)
{
return new DocumentValidStatus(false, error.getLocalizedMessage());
}

private static final DocumentValidStatus STATE_InitialInvalid = new DocumentValidStatus(false, "not validated yet");
private static final DocumentValidStatus STATE_Staled = new DocumentValidStatus(false, "staled");
private static final DocumentValidStatus STATE_Valid = new DocumentValidStatus(true, null);
private static final DocumentValidStatus STATE_ChildInvalid = new DocumentValidStatus(false, "child invalid");

@JsonProperty("valid")
private final boolean valid;
@JsonProperty("reason")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private final String reason;
@JsonProperty("fieldName")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private final String fieldName;

private transient Integer _hashcode;
private transient String _toString;

private DocumentValidStatus(final boolean valid, final String reason)
{
this.valid = valid;
this.reason = reason;
this.fieldName = null;
}

private DocumentValidStatus(final boolean valid, final String reason, final String fieldName)
{
super();
this.valid = valid;
this.reason = reason;
this.fieldName = fieldName;

}

@Override
Expand All @@ -78,11 +111,6 @@ public String toString()
return _toString;
}

public String toJson()
{
return toString();
}

@Override
public int hashCode()
{
Expand Down

0 comments on commit 286fdb2

Please sign in to comment.