From 3d5f4fcecb8d1cd3bf83b832579abdcf1235ee83 Mon Sep 17 00:00:00 2001 From: Alexander Fedorov Date: Sun, 2 May 2021 09:53:43 +0300 Subject: [PATCH] Bug 573296 - [Passage] rework EMF migration facilities, part 4 Extract validation error messages Signed-off-by: Alexander Fedorov --- .../META-INF/MANIFEST.MF | 1 + .../passage/lic/emf/ecore/LicensingEcore.java | 27 ---------- .../lic/emf/validation/ErrorMessages.java | 54 +++++++++++++++++++ .../ui/wizards/IssueLicensePackPage.java | 13 +++-- .../floating/IssueLicensePackPage.java | 10 ++-- .../licenses/core/IssueFloatingLicense.java | 4 +- .../licenses/core/IssuePersonalLicense.java | 9 ++-- .../products/core/ProductVersionKeys.java | 4 +- 8 files changed, 78 insertions(+), 44 deletions(-) create mode 100644 bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/validation/ErrorMessages.java diff --git a/bundles/org.eclipse.passage.lic.emf/META-INF/MANIFEST.MF b/bundles/org.eclipse.passage.lic.emf/META-INF/MANIFEST.MF index f32e8ba70..9ca332823 100644 --- a/bundles/org.eclipse.passage.lic.emf/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.passage.lic.emf/META-INF/MANIFEST.MF @@ -28,6 +28,7 @@ Export-Package: org.eclipse.passage.lic.emf.ecore; org.eclipse.passage.lic.users.migration.tests", org.eclipse.passage.lic.emf.meta, org.eclipse.passage.lic.emf.resource, + org.eclipse.passage.lic.emf.validation, org.eclipse.passage.lic.internal.emf; x-friends:="org.eclipse.passage.lic.hc, org.eclipse.passage.lbc.base, diff --git a/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/LicensingEcore.java b/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/LicensingEcore.java index 9de436949..d7e7a22d1 100644 --- a/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/LicensingEcore.java +++ b/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/ecore/LicensingEcore.java @@ -12,13 +12,9 @@ *******************************************************************************/ package org.eclipse.passage.lic.emf.ecore; -import org.eclipse.emf.common.util.Diagnostic; import org.eclipse.emf.ecore.EClass; import org.eclipse.emf.ecore.EDataType; -import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EPackage; -import org.eclipse.emf.ecore.util.Diagnostician; -import org.eclipse.passage.lic.internal.emf.i18n.EmfMessages; public class LicensingEcore { @@ -52,27 +48,4 @@ public static String composeFullQualifiedName(EDataType eDataType) { return sb.toString(); } - public static String extractValidationError(EObject eObject) { - if (eObject == null) { - return EmfMessages.LicensingEcore_input_invalid; - } - final Diagnostic result = Diagnostician.INSTANCE.validate(eObject); - if (result.getSeverity() == Diagnostic.OK) { - return null; - } - // Get the error count and create an appropriate Error message: - final int errorCount = result.getChildren().size(); - final String header = EmfMessages.LicensingEcore_inpur_header; - final String entry = EmfMessages.LicensingEcore_input_entry; - final StringBuilder sb = new StringBuilder(); - sb.append(String.format(header, errorCount)); - sb.append('\n'); - int messageCount = 0; - for (final Diagnostic d : result.getChildren()) { - sb.append('\n'); - sb.append(String.format(entry, ++messageCount, d.getMessage())); - } - return sb.toString(); - } - } diff --git a/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/validation/ErrorMessages.java b/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/validation/ErrorMessages.java new file mode 100644 index 000000000..b333a8d3f --- /dev/null +++ b/bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/emf/validation/ErrorMessages.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * Copyright (c) 2021 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.lic.emf.validation; + +import java.util.Optional; +import java.util.function.Function; + +import org.eclipse.emf.common.util.Diagnostic; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.util.Diagnostician; +import org.eclipse.passage.lic.internal.emf.i18n.EmfMessages; + +/** + * Summarizes validation errors for the given {@link EObject} + * + * @since 2.0 + */ +public final class ErrorMessages implements Function> { + + @Override + public Optional apply(EObject input) { + if (input == null) { + return Optional.of(EmfMessages.LicensingEcore_input_invalid); + } + final Diagnostic result = Diagnostician.INSTANCE.validate(input); + if (result.getSeverity() == Diagnostic.OK) { + return Optional.empty(); + } + // Get the error count and create an appropriate Error message: + final int errorCount = result.getChildren().size(); + final String header = EmfMessages.LicensingEcore_inpur_header; + final String entry = EmfMessages.LicensingEcore_input_entry; + final StringBuilder sb = new StringBuilder(); + sb.append(String.format(header, errorCount)); + sb.append('\n'); + int messageCount = 0; + for (final Diagnostic d : result.getChildren()) { + sb.append('\n'); + sb.append(String.format(entry, ++messageCount, d.getMessage())); + } + return Optional.of(sb.toString()); + } + +} diff --git a/bundles/org.eclipse.passage.loc.dashboard.ui/src/org/eclipse/passage/loc/dashboard/ui/wizards/IssueLicensePackPage.java b/bundles/org.eclipse.passage.loc.dashboard.ui/src/org/eclipse/passage/loc/dashboard/ui/wizards/IssueLicensePackPage.java index 2d41c00b7..510ac2811 100644 --- a/bundles/org.eclipse.passage.loc.dashboard.ui/src/org/eclipse/passage/loc/dashboard/ui/wizards/IssueLicensePackPage.java +++ b/bundles/org.eclipse.passage.loc.dashboard.ui/src/org/eclipse/passage/loc/dashboard/ui/wizards/IssueLicensePackPage.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019, 2020 ArSysOp + * Copyright (c) 2019, 2021 ArSysOp * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -12,6 +12,7 @@ *******************************************************************************/ package org.eclipse.passage.loc.dashboard.ui.wizards; +import java.util.Optional; import java.util.function.Supplier; import org.eclipse.e4.core.contexts.IEclipseContext; @@ -27,7 +28,7 @@ import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.wizard.WizardPage; -import org.eclipse.passage.lic.emf.ecore.LicensingEcore; +import org.eclipse.passage.lic.emf.validation.ErrorMessages; import org.eclipse.passage.lic.licenses.LicensePackDescriptor; import org.eclipse.passage.lic.licenses.model.api.LicenseGrant; import org.eclipse.passage.lic.licenses.model.api.LicensePack; @@ -43,6 +44,7 @@ public class IssueLicensePackPage extends WizardPage { private final IEclipseContext context; private final Supplier data; + private final ErrorMessages validate; private LicensePack license; private VViewModelProperties properties; private Composite base; @@ -51,6 +53,7 @@ protected IssueLicensePackPage(String name, Supplier dat super(name); this.context = context; this.data = data; + this.validate = new ErrorMessages(); setTitle(IssueLicensePageMessages.IssueLicensePackPage_page_title); setDescription(IssueLicensePageMessages.IssueLicensePackPage_page_description); } @@ -115,9 +118,9 @@ private void updatePage() { } protected boolean validatePage() { - String errors = LicensingEcore.extractValidationError(license); - setErrorMessage(errors); - return errors == null; + Optional errors = validate.apply(license); + setErrorMessage(errors.orElse(null));// framework requires null + return errors.isEmpty(); } public LicensePackDescriptor pack() { diff --git a/bundles/org.eclipse.passage.loc.dashboard.ui/src/org/eclipse/passage/loc/dashboard/ui/wizards/floating/IssueLicensePackPage.java b/bundles/org.eclipse.passage.loc.dashboard.ui/src/org/eclipse/passage/loc/dashboard/ui/wizards/floating/IssueLicensePackPage.java index ef330e46c..55f37b44d 100644 --- a/bundles/org.eclipse.passage.loc.dashboard.ui/src/org/eclipse/passage/loc/dashboard/ui/wizards/floating/IssueLicensePackPage.java +++ b/bundles/org.eclipse.passage.loc.dashboard.ui/src/org/eclipse/passage/loc/dashboard/ui/wizards/floating/IssueLicensePackPage.java @@ -29,7 +29,7 @@ import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.wizard.WizardPage; -import org.eclipse.passage.lic.emf.ecore.LicensingEcore; +import org.eclipse.passage.lic.emf.validation.ErrorMessages; import org.eclipse.passage.lic.licenses.model.api.FloatingLicensePack; import org.eclipse.passage.loc.internal.api.FloatingLicenseRequest; import org.eclipse.passage.loc.internal.api.OperatorLicenseService; @@ -44,6 +44,7 @@ public final class IssueLicensePackPage extends WizardPage { private final IEclipseContext context; private final Supplier data; + private final ErrorMessages validate; private final Adapter update = new EContentAdapter() { @Override public void notifyChanged(Notification notification) { @@ -58,6 +59,7 @@ public void notifyChanged(Notification notification) { super(name); this.context = context; this.data = data; + this.validate = new ErrorMessages(); setTitle(IssueLicensePageMessages.IssueLicensePackPage_page_title); setDescription(IssueLicensePageMessages.IssueLicensePackPage_page_description); } @@ -127,9 +129,9 @@ private void renderEmfForms() { } protected boolean validatePage() { - String errors = LicensingEcore.extractValidationError(license); - setErrorMessage(errors); - return errors == null; + Optional errors = validate.apply(license); + setErrorMessage(errors.orElse(null));// framework requires null + return errors.isEmpty(); } FloatingLicensePack pack() { diff --git a/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/IssueFloatingLicense.java b/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/IssueFloatingLicense.java index f43ecb761..173ad6b2e 100644 --- a/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/IssueFloatingLicense.java +++ b/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/IssueFloatingLicense.java @@ -26,7 +26,7 @@ import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.util.EcoreUtil; -import org.eclipse.passage.lic.emf.ecore.LicensingEcore; +import org.eclipse.passage.lic.emf.validation.ErrorMessages; import org.eclipse.passage.lic.internal.api.LicensedProduct; import org.eclipse.passage.lic.internal.api.LicensingException; import org.eclipse.passage.lic.internal.api.ServiceInvocationResult; @@ -110,7 +110,7 @@ private ServiceInvocationResult persistLicenseFiles(Float private ServiceInvocationResult> persist(EObject target, LicensedProduct product, // Path folder, String decrypted, String encrypted) { // validate - Optional errors = Optional.ofNullable(LicensingEcore.extractValidationError(target)); + Optional errors = new ErrorMessages().apply(target); if (errors.isPresent()) { return new BaseServiceInvocationResult<>(new Trouble(new LicenseValidationFailed(), errors.get())); } diff --git a/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/IssuePersonalLicense.java b/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/IssuePersonalLicense.java index 4e4aa5ac1..da77c37d7 100644 --- a/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/IssuePersonalLicense.java +++ b/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/IssuePersonalLicense.java @@ -15,11 +15,12 @@ import java.io.IOException; import java.nio.file.Path; import java.util.Date; +import java.util.Optional; import java.util.UUID; import java.util.function.Supplier; import org.eclipse.emf.ecore.util.EcoreUtil; -import org.eclipse.passage.lic.emf.ecore.LicensingEcore; +import org.eclipse.passage.lic.emf.validation.ErrorMessages; import org.eclipse.passage.lic.internal.api.LicensedProduct; import org.eclipse.passage.lic.internal.api.LicensingException; import org.eclipse.passage.lic.internal.api.ServiceInvocationResult; @@ -59,9 +60,9 @@ final class IssuePersonalLicense { ServiceInvocationResult issue(Supplier template) { LicensePack license = adjsut(EcoreUtil.copy(template.get())); - String errors = LicensingEcore.extractValidationError(license); - if (errors != null) { - return new BaseServiceInvocationResult<>(new Trouble(new LicenseValidationFailed(), errors)); + Optional errors = new ErrorMessages().apply(license); + if (errors.isPresent()) { + return new BaseServiceInvocationResult<>(new Trouble(new LicenseValidationFailed(), errors.get())); } try { new UpdateLicensePlan(licenses).withPersonal(EcoreUtil.copy(license)); diff --git a/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionKeys.java b/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionKeys.java index b52d87162..ae8902695 100644 --- a/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionKeys.java +++ b/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionKeys.java @@ -21,7 +21,7 @@ import org.apache.logging.log4j.Logger; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; -import org.eclipse.passage.lic.emf.ecore.LicensingEcore; +import org.eclipse.passage.lic.emf.validation.ErrorMessages; import org.eclipse.passage.lic.internal.api.LicensedProduct; import org.eclipse.passage.lic.internal.api.LicensingException; import org.eclipse.passage.lic.internal.api.io.StreamCodec; @@ -97,7 +97,7 @@ private Optional validate(ProductVersionDescriptor target) { if (!(target instanceof ProductVersion)) { return Optional.empty(); } - return Optional.ofNullable(LicensingEcore.extractValidationError(((ProductVersion) target).getProduct())); + return new ErrorMessages().apply(((ProductVersion) target).getProduct()); } private Optional keyIsPresent(LicensedProduct target) throws LicensingException {