Skip to content

Commit c2c6793

Browse files
committed
ID Builder can be configured. New Pair id builder
1 parent 46b7424 commit c2c6793

12 files changed

Lines changed: 322 additions & 9 deletions

File tree

neo4j-json/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ It's the name of class that implements `org.neo4j.helpers.json.document.Document
5050
You can choose from:
5151
* `org.neo4j.helpers.json.document.impl.DocumentIdBuilderTypeId`
5252
* `org.neo4j.helpers.json.document.impl.DocumentIdBuilderId`
53+
* `org.neo4j.helpers.json.document.impl.DocumentIdBuilderPair` with properties:
54+
** `builder_id_pair_key1`
55+
** `builder_id_pair_key2`
5356
* your own implementation
5457

5558

neo4j-json/src/main/java/org/neo4j/helpers/json/config/impl/JsonHelperConfigurationByNode.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,9 @@ public class JsonHelperConfigurationByNode extends JsonHelperConfiguration {
4747
public JsonHelperConfigurationByNode(Node confNode) {
4848
super();
4949
this.configuration = new HashMap<>();
50-
this.configuration.put("root_node_key_property", String.valueOf(confNode.getProperty("root_node_key_property")));
51-
this.configuration.put("document_id_builder", String.valueOf(confNode.getProperty("document_id_builder")));
52-
this.configuration.put("document_relation_builder", String.valueOf(confNode.getProperty("document_relation_builder")));
53-
this.configuration.put("document_label_builder", String.valueOf(confNode.getProperty("document_label_builder")));
54-
this.configuration.put("document_default_label", String.valueOf(confNode.getProperty("document_default_label")));
55-
this.configuration.put("log_discard_events", String.valueOf(confNode.getProperty("log_discard_events")));
50+
confNode.getAllProperties().entrySet().forEach(es -> {
51+
this.configuration.put(es.getKey(), String.valueOf(es.getValue()));
52+
});
5653
}
5754

5855
/*
@@ -76,6 +73,7 @@ protected DocumentGrapherExecutionContext buildContext(GraphDatabaseService db,
7673
context.setRootNodeKeyProperty(keyProperty);
7774

7875
DocumentIdBuilder documentIdBuilder = (DocumentIdBuilder) newInstance(docIdBuilderString);
76+
documentIdBuilder.init(configuration);
7977
context.setDocumentIdBuilder(documentIdBuilder);
8078

8179
DocumentRelationBuilder documentRelationBuilder = (DocumentRelationBuilder) newInstance(docRelBuilderString);

neo4j-json/src/main/java/org/neo4j/helpers/json/document/DocumentIdBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,10 @@ public interface DocumentIdBuilder {
3535
* @return
3636
*/
3737
DocumentId buildId(Map<String,Object> obj);
38+
39+
/**
40+
* Configure the builder with parameters
41+
* @param configuration
42+
*/
43+
void init(Map<String, String> configuration);
3844
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ public DocumentId buildId(Map<String,Object> obj) {
4747
return new DocumentIdId(idObj);
4848
}
4949

50+
@Override
51+
public void init(Map<String, String> configuration) {
52+
//nothing to configure
53+
}
54+
5055
}
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.Validate;
25+
import org.neo4j.helpers.json.document.DocumentId;
26+
import org.neo4j.helpers.json.document.DocumentIdBuilder;
27+
28+
/**
29+
* Build {@link DocumentIdPair}
30+
* @author Omar Rampado
31+
*
32+
*/
33+
public class DocumentIdBuilderPair implements DocumentIdBuilder {
34+
35+
private String key1;
36+
private String key2;
37+
38+
/*
39+
* (non-Javadoc)
40+
*
41+
* @see
42+
* org.neo4j.helpers.json.document.DocumentIdBuilder#buildId(java.util.Map)
43+
*/
44+
@Override
45+
public DocumentId buildId(Map<String,Object> obj) {
46+
Object obj1 = obj.get(this.key1);
47+
Object obj2 = obj.get(this.key2);
48+
49+
Validate.notNull(obj1, "every object must have "+this.key1+": "+obj);
50+
Validate.notNull(obj2, "every object must have "+this.key2+": "+obj);
51+
52+
return new DocumentIdPair(key1, key2, obj1, obj2);
53+
}
54+
55+
@Override
56+
public void init(Map<String, String> configuration) {
57+
this.key1 = configuration.get("builder_id_pair_key1");
58+
this.key2 = configuration.get("builder_id_pair_key2");
59+
}
60+
61+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ public DocumentId buildId(Map<String,Object> obj) {
4949
return new DocumentIdTypeId(typeObj, idObj);
5050
}
5151

52+
@Override
53+
public void init(Map<String, String> configuration) {
54+
// nothing to configure
55+
56+
}
57+
5258
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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.HashMap;
23+
import java.util.Map;
24+
25+
import org.neo4j.helpers.json.document.DocumentId;
26+
import org.neo4j.helpers.json.document.utils.IdValueTypeSafe;
27+
28+
/**
29+
* DocumentId composed by a pair of values, customizable
30+
* @author Omar Rampado
31+
*
32+
*/
33+
public class DocumentIdPair implements DocumentId {
34+
35+
private String key1;
36+
private String key2;
37+
private IdValueTypeSafe v1;
38+
private IdValueTypeSafe v2;
39+
private Map<String, String> fields;
40+
41+
/**
42+
* @param v2
43+
* @param v1
44+
*/
45+
public DocumentIdPair(String k1, String k2, Object v1, Object v2) {
46+
super();
47+
this.key1 = k1;
48+
this.key2 = k2;
49+
this.v1 = new IdValueTypeSafe(v1);
50+
this.v2 = new IdValueTypeSafe(v2);
51+
this.fields = new HashMap<>(2);
52+
this.fields.put(key1, this.v1.toString());
53+
this.fields.put(key2, this.v2.toString());
54+
}
55+
56+
57+
58+
/* (non-Javadoc)
59+
* @see org.neo4j.helpers.json.document.DocumentId#getFields()
60+
*/
61+
@Override
62+
public Map<String, String> getFields() {
63+
return this.fields;
64+
}
65+
66+
67+
68+
/* (non-Javadoc)
69+
* @see org.neo4j.helpers.json.document.DocumentId#toCypherFilter()
70+
*/
71+
@Override
72+
public String toCypherFilter() {
73+
StringBuilder sb = new StringBuilder();
74+
sb.append(this.key1);
75+
sb.append(": ");
76+
sb.append(this.v1.toCypher());
77+
78+
sb.append(", ");
79+
80+
sb.append(this.key2);
81+
sb.append(": ");
82+
sb.append(this.v2.toCypher());
83+
84+
85+
return sb.toString();
86+
}
87+
88+
}

neo4j-json/src/main/java/org/neo4j/helpers/json/document/utils/IdValueTypeSafe.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public IdValueTypeSafe(Object value) {
4848
public String toCypher(){
4949
if(isString())
5050
{
51-
return "'"+toString()+"'";
51+
return "'"+toString().replaceAll("'", "\\\\'")+"'";
5252
}
5353
return toString();
5454
}

neo4j-json/src/test/java/org/neo4j/helpers/json/config/JsonHelperConfigurationTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import static org.easymock.EasyMock.expect;
2222
import static org.easymock.EasyMock.replay;
2323

24+
import java.util.HashMap;
25+
import java.util.Map;
26+
2427
import org.easymock.EasyMockRunner;
2528
import org.easymock.Mock;
2629
import org.easymock.MockType;
@@ -59,13 +62,22 @@ public class JsonHelperConfigurationTest {
5962
*/
6063
@Before
6164
public void setUp() throws Exception {
65+
Map<String, Object> allProps = new HashMap<>();
66+
allProps.put("configuration", "byNode");
67+
allProps.put("root_node_key_property", "_root_");
68+
allProps.put("document_id_builder", "org.neo4j.helpers.json.document.impl.DocumentIdBuilderId");
69+
allProps.put("document_relation_builder", "org.neo4j.helpers.json.document.impl.DocumentRelationBuilderByKey");
70+
allProps.put("document_label_builder", "org.neo4j.helpers.json.document.impl.DocumentLabelBuilderById");
71+
6272
JsonHelperConfiguration.reset();
73+
expect(confNode.getAllProperties()).andStubReturn(allProps);
74+
/*
6375
expect(confNode.getProperty("configuration")).andStubReturn("byNode");
6476
expect(confNode.getProperty("root_node_key_property")).andStubReturn("_root_");
6577
expect(confNode.getProperty("document_id_builder")).andStubReturn("org.neo4j.helpers.json.document.impl.DocumentIdBuilderId");
6678
expect(confNode.getProperty("document_relation_builder")).andStubReturn("org.neo4j.helpers.json.document.impl.DocumentRelationBuilderByKey");
6779
expect(confNode.getProperty("document_label_builder")).andStubReturn("org.neo4j.helpers.json.document.impl.DocumentLabelBuilderById");
68-
80+
*/
6981
expect(db.findNode(Label.label("JSON_CONFIG"), "configuration", "byNode")).andReturn(confNode);
7082
replay(db,log, confNode);
7183
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.HashMap;
23+
import java.util.Map;
24+
25+
import org.junit.Assert;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.neo4j.helpers.json.document.DocumentId;
29+
30+
public class DocumentIdBuilderPairTest {
31+
32+
DocumentIdBuilderPair builder;
33+
34+
@Before
35+
public void setUp() throws Exception {
36+
this.builder = new DocumentIdBuilderPair();
37+
Map<String, String> conf = new HashMap<>();
38+
conf.put("builder_id_pair_key1", "attribute");
39+
conf.put("builder_id_pair_key2", "name");
40+
this.builder.init(conf);
41+
}
42+
43+
@Test
44+
public void testBuildId() {
45+
Map<String, Object> map = new HashMap<>();
46+
map.put("attribute", 12);
47+
map.put("name", "artist");
48+
DocumentId buildId = builder.buildId(map);
49+
Assert.assertEquals("attribute: 12, name: 'artist'", buildId.toCypherFilter());
50+
}
51+
52+
@Test(expected=NullPointerException.class)
53+
public void testBuildIdNoType() {
54+
Map<String, Object> map = new HashMap<>();
55+
map.put("name", 12);
56+
builder.buildId(map);
57+
}
58+
59+
@Test(expected=NullPointerException.class)
60+
public void testBuildIdNoId() {
61+
Map<String, Object> map = new HashMap<>();
62+
map.put("attribute", "artist");
63+
builder.buildId(map);
64+
}
65+
}

0 commit comments

Comments
 (0)