Skip to content

Commit

Permalink
add intention related methods and qualification checker
Browse files Browse the repository at this point in the history
  • Loading branch information
ruadrian committed Apr 24, 2024
1 parent 36844b1 commit 712424a
Show file tree
Hide file tree
Showing 10 changed files with 926 additions and 42 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ch.sbb.scion.rcp.microfrontend.e3.app.demo.view.model;

import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.WritableValue;

public class ApplicationQualificationCheckModel {

private final IObservableValue<String> capabilityId = new WritableValue<>("", String.class);

private final IObservableValue<String> appSymbolicName = new WritableValue<>("", String.class);

public IObservableValue<String> getCapabilityId() {
return capabilityId;
}

public IObservableValue<String> getAppSymbolicName() {
return appSymbolicName;
}

public void clearValues() {
capabilityId.setValue("");
appSymbolicName.setValue("");
}

public AppSymbolicNameAndCapabilityId getAppSymbolicNameAndCapabilityId() {
return new AppSymbolicNameAndCapabilityId(appSymbolicName.getValue(), capabilityId.getValue());
}

public record AppSymbolicNameAndCapabilityId(String appSymbolicName, String capabilityId) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ch.sbb.scion.rcp.microfrontend.e3.app.demo.view.model;

import java.util.Map.Entry;

import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.WritableValue;

import ch.sbb.scion.rcp.microfrontend.model.Intention;
import ch.sbb.scion.rcp.microfrontend.model.Qualifier;

public class IntentionModel {

private final IObservableValue<String> type = new WritableValue<>("", String.class);

private final IObservableList<Entry<String, String>> qualifiers = new WritableList<>();

public IObservableValue<String> getType() {
return type;
}

public IObservableList<Entry<String, String>> getQualifiers() {
return qualifiers;
}

public void clearValues() {
type.setValue("");
qualifiers.clear();
}

public Intention getIntention() {
var intentionBuilder = Intention.builder().type(type.getValue());
if (!qualifiers.isEmpty()) {
var qualifier = new Qualifier();
qualifiers.stream().forEach(x -> qualifier.set(x.getKey(), x.getValue()));
intentionBuilder.qualifier(qualifier);
}
return intentionBuilder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(() => {
// ======= SCRIPT PLACEHOLDERS BEGIN =======
const helpers = {fromJson: /@@helpers.fromJson@@/};
const refs = {ManifestService: /@@refs.ManifestService@@/};
const appSymbolicName = helpers.fromJson('/@@appSymbolicName@@/')
const qualifiedFor = {
capabilityId: helpers.fromJson('/@@qualifiedFor.capabilityId@@/'),
};
// ======= SCRIPT PLACEHOLDERS END =======

return refs.ManifestService.isApplicationQualified$(appSymbolicName, qualifiedFor);
})()
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(() => {
// ======= SCRIPT PLACEHOLDERS BEGIN =======
const helpers = {fromJson: /@@helpers.fromJson@@/};
const refs = {ManifestService: /@@refs.ManifestService@@/};
const filter = {
id: helpers.fromJson('/@@filter.id@@/'),
type: helpers.fromJson('/@@filter.type@@/'),
qualifier: helpers.fromJson('/@@filter.qualifier@@/'),
appSymbolicName: helpers.fromJson('/@@filter.appSymbolicName@@/'),
};
// ======= SCRIPT PLACEHOLDERS END =======

return refs.ManifestService.lookupIntentions$({
id: filter.id ?? undefined,
type: filter.type ?? undefined,
qualifier: filter.qualifier ?? undefined,
appSymbolicName: filter.appSymbolicName ?? undefined,
});
})()
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// ======= SCRIPT PLACEHOLDERS BEGIN =======
const helpers = {fromJson: /@@helpers.fromJson@@/};
const callback = window['/@@callback@@/'];
const intention = helpers.fromJson('/@@intention@@/');
const refs = {ManifestService: /@@refs.ManifestService@@/};
// ======= SCRIPT PLACEHOLDERS END =======

try {
const id = await refs.ManifestService.registerIntention(intention);
callback(null, id);
}
catch (error) {
callback(error.message || `${error}` || 'ERROR');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// ======= SCRIPT PLACEHOLDERS BEGIN =======
const helpers = {fromJson: /@@helpers.fromJson@@/};
const callback = window['/@@callback@@/'];
const refs = {ManifestService: /@@refs.ManifestService@@/};
const filter = {
id: helpers.fromJson('/@@filter.id@@/'),
type: helpers.fromJson('/@@filter.type@@/'),
qualifier: helpers.fromJson('/@@filter.qualifier@@/'),
appSymbolicName: helpers.fromJson('/@@filter.appSymbolicName@@/'),
};
// ======= SCRIPT PLACEHOLDERS END =======

try {
await refs.ManifestService.unregisterIntentions({
id: filter.id ?? undefined,
type: filter.type ?? undefined,
qualifier: filter.qualifier ?? undefined,
appSymbolicName: filter.appSymbolicName ?? undefined,
});
callback(null);
}
catch (error) {
callback(error.message || `${error}` || 'ERROR');
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import ch.sbb.scion.rcp.microfrontend.model.Application;
import ch.sbb.scion.rcp.microfrontend.model.Capability;
import ch.sbb.scion.rcp.microfrontend.model.CapabilityIdentifier;
import ch.sbb.scion.rcp.microfrontend.model.Intention;
import ch.sbb.scion.rcp.microfrontend.model.ManifestObjectFilter;
import ch.sbb.scion.rcp.microfrontend.subscriber.ISubscriber;
import ch.sbb.scion.rcp.microfrontend.subscriber.ISubscription;
Expand All @@ -20,12 +22,12 @@ public interface ManifestService {
CompletableFuture<List<Application>> getApplications();

/**
* @see "https://scion-microfrontend-platform-api.vercel.app/classes/ManifestService.html#lookupCapabilities"
* @see "https://scion-microfrontend-platform-api.vercel.app/classes/ManifestService.html#lookupCapabilities_"
*/
ISubscription lookupCapabilities(final ISubscriber<List<Capability>> subscriber);

/**
* @see "https://scion-microfrontend-platform-api.vercel.app/classes/ManifestService.html#lookupCapabilities"
* @see "https://scion-microfrontend-platform-api.vercel.app/classes/ManifestService.html#lookupCapabilities_"
*/
ISubscription lookupCapabilities(final ManifestObjectFilter filter, final ISubscriber<List<Capability>> subscriber);

Expand All @@ -44,4 +46,34 @@ public interface ManifestService {
*/
CompletableFuture<Void> unregisterCapabilities(final ManifestObjectFilter filter);

/**
* @see "https://scion-microfrontend-platform-api.vercel.app/classes/ManifestService.html#lookupIntentions_"
*/
ISubscription lookupIntentions(final ISubscriber<List<Intention>> subscriber);

/**
* @see "https://scion-microfrontend-platform-api.vercel.app/classes/ManifestService.html#lookupIntentions_"
*/
ISubscription lookupIntentions(final ManifestObjectFilter filter, final ISubscriber<List<Intention>> subscriber);

/**
* @see "https://scion-microfrontend-platform-api.vercel.app/classes/ManifestService.html#registerIntention"
*/
CompletableFuture<String> registerIntention(final Intention intention);

/**
* @see "https://scion-microfrontend-platform-api.vercel.app/classes/ManifestService.html#unregisterIntentions"
*/
CompletableFuture<Void> unregisterIntentions();

/**
* @see "https://scion-microfrontend-platform-api.vercel.app/classes/ManifestService.html#unregisterIntentions"
*/
CompletableFuture<Void> unregisterIntentions(final ManifestObjectFilter filter);

/**
* @see "https://scion-microfrontend-platform-api.vercel.app/classes/ManifestService.html#isApplicationQualified_"
*/
ISubscription isApplicationQualified(final String appSymbolicName, final CapabilityIdentifier qualifiedFor,
final ISubscriber<Boolean> subscriber);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import ch.sbb.scion.rcp.microfrontend.internal.gson.GsonFactory;
import ch.sbb.scion.rcp.microfrontend.model.Application;
import ch.sbb.scion.rcp.microfrontend.model.Capability;
import ch.sbb.scion.rcp.microfrontend.model.CapabilityIdentifier;
import ch.sbb.scion.rcp.microfrontend.model.Intention;
import ch.sbb.scion.rcp.microfrontend.model.ManifestObjectFilter;
import ch.sbb.scion.rcp.microfrontend.script.Script;
import ch.sbb.scion.rcp.microfrontend.script.Script.Flags;
Expand Down Expand Up @@ -123,4 +125,95 @@ public CompletableFuture<Void> unregisterCapabilities(final ManifestObjectFilter
return unregistered;
}

@Override
public ISubscription lookupIntentions(final ISubscriber<List<Intention>> subscriber) {
return lookupIntentions(null, subscriber);
}

@Override
public ISubscription lookupIntentions(final ManifestObjectFilter filter, final ISubscriber<List<Intention>> subscriber) {
Objects.requireNonNull(subscriber);
var manifestObjectFilter = Optional.ofNullable(filter).orElse(new ManifestObjectFilter());
var observeIIFE = new Script(Resources.readString("js/sci-manifest-service/lookup-intentions.iife.js"))
.replacePlaceholder("refs.ManifestService", Refs.ManifestService)
.replacePlaceholder("filter.id", manifestObjectFilter.id(), Flags.ToJson)
.replacePlaceholder("filter.type", manifestObjectFilter.type(), Flags.ToJson)
.replacePlaceholder("filter.qualifier", manifestObjectFilter.qualifier(), Flags.ToJson)
.replacePlaceholder("filter.appSymbolicName", manifestObjectFilter.appSymbolicName(), Flags.ToJson)
.replacePlaceholder("helpers.fromJson", Helpers.fromJson).substitute();

var observable = new RxJsObservable<List<Intention>>(microfrontendPlatformRcpHost.whenHostBrowser, observeIIFE,
new ParameterizedType(List.class, Intention.class));
return observable.subscribe(subscriber);
}

@Override
public CompletableFuture<String> registerIntention(final Intention intention) {
Objects.requireNonNull(intention);
var registered = new CompletableFuture<String>();
new JavaCallback(microfrontendPlatformRcpHost.whenHostBrowser, args -> {
var error = args[0];
if (error == null) {
registered.complete((String) args[1]);
}
else {
registered.completeExceptionally(new RuntimeException((String) error));
}
}).installOnce().thenAccept(callback -> {
new JavaScriptExecutor(microfrontendPlatformRcpHost.hostBrowser,
Resources.readString("js/sci-manifest-service/register-intention.js")).replacePlaceholder("callback", callback.name)
.replacePlaceholder("intention", intention, Flags.ToJson).replacePlaceholder("refs.ManifestService", Refs.ManifestService)
.replacePlaceholder("helpers.fromJson", Helpers.fromJson).runInsideAsyncFunction().execute();
});

return registered;
}

@Override
public CompletableFuture<Void> unregisterIntentions() {
return unregisterIntentions(null);
}

@Override
public CompletableFuture<Void> unregisterIntentions(final ManifestObjectFilter filter) {
var unregistered = new CompletableFuture<Void>();
var manifestObjectFilter = Optional.ofNullable(filter).orElse(new ManifestObjectFilter());
new JavaCallback(microfrontendPlatformRcpHost.whenHostBrowser, args -> {
var error = args[0];
if (error == null) {
unregistered.complete(null);
}
else {
unregistered.completeExceptionally(new RuntimeException((String) error));
}
}).installOnce().thenAccept(callback -> {
new JavaScriptExecutor(microfrontendPlatformRcpHost.hostBrowser,
Resources.readString("js/sci-manifest-service/unregister-intentions.js")).replacePlaceholder("callback", callback.name)
.replacePlaceholder("refs.ManifestService", Refs.ManifestService)
.replacePlaceholder("filter.id", manifestObjectFilter.id(), Flags.ToJson)
.replacePlaceholder("filter.type", manifestObjectFilter.type(), Flags.ToJson)
.replacePlaceholder("filter.qualifier", manifestObjectFilter.qualifier(), Flags.ToJson)
.replacePlaceholder("filter.appSymbolicName", manifestObjectFilter.appSymbolicName(), Flags.ToJson)
.replacePlaceholder("helpers.fromJson", Helpers.fromJson).runInsideAsyncFunction().execute();
});

return unregistered;
}

@Override
public ISubscription isApplicationQualified(final String appSymbolicName, final CapabilityIdentifier qualifiedFor,
final ISubscriber<Boolean> subscriber) {
Objects.requireNonNull(appSymbolicName);
Objects.requireNonNull(qualifiedFor);
Objects.requireNonNull(qualifiedFor.capabilityId());
Objects.requireNonNull(subscriber);
var observeIIFE = new Script(Resources.readString("js/sci-manifest-service/is-application-qualified.iife.js"))
.replacePlaceholder("refs.ManifestService", Refs.ManifestService)
.replacePlaceholder("appSymbolicName", appSymbolicName, Flags.ToJson)
.replacePlaceholder("qualifiedFor.capabilityId", qualifiedFor.capabilityId(), Flags.ToJson)
.replacePlaceholder("helpers.fromJson", Helpers.fromJson).substitute();

var observable = new RxJsObservable<Boolean>(microfrontendPlatformRcpHost.whenHostBrowser, observeIIFE, Boolean.class);
return observable.subscribe(subscriber);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Project: RCS - Rail Control System
*
* © Copyright by SBB AG, Alle Rechte vorbehalten
*/
package ch.sbb.scion.rcp.microfrontend.model;

/**
* Identifies a capability
*/
public interface CapabilityIdentifier {

/**
* @return the id of the represented capability, not null
*/
String capabilityId();
}

0 comments on commit 712424a

Please sign in to comment.