Skip to content

Commit

Permalink
feat(viz): Filter appropriate components for Camel Route nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
lordrip committed Nov 3, 2023
1 parent 5618419 commit 9d0a747
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 27 deletions.
13 changes: 13 additions & 0 deletions packages/ui/src/camel-utils/camel-to-tile.adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ describe('camelComponentToTile', () => {
expect(tile.tags).toEqual(['label1', 'label2']);
expect(tile.version).toEqual('4.0.0');
});

it('should populate tags with `consumerOnly` and `producerOnly` when applicable', () => {
const componentDef = {
component: {
consumerOnly: true,
producerOnly: true,
},
} as ICamelComponentDefinition;

const tile = camelComponentToTile(componentDef);

expect(tile.tags).toEqual(['consumerOnly', 'producerOnly']);
});
});

describe('camelProcessorToTile', () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/ui/src/camel-utils/camel-to-tile.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export const camelComponentToTile = (componentDef: ICamelComponentDefinition): I
if (label) {
tags.push(...label.split(','));
}
if (componentDef.component.consumerOnly) {
tags.push('consumerOnly');
}
if (componentDef.component.producerOnly) {
tags.push('producerOnly');
}

return {
type: CatalogKind.Component,
Expand Down
47 changes: 44 additions & 3 deletions packages/ui/src/models/camel/camel-route-resource.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { RouteDefinition } from '@kaoto-next/camel-catalog/types';
import { ITile } from '../../components/Catalog';
import { isDefined } from '../../utils';
import { CatalogFilter } from '../catalog-filter';
import { CatalogKind } from '../catalog-kind';
import { AddStepMode } from '../visualization/base-visual-entity';
import { CamelRouteVisualEntity, isCamelRoute } from '../visualization/flows';
import { flowTemplateService } from '../visualization/flows/flow-templates-service';
import { CamelComponentSchemaService } from '../visualization/flows/support/camel-component-schema.service';
import { CamelRouteVisualEntityData } from '../visualization/flows/support/camel-component-types';
import { BeansEntity, isBeans } from '../visualization/metadata';
import { BeansAwareResource, CamelResource } from './camel-resource';
Expand Down Expand Up @@ -95,10 +96,50 @@ export class CamelRouteResource implements CamelResource, BeansAwareResource {

/** Components Catalog related methods */
getCompatibleComponents(mode: AddStepMode, visualEntityData: CamelRouteVisualEntityData): CatalogFilter {
return {
filterFunction: this.getFilterFunction(mode, visualEntityData),
};
}

private getFilterFunction(mode: AddStepMode, visualEntityData: CamelRouteVisualEntityData): (item: ITile) => boolean {
if (mode === AddStepMode.ReplaceStep && visualEntityData.path === 'from') {
/**
* For the `from` step we want to show only components which are not `producerOnly`,
* as this mean that they can be used only as a consumer.
*/
return (item: ITile) => {
return item.type === CatalogKind.Component && !item.tags.includes('producerOnly');
};
}

if (mode === AddStepMode.InsertSpecialChildStep) {
return CamelComponentSchemaService.getCompatibleComponents(visualEntityData.processorName);
/**
* specialChildren is a map of processor names and their special children.
*/
const specialChildren: Record<string, string[]> = {
choice: ['when', 'otherwise'],
doTry: ['doCatch', 'doFinally'],
};

/**
* For special child steps, we need to check which type of processor it is, in order to determine
* what kind of components we want to show.
*/
return (item: ITile) => {
if (item.type !== CatalogKind.Processor || specialChildren[visualEntityData.processorName] === undefined) {
return false;
}

return specialChildren[visualEntityData.processorName].includes(item.name);
};
}

return {};
/**
* For the rest, we want to filter out components that are `consumerOnly`,
* as this mean that they can be used only as a consumer.
*/
return (item: ITile) => {
return !item.tags.includes('consumerOnly');
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,11 @@ export class CamelRouteVisualEntity implements BaseVisualCamelEntity {
const stepsProperties = CamelComponentSchemaService.getProcessorStepsProperties(
(data as CamelRouteVisualEntityData).processorName as keyof ProcessorDefinition,
);
const catalogFilter = CamelComponentSchemaService.getCompatibleComponents(
(data as CamelRouteVisualEntityData).processorName as keyof ProcessorDefinition,
);
const canHavePreviousStep = CamelComponentSchemaService.canHavePreviousStep(
(data as CamelRouteVisualEntityData).processorName,
);
const canHaveChildren = stepsProperties.find((property) => property.type === 'branch') !== undefined;
const canHaveSpecialChildren = Object.keys(catalogFilter).length > 0;
const canHaveSpecialChildren = Object.keys(stepsProperties).length > 1;

return {
canHavePreviousStep,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { JSONSchemaType } from 'ajv';
import { isDefined } from '../../../../utils';
import { ICamelComponentProperty } from '../../../camel-components-catalog';
import { ICamelProcessorProperty } from '../../../camel-processors-catalog';
import { CatalogFilter } from '../../../catalog-filter';
import { CatalogKind } from '../../../catalog-kind';
import { VisualComponentSchema } from '../../base-visual-entity';
import { CamelCatalogService } from '../camel-catalog.service';
Expand Down Expand Up @@ -77,25 +76,6 @@ export class CamelComponentSchemaService {
return !this.DISABLED_SIBLING_STEPS.includes(processorName);
}

static getCompatibleComponents(processorName: keyof ProcessorDefinition): CatalogFilter {
switch (processorName) {
case 'choice':
return {
kinds: [CatalogKind.Processor],
names: ['when', 'otherwise'],
};

case 'doTry':
return {
kinds: [CatalogKind.Processor],
names: ['doCatch', 'doFinally'],
};

default:
return {};
}
}

static getProcessorStepsProperties(processorName: keyof ProcessorDefinition): CamelProcessorStepsProperties[] {
switch (processorName) {
/** choice */ case 'when':
Expand Down

0 comments on commit 9d0a747

Please sign in to comment.