You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@syncope.apache.org by GitBox <gi...@apache.org> on 2022/09/27 13:16:49 UTC

[GitHub] [syncope] ilgrosso commented on a diff in pull request #376: SYNCOPE-1695 audit view improvements

ilgrosso commented on code in PR #376:
URL: https://github.com/apache/syncope/pull/376#discussion_r981224603


##########
client/idrepo/console/src/main/java/org/apache/syncope/client/console/audit/AuditHistoryDetails.java:
##########
@@ -118,74 +153,176 @@ private static <T> Class<T> cast(final Class<?> aClass) {
 
     private static final ObjectMapper MAPPER = JsonMapper.builder().
             nodeFactory(new SortingNodeFactory()).build().
-            registerModule(new SimpleModule().addSerializer(new SortedSetJsonSerializer(cast(Set.class))));
+            registerModule(new SimpleModule().addSerializer(new SortedSetJsonSerializer(cast(Set.class)))).
+            registerModule(new JavaTimeModule());
 
     public AuditHistoryDetails(
-            final MultilevelPanel mlp,
-            final AuditEntry selected,
+            final String id,
             final EntityTO currentEntity,
+            final AuditElements.EventCategoryType type,
+            final String category,
             final String auditRestoreEntitlement) {
 
-        super();
+        super(id);
 
-        AuditEntry current = new AuditEntry();
-        if (currentEntity instanceof AnyTO) {
-            current.setWho(((AnyTO) currentEntity).getCreator());
-            current.setDate(((AnyTO) currentEntity).getCreationDate());
-        } else {
-            current.setWho(SyncopeConsoleSession.get().getSelfTO().getUsername());
-            current.setDate(OffsetDateTime.now());
-        }
-        try {
-            current.setBefore(MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(currentEntity));
-        } catch (JsonProcessingException e) {
-            LOG.error("While serializing current entity", e);
-            throw new WicketRuntimeException(e);
-        }
+        this.setOutputMarkupId(true);
+        this.reference = (Class<T>) currentEntity.getClass();
+        this.currentEntity = currentEntity;
+        this.type = type;
+        this.category = category;
 
-        add(new Label("current", getString("current")));
-        add(new Label("previous", getString("previous")));
+        IChoiceRenderer<AuditEntry> choiceRenderer = new IChoiceRenderer<>() {
 
-        @SuppressWarnings("unchecked")
-        Class<T> reference = (Class<T>) currentEntity.getClass();
-        add(new JsonDiffPanel(null, toJSON(current, reference), toJSON(selected, reference), null) {
+            private static final long serialVersionUID = -3724971416312135885L;
 
-            private static final long serialVersionUID = 2087989787864619493L;
+            @Override
+            public String getDisplayValue(final AuditEntry value) {
+                return SyncopeConsoleSession.get().getDateFormat().format(value.getDate());
+            }
 
             @Override
-            public void onSubmit(final AjaxRequestTarget target) {
-                modal.close(target);
+            public String getIdValue(final AuditEntry value, final int i) {
+                return Long.toString(value.getDate().toInstant().toEpochMilli());
+            }
+
+            @Override
+            public AuditEntry getObject(final String id, final IModel<? extends List<? extends AuditEntry>> choices) {
+                return choices.getObject().stream()
+                        .filter(c -> StringUtils.isNotBlank(id)
+                                && Long.valueOf(id) == c.getDate().toInstant().toEpochMilli()).findFirst()
+                        .orElse(null);
+            }
+        };
+        // add also select to choose with which version compare
+
+        beforeVersionsPanel =
+                new AjaxDropDownChoicePanel<>("beforeVersions", getString("beforeVersions"), new Model<>(), true);
+        beforeVersionsPanel.setChoiceRenderer(choiceRenderer);
+        beforeVersionsPanel.add(new IndicatorAjaxEventBehavior(Constants.ON_CHANGE) {
+
+            private static final long serialVersionUID = -6383712635009760397L;
+
+            @Override
+            protected void onEvent(final AjaxRequestTarget target) {
+                AuditEntry beforeEntry = beforeVersionsPanel.getModelObject() == null
+                        ? latestAuditEntry
+                        : beforeVersionsPanel.getModelObject();
+                AuditEntry afterEntry = afterVersionsPanel.getModelObject() == null
+                        ? after
+                        : buildAfterAuditEntry(beforeEntry);
+                AuditHistoryDetails.this.addOrReplace(new JsonDiffPanel(toJSON(beforeEntry, reference),
+                        toJSON(afterEntry, reference)));
+                // change after audit entries in order to match only the ones newer than the current after one
+                afterVersionsPanel.setChoices(auditEntries.stream().filter(ae ->
+                                ae.getDate().isAfter(beforeEntry.getDate())
+                                        || ae.getDate().isEqual(beforeEntry.getDate()))
+                        .collect(Collectors.toList()));
+                // set the new after entry
+                afterVersionsPanel.setModelObject(afterEntry);
+                target.add(AuditHistoryDetails.this);
+            }
+        });
+        afterVersionsPanel =
+                new AjaxDropDownChoicePanel<>("afterVersions", getString("afterVersions"), new Model<>(),
+                        true);
+        afterVersionsPanel.setChoiceRenderer(choiceRenderer);
+        afterVersionsPanel.add(new IndicatorAjaxEventBehavior(Constants.ON_CHANGE) {
+
+            private static final long serialVersionUID = -6383712635009760397L;
+
+            @Override
+            protected void onEvent(final AjaxRequestTarget target) {
+                AuditHistoryDetails.this.addOrReplace(
+                        new JsonDiffPanel(toJSON(beforeVersionsPanel.getModelObject() == null
+                                ? latestAuditEntry
+                                : beforeVersionsPanel.getModelObject(), reference),
+                                toJSON(afterVersionsPanel.getModelObject() == null
+                                        ? after
+                                        : buildAfterAuditEntry(afterVersionsPanel.getModelObject()), reference)));
+                target.add(AuditHistoryDetails.this);
             }
         });
+        add(beforeVersionsPanel.setOutputMarkupId(true));
+        add(afterVersionsPanel.setOutputMarkupId(true));
 
-        AjaxLink<Void> restore = new AjaxLink<>("restore") {
+        restore = new AjaxLink<>("restore") {
 
             private static final long serialVersionUID = -817438685948164787L;
 
             @Override
             public void onClick(final AjaxRequestTarget target) {
                 try {
-                    String json = selected.getBefore() == null
-                            ? MAPPER.readTree(selected.getOutput()).get("entity").toPrettyString()
-                            : selected.getBefore();
+                    AuditEntry before = beforeVersionsPanel.getModelObject() == null
+                            ? latestAuditEntry
+                            : beforeVersionsPanel.getModelObject();
+                    String json = before.getBefore() == null
+                            ? MAPPER.readTree(before.getOutput()).get("entity") == null
+                            ? MAPPER.readTree(before.getOutput()).toPrettyString()
+                            : MAPPER.readTree(before.getOutput()).get("entity").toPrettyString()
+                            : before.getBefore();
                     restore(json, target);
-
-                    mlp.prev(target);
                 } catch (JsonProcessingException e) {
                     throw new WicketRuntimeException(e);
                 }
             }
         };
         MetaDataRoleAuthorizationStrategy.authorize(restore, ENABLE, auditRestoreEntitlement);
         add(restore);
+        
+        init();
     }
 
     protected abstract void restore(String json, AjaxRequestTarget target);
 
+    protected void init() {
+        // audit fetch size is fixed, for the moment... 
+        this.auditEntries.clear();
+        this.auditEntries.addAll(new AuditRestClient().search(
+                currentEntity.getKey(),
+                1,
+                500,

Review Comment:
   500? Isn't that too big?



##########
common/idrepo/lib/src/main/java/org/apache/syncope/common/lib/types/AuditElements.java:
##########
@@ -33,9 +33,9 @@ public enum EventCategoryType {
         LOGIC("LOGIC"),
         WA("WA"),
         TASK("TASK"),
-        PROPAGATION("PropagationTask"),
-        PULL("PullTask"),
-        PUSH("PushTask"),
+        PROPAGATION("PROPAGATION"),

Review Comment:
   since now all enum cases have their `name()` associated, can't we just change this enum as follows?
   
   ```java
       public enum EventCategoryType {
           LOGIC,
           WA,
           TASK,
           PROPAGATION,
           PULL,
           PUSH,
           CUSTOM;
       }
   ```
       



##########
client/idrepo/console/pom.xml:
##########
@@ -278,7 +278,6 @@ under the License.
               <jvmArguments>
                 -Djavax.net.ssl.trustStore=${basedir}/../../../fit/wa-reference/src/test/resources/keystore.jks -Djavax.net.ssl.trustStorePassword=password
                 -Dwicket.core.settings.general.configuration-type=development

Review Comment:
   Please restore this



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@syncope.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org