com.flowcentraltech.flowcentral.audit.web.writers.EntityAuditSnapshotViewWriter Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2021-2024 FlowCentral Technologies Limited.
*
* 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 com.flowcentraltech.flowcentral.audit.web.writers;
import com.flowcentraltech.flowcentral.application.business.ApplicationModuleService;
import com.flowcentraltech.flowcentral.application.data.EntityDef;
import com.flowcentraltech.flowcentral.audit.business.AuditModuleService;
import com.flowcentraltech.flowcentral.audit.web.widgets.EntityAuditSnapshotViewWidget;
import com.flowcentraltech.flowcentral.common.data.FormattedAudit;
import com.flowcentraltech.flowcentral.common.data.FormattedEntityAudit;
import com.flowcentraltech.flowcentral.common.data.FormattedFieldAudit;
import com.flowcentraltech.flowcentral.configuration.constants.AuditEventType;
import com.tcdng.unify.core.UnifyException;
import com.tcdng.unify.core.annotation.Component;
import com.tcdng.unify.core.annotation.Configurable;
import com.tcdng.unify.core.annotation.Writes;
import com.tcdng.unify.core.constant.LocaleType;
import com.tcdng.unify.web.ui.widget.ResponseWriter;
import com.tcdng.unify.web.ui.widget.Widget;
import com.tcdng.unify.web.ui.widget.writer.AbstractWidgetWriter;
/**
* Entity audit snapshot view writer.
*
* @author FlowCentral Technologies Limited
* @since 1.0
*/
@Writes(EntityAuditSnapshotViewWidget.class)
@Component("entityauditsnapshotview-writer")
public class EntityAuditSnapshotViewWriter extends AbstractWidgetWriter {
@Configurable
private ApplicationModuleService applicationModuleService;
@Configurable
private AuditModuleService auditModuleService;
@Override
protected void doWriteStructureAndContent(ResponseWriter writer, Widget widget) throws UnifyException {
EntityAuditSnapshotViewWidget entityAuditSnapshotViewWidget = (EntityAuditSnapshotViewWidget) widget;
writer.write("");
final Long entityAuditKeysId = entityAuditSnapshotViewWidget.getEntityAuditKeysId();
final FormattedAudit formattedAudit = auditModuleService
.getSnapshotFormattedAuditByEntityAuditKeys(entityAuditKeysId);
final FormattedEntityAudit[] snapshots = formattedAudit.getSnapshots();
final String fieldHeader = resolveSessionMessage("$m{entityauditsnapshotview.header.field}");
final String oldValueHeader = resolveSessionMessage("$m{entityauditsnapshotview.header.oldvalue}");
final String newValueHeader = resolveSessionMessage("$m{entityauditsnapshotview.header.newvalue}");
for (int i = 0; i < snapshots.length; i++) {
FormattedEntityAudit snapshot = snapshots[i];
final EntityDef entityDef = applicationModuleService.getEntityDef(snapshot.getEntity());
writer.write("");
writer.write("");
for (int j = 0; j < i; j++) {
// Child depth padding
writer.write("");
writer.write("");
}
// Snapshot
writer.write("");
final AuditEventType eventType = AuditEventType.fromName(snapshot.getEventType());
final boolean showHighlight = !eventType.isView();
final String highlight = showHighlight
? (eventType.isCreate() ? "eagreen" : (eventType.isUpdate() ? "eaorange" : "eared"))
: "eawhite";
final String eventDesc = getListItemByKey(LocaleType.SESSION, "auditeventtypelist", eventType.code())
.getListDescription();
writeHeader(writer, "$m{entityauditsnapshotview.entity}", entityDef.getLabel(), null);
writeHeader(writer, "$m{entityauditsnapshotview.event}", eventDesc, highlight);
writer.write("");
// Details
writer.write("");
writer.write("");
writer.write("").writeWithHtmlEscape(fieldHeader).write(" ");
writer.write("").writeWithHtmlEscape(oldValueHeader).write(" ");
writer.write("").writeWithHtmlEscape(newValueHeader).write(" ");
writer.write(" ");
for (FormattedFieldAudit fieldSnapshot : snapshot.getFields()) {
if (entityDef.isField(fieldSnapshot.getFieldName())) {
final boolean highlightChange = showHighlight && fieldSnapshot.changed();
final boolean delete = eventType.isDelete();
writer.write("");
writer.write("")
.writeWithHtmlEscape(entityDef.getFieldDef(fieldSnapshot.getFieldName()).getFieldLabel())
.write(" ");
writer.write("");
writeValue(writer, fieldSnapshot.getOldValue());
writer.write(" ");
writer.write("");
writeValue(writer, fieldSnapshot.getNewValue());
writer.write(" ");
writer.write(" ");
}
}
writer.write("
");
writer.write("");
writer.write("");
// Snapshot end
writer.write("");
writer.write("");
}
writer.write("");
}
private void writeHeader(ResponseWriter writer, String labelKey, String headerText, String textClass)
throws UnifyException {
writer.write("");
writer.write("");
writer.writeWithHtmlEscape(resolveSessionMessage(labelKey));
writer.write(":");
writer.write("");
if (textClass != null) {
writer.write("");
writer.writeWithHtmlEscape(headerText);
writer.write("");
writer.write("");
}
private void writeValue(ResponseWriter writer, String[] val) throws UnifyException {
if (val != null) {
for (int i = 0; i < val.length; i++) {
if (i > 0) {
writer.write("
");
}
writer.writeWithHtmlEscape(val[i]);
}
}
}
}