This is an automated email from the ASF dual-hosted git repository.
marat pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-karavan.git
The following commit(s) were added to refs/heads/main by this push:
new b6eab68 Fix #27 (#28)
b6eab68 is described below
commit b6eab68b56b7a6aab858703e228c0e88435df1c8
Author: Marat Gubaidullin <[email protected]>
AuthorDate: Fri Oct 15 21:41:26 2021 -0400
Fix #27 (#28)
* Fix Choice When YAML DSL output
* Fix #27
---
karavan-designer/src/designer/api/CamelApi.tsx | 36 +++++-----
karavan-designer/src/designer/model/CamelModel.tsx | 11 +--
karavan-designer/src/designer/ui/DslElement.tsx | 5 +-
karavan-designer/src/designer/ui/DslProperties.tsx | 8 ++-
karavan-generator/pom.xml | 11 +++
.../karavan/generator/CamelModelGenerator.java | 84 ++++++++--------------
.../main/resources/CamelApi.addStep.choiceStep.tx | 20 ++++++
.../main/resources/CamelApi.addStep.otherwise.tx | 5 ++
.../src/main/resources/CamelApi.addStep.when.tx | 5 ++
.../src/main/resources/CamelApi.createOtherwise.tx | 6 ++
.../src/main/resources/CamelApi.createWhen.tx | 7 ++
.../src/main/resources/CamelApi.deleteWhen.tx | 12 ++++
12 files changed, 125 insertions(+), 85 deletions(-)
diff --git a/karavan-designer/src/designer/api/CamelApi.tsx
b/karavan-designer/src/designer/api/CamelApi.tsx
index 5e1c946..7bdb044 100644
--- a/karavan-designer/src/designer/api/CamelApi.tsx
+++ b/karavan-designer/src/designer/api/CamelApi.tsx
@@ -22,7 +22,7 @@ import { CamelElement,
RemoveHeadersStep,
IdempotentConsumerStep,
InterceptStep,
- WhenStep,
+ When,
ProcessStep,
OnFallbackStep,
MarshalStep,
@@ -266,12 +266,12 @@ export class CamelApi {
return interceptStep
}
- static createWhen = (element: any): WhenStep => {
- const whenStep = element ? new WhenStep({...element.when}) : new
WhenStep()
- whenStep.when.expression =
CamelApi.createExpression(element?.when?.expression)
- whenStep.when.steps = CamelApi.createSteps(element?.when?.steps)
- whenStep.uuid = element?.uuid ? element.uuid : whenStep.uuid
- return whenStep
+ static createWhen = (element: any): When => {
+ const when = element ? new When({...element}) : new When()
+ when.expression = CamelApi.createExpression(element?.expression)
+ when.steps = CamelApi.createSteps(element?.steps)
+ when.uuid = element?.uuid ? element.uuid : when.uuid
+ return when
}
static createProcess = (element: any): ProcessStep => {
@@ -612,7 +612,7 @@ export class CamelApi {
case 'removeHeadersStep': return (step as
RemoveHeadersStep).removeHeaders
case 'idempotentConsumerStep': return (step as
IdempotentConsumerStep).idempotentConsumer
case 'interceptStep': return (step as InterceptStep).intercept
- case 'whenStep': return (step as WhenStep).when
+ case 'when': return (step as When)
case 'processStep': return (step as ProcessStep).process
case 'onFallbackStep': return (step as OnFallbackStep).onFallback
case 'marshalStep': return (step as MarshalStep).marshal
@@ -671,13 +671,13 @@ export class CamelApi {
break;
case 'choiceStep':
const choiceChildren = (el as ChoiceStep).choice?.when ||
[];
- if (el.uuid === parentId && step.dslName === 'whenStep') {
- choiceChildren.push(step as WhenStep);
+ if (el.uuid === parentId && step.dslName === 'when') {
+ choiceChildren.push(step as When);
(el as ChoiceStep).choice.when = choiceChildren;
} else if (el.uuid === parentId && step.dslName ===
'otherwise' && !(el as ChoiceStep).choice.otherwise) {
(el as ChoiceStep).choice.otherwise = step;
} else {
- (el as ChoiceStep).choice.when =
CamelApi.addStep(choiceChildren, step, parentId) as WhenStep[];
+ (el as ChoiceStep).choice.when =
CamelApi.addStep(choiceChildren, step, parentId) as When[];
const otherwise = (el as ChoiceStep).choice.otherwise;
if (otherwise?.uuid === parentId){
otherwise.steps = otherwise.steps ?
[...otherwise.steps] : [];
@@ -784,10 +784,10 @@ export class CamelApi {
if (el.uuid === parentId) sagaChildren.push(step)
else (el as SagaStep).saga.steps =
CamelApi.addStep(sagaChildren, step, parentId);
break;
- case 'whenStep':
- const whenChildren = (el as WhenStep).when?.steps || [];
+ case 'when':
+ const whenChildren = (el as When).steps || [];
if (el.uuid === parentId) whenChildren.push(step)
- else (el as WhenStep).when.steps =
CamelApi.addStep(whenChildren, step, parentId);
+ else (el as When).steps = CamelApi.addStep(whenChildren,
step, parentId);
break;
case 'doFinallyStep':
const doFinallyChildren = (el as
DoFinallyStep).doFinally?.steps || [];
@@ -851,7 +851,7 @@ export class CamelApi {
case 'resequenceStep': (step as
ResequenceStep).resequence.steps = CamelApi.deleteStep((step as
ResequenceStep).resequence.steps, uuidToDelete); break;
case 'pipelineStep': (step as
PipelineStep).pipeline.steps = CamelApi.deleteStep((step as
PipelineStep).pipeline.steps, uuidToDelete); break;
case 'sagaStep': (step as SagaStep).saga.steps =
CamelApi.deleteStep((step as SagaStep).saga.steps, uuidToDelete); break;
- case 'whenStep': (step as WhenStep).when.steps =
CamelApi.deleteStep((step as WhenStep).when.steps, uuidToDelete); break;
+ case 'when': (step as When).steps =
CamelApi.deleteStep((step as When).steps, uuidToDelete); break;
case 'doFinallyStep': (step as
DoFinallyStep).doFinally.steps = CamelApi.deleteStep((step as
DoFinallyStep).doFinally.steps, uuidToDelete); break;
case 'filterStep': (step as FilterStep).filter.steps =
CamelApi.deleteStep((step as FilterStep).filter.steps, uuidToDelete); break;
case 'aggregateStep': (step as
AggregateStep).aggregate.steps = CamelApi.deleteStep((step as
AggregateStep).aggregate.steps, uuidToDelete); break;
@@ -864,12 +864,12 @@ export class CamelApi {
return result
}
- static deleteWhen = (whens: WhenStep[] | undefined, uuidToDelete: string):
WhenStep[] => {
- const result: WhenStep[] = []
+ static deleteWhen = (whens: When[] | undefined, uuidToDelete: string):
When[] => {
+ const result: When[] = []
if (whens !== undefined){
whens.forEach(when => {
if (when.uuid !== uuidToDelete) {
- when.when.steps = CamelApi.deleteStep(when.when.steps,
uuidToDelete);
+ when.steps = CamelApi.deleteStep(when.steps, uuidToDelete);
result.push(when);
}
})
diff --git a/karavan-designer/src/designer/model/CamelModel.tsx
b/karavan-designer/src/designer/model/CamelModel.tsx
index 0e5f42f..71f92cc 100644
--- a/karavan-designer/src/designer/model/CamelModel.tsx
+++ b/karavan-designer/src/designer/model/CamelModel.tsx
@@ -93,7 +93,7 @@ export class ThrowExceptionStep extends ProcessorStep {
export class Choice extends CamelElement {
inheritErrorHandler?: boolean
otherwise?: Otherwise
- when?: WhenStep [] = []
+ when?: When [] = []
public constructor(init?: Partial<Choice>) {
super('choice')
@@ -1268,15 +1268,6 @@ export class When extends CamelElement {
Object.assign(this, init)
}
}
-export class WhenStep extends ProcessorStep {
- when: When = new When()
-
- public constructor(init?: Partial<When>) {
- super('whenStep')
- Object.assign(this, {when: new When({...init})})
- }
-}
-
export class ClaimCheck extends CamelElement {
filter?: string
inheritErrorHandler?: boolean
diff --git a/karavan-designer/src/designer/ui/DslElement.tsx
b/karavan-designer/src/designer/ui/DslElement.tsx
index cb2acfd..06c9878 100644
--- a/karavan-designer/src/designer/ui/DslElement.tsx
+++ b/karavan-designer/src/designer/ui/DslElement.tsx
@@ -21,11 +21,10 @@ import {
import '../karavan.css';
import AddIcon from "@patternfly/react-icons/dist/js/icons/plus-circle-icon";
import DeleteIcon from "@patternfly/react-icons/dist/js/icons/times-icon";
-import {CamelElement, Otherwise, ProcessorStep, WhenStep} from
"../model/CamelModel";
+import {CamelElement, Otherwise, ProcessorStep, When} from
"../model/CamelModel";
import {CamelApi} from "../api/CamelApi";
import {CamelUi} from "../api/CamelUi";
import {EventBus} from "../api/EventBus";
-import {CamelApiExt} from "../api/CamelApiExt";
interface Props {
step: CamelElement,
@@ -88,7 +87,7 @@ export class DslElement extends React.Component<Props, State>
{
return (this.state.element as any).steps
}
- getWhens = (): WhenStep[] => {
+ getWhens = (): When[] => {
return (this.state.element as any).when
}
diff --git a/karavan-designer/src/designer/ui/DslProperties.tsx
b/karavan-designer/src/designer/ui/DslProperties.tsx
index fdab21c..8707e46 100644
--- a/karavan-designer/src/designer/ui/DslProperties.tsx
+++ b/karavan-designer/src/designer/ui/DslProperties.tsx
@@ -89,13 +89,19 @@ export class DslProperties extends React.Component<Props,
State> {
}
expressionChanged = (language: string, value: string | undefined) => {
+ console.log(this.state.step)
+ console.log(this.state.element)
if (this.state.step && this.state.element) {
const clone = (CamelYaml.cloneStep(this.state.step));
const e: any = {};
e.language = language;
e[language] = value;
const exp: any = new Expression(e);
- (clone as any)[this.state.element?.dslName].expression = exp;
+ if (this.state.element?.dslName === 'when'){
+ (clone as any).expression = exp;
+ } else {
+ (clone as any)[this.state.element?.dslName].expression = exp;
+ }
this.setStep(clone);
this.props.onPropertyUpdate?.call(this, clone,
this.state.step.uuid);
}
diff --git a/karavan-generator/pom.xml b/karavan-generator/pom.xml
index f7964a9..4603560 100644
--- a/karavan-generator/pom.xml
+++ b/karavan-generator/pom.xml
@@ -65,4 +65,15 @@
<version>${version.camel-kamelet}</version>
</dependency>
</dependencies>
+ <build>
+ <resources>
+ <resource>
+ <directory>src/main/resources</directory>
+ <filtering>true</filtering>
+ <includes>
+ <include>**/**</include>
+ </includes>
+ </resource>
+ </resources>
+ </build>
</project>
diff --git
a/karavan-generator/src/main/java/org/apache/camel/karavan/generator/CamelModelGenerator.java
b/karavan-generator/src/main/java/org/apache/camel/karavan/generator/CamelModelGenerator.java
index d472d10..0e060a5 100644
---
a/karavan-generator/src/main/java/org/apache/camel/karavan/generator/CamelModelGenerator.java
+++
b/karavan-generator/src/main/java/org/apache/camel/karavan/generator/CamelModelGenerator.java
@@ -98,7 +98,7 @@ public final class CamelModelGenerator {
models.forEach((name, elementProps) -> {
String pcode = generateElementCode(name, elementProps);
camelModel.append(pcode).append(System.lineSeparator());
- if (!name.equalsIgnoreCase("otherwise")){
+ if (!name.equalsIgnoreCase("otherwise") &&
!name.equalsIgnoreCase("when")){
camelModel.append(generateStepCode(name)).append(System.lineSeparator());
}
});
@@ -118,8 +118,10 @@ public final class CamelModelGenerator {
camelApi.append(" FromStep, \n");
camelApi.append(" Expression, \n");
processors.values().forEach(s -> {
- if (s.toLowerCase().equals("otherwise")){
+ if (s.equalsIgnoreCase("otherwise")) {
camelApi.append(" Otherwise, \n");
+ } else if (s.equalsIgnoreCase("when")) {
+ camelApi.append(" When, \n");
} else {
camelApi.append(" ").append(s).append("Step, \n");
}
@@ -166,8 +168,10 @@ public final class CamelModelGenerator {
" switch (step.dslName){\n" +
" case 'fromStep' : return (step as
FromStep).from\n");
processors.values().forEach(s -> {
- if (s.toLowerCase().startsWith("otherwise")){
+ if (s.equalsIgnoreCase("otherwise")) {
camelApi.append(" case 'otherwise': return (step as
Otherwise)\n");
+ } else if (s.equalsIgnoreCase("when")){
+ camelApi.append(" case 'when': return (step as
When)\n");
} else {
camelApi.append(" case
'").append(deCapitalize(s)).append("Step': return (step as
").append(capitalize(s)).append("Step).").append(deCapitalize(s)).append("\n");
}
@@ -189,34 +193,11 @@ public final class CamelModelGenerator {
String stepField = deCapitalize(s.getKey()).concat("Step");
if (name.equals("choice")){
- camelApi.append(
- " case 'choiceStep':\n" +
- " const choiceChildren =
(el as ChoiceStep).choice?.when || [];\n" +
- " if (el.uuid === parentId
&& step.dslName === 'whenStep') {\n" +
- "
choiceChildren.push(step as WhenStep);\n" +
- " (el as
ChoiceStep).choice.when = choiceChildren;\n" +
- " } else if (el.uuid ===
parentId && step.dslName === 'otherwise' && !(el as
ChoiceStep).choice.otherwise) {\n" +
- " (el as
ChoiceStep).choice.otherwise = step;\n" +
- " } else {\n" +
- " (el as
ChoiceStep).choice.when = CamelApi.addStep(choiceChildren, step, parentId) as
WhenStep[];\n" +
- " const otherwise = (el
as ChoiceStep).choice.otherwise;\n" +
- " if (otherwise?.uuid
=== parentId){\n" +
- " otherwise.steps =
otherwise.steps ? [...otherwise.steps] : [];\n" +
- "
otherwise.steps.push(step);\n" +
- " (el as
ChoiceStep).choice.otherwise = otherwise\n" +
- " } else if (otherwise
&& otherwise.steps && otherwise.steps.length > 0){\n" +
- " otherwise.steps =
CamelApi.addStep(otherwise.steps, step, parentId);\n" +
- " (el as
ChoiceStep).choice.otherwise = otherwise;\n" +
- " }\n" +
- " }\n" +
- " break;\n");
+
camelApi.append(getTemplateFile("CamelApi.addStep.choiceStep.tx").concat("\n"));
} else if (name.equals("otherwise")){
- camelApi.append(
- " case 'otherwise':\n" +
- " const otherwiseChildren = (el as
Otherwise).steps || [];\n" +
- " if (el.uuid === parentId)
otherwiseChildren.push(step)\n" +
- " else (el as Otherwise).steps =
CamelApi.addStep(otherwiseChildren, step, parentId);\n" +
- " break;\n");
+
camelApi.append(getTemplateFile("CamelApi.addStep.otherwise.tx").concat("\n"));
+ } else if (name.equals("when")){
+
camelApi.append(getTemplateFile("CamelApi.addStep.when.tx").concat("\n"));
} else if (s.getValue().stream().filter(e ->
e.name.equals("steps")).count() > 0) {
camelApi.append(String.format(
" case '%1$s':\n" +
@@ -247,12 +228,12 @@ public final class CamelModelGenerator {
String name = deCapitalize(s.getKey());
String stepClass = capitalize(s.getKey()).concat("Step");
String stepField = deCapitalize(s.getKey()).concat("Step");
- if (name.equals("otherwise")){
+ if (name.equals("otherwise")) {
camelApi.append(" case 'otherwise':
(step as Otherwise).steps = CamelApi.deleteStep((step as Otherwise).steps,
uuidToDelete); break;\n");
+ } else if (name.equals("when")){
+ camelApi.append(" case 'when': (step as
When).steps = CamelApi.deleteStep((step as When).steps, uuidToDelete);
break;\n");
} else if (name.equals("choice")){
camelApi.append(" case 'choiceStep':\n"
+
- " console.log(step);\n" +
- "
console.log(uuidToDelete);\n" +
" const otherwise = (step
as ChoiceStep).choice.otherwise;\n" +
" if (otherwise &&
otherwise.uuid === uuidToDelete) {\n" +
" (step as
ChoiceStep).choice.otherwise = undefined;\n" +
@@ -275,19 +256,7 @@ public final class CamelModelGenerator {
" }\n" +
" return result\n" +
" }\n\n");
- camelApi.append(
- " static deleteWhen = (whens: WhenStep[] | undefined,
uuidToDelete: string): WhenStep[] => {\n" +
- " const result: WhenStep[] = []\n" +
- " if (whens !== undefined){\n" +
- " whens.forEach(when => {\n" +
- " if (when.uuid !== uuidToDelete) {\n" +
- " when.when.steps =
CamelApi.deleteStep(when.when.steps, uuidToDelete);\n" +
- " result.push(when);\n" +
- " }\n" +
- " })\n" +
- " }\n" +
- " return result\n" +
- " }\n\n");
+
camelApi.append(getTemplateFile("CamelApi.deleteWhen.tx").concat("\n\n"));
// Expression language finder
camelApi.append(" static getExpressionLanguage = (init?:
Partial<Expression>): string | undefined => {\n");
@@ -350,12 +319,9 @@ public final class CamelModelGenerator {
private String createCreateFunction(String name, List<ElementProp>
elProps) {
if (name.equalsIgnoreCase("otherwise")){
- return " static createOtherwise = (element: any): Otherwise =>
{\n" +
- " const otherwise = element ? new
Otherwise({...element}) : new Otherwise();\n" +
- " otherwise.steps =
CamelApi.createSteps(element?.steps);\n" +
- " if (element?.uuid) otherwise.uuid =
element?.uuid;\n" +
- " return otherwise\n" +
- " }\n\n";
+ return
getTemplateFile("CamelApi.createOtherwise.tx").concat("\n\n");
+ } else if (name.equalsIgnoreCase("when")){
+ return getTemplateFile("CamelApi.createWhen.tx").concat("\n\n");
}
String stepClass = capitalize(name).concat("Step");
String stepField = deCapitalize(name).concat("Step");
@@ -414,7 +380,8 @@ public final class CamelModelGenerator {
String arrayTypeClass =
getArrayTypeClass(properties.getJsonObject(s));
String arrayType = processors.get(arrayTypeClass);
if (arrayType != null) {
- props.add(new ElementProp(propName, type, false,
true, true, arrayType, false, getTypeCode(type, arrayType.concat("Step"))));
+ String typeCode = propName.equals("when") ?
getTypeCode(type, arrayType) : getTypeCode(type, arrayType.concat("Step"));
+ props.add(new ElementProp(propName, type, false,
true, true, arrayType, false, typeCode));
} else if
(arrayTypeClass.equals("org.apache.camel.model.ProcessorDefinition")) {
props.add(new ElementProp(propName, type, false,
true, true, arrayType, true, getTypeCode(type, "ProcessorStep")));
}
@@ -546,4 +513,15 @@ public final class CamelModelGenerator {
}
}
+ public String getTemplateFile(String name) {
+ try {
+ InputStream inputStream =
CamelModelGenerator.class.getClassLoader().getResourceAsStream(name);
+ String data = new BufferedReader(new
InputStreamReader(inputStream))
+
.lines().collect(Collectors.joining(System.getProperty("line.separator")));
+ return data;
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
}
diff --git
a/karavan-generator/src/main/resources/CamelApi.addStep.choiceStep.tx
b/karavan-generator/src/main/resources/CamelApi.addStep.choiceStep.tx
new file mode 100644
index 0000000..e10f484
--- /dev/null
+++ b/karavan-generator/src/main/resources/CamelApi.addStep.choiceStep.tx
@@ -0,0 +1,20 @@
+ case 'choiceStep':
+ const choiceChildren = (el as ChoiceStep).choice?.when ||
[];
+ if (el.uuid === parentId && step.dslName === 'when') {
+ choiceChildren.push(step as When);
+ (el as ChoiceStep).choice.when = choiceChildren;
+ } else if (el.uuid === parentId && step.dslName ===
'otherwise' && !(el as ChoiceStep).choice.otherwise) {
+ (el as ChoiceStep).choice.otherwise = step;
+ } else {
+ (el as ChoiceStep).choice.when =
CamelApi.addStep(choiceChildren, step, parentId) as When[];
+ const otherwise = (el as ChoiceStep).choice.otherwise;
+ if (otherwise?.uuid === parentId){
+ otherwise.steps = otherwise.steps ?
[...otherwise.steps] : [];
+ otherwise.steps.push(step);
+ (el as ChoiceStep).choice.otherwise = otherwise
+ } else if (otherwise && otherwise.steps &&
otherwise.steps.length > 0){
+ otherwise.steps =
CamelApi.addStep(otherwise.steps, step, parentId);
+ (el as ChoiceStep).choice.otherwise = otherwise;
+ }
+ }
+ break;
diff --git a/karavan-generator/src/main/resources/CamelApi.addStep.otherwise.tx
b/karavan-generator/src/main/resources/CamelApi.addStep.otherwise.tx
new file mode 100644
index 0000000..01592bd
--- /dev/null
+++ b/karavan-generator/src/main/resources/CamelApi.addStep.otherwise.tx
@@ -0,0 +1,5 @@
+ case 'otherwise':
+ const otherwiseChildren = (el as Otherwise).steps || [];
+ if (el.uuid === parentId) otherwiseChildren.push(step)
+ else (el as Otherwise).steps =
CamelApi.addStep(otherwiseChildren, step, parentId);
+ break;
diff --git a/karavan-generator/src/main/resources/CamelApi.addStep.when.tx
b/karavan-generator/src/main/resources/CamelApi.addStep.when.tx
new file mode 100644
index 0000000..69b6907
--- /dev/null
+++ b/karavan-generator/src/main/resources/CamelApi.addStep.when.tx
@@ -0,0 +1,5 @@
+ case 'when':
+ const whenChildren = (el as When).steps || [];
+ if (el.uuid === parentId) whenChildren.push(step)
+ else (el as When).steps = CamelApi.addStep(whenChildren,
step, parentId);
+ break;
diff --git a/karavan-generator/src/main/resources/CamelApi.createOtherwise.tx
b/karavan-generator/src/main/resources/CamelApi.createOtherwise.tx
new file mode 100644
index 0000000..797e4ef
--- /dev/null
+++ b/karavan-generator/src/main/resources/CamelApi.createOtherwise.tx
@@ -0,0 +1,6 @@
+ static createOtherwise = (element: any): Otherwise => {
+ const otherwise = element ? new Otherwise({...element}) : new
Otherwise();
+ otherwise.steps = CamelApi.createSteps(element?.steps);
+ if (element?.uuid) otherwise.uuid = element?.uuid;
+ return otherwise
+ }
diff --git a/karavan-generator/src/main/resources/CamelApi.createWhen.tx
b/karavan-generator/src/main/resources/CamelApi.createWhen.tx
new file mode 100644
index 0000000..35b03fa
--- /dev/null
+++ b/karavan-generator/src/main/resources/CamelApi.createWhen.tx
@@ -0,0 +1,7 @@
+ static createWhen = (element: any): When => {
+ const when = element ? new When({...element}) : new When()
+ when.expression = CamelApi.createExpression(element?.expression)
+ when.steps = CamelApi.createSteps(element?.steps)
+ when.uuid = element?.uuid ? element.uuid : when.uuid
+ return when
+ }
diff --git a/karavan-generator/src/main/resources/CamelApi.deleteWhen.tx
b/karavan-generator/src/main/resources/CamelApi.deleteWhen.tx
new file mode 100644
index 0000000..f616b07
--- /dev/null
+++ b/karavan-generator/src/main/resources/CamelApi.deleteWhen.tx
@@ -0,0 +1,12 @@
+ static deleteWhen = (whens: When[] | undefined, uuidToDelete: string):
When[] => {
+ const result: When[] = []
+ if (whens !== undefined){
+ whens.forEach(when => {
+ if (when.uuid !== uuidToDelete) {
+ when.steps = CamelApi.deleteStep(when.steps, uuidToDelete);
+ result.push(when);
+ }
+ })
+ }
+ return result
+ }