Skip to content

Commit

Permalink
Bug 573296 - [Passage] rework EMF migration facilities, part 4
Browse files Browse the repository at this point in the history
Extract validation error messages

Signed-off-by: Alexander Fedorov <[email protected]>
  • Loading branch information
ruspl-afed committed May 2, 2021
1 parent a7a5b3e commit 3d5f4fc
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 44 deletions.
1 change: 1 addition & 0 deletions bundles/org.eclipse.passage.lic.emf/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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<EObject, Optional<String>> {

@Override
public Optional<String> 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());
}

}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -43,6 +44,7 @@ public class IssueLicensePackPage extends WizardPage {

private final IEclipseContext context;
private final Supplier<PersonalLicenseRequest> data;
private final ErrorMessages validate;
private LicensePack license;
private VViewModelProperties properties;
private Composite base;
Expand All @@ -51,6 +53,7 @@ protected IssueLicensePackPage(String name, Supplier<PersonalLicenseRequest> dat
super(name);
this.context = context;
this.data = data;
this.validate = new ErrorMessages();
setTitle(IssueLicensePageMessages.IssueLicensePackPage_page_title);
setDescription(IssueLicensePageMessages.IssueLicensePackPage_page_description);
}
Expand Down Expand Up @@ -115,9 +118,9 @@ private void updatePage() {
}

protected boolean validatePage() {
String errors = LicensingEcore.extractValidationError(license);
setErrorMessage(errors);
return errors == null;
Optional<String> errors = validate.apply(license);
setErrorMessage(errors.orElse(null));// framework requires null
return errors.isEmpty();
}

public LicensePackDescriptor pack() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,6 +44,7 @@ public final class IssueLicensePackPage extends WizardPage {

private final IEclipseContext context;
private final Supplier<FloatingLicenseRequest> data;
private final ErrorMessages validate;
private final Adapter update = new EContentAdapter() {
@Override
public void notifyChanged(Notification notification) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -127,9 +129,9 @@ private void renderEmfForms() {
}

protected boolean validatePage() {
String errors = LicensingEcore.extractValidationError(license);
setErrorMessage(errors);
return errors == null;
Optional<String> errors = validate.apply(license);
setErrorMessage(errors.orElse(null));// framework requires null
return errors.isEmpty();
}

FloatingLicensePack pack() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -110,7 +110,7 @@ private ServiceInvocationResult<IssuedFloatingLicense> persistLicenseFiles(Float
private ServiceInvocationResult<List<Path>> persist(EObject target, LicensedProduct product, //
Path folder, String decrypted, String encrypted) {
// validate
Optional<String> errors = Optional.ofNullable(LicensingEcore.extractValidationError(target));
Optional<String> errors = new ErrorMessages().apply(target);
if (errors.isPresent()) {
return new BaseServiceInvocationResult<>(new Trouble(new LicenseValidationFailed(), errors.get()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,9 +60,9 @@ final class IssuePersonalLicense {

ServiceInvocationResult<IssuedLicense> issue(Supplier<LicensePack> 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<String> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,7 +97,7 @@ private Optional<String> 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<String> keyIsPresent(LicensedProduct target) throws LicensingException {
Expand Down

0 comments on commit 3d5f4fc

Please sign in to comment.