github-code-scanning[bot] commented on code in PR #376: URL: https://github.com/apache/syncope/pull/376#discussion_r981357153
########## client/idrepo/console/src/main/java/org/apache/syncope/client/console/audit/AuditHistoryDetails.java: ########## @@ -118,74 +153,176 @@ 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() { Review Comment: ## Confusing non-overriding of package-private method This method does not override [Component.init](1) because it is private to another package. [Show more details](https://github.com/apache/syncope/security/code-scanning/1159) ########## client/idrepo/console/src/main/java/org/apache/syncope/client/console/audit/AuditHistoryDetails.java: ########## @@ -118,74 +153,176 @@ 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() Review Comment: ## Missing catch of NumberFormatException Potential uncaught 'java.lang.NumberFormatException'. [Show more details](https://github.com/apache/syncope/security/code-scanning/1158) -- 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-unsubscr...@syncope.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org