Skip to content

Commit 51b5d18

Browse files
rjrudinMarkLogic Builder
authored andcommitted
DHFPROD-3193: language is now replaced with lang when loading artifacts
Also fixed javadoc error in Mapping.java
1 parent a7d643f commit 51b5d18

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

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

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package com.marklogic.hub.deploy.commands;
1717

18+
import com.fasterxml.jackson.databind.JsonNode;
19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import com.fasterxml.jackson.databind.node.ObjectNode;
1821
import com.marklogic.appdeployer.AppConfig;
1922
import com.marklogic.appdeployer.command.AbstractCommand;
2023
import com.marklogic.appdeployer.command.CommandContext;
@@ -28,8 +31,10 @@
2831
import com.marklogic.client.ext.util.DefaultDocumentPermissionsParser;
2932
import com.marklogic.client.ext.util.DocumentPermissionsParser;
3033
import com.marklogic.client.io.DocumentMetadataHandle;
34+
import com.marklogic.client.io.JacksonHandle;
3135
import com.marklogic.client.io.StringHandle;
3236
import com.marklogic.hub.HubConfig;
37+
import com.marklogic.mgmt.util.ObjectMapperFactory;
3338
import org.apache.commons.io.IOUtils;
3439
import org.springframework.beans.factory.annotation.Autowired;
3540
import org.springframework.core.io.Resource;
@@ -41,6 +46,7 @@
4146
import java.nio.file.*;
4247
import java.nio.file.attribute.BasicFileAttributes;
4348
import java.util.Date;
49+
import java.util.Iterator;
4450
import java.util.regex.Pattern;
4551

4652
/**
@@ -53,15 +59,18 @@ public class LoadUserArtifactsCommand extends AbstractCommand {
5359
private HubConfig hubConfig;
5460

5561
private DocumentPermissionsParser documentPermissionsParser = new DefaultDocumentPermissionsParser();
62+
private ObjectMapper objectMapper;
5663

5764
public void setForceLoad(boolean forceLoad) {
5865
this.forceLoad = forceLoad;
5966
}
6067

6168
private boolean forceLoad = false;
6269

70+
6371
public LoadUserArtifactsCommand() {
6472
super();
73+
this.objectMapper = ObjectMapperFactory.getObjectMapper();
6574
setExecuteSortOrder(SortOrderConstants.DEPLOY_TRIGGERS + 1);
6675
}
6776

@@ -279,19 +288,60 @@ private void addResourceToWriteSets(
279288
) throws IOException {
280289
if (forceLoad || propertiesModuleManager.hasFileBeenModifiedSinceLastLoaded(r.getFile())) {
281290
InputStream inputStream = r.getInputStream();
282-
StringHandle handle = new StringHandle(IOUtils.toString(inputStream));
283-
inputStream.close();
284-
for (DocumentWriteSet writeSet: writeSets) {
285-
writeSet.add(docId, meta, handle);
291+
292+
JsonNode json;
293+
try {
294+
json = objectMapper.readTree(inputStream);
295+
} finally {
296+
inputStream.close();
297+
}
298+
299+
if (json instanceof ObjectNode && json.has("language")) {
300+
json = replaceLanguageWithLang((ObjectNode)json);
301+
try {
302+
objectMapper.writeValue(r.getFile(), json);
303+
} catch (Exception ex) {
304+
logger.warn("Unable to replace 'language' with 'lang' in artifact file: " + r.getFile().getAbsolutePath()
305+
+ ". You should replace 'language' with 'lang' yourself in this file. Error cause: " + ex.getMessage(), ex);
306+
}
307+
}
308+
309+
for (DocumentWriteSet writeSet : writeSets) {
310+
writeSet.add(docId, meta, new JacksonHandle(json));
286311
}
287312
propertiesModuleManager.saveLastLoadedTimestamp(r.getFile(), new Date());
288313
}
289314
}
290315

316+
/**
317+
* Per DHFPROD-3193 and an update to MarkLogic 10.0-2, "lang" must now be used instead of "language". To ensure that
318+
* a user artifact is never loaded with "language", this command handles both updating the JSON that will be loaded
319+
* into MarkLogic and updating the artifact file.
320+
*
321+
* @param object
322+
* @return
323+
*/
324+
protected ObjectNode replaceLanguageWithLang(ObjectNode object) {
325+
ObjectNode newObject = objectMapper.createObjectNode();
326+
newObject.put("lang", object.get("language").asText());
327+
Iterator<String> fieldNames = object.fieldNames();
328+
while (fieldNames.hasNext()) {
329+
String fieldName = fieldNames.next();
330+
if (!"language".equals(fieldName)) {
331+
newObject.set(fieldName, object.get(fieldName));
332+
}
333+
}
334+
return newObject;
335+
}
336+
291337
public void setHubConfig(HubConfig hubConfig) {
292338
this.hubConfig = hubConfig;
293339
}
294340

341+
public void setObjectMapper(ObjectMapper objectMapper) {
342+
this.objectMapper = objectMapper;
343+
}
344+
295345
abstract class ResourceToURI {
296346
public abstract String toURI(Resource r) throws IOException;
297347
}

marklogic-data-hub/src/main/java/com/marklogic/hub/mapping/Mapping.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ static Mapping create(String mappingName, HubEntity entity) {
135135
/**
136136
* *CAREFUL - DO NOT TOUCH IF YOU DON'T EXPLICITLY KNOW WHAT THIS VALUE REPRESENTS*
137137
* Sets the language for the mapping to use for MarkLogic server
138-
* @param language - sets the language key for the server - don't modify please!
138+
* @param lang - sets the language key for the server - don't modify please!
139139
*/
140140
void setLang(String lang);
141141

marklogic-data-hub/src/test/java/com/marklogic/hub/deploy/commands/LoadUserArtifactsCommandTest.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
*/
1616
package com.marklogic.hub.deploy.commands;
1717

18+
import com.fasterxml.jackson.databind.node.ObjectNode;
1819
import com.marklogic.hub.HubTestBase;
1920
import com.marklogic.hub.ApplicationConfig;
21+
import com.marklogic.mgmt.util.ObjectMapperFactory;
2022
import org.junit.jupiter.api.BeforeEach;
2123
import org.junit.jupiter.api.Test;
2224
import org.junit.jupiter.api.extension.ExtendWith;
@@ -27,8 +29,7 @@
2729
import java.nio.file.Path;
2830
import java.nio.file.Paths;
2931

30-
import static org.junit.jupiter.api.Assertions.assertFalse;
31-
import static org.junit.jupiter.api.Assertions.assertTrue;
32+
import static org.junit.jupiter.api.Assertions.*;
3233

3334

3435
@ExtendWith(SpringExtension.class)
@@ -40,6 +41,20 @@ public void setup() {
4041
loadUserArtifactsCommand.setHubConfig(getDataHubAdminConfig());
4142
}
4243

44+
@Test
45+
public void replaceLanguageWithLang() {
46+
ObjectNode object = ObjectMapperFactory.getObjectMapper().createObjectNode();
47+
object.put("language", "zxx");
48+
object.put("something", "else");
49+
50+
object = loadUserArtifactsCommand.replaceLanguageWithLang(object);
51+
assertEquals("zxx", object.get("lang").asText());
52+
assertEquals("else", object.get("something").asText());
53+
assertFalse(object.has("language"));
54+
assertEquals("lang", object.fieldNames().next(),
55+
"lang should still be the first field name in the JSON object");
56+
}
57+
4358
@Test
4459
public void testIsEntityDir() {
4560
Path startPath = Paths.get("/tmp/my-project/plugins/entities");

0 commit comments

Comments
 (0)