Skip to content

Commit 67226b7

Browse files
committed
Merge pull request #134 from maeisabelle/102-ScaffoldingForRest
102 - Scaffolding for Rest
2 parents 3cd2cab + adacee7 commit 67226b7

File tree

8 files changed

+313
-8
lines changed

8 files changed

+313
-8
lines changed

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

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
*/
1616
package com.marklogic.hub;
1717

18+
import java.io.BufferedReader;
19+
import java.io.BufferedWriter;
1820
import java.io.File;
21+
import java.io.FileWriter;
1922
import java.io.IOException;
2023
import java.io.InputStream;
24+
import java.io.InputStreamReader;
2125
import java.io.PrintWriter;
2226
import java.nio.file.Files;
2327
import java.nio.file.Path;
@@ -33,7 +37,7 @@
3337
public class Scaffolding {
3438

3539
static final private Logger LOGGER = LoggerFactory.getLogger(Scaffolding.class);
36-
40+
3741
public static File getEntityDir(File userlandDir, String entityName) {
3842
File entitiesDir = new File(userlandDir, "entities");
3943
File entityDir = new File(entitiesDir, entityName);
@@ -97,4 +101,107 @@ private static void writeFile(String srcFile, Path dstFile)
97101
.getResourceAsStream(srcFile);
98102
Files.copy(inputStream, dstFile);
99103
}
104+
105+
public static void createRestExtension(String entityName, String extensionName,
106+
FlowType flowType, PluginFormat pluginFormat, File userlandDir) throws IOException {
107+
LOGGER.info(extensionName);
108+
String scaffoldRestServicesPath = "scaffolding/rest/services/";
109+
String fileContent = getFileContent(scaffoldRestServicesPath + pluginFormat + "/template." + pluginFormat, extensionName);
110+
File dstFile = createEmptyRestExtensionFile(entityName, extensionName, flowType, pluginFormat, userlandDir);
111+
writeToFile(fileContent, dstFile);
112+
writeMetadataForFile(dstFile, scaffoldRestServicesPath + "metadata/template.xml", extensionName);
113+
}
114+
115+
public static void createRestTransform(String entityName, String transformName,
116+
FlowType flowType, PluginFormat pluginFormat, File userlandDir) throws IOException {
117+
LOGGER.info(transformName);
118+
String scaffoldRestTransformsPath = "scaffolding/rest/transforms/";
119+
String fileContent = getFileContent(scaffoldRestTransformsPath + pluginFormat + "/template." + pluginFormat, transformName);
120+
File dstFile = createEmptyRestTransformFile(entityName, transformName, flowType, pluginFormat, userlandDir);
121+
writeToFile(fileContent, dstFile);
122+
writeMetadataForFile(dstFile, scaffoldRestTransformsPath + "metadata/template.xml", transformName);
123+
}
124+
125+
private static void writeToFile(String fileContent, File dstFile)
126+
throws IOException {
127+
LOGGER.info(fileContent);
128+
LOGGER.info(dstFile.getAbsolutePath());
129+
FileWriter fw = new FileWriter(dstFile);
130+
BufferedWriter bw = new BufferedWriter(fw);
131+
bw.write(fileContent);
132+
bw.close();
133+
}
134+
135+
private static File createEmptyRestExtensionFile(String entityName, String extensionName,
136+
FlowType flowType, PluginFormat pluginFormat, File userlandDir) throws IOException {
137+
File restDir = getRestDirectory(userlandDir, entityName, flowType);
138+
return createEmptyFile(restDir, "services", extensionName + "." + pluginFormat);
139+
}
140+
141+
private static File createEmptyRestTransformFile(String entityName, String transformName,
142+
FlowType flowType, PluginFormat pluginFormat, File userlandDir) throws IOException {
143+
File restDir = getRestDirectory(userlandDir, entityName, flowType);
144+
return createEmptyFile(restDir, "transforms", transformName + "." + pluginFormat);
145+
}
146+
147+
private static File createEmptyFile(File directory, String subDirectoryName, String fileName) throws IOException {
148+
File fileDirectory = directory;
149+
if(subDirectoryName!=null) {
150+
fileDirectory = new File(directory, subDirectoryName);
151+
}
152+
fileDirectory.mkdirs();
153+
File file = new File(fileDirectory, fileName);
154+
file.createNewFile();
155+
return file;
156+
}
157+
158+
private static File getRestDirectory(File userlandDir, String entityName,
159+
FlowType flowType) {
160+
return getFlowDir(userlandDir, entityName,
161+
"REST", flowType);
162+
}
163+
164+
private static void writeMetadataForFile(File file, String metadataTemplatePath, String metadataName) throws IOException {
165+
String fileContent = getFileContent(metadataTemplatePath, metadataName);
166+
File metadataFile = createEmptyMetadataForFile(file, metadataName);
167+
writeToFile(fileContent, metadataFile);
168+
}
169+
170+
private static File createEmptyMetadataForFile(File file, String metadataName) throws IOException {
171+
File metadataDir = new File(file.getParentFile(), "metadata");
172+
metadataDir.mkdir();
173+
File metadataFile = new File(metadataDir, metadataName + ".xml");
174+
metadataFile.createNewFile();
175+
return metadataFile;
176+
}
177+
178+
private static String getFileContent(String srcFile, String placeholder) throws IOException {
179+
StringBuilder output = new StringBuilder();
180+
InputStream inputStream = null;
181+
BufferedReader rdr = null;
182+
try {
183+
inputStream = Scaffolding.class.getClassLoader()
184+
.getResourceAsStream(srcFile);
185+
rdr = new BufferedReader(new InputStreamReader(inputStream));
186+
String bufferedLine = null;
187+
while ((bufferedLine = rdr.readLine()) != null) {
188+
if(bufferedLine.contains("placeholder")) {
189+
bufferedLine = bufferedLine.replace("placeholder", placeholder);
190+
}
191+
output.append(bufferedLine);
192+
output.append("\n");
193+
}
194+
} catch (IOException e) {
195+
LOGGER.error(e.getMessage(), e);
196+
throw e;
197+
} finally {
198+
if(inputStream != null) {
199+
inputStream.close();
200+
}
201+
if(rdr != null) {
202+
rdr.close();
203+
}
204+
}
205+
return output.toString();
206+
}
100207
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!-- Copyright 2016 MarkLogic Corporation. All Rights Reserved. -->
2+
<metadata>
3+
<title>placeholder</title>
4+
<description>
5+
<div>
6+
placeholder resource extension
7+
</div>
8+
</description>
9+
<method name="GET">
10+
<!-- <param name="param1" type="xs:string" /> -->
11+
<!-- <param name="param2" type="xs:boolean" /> -->
12+
<!-- <param name="param3" type="xs:decimal" /> -->
13+
<!-- <param name="param4" type="xs:float" /> -->
14+
<!-- <param name="param5" type="xs:double" /> -->
15+
<!-- <param name="param6" type="xs:dateTime" /> -->
16+
</method>
17+
<method name="PUT">
18+
<!-- <param name="param1" type="xs:string" /> -->
19+
<!-- <param name="param2" type="xs:boolean" /> -->
20+
<!-- <param name="param3" type="xs:decimal" /> -->
21+
<!-- <param name="param4" type="xs:float" /> -->
22+
<!-- <param name="param5" type="xs:double" /> -->
23+
<!-- <param name="param6" type="xs:dateTime" /> -->
24+
</method>
25+
<method name="POST">
26+
<!-- <param name="param1" type="xs:string" /> -->
27+
<!-- <param name="param2" type="xs:boolean" /> -->
28+
<!-- <param name="param3" type="xs:decimal" /> -->
29+
<!-- <param name="param4" type="xs:float" /> -->
30+
<!-- <param name="param5" type="xs:double" /> -->
31+
<!-- <param name="param6" type="xs:dateTime" /> -->
32+
</method>
33+
<method name="DELETE">
34+
<!-- <param name="param1" type="xs:string" /> -->
35+
<!-- <param name="param2" type="xs:boolean" /> -->
36+
<!-- <param name="param3" type="xs:decimal" /> -->
37+
<!-- <param name="param4" type="xs:float" /> -->
38+
<!-- <param name="param5" type="xs:double" /> -->
39+
<!-- <param name="param6" type="xs:dateTime" /> -->
40+
</method>
41+
</metadata>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function get(context, params) {
2+
return null;
3+
};
4+
5+
function post(context, params, input) {
6+
return null;
7+
};
8+
9+
function put(context, params, input) {
10+
return null;
11+
};
12+
13+
function deleteFunction(context, params) {
14+
return null;
15+
};
16+
17+
exports.GET = get;
18+
exports.POST = post;
19+
exports.PUT = put;
20+
exports.DELETE = deleteFunction;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
xquery version "1.0-ml";
2+
3+
module namespace extensionNSPrefix =
4+
"http://marklogic.com/rest-api/resource/placeholder";
5+
6+
declare function extensionNSPrefix:get(
7+
$context as map:map,
8+
$params as map:map
9+
) as document-node()* {
10+
document { "GET called on the placeholder extension" }
11+
};
12+
13+
declare function extensionNSPrefix:put(
14+
$context as map:map,
15+
$params as map:map,
16+
$input as document-node()*
17+
) as document-node()? {
18+
document { "PUT called on the placeholder extension" }
19+
};
20+
21+
declare function extensionNSPrefix:post(
22+
$context as map:map,
23+
$params as map:map,
24+
$input as document-node()*
25+
) as document-node()* {
26+
document { "POST called on the placeholder extension" }
27+
};
28+
29+
declare function extensionNSPrefix:delete(
30+
$context as map:map,
31+
$params as map:map
32+
) as document-node()? {
33+
document { "DELETE called on the placeholder extension" }
34+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!-- Copyright 2016 MarkLogic Corporation. All Rights Reserved. -->
2+
<metadata>
3+
<title>placeholder</title>
4+
<description>
5+
<div>
6+
placeholder transform
7+
</div>
8+
</description>
9+
<!-- <param name="param1" type="xs:string" /> -->
10+
<!-- <param name="param2" type="xs:boolean" /> -->
11+
<!-- <param name="param3" type="xs:decimal" /> -->
12+
<!-- <param name="param4" type="xs:float" /> -->
13+
<!-- <param name="param5" type="xs:double" /> -->
14+
<!-- <param name="param6" type="xs:dateTime" /> -->
15+
</metadata>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function placeholder(context, params, content)
2+
{
3+
return content;
4+
};
5+
6+
exports.transform = placeholder;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
xquery version "1.0-ml";
2+
3+
module namespace transformNSPrefix =
4+
"http://marklogic.com/rest-api/transform/placeholder";
5+
6+
declare function transformNSPrefix:transform(
7+
$context as map:map,
8+
$params as map:map,
9+
$content as document-node()
10+
) as document-node()
11+
{
12+
$content
13+
};

marklogic-data-hub/src/test/java/com/marklogic/hub/ScaffoldingTest.java

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
public class ScaffoldingTest extends HubTestBase {
2424

25-
private File pluginsDir = new File("./test-ye-plugins");
25+
private String pluginPath = "./test-ye-plugins";
26+
private File pluginsDir = new File(pluginPath);
2627

2728
@BeforeClass
2829
public static void setup() {
@@ -44,12 +45,12 @@ public void createEntity() {
4445
File entityDir = Scaffolding.getEntityDir(pluginsDir, "my-fun-test");
4546
assertTrue(entityDir.exists());
4647
assertEquals(
47-
new File("./test-ye-plugins/entities/my-fun-test").toPath(),
48+
new File(pluginPath + "/entities/my-fun-test").toPath(),
4849
entityDir.toPath());
4950

5051
File flowDir = Scaffolding.getFlowDir(pluginsDir, "my-fun-test", "blah", FlowType.INPUT);
5152
assertEquals(new File(
52-
"./test-ye-plugins/entities/my-fun-test/input/blah").toPath(),
53+
pluginPath + "/entities/my-fun-test/input/blah").toPath(),
5354
flowDir.toPath());
5455
assertFalse(flowDir.exists());
5556
}
@@ -82,11 +83,11 @@ private void createInputFlow(PluginFormat pluginFormat, Format dataFormat) throw
8283

8384
File entityDir = Scaffolding.getEntityDir(pluginsDir, "my-fun-test");
8485
assertTrue(entityDir.exists());
85-
assertEquals(new File("./test-ye-plugins/entities/my-fun-test").toPath(), entityDir.toPath());
86+
assertEquals(new File(pluginPath + "/entities/my-fun-test").toPath(), entityDir.toPath());
8687

8788
Scaffolding.createFlow("my-fun-test", "test-input", FlowType.INPUT, pluginFormat, dataFormat, pluginsDir);
8889
File flowDir = Scaffolding.getFlowDir(pluginsDir, "my-fun-test", "test-input", FlowType.INPUT);
89-
assertEquals(new File("./test-ye-plugins/entities/my-fun-test/input/test-input").toPath(), flowDir.toPath());
90+
assertEquals(new File(pluginPath + "/entities/my-fun-test/input/test-input").toPath(), flowDir.toPath());
9091
assertTrue(flowDir.exists());
9192

9293
File flowDescriptor = new File(flowDir, "test-input.xml");
@@ -121,11 +122,11 @@ private void createConformanceFlow(PluginFormat pluginFormat, Format dataFormat)
121122

122123
File entityDir = Scaffolding.getEntityDir(pluginsDir, "my-fun-test");
123124
assertTrue(entityDir.exists());
124-
assertEquals(new File("./test-ye-plugins/entities/my-fun-test").toPath(), entityDir.toPath());
125+
assertEquals(new File(pluginPath + "/entities/my-fun-test").toPath(), entityDir.toPath());
125126

126127
Scaffolding.createFlow("my-fun-test", "test-conformance", FlowType.CONFORMANCE, pluginFormat, dataFormat, pluginsDir);
127128
File flowDir = Scaffolding.getFlowDir(pluginsDir, "my-fun-test", "test-conformance", FlowType.CONFORMANCE);
128-
assertEquals(new File("./test-ye-plugins/entities/my-fun-test/conformance/test-conformance").toPath(), flowDir.toPath());
129+
assertEquals(new File(pluginPath + "/entities/my-fun-test/conformance/test-conformance").toPath(), flowDir.toPath());
129130
assertTrue(flowDir.exists());
130131

131132
File flowDescriptor = new File(flowDir, "test-conformance.xml");
@@ -153,4 +154,72 @@ private void createConformanceFlow(PluginFormat pluginFormat, Format dataFormat)
153154
assertTrue(triplesDir.exists());
154155
assertTrue(triplesContent.exists());
155156
}
157+
158+
@Test
159+
public void createXqyRestExtension() throws IOException {
160+
String entityName = "my-fun-test";
161+
String extensionName = "myExtension";
162+
FlowType flowType = FlowType.CONFORMANCE;
163+
PluginFormat pluginFormat = PluginFormat.XQUERY;
164+
Scaffolding.createRestExtension(entityName, extensionName, flowType, pluginFormat, pluginsDir);
165+
File restDir = new File(pluginsDir.getAbsolutePath() + "/entities/"+ entityName + "/"+ flowType.name() +"/REST");
166+
assertTrue(restDir.exists());
167+
File restServicesDir = new File(restDir, "services");
168+
assertTrue(restServicesDir.exists());
169+
File restExtensionFile = new File(restServicesDir, extensionName + "." + pluginFormat);
170+
assertTrue(restExtensionFile.exists());
171+
File restExtensionMetadataDir = new File(restServicesDir, "metadata");
172+
assertTrue(restExtensionMetadataDir.exists());
173+
File restExtensionMetadataFile = new File(restExtensionMetadataDir, extensionName + ".xml");
174+
assertTrue(restExtensionMetadataFile.exists());
175+
}
176+
177+
@Test
178+
public void createSjsRestExtension() throws IOException {
179+
String entityName = "my-fun-test";
180+
String extensionName = "myExtension";
181+
FlowType flowType = FlowType.INPUT;
182+
PluginFormat pluginFormat = PluginFormat.JAVASCRIPT;
183+
Scaffolding.createRestExtension(entityName, extensionName, flowType, pluginFormat, pluginsDir);
184+
File restDir = new File(pluginsDir.getAbsolutePath() + "/entities/"+ entityName + "/"+ flowType.name() +"/REST");
185+
assertTrue(restDir.exists());
186+
File restServicesDir = new File(restDir, "services");
187+
assertTrue(restServicesDir.exists());
188+
File restExtensionFile = new File(restServicesDir, extensionName + "." + pluginFormat);
189+
assertTrue(restExtensionFile.exists());
190+
File restExtensionMetadataDir = new File(restServicesDir, "metadata");
191+
assertTrue(restExtensionMetadataDir.exists());
192+
File restExtensionMetadataFile = new File(restExtensionMetadataDir, extensionName + ".xml");
193+
assertTrue(restExtensionMetadataFile.exists());
194+
}
195+
196+
@Test
197+
public void createXqyRestTransform() throws IOException {
198+
String entityName = "my-fun-test";
199+
String transformName = "myTransform";
200+
FlowType flowType = FlowType.CONFORMANCE;
201+
PluginFormat pluginFormat = PluginFormat.XQUERY;
202+
Scaffolding.createRestTransform(entityName, transformName, flowType, pluginFormat, pluginsDir);
203+
File restDir = new File(pluginsDir.getAbsolutePath() + "/entities/"+ entityName + "/"+ flowType.name() +"/REST");
204+
assertTrue(restDir.exists());
205+
File restTransformDir = new File(restDir, "transforms");
206+
assertTrue(restTransformDir.exists());
207+
File restTransformFile = new File(restTransformDir, transformName + "." + pluginFormat);
208+
assertTrue(restTransformFile.exists());
209+
}
210+
211+
@Test
212+
public void createSjsRestTransform() throws IOException {
213+
String entityName = "my-fun-test";
214+
String transformName = "myTransform";
215+
FlowType flowType = FlowType.CONFORMANCE;
216+
PluginFormat pluginFormat = PluginFormat.JAVASCRIPT;
217+
Scaffolding.createRestTransform(entityName, transformName, flowType, pluginFormat, pluginsDir);
218+
File restDir = new File(pluginsDir.getAbsolutePath() + "/entities/"+ entityName + "/"+ flowType.name() +"/REST");
219+
assertTrue(restDir.exists());
220+
File restTransformDir = new File(restDir, "transforms");
221+
assertTrue(restTransformDir.exists());
222+
File restTransformFile = new File(restTransformDir, transformName + "." + pluginFormat);
223+
assertTrue(restTransformFile.exists());
224+
}
156225
}

0 commit comments

Comments
 (0)