Skip to content

Commit 6afd12a

Browse files
authored
Merge pull request #407 from wttech/add-data-source-support
added data source support
2 parents 5075f19 + f134354 commit 6afd12a

File tree

27 files changed

+932
-42
lines changed

27 files changed

+932
-42
lines changed

app/aem/actions.main/src/main/java/com/cognifide/apm/main/actions/ActionGroup.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@
2323
public class ActionGroup {
2424

2525
public static final String CORE = "core";
26+
27+
public static final String DATASOURCE = "datasource";
2628
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* ========================LICENSE_START=================================
3+
* AEM Permission Management
4+
* %%
5+
* Copyright (C) 2013 Wunderman Thompson Technology
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* =========================LICENSE_END==================================
19+
*/
20+
package com.cognifide.apm.main.actions.internal.datasources;
21+
22+
import com.cognifide.apm.api.actions.Action;
23+
import com.cognifide.apm.api.actions.annotations.Mapper;
24+
import com.cognifide.apm.api.actions.annotations.Mapping;
25+
import com.cognifide.apm.api.actions.annotations.Required;
26+
import com.cognifide.apm.main.actions.ActionGroup;
27+
import java.util.List;
28+
import org.apache.commons.lang3.NotImplementedException;
29+
30+
@Mapper(value = "LEVELS", group = ActionGroup.DATASOURCE)
31+
public class LevelsMapper {
32+
33+
@Mapping(
34+
examples = "LEVELS('/content', [\n" +
35+
"\t{regex: '(.+)_(.+)',paramNames: ['param1', 'param2']}, # 1st level\n" +
36+
"\t{excludeRegex: '.+:.+'}, # 2nd level\n" +
37+
"\t{template: '/apps/test/pageTemplate', resourceType: 'test/pageRenderer'}, # 3rd level\n" +
38+
"\t{properties: [ # 4th level\n" +
39+
"\t\t{name: 'jcr:primaryType', regex: 'cq:Page'},\n" +
40+
"\t\t{name: 'jcr:primaryType', excludeRegex: 'cq:PageContent'},\n" +
41+
"\t\t{name: 'jcr:content/cq:template', regex: '/apps/test/pageTemplate'},\n" +
42+
"\t\t{name: 'jcr:content/sling:resourceType', regex: 'test/pageRenderer'}\n" +
43+
"\t]}\n" +
44+
"])",
45+
reference = "Provides levels of content for given resource path matching given content structure map"
46+
)
47+
public Action mapAction(
48+
@Required(value = "rootPath", description = "Root path") String rootPath,
49+
@Required(value = "structureMap", description = "Map of content structure") List<Object> structureMap) {
50+
throw new NotImplementedException("");
51+
}
52+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* ========================LICENSE_START=================================
3+
* AEM Permission Management
4+
* %%
5+
* Copyright (C) 2013 Wunderman Thompson Technology
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* =========================LICENSE_END==================================
19+
*/
20+
package com.cognifide.apm.main.actions.internal.datasources;
21+
22+
import com.cognifide.apm.api.actions.Action;
23+
import com.cognifide.apm.api.actions.annotations.Mapper;
24+
import com.cognifide.apm.api.actions.annotations.Mapping;
25+
import com.cognifide.apm.api.actions.annotations.Required;
26+
import com.cognifide.apm.main.actions.ActionGroup;
27+
import org.apache.commons.lang3.NotImplementedException;
28+
29+
@Mapper(value = "LOWER", group = ActionGroup.DATASOURCE)
30+
public class LowerMapper {
31+
32+
@Mapping(
33+
examples = "LOWER('en_GB')",
34+
reference = "Converts string to lower case"
35+
)
36+
public Action mapAction(
37+
@Required(value = "value", description = "string") String value) {
38+
throw new NotImplementedException("");
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* ========================LICENSE_START=================================
3+
* AEM Permission Management
4+
* %%
5+
* Copyright (C) 2013 Wunderman Thompson Technology
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* =========================LICENSE_END==================================
19+
*/
20+
package com.cognifide.apm.main.actions.internal.datasources;
21+
22+
import com.cognifide.apm.api.actions.Action;
23+
import com.cognifide.apm.api.actions.annotations.Mapper;
24+
import com.cognifide.apm.api.actions.annotations.Mapping;
25+
import com.cognifide.apm.api.actions.annotations.Required;
26+
import com.cognifide.apm.main.actions.ActionGroup;
27+
import org.apache.commons.lang3.NotImplementedException;
28+
29+
@Mapper(value = "UPPER", group = ActionGroup.DATASOURCE)
30+
public class UpperMapper {
31+
32+
@Mapping(
33+
examples = "UPPER('en_GB')",
34+
reference = "Converts string to upper case"
35+
)
36+
public Action mapAction(
37+
@Required(value = "value", description = "string") String value) {
38+
throw new NotImplementedException("");
39+
}
40+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* ========================LICENSE_START=================================
3+
* AEM Permission Management
4+
* %%
5+
* Copyright (C) 2013 Wunderman Thompson Technology
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* =========================LICENSE_END==================================
19+
*/
20+
package com.cognifide.apm.main.actions.internal.datasources;
21+
22+
import com.cognifide.apm.api.actions.Action;
23+
import com.cognifide.apm.api.actions.annotations.Mapper;
24+
import com.cognifide.apm.api.actions.annotations.Mapping;
25+
import com.cognifide.apm.api.actions.annotations.Required;
26+
import com.cognifide.apm.main.actions.ActionGroup;
27+
import java.util.Map;
28+
import org.apache.commons.lang3.NotImplementedException;
29+
30+
@Mapper(value = "VALUEMAP", group = ActionGroup.DATASOURCE)
31+
public class ValueMapMapper {
32+
33+
@Mapping(
34+
examples = "VALUEMAP('/content/dam')",
35+
reference = "Provides value map with all properties for given resource path"
36+
)
37+
public Action mapAction(
38+
@Required(value = "path", description = "Resource path") String path) {
39+
throw new NotImplementedException("");
40+
}
41+
42+
@Mapping(
43+
examples = "VALUEMAP('/content/dam', {\n" +
44+
"\tregex: 'prop.+',\n" +
45+
"\texcludeRegex: '.+:.+'\n" +
46+
"})",
47+
reference = "Provides value map with properties which matching given regex expressions for given resource path"
48+
)
49+
public Action mapAction(
50+
@Required(value = "path", description = "Resource path") String path,
51+
@Required(value = "regexMap", description = "Map of regex expressions") Map<String, String> regex) {
52+
throw new NotImplementedException("");
53+
}
54+
}

app/aem/core/src/main/antlr/ApmLang.g4

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ structureKey
6565
| STRING_LITERAL
6666
;
6767

68+
dataSource
69+
: identifier BRACKET_BEGIN (argument (COMMA argument)*)? BRACKET_END
70+
;
71+
6872
structureValue
6973
: value
7074
| argument
@@ -103,6 +107,7 @@ plus
103107
expression
104108
: expression plus expression
105109
| value
110+
| dataSource
106111
;
107112

108113
argument
@@ -176,6 +181,12 @@ STRUCTURE_BEGIN
176181
STRUCTURE_END
177182
: '}'
178183
;
184+
BRACKET_BEGIN
185+
: '('
186+
;
187+
BRACKET_END
188+
: ')'
189+
;
179190
COMMA
180191
: ','
181192
;
@@ -271,7 +282,7 @@ fragment IdentifierPart
271282
: Letter LetterOrDigit*
272283
;
273284
fragment VariablePart
274-
: IdentifierPart (ARRAY_BEGIN LetterOrDigit+ ARRAY_END)?
285+
: IdentifierPart (ARRAY_BEGIN (NUMBER_LITERAL | IDENTIFIER | STRING_LITERAL) ARRAY_END)*
275286
;
276287
fragment PathPart
277288
: '/' (~[\r\n\t ])+

app/aem/core/src/main/java/com/cognifide/apm/core/grammar/ReferenceFinder.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.cognifide.apm.core.grammar.antlr.ApmLangBaseVisitor;
2828
import com.cognifide.apm.core.grammar.antlr.ApmLangParser;
2929
import com.cognifide.apm.core.grammar.common.Functions;
30+
import com.cognifide.apm.core.grammar.datasource.DataSourceInvoker;
3031
import com.cognifide.apm.core.grammar.executioncontext.ExecutionContext;
3132
import com.cognifide.apm.core.grammar.parsedscript.ParsedScript;
3233
import com.cognifide.apm.core.progress.ProgressImpl;
@@ -46,9 +47,12 @@ public class ReferenceFinder {
4647

4748
private final ResourceResolver resourceResolver;
4849

49-
public ReferenceFinder(ScriptFinder scriptFinder, ResourceResolver resourceResolver) {
50+
private final DataSourceInvoker dataSourceInvoker;
51+
52+
public ReferenceFinder(ScriptFinder scriptFinder, ResourceResolver resourceResolver, DataSourceInvoker dataSourceInvoker) {
5053
this.scriptFinder = scriptFinder;
5154
this.resourceResolver = resourceResolver;
55+
this.dataSourceInvoker = dataSourceInvoker;
5256
}
5357

5458
public List<Script> findReferences(Script script) {
@@ -81,7 +85,7 @@ public ReferenceGraph getReferenceGraph(Script... scripts) {
8185
private void fillReferenceGraph(ReferenceGraph refGraph, Script script) {
8286
if (refGraph.getNode(script) == null) {
8387
ApmLangParser.ApmContext apmContext = ParsedScript.create(script).getApm();
84-
ExecutionContext executionContext = ExecutionContext.create(scriptFinder, resourceResolver, script, new ProgressImpl(resourceResolver.getUserID()));
88+
ExecutionContext executionContext = ExecutionContext.create(scriptFinder, resourceResolver, dataSourceInvoker, script, new ProgressImpl(resourceResolver.getUserID()));
8589
findReferences(refGraph, refGraph.addNode(script), ImmutableList.of(script.getPath()), executionContext, apmContext);
8690
}
8791
}

app/aem/core/src/main/java/com/cognifide/apm/core/grammar/ScriptRunner.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.cognifide.apm.core.grammar.argument.ArgumentResolverException;
4040
import com.cognifide.apm.core.grammar.argument.Arguments;
4141
import com.cognifide.apm.core.grammar.common.Functions;
42+
import com.cognifide.apm.core.grammar.datasource.DataSourceInvoker;
4243
import com.cognifide.apm.core.grammar.executioncontext.ExecutionContext;
4344
import com.cognifide.apm.core.grammar.parsedscript.InvalidSyntaxException;
4445
import com.cognifide.apm.core.grammar.parsedscript.InvalidSyntaxMessageFactory;
@@ -69,16 +70,19 @@ public class ScriptRunner {
6970

7071
private final ActionInvoker actionInvoker;
7172

72-
public ScriptRunner(ScriptFinder scriptFinder, ResourceResolver resourceResolver, boolean validateOnly, ActionInvoker actionInvoker) {
73+
private final DataSourceInvoker dataSourceInvoker;
74+
75+
public ScriptRunner(ScriptFinder scriptFinder, ResourceResolver resourceResolver, boolean validateOnly, ActionInvoker actionInvoker, DataSourceInvoker dataSourceInvoker) {
7376
this.scriptFinder = scriptFinder;
7477
this.resourceResolver = resourceResolver;
7578
this.validateOnly = validateOnly;
7679
this.actionInvoker = actionInvoker;
80+
this.dataSourceInvoker = dataSourceInvoker;
7781
}
7882

7983
public Progress execute(Script script, Progress progress, Map<String, String> initialDefinitions) {
8084
try {
81-
ExecutionContext executionContext = ExecutionContext.create(scriptFinder, resourceResolver, script, progress);
85+
ExecutionContext executionContext = ExecutionContext.create(scriptFinder, resourceResolver, dataSourceInvoker, script, progress);
8286
initialDefinitions.forEach((name, value) -> executionContext.setVariable(name, new ApmString(value)));
8387
Executor executor = new Executor(executionContext);
8488
executor.visit(executionContext.getRoot().getApm());
@@ -268,7 +272,7 @@ private Status visitGenericCommandValidateMode(ParserRuleContext ctx, String com
268272

269273
@Override
270274
public Status visitImportScript(ImportScriptContext ctx) {
271-
ImportScript.Result result = new ImportScript(executionContext).importScript(ctx);
275+
ImportScript.Result result = new ImportScript(executionContext, resourceResolver, dataSourceInvoker).importScript(ctx);
272276
executionContext.getVariableHolder().setAll(result.getVariableHolder());
273277
progress(ctx, Status.SUCCESS, "import", result.toMessages());
274278
return Status.SUCCESS;

app/aem/core/src/main/java/com/cognifide/apm/core/grammar/argument/ArgumentResolver.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.cognifide.apm.core.grammar.antlr.ApmLangBaseVisitor;
3131
import com.cognifide.apm.core.grammar.antlr.ApmLangParser;
3232
import com.cognifide.apm.core.grammar.common.Functions;
33+
import com.cognifide.apm.core.grammar.datasource.DataSourceInvoker;
3334
import com.cognifide.apm.core.grammar.executioncontext.VariableHolder;
3435
import java.util.ArrayList;
3536
import java.util.Arrays;
@@ -44,15 +45,22 @@
4445
import org.apache.commons.collections4.ListUtils;
4546
import org.apache.commons.lang.text.StrSubstitutor;
4647
import org.apache.commons.lang3.StringUtils;
48+
import org.apache.sling.api.resource.ResourceResolver;
4749

4850
public class ArgumentResolver {
4951

5052
private final VariableHolder variableHolder;
5153

5254
private final SingleArgumentResolver singleArgumentResolver;
5355

54-
public ArgumentResolver(VariableHolder variableHolder) {
56+
private final ResourceResolver resourceResolver;
57+
58+
private final DataSourceInvoker dataSourceInvoker;
59+
60+
public ArgumentResolver(VariableHolder variableHolder, ResourceResolver resourceResolver, DataSourceInvoker dataSourceInvoker) {
5561
this.variableHolder = variableHolder;
62+
this.resourceResolver = resourceResolver;
63+
this.dataSourceInvoker = dataSourceInvoker;
5664
this.singleArgumentResolver = new SingleArgumentResolver();
5765
}
5866

@@ -152,8 +160,8 @@ public ApmType visitStructure(ApmLangParser.StructureContext ctx) {
152160
.collect(Collectors.toMap(
153161
ApmPair::getKey,
154162
ApmPair::getValue,
155-
(key, value) -> {
156-
throw new IllegalStateException(String.format("Duplicate key %s", key));
163+
(existing, replacement) -> {
164+
throw new IllegalStateException("Duplicate key");
157165
},
158166
LinkedHashMap::new
159167
));
@@ -193,6 +201,8 @@ public ApmType visitExpression(ApmLangParser.ExpressionContext ctx) {
193201
}
194202
} else if (ctx.value() != null) {
195203
return visit(ctx.value());
204+
} else if (ctx.dataSource() != null) {
205+
return visit(ctx.dataSource());
196206
} else {
197207
return super.visitExpression(ctx);
198208
}
@@ -239,5 +249,17 @@ public ApmType visitVariable(ApmLangParser.VariableContext ctx) {
239249
String name = Functions.getIdentifier(ctx.variableIdentifier());
240250
return variableHolder.get(name);
241251
}
252+
253+
@Override
254+
public ApmType visitDataSource(ApmLangParser.DataSourceContext ctx) {
255+
String name = Functions.getIdentifier(ctx.identifier());
256+
List<ApmType> values = ctx.children
257+
.stream()
258+
.map(child -> child.accept(this))
259+
.filter(value -> !(value instanceof ApmEmpty))
260+
.collect(Collectors.toList());
261+
return Optional.ofNullable(dataSourceInvoker.determine(name, resourceResolver, values))
262+
.orElseThrow(() -> new ArgumentResolverException(String.format("Data source \"%s\" not found", name.toUpperCase())));
263+
}
242264
}
243265
}

0 commit comments

Comments
 (0)