Skip to content

Commit 6b8456e

Browse files
committed
New DocumentLabelBuilderByType and bugfix
1 parent f7c84c1 commit 6b8456e

5 files changed

Lines changed: 156 additions & 3 deletions

File tree

neo4j-json/src/main/java/org/neo4j/helpers/json/document/impl/DocumentGrapherRecursive.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ private Node upsertDocument(String key, Map inDocument, int level) {
121121
//set properties
122122
final Node n = node;
123123

124-
document.forEach((k, v) -> n.setProperty(k, v));
124+
document.forEach((k, v) -> {
125+
if(v != null){
126+
n.setProperty(k, v);
127+
}
128+
});
125129

126130
DocumentRelationContext context = new DocumentRelationContext();
127131
context.setDocumentKey(key);

neo4j-json/src/main/java/org/neo4j/helpers/json/document/impl/DocumentLabelBuilderById.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@
2525
import org.neo4j.helpers.json.document.DocumentLabelBuilder;
2626

2727
/**
28-
* Build label from "type" property of node
28+
* Build label from "id" property of node
2929
* @author Omar Rampado
30-
* FIXME implement
3130
*
3231
*/
3332
public class DocumentLabelBuilderById implements DocumentLabelBuilder {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Copyright (c) 2016 LARUS Business Automation [http://www.larus-ba.it]
3+
* <p>
4+
* This file is part of the "LARUS Integration Framework for Neo4j".
5+
* <p>
6+
* The "LARUS Integration Framework for Neo4j" is licensed
7+
* 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+
* <p>
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
* <p>
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+
*/
19+
20+
package org.neo4j.helpers.json.document.impl;
21+
22+
import java.util.Map;
23+
24+
import org.apache.commons.lang3.text.WordUtils;
25+
import org.neo4j.graphdb.Label;
26+
import org.neo4j.helpers.json.document.DocumentLabelBuilder;
27+
28+
/**
29+
* Build label from "type" property of node
30+
* @author Omar Rampado
31+
*
32+
*/
33+
public class DocumentLabelBuilderByType implements DocumentLabelBuilder {
34+
35+
private String defaultLabel;
36+
37+
/* (non-Javadoc)
38+
* @see org.neo4j.helpers.json.document.DocumentLabelBuilder#buildLabel(java.util.Map)
39+
*/
40+
@Override
41+
public Label buildLabel(Map<String,Object> obj) {
42+
String label = defaultLabel;
43+
44+
Object type = obj.get("type");
45+
if(type != null)
46+
{
47+
label = String.valueOf(type);
48+
label = WordUtils.capitalizeFully(label,'_',' ')
49+
.replaceAll("_", "")
50+
.replaceAll(" ", "");
51+
}
52+
53+
return Label.label(label);
54+
}
55+
56+
@Override
57+
public void setDefaultLabel(String label) {
58+
defaultLabel = label;
59+
}
60+
61+
}

neo4j-json/src/test/java/org/neo4j/helpers/json/JsonHelperDefaultTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,23 @@ public void shouldAddOnlyOneNode() {
107107
Assert.assertEquals("Wrong node","Genesis", result.single().get("n.name").asString());
108108

109109
}
110+
111+
@Test
112+
public void shouldNotDiscardForNull() {
113+
String key = "shouldAddOnlyOneNode";
114+
String json = "{\"id\": 1, "
115+
+ "\"type\": \"artist\","
116+
+ "\"name\": \"Genesis\","
117+
+ "\"available_markets\" : [ ],"
118+
+ "\"preview_url\" : null"
119+
+ "}";
120+
121+
session.run(CALL_UPSERT, Values.parameters( "key", key, "json", json ));
122+
StatementResult result = session.run("MATCH (n {id: 1, type: 'artist'}) RETURN n.name");
123+
Assert.assertEquals("Wrong node","Genesis", result.single().get("n.name").asString());
124+
125+
}
126+
110127

111128
@Test
112129
public void shouldDiscard() {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Copyright (c) 2016 LARUS Business Automation [http://www.larus-ba.it]
3+
* <p>
4+
* This file is part of the "LARUS Integration Framework for Neo4j".
5+
* <p>
6+
* The "LARUS Integration Framework for Neo4j" is licensed
7+
* 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+
* <p>
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
* <p>
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+
*/
19+
package org.neo4j.helpers.json.document.impl;
20+
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
24+
import org.junit.Assert;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
import org.neo4j.graphdb.Label;
28+
29+
30+
/**
31+
* @author Omar Rampado
32+
*
33+
*/
34+
public class DocumentLabelBuilderByTypeTest {
35+
36+
DocumentLabelBuilderByType builder;
37+
38+
/**
39+
* @throws java.lang.Exception
40+
*/
41+
@Before
42+
public void setUp() throws Exception {
43+
builder = new DocumentLabelBuilderByType();
44+
}
45+
46+
@Test
47+
public void testBuildLabelUnderscore() {
48+
builder.setDefaultLabel("NODE");
49+
Map<String, Object> map = new HashMap<>();
50+
map.put("type", "big_artist");
51+
Label label = builder.buildLabel(map);
52+
Assert.assertEquals("BigArtist", label.name());
53+
}
54+
55+
@Test
56+
public void testBuildLabelSpace() {
57+
builder.setDefaultLabel("NODE");
58+
Map<String, Object> map = new HashMap<>();
59+
map.put("type", "big artist");
60+
Label label = builder.buildLabel(map);
61+
Assert.assertEquals("BigArtist", label.name());
62+
}
63+
64+
@Test
65+
public void testBuildLabelDefault() {
66+
builder.setDefaultLabel("NODE");
67+
Map<String, Object> map = new HashMap<>();
68+
map.put("id", "123");
69+
Label label = builder.buildLabel(map);
70+
Assert.assertEquals("NODE", label.name());
71+
}
72+
}

0 commit comments

Comments
 (0)