View Javadoc
1   package org.neo4j.talend;
2   
3   import java.lang.reflect.Field;
4   import java.util.HashMap;
5   import java.util.List;
6   import java.util.Map;
7   
8   /**
9    * Abstract class for batch inserter classes.
10   * It has some usefull helper.
11   */
12  abstract class Neo4jBatchInserterAbstract {
13  
14      /**
15       * Neo4jBatchDatabase.
16       */
17      protected Neo4jBatchDatabase batchDb;
18  
19      /**
20       * Default construtor.
21       *
22       * @param batchDb Batch inserter database
23       */
24      public Neo4jBatchInserterAbstract(Neo4jBatchDatabase batchDb) {
25          this.batchDb = batchDb;
26      }
27  
28      /**
29       * Create an object (ie a node or a relationship) into the batch database.
30       *
31       * @param incoming   Talend incoming object
32       * @param columnList Attribute list of Talend object
33       */
34      public abstract void create(Object incoming, List<String> columnList) throws IllegalAccessException;
35  
36      /**
37       * What we do when the job is finished ?
38       */
39      public abstract void finish();
40  
41      /**
42       * Retrieve the value of a field on an object
43       *
44       * @param obj      the object
45       * @param property the property to retrieve
46       * @return The property value, or null if not found
47       */
48      protected Object getObjectProperty(Object obj, String property) throws IllegalAccessException {
49          Object value = null;
50  
51          Class<?> cls = obj.getClass();
52          Field[] fields = cls.getFields();
53  
54          for (Field field : fields) {
55              if (field.getName().equals(property)) {
56                  value = field.get(obj);
57              }
58          }
59  
60          return value;
61      }
62  
63      /**
64       * Convert a Talend object to a map of attributes.
65       *
66       * @param obj        The talend object
67       * @param columnList List of object properties
68       * @return Map of attributes
69       * @throws IllegalAccessException
70       */
71      protected Map<String, Object> constructMapFromObject(Object obj, List<String> columnList) throws IllegalAccessException {
72          // Construct the property map
73          Map<String, Object> properties = new HashMap<>();
74          for (String column : columnList) {
75              Object value = this.getObjectProperty(obj, column);
76  
77              // Type conversion for Neo4j
78              Object transValue = null;
79              if (value != null) {
80  
81                  if (value instanceof java.sql.Date) {
82                      transValue = ((java.sql.Date) value).getTime();
83                  }
84  
85                  if (value instanceof java.sql.Timestamp) {
86                      transValue = ((java.sql.Timestamp) value).getTime();
87                  }
88  
89                  if (value instanceof java.util.Date) {
90                      transValue = ((java.util.Date) value).getTime();
91                  }
92  
93                  if (transValue == null) {
94                      transValue = value;
95                  }
96  
97                  properties.put(column, transValue);
98              }
99          }
100         return properties;
101     }
102 
103 }