Skip to content

Commit aa4895d

Browse files
committed
added entity modeler
fixing failing tests
1 parent 1da8a43 commit aa4895d

File tree

121 files changed

+4335
-1280
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+4335
-1280
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ flow.properties
2121
node_modules/
2222
.tmp/
2323
*.sublime-*
24+
.vscode/

marklogic-data-hub/src/main/java/com/marklogic/hub/DataHub.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ public void validateServer() throws ServerValidationException {
174174
alteredString += "0";
175175
}
176176
int major = Integer.parseInt(alteredString.substring(0, 1));
177-
int ver = Integer.parseInt(alteredString.substring(0, 3));
178-
boolean isNightly = versionString.matches("[^-]+-\\d{8}");
179-
if (major < 8 || (!isNightly && ver < 804)) {
177+
// int ver = Integer.parseInt(alteredString.substring(0, 3));
178+
// boolean isNightly = versionString.matches("[^-]+-\\d{8}");
179+
if (major < 9) {
180180
throw new ServerValidationException("Invalid MarkLogic Server Version: " + versionString);
181181
}
182182

marklogic-data-hub/src/main/java/com/marklogic/hub/deploy/commands/LoadUserModulesCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void execute(CommandContext context) {
8585
DefaultModulesLoader modulesLoader = getStagingModulesLoader(config);
8686
modulesLoader.loadModules(baseDir, new AssetModulesFinder(), stagingClient);
8787

88-
JSONDocumentManager entityDocMgr = stagingClient.newJSONDocumentManager();
88+
JSONDocumentManager entityDocMgr = finalClient.newJSONDocumentManager();
8989

9090
AllButAssetsModulesFinder allButAssetsModulesFinder = new AllButAssetsModulesFinder();
9191

marklogic-data-hub/src/main/java/com/marklogic/hub/scaffold/Scaffolding.java

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,39 @@
1515
*/
1616
package com.marklogic.hub.scaffold;
1717

18-
import com.marklogic.client.helper.LoggingObject;
19-
import com.marklogic.client.io.Format;
20-
import com.marklogic.hub.error.ScaffoldingValidationException;
21-
import com.marklogic.hub.flow.FlowType;
22-
import com.marklogic.hub.flow.SimpleFlow;
23-
import com.marklogic.hub.plugin.PluginFormat;
24-
2518
import java.io.*;
19+
import java.nio.charset.StandardCharsets;
2620
import java.nio.file.Files;
2721
import java.nio.file.Path;
2822
import java.nio.file.Paths;
2923

24+
import com.marklogic.client.DatabaseClient;
25+
import com.marklogic.client.extensions.ResourceManager;
26+
import com.marklogic.client.extensions.ResourceServices;
27+
import com.marklogic.client.helper.LoggingObject;
28+
import com.marklogic.client.io.StringHandle;
29+
import com.marklogic.client.util.RequestParameters;
30+
import com.marklogic.hub.error.ScaffoldingValidationException;
31+
import com.marklogic.hub.plugin.PluginFormat;
32+
import com.sun.jersey.api.client.ClientHandlerException;
33+
34+
import com.marklogic.client.io.Format;
35+
import com.marklogic.hub.flow.FlowType;
36+
import com.marklogic.hub.flow.SimpleFlow;
37+
3038
public class Scaffolding extends LoggingObject {
3139

3240
private String projectDir;
3341
private Path pluginsDir;
3442
private Path entitiesDir;
3543
private ScaffoldingValidator validator;
44+
private DatabaseClient databaseClient;
3645

37-
public Scaffolding(String projectDir) {
46+
public Scaffolding(String projectDir, DatabaseClient databaseClient) {
3847
this.projectDir = projectDir;
3948
this.pluginsDir = Paths.get(this.projectDir, "plugins");
4049
this.entitiesDir = this.pluginsDir.resolve("entities");
50+
this.databaseClient = databaseClient;
4151
validator = new ScaffoldingValidator(projectDir);
4252
}
4353

@@ -55,7 +65,14 @@ public void createEntity(String entityName) throws FileNotFoundException {
5565
}
5666

5767
public void createFlow(String entityName, String flowName,
58-
FlowType flowType, PluginFormat pluginFormat, Format dataFormat)
68+
FlowType flowType, PluginFormat pluginFormat,
69+
Format dataFormat) throws IOException {
70+
createFlow(entityName, flowName, flowType, pluginFormat, dataFormat, false);
71+
}
72+
73+
public void createFlow(String entityName, String flowName,
74+
FlowType flowType, PluginFormat pluginFormat,
75+
Format dataFormat, boolean useEsModel)
5976
throws IOException {
6077
Path flowDir = getFlowDir(entityName, flowName, flowType);
6178

@@ -73,8 +90,17 @@ public void createFlow(String entityName, String flowName,
7390

7491
Path contentDir = flowDir.resolve("content");
7592
contentDir.toFile().mkdirs();
76-
writeFile("scaffolding/" + flowType + "/" + pluginFormat + "/content." + pluginFormat,
93+
94+
95+
if (useEsModel) {
96+
ContentPlugin cp = new ContentPlugin(databaseClient);
97+
String content = cp.getContents(entityName, pluginFormat);
98+
writeBuffer(content, contentDir.resolve("content." + pluginFormat));
99+
}
100+
else {
101+
writeFile("scaffolding/" + flowType + "/" + pluginFormat + "/content." + pluginFormat,
77102
contentDir.resolve("content." + pluginFormat));
103+
}
78104

79105
Path headerDir = flowDir.resolve("headers");
80106
headerDir.toFile().mkdirs();
@@ -104,6 +130,14 @@ private void writeFile(String srcFile, Path dstFile) throws IOException {
104130
}
105131
}
106132

133+
private void writeBuffer(String buffer, Path dstFile) throws IOException {
134+
logger.info("writing: " + dstFile.toString());
135+
if (!dstFile.toFile().exists()) {
136+
InputStream inputStream = new ByteArrayInputStream(buffer.getBytes(StandardCharsets.UTF_8));
137+
Files.copy(inputStream, dstFile);
138+
}
139+
}
140+
107141
public void createRestExtension(String entityName, String extensionName,
108142
FlowType flowType, PluginFormat pluginFormat) throws IOException, ScaffoldingValidationException {
109143
logger.info(extensionName);
@@ -224,4 +258,33 @@ public static String getAbsolutePath(String first, String... more) {
224258
}
225259
return absolutePath.toString();
226260
}
261+
262+
public class ContentPlugin extends ResourceManager {
263+
private static final String NAME = "scaffold-content";
264+
265+
private RequestParameters params = new RequestParameters();
266+
267+
public ContentPlugin(DatabaseClient client) {
268+
super();
269+
client.init(NAME, this);
270+
}
271+
272+
public String getContents(String entityName, PluginFormat pluginFormat) throws IOException {
273+
try {
274+
params.add("entity", entityName);
275+
params.add("pluginFormat", pluginFormat.toString());
276+
ResourceServices.ServiceResultIterator resultItr = this.getServices().get(params);
277+
if (resultItr == null || ! resultItr.hasNext()) {
278+
throw new IOException("Unable to get Content Plugin scaffold");
279+
}
280+
ResourceServices.ServiceResult res = resultItr.next();
281+
return res.getContent(new StringHandle()).get();
282+
}
283+
catch(ClientHandlerException e) {
284+
}
285+
return "{}";
286+
}
287+
288+
}
289+
227290
}

marklogic-data-hub/src/main/resources/ml-modules/root/com.marklogic.hub/lib/flow-lib.xqy

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -562,15 +562,15 @@ declare function flow:make-envelope(
562562
map:put($o, "headers", $headers),
563563
map:put($o, "triples", $triples),
564564
map:put($o, "instance",
565-
if ($content instance of map:map) then
565+
if ($content castable as map:map) then
566566
flow:instance-to-canonical-json($content)
567567
else
568568
$content
569569
),
570570
map:put($o, "attachments",
571571
let $content := map:get($map, "content")
572572
return
573-
if ($content instance of map:map) then
573+
if ($content castable as map:map) then
574574
map:get($content, "$attachments")
575575
else
576576
()
@@ -590,9 +590,8 @@ declare function flow:make-envelope(
590590
<instance>
591591
{
592592
let $content := map:get($map, "content")
593-
let $_ := xdmp:log(("content: [", $content, "]"))
594593
return
595-
if ($content instance of map:map) then
594+
if ($content castable as map:map) then
596595
flow:instance-to-canonical-xml($content)
597596
else
598597
$content
@@ -602,7 +601,7 @@ declare function flow:make-envelope(
602601
{
603602
let $content := map:get($map, "content")
604603
return
605-
if ($content instance of map:map) then
604+
if ($content castable as map:map) then
606605
map:get($content, "$attachments")
607606
else
608607
()
@@ -844,10 +843,12 @@ declare function flow:run-plugin(
844843

845844
let $resp :=
846845
typeswitch($resp)
847-
case map:map return
848-
$resp
849846
case object-node() | json:object return
850-
if ($data-format = $XML) then
847+
(: map:map lands here too :)
848+
(: map:map is ES response type :)
849+
if ($resp castable as map:map) then
850+
$resp
851+
else if ($data-format = $XML) then
851852
json:transform-from-json($resp, json:config("custom"))
852853
else
853854
$resp
@@ -938,14 +939,22 @@ declare function flow:run-writer(
938939
xdmp:rethrow()
939940
}
940941
let $duration := xdmp:elapsed-time() - $before
942+
let $is-xml :=
943+
let $e :=
944+
if ($envelope instance of document-node()) then
945+
$envelope/node()
946+
else
947+
$envelope
948+
return
949+
$e instance of element()
941950
let $_ :=
942951
trace:plugin-trace(
943952
$identifier,
944953
$module-uri,
945954
"writer",
946955
$flow-type,
947956
$envelope,
948-
if ($envelope instance of element()) then ()
957+
if ($is-xml) then ()
949958
else null-node {},
950959
$duration
951960
)

marklogic-data-hub/src/main/resources/ml-modules/root/com.marklogic.hub/lib/hub-entities.xqy

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@ declare variable $ENTITY-MODEL-COLLECTION := "http://marklogic.com/entity-servic
99

1010
declare option xdmp:mapping "false";
1111

12+
declare function hent:get-model($entity-name as xs:string)
13+
{
14+
let $model := fn:collection($ENTITY-MODEL-COLLECTION)[info/title = $entity-name]
15+
where fn:exists($model)
16+
return
17+
let $model-map as map:map? := $model
18+
let $refs := $model//*[fn:local-name(.) = '$ref'][fn:starts-with(., "#/definitions")] ! fn:replace(., "#/definitions/", "")
19+
let $_ :=
20+
let $definitions := $model-map=>map:get("definitions")
21+
for $ref in $refs
22+
let $other-model as map:map? := hent:get-model($ref)
23+
let $other-defs := $other-model=>map:get("definitions")
24+
for $key in map:keys($other-defs)
25+
return
26+
$definitions=>map:put($key, $other-defs=>map:get($key))
27+
return
28+
$model-map
29+
};
30+
1231
declare function hent:dump-indexes()
1332
{
1433
let $uber-model := map:map()

marklogic-data-hub/src/main/resources/ml-modules/services/metadata/scaffold-content.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Scaffold for Content plugins
77
</div>
88
</description>
9-
<method name="POST">
9+
<method name="GET">
1010
<param name="entity" type="xs:string"/>
1111
<param name="pluginFormat" type="xs:string"/>
1212
</method>

0 commit comments

Comments
 (0)