diff --git a/bundles/org.eclipse.passage.lic.licenses.migration/src/org/eclipse/passage/lic/internal/licenses/migration/tobemoved/BaseXmiConditionTransport.java b/bundles/org.eclipse.passage.lic.licenses.migration/src/org/eclipse/passage/lic/internal/licenses/migration/tobemoved/BaseXmiConditionTransport.java new file mode 100644 index 000000000..361be46b6 --- /dev/null +++ b/bundles/org.eclipse.passage.lic.licenses.migration/src/org/eclipse/passage/lic/internal/licenses/migration/tobemoved/BaseXmiConditionTransport.java @@ -0,0 +1,109 @@ +/******************************************************************************* + * Copyright (c) 2020, 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.internal.licenses.migration.tobemoved; + +import java.io.IOException; +import java.io.InputStream; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.xmi.XMLResource; +import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl; +import org.eclipse.passage.lic.internal.api.EvaluationType; +import org.eclipse.passage.lic.internal.api.conditions.Condition; +import org.eclipse.passage.lic.internal.api.conditions.MatchingRule; +import org.eclipse.passage.lic.internal.api.conditions.mining.ConditionTransport; +import org.eclipse.passage.lic.internal.api.conditions.mining.ContentType; +import org.eclipse.passage.lic.internal.base.conditions.BaseCondition; +import org.eclipse.passage.lic.internal.base.conditions.BaseEvaluationInstructions; +import org.eclipse.passage.lic.internal.base.conditions.BaseValidityPeriodClosed; +import org.eclipse.passage.lic.internal.base.conditions.BaseVersionMatch; +import org.eclipse.passage.lic.internal.base.conditions.MatchingRuleDefault; +import org.eclipse.passage.lic.internal.base.conditions.MatchingRuleForIdentifier; +import org.eclipse.passage.lic.internal.licenses.migration.LicensesResourceHandler; +import org.eclipse.passage.lic.licenses.model.api.LicenseGrant; +import org.eclipse.passage.lic.licenses.model.api.LicensePack; + +abstract class BaseXmiConditionTransport implements ConditionTransport { + + private final ContentType type = new ContentType.Xml(); + private final Predicate filter; + + protected BaseXmiConditionTransport() { + this(p -> true); + } + + protected BaseXmiConditionTransport(Predicate filter) { + this.filter = filter; + } + + @Override + public ContentType id() { + return type; + } + + @Override + public Collection read(InputStream input) throws IOException { + Resource resource = new XMIResourceImpl(); + resource.load(input, + Collections.singletonMap(XMLResource.OPTION_RESOURCE_HANDLER, new LicensesResourceHandler())); + return resource.getContents().stream() // + .filter(LicensePack.class::isInstance) // + .map(LicensePack.class::cast) // + .filter(filter) // + .map(LicensePack::getLicenseGrants) // + .flatMap(i -> StreamSupport.stream(i.spliterator(), false)) // + .map(this::condition) // + .collect(Collectors.toList()); + } + + private Condition condition(LicenseGrant descriptor) { + return new BaseCondition(descriptor.getIdentifier(), // + descriptor.getFeatureIdentifier(), // + new BaseVersionMatch(descriptor.getMatchVersion(), // + rule(descriptor.getMatchRule())), // + new BaseValidityPeriodClosed(// + fromDate(descriptor.getValidFrom()), // + fromDate(descriptor.getValidUntil())), // + new BaseEvaluationInstructions(// + new EvaluationType.Of(descriptor.getConditionType()), // + descriptor.getConditionExpression())); + } + + /** + * It looks like default matching rule is not persisted my EMF, this we expect + * {@code null} here + */ + private MatchingRule rule(String origin) { + if (origin == null) { + return new MatchingRuleDefault(); + } + return new MatchingRuleForIdentifier(origin).get(); + } + + private ZonedDateTime fromDate(Date date) { + if (date == null) { + // it should be in the past to be marked as invalid + return ZonedDateTime.now().minusMinutes(1); + } + return ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); + } + +} diff --git a/bundles/org.eclipse.passage.lic.licenses.migration/src/org/eclipse/passage/lic/internal/licenses/migration/tobemoved/UserFilteringConditionTransport.java b/bundles/org.eclipse.passage.lic.licenses.migration/src/org/eclipse/passage/lic/internal/licenses/migration/tobemoved/UserFilteringConditionTransport.java new file mode 100644 index 000000000..e5e91c347 --- /dev/null +++ b/bundles/org.eclipse.passage.lic.licenses.migration/src/org/eclipse/passage/lic/internal/licenses/migration/tobemoved/UserFilteringConditionTransport.java @@ -0,0 +1,25 @@ +/******************************************************************************* + * Copyright (c) 2020, 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.internal.licenses.migration.tobemoved; + +import java.util.Objects; +import java.util.function.Supplier; + +public final class UserFilteringConditionTransport extends BaseXmiConditionTransport { + + public UserFilteringConditionTransport(Supplier user) { + super(pack -> user.get().equals(pack.getUserIdentifier())); + Objects.requireNonNull(user, "UserFilteringConditionTransport::user"); //$NON-NLS-1$ + } + +} diff --git a/bundles/org.eclipse.passage.lic.licenses.migration/src/org/eclipse/passage/lic/internal/licenses/migration/tobemoved/XmiConditionTransport.java b/bundles/org.eclipse.passage.lic.licenses.migration/src/org/eclipse/passage/lic/internal/licenses/migration/tobemoved/XmiConditionTransport.java index 2a2f48764..81e3649c5 100644 --- a/bundles/org.eclipse.passage.lic.licenses.migration/src/org/eclipse/passage/lic/internal/licenses/migration/tobemoved/XmiConditionTransport.java +++ b/bundles/org.eclipse.passage.lic.licenses.migration/src/org/eclipse/passage/lic/internal/licenses/migration/tobemoved/XmiConditionTransport.java @@ -12,87 +12,6 @@ *******************************************************************************/ package org.eclipse.passage.lic.internal.licenses.migration.tobemoved; -import java.io.IOException; -import java.io.InputStream; -import java.time.ZoneId; -import java.time.ZonedDateTime; -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.stream.Collectors; -import java.util.stream.StreamSupport; - -import org.eclipse.emf.ecore.resource.Resource; -import org.eclipse.emf.ecore.xmi.XMLResource; -import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl; -import org.eclipse.passage.lic.internal.api.EvaluationType; -import org.eclipse.passage.lic.internal.api.conditions.Condition; -import org.eclipse.passage.lic.internal.api.conditions.MatchingRule; -import org.eclipse.passage.lic.internal.api.conditions.mining.ConditionTransport; -import org.eclipse.passage.lic.internal.api.conditions.mining.ContentType; -import org.eclipse.passage.lic.internal.base.conditions.BaseCondition; -import org.eclipse.passage.lic.internal.base.conditions.BaseEvaluationInstructions; -import org.eclipse.passage.lic.internal.base.conditions.BaseValidityPeriodClosed; -import org.eclipse.passage.lic.internal.base.conditions.BaseVersionMatch; -import org.eclipse.passage.lic.internal.base.conditions.MatchingRuleDefault; -import org.eclipse.passage.lic.internal.base.conditions.MatchingRuleForIdentifier; -import org.eclipse.passage.lic.internal.licenses.migration.LicensesResourceHandler; -import org.eclipse.passage.lic.licenses.model.api.LicenseGrant; -import org.eclipse.passage.lic.licenses.model.api.LicensePack; - -public final class XmiConditionTransport implements ConditionTransport { - - private final ContentType type = new ContentType.Xml(); - - @Override - public ContentType id() { - return type; - } - - @Override - public Collection read(InputStream input) throws IOException { - Resource resource = new XMIResourceImpl(); - resource.load(input, - Collections.singletonMap(XMLResource.OPTION_RESOURCE_HANDLER, new LicensesResourceHandler())); - return resource.getContents().stream() // - .filter(LicensePack.class::isInstance) // - .map(LicensePack.class::cast) // - .map(LicensePack::getLicenseGrants) // - .flatMap(i -> StreamSupport.stream(i.spliterator(), false)) // - .map(this::condition) // - .collect(Collectors.toList()); - } - - private Condition condition(LicenseGrant descriptor) { - return new BaseCondition(descriptor.getIdentifier(), // - descriptor.getFeatureIdentifier(), // - new BaseVersionMatch(descriptor.getMatchVersion(), // - rule(descriptor.getMatchRule())), // - new BaseValidityPeriodClosed(// - fromDate(descriptor.getValidFrom()), // - fromDate(descriptor.getValidUntil())), // - new BaseEvaluationInstructions(// - new EvaluationType.Of(descriptor.getConditionType()), // - descriptor.getConditionExpression())); - } - - /** - * It looks like default matching rule is not persisted my EMF, this we expect - * {@code null} here - */ - private MatchingRule rule(String origin) { - if (origin == null) { - return new MatchingRuleDefault(); - } - return new MatchingRuleForIdentifier(origin).get(); - } - - private ZonedDateTime fromDate(Date date) { - if (date == null) { - // it should be in the past to be marked as invalid - return ZonedDateTime.now().minusMinutes(1); - } - return ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); - } +public final class XmiConditionTransport extends BaseXmiConditionTransport { }