Merge branch 'master' of ssh://apples.lambdacomplex.org/git/disclosr
[disclosr.git] / admin / neo4jimporter / src / main / java / StAXSample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import org.neo4j.graphdb.DynamicLabel;
import org.neo4j.graphdb.DynamicRelationshipType;
import org.neo4j.graphdb.Label;
import org.neo4j.unsafe.batchinsert.BatchInserter;
import org.neo4j.unsafe.batchinsert.BatchInserters;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
 
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.events.XMLEvent;
 
public class StAXSample {
 
 
    HashMap<String, Long> agencyIDs = new HashMap<String, Long>();
    HashMap<String, Boolean> agencyFullVersion = new HashMap<String, Boolean>();
    Label agencyLabel = DynamicLabel.label("Agency");
    HashMap<String, Long> locationIDs = new HashMap<String, Long>();   
    Label locationLabel = DynamicLabel.label("Location");
    HashMap<String, Long> functionIDs = new HashMap<String, Long>();
    Label functionLabel = DynamicLabel.label("Function");
    HashMap<String, Long> statusIDs = new HashMap<String, Long>();
    Label statusLabel = DynamicLabel.label("Location");
    BatchInserter inserter;
 
    private String filename;
 
    public StAXSample() {
    }
 
    public static void main(String[] args) {
        /*if (args.length != 1) {
            System.out.println("Usage: StAXSample file.xml");
            System.exit(-1);
        } */
 
        StAXSample ss = new StAXSample();
        //ss.setFilename(args[0]);
        ss.setFilename("agency-sample.xml");
        ss.run();
    }
 
    public void run() {
 
        Map<String, String> config = new HashMap<String, String>();
        config.put("neostore.nodestore.db.mapped_memory", "90M");
        inserter = BatchInserters.inserter("target/batchinserter-example-config", config);
        inserter.createDeferredSchemaIndex(agencyLabel).on("agency_no");
        inserter.createDeferredSchemaIndex(locationLabel).on("location_name");
        inserter.createDeferredSchemaIndex(functionLabel).on("thesaurus_term");
        inserter.createDeferredSchemaIndex(statusLabel).on("status_name");
 
        try {
            XMLInputFactory xmlif = XMLInputFactory.newInstance();
            xmlif.setProperty(
                    XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,
                    Boolean.TRUE);
            xmlif.setProperty(
                    XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,
                    Boolean.FALSE);
            //set the IS_COALESCING property to true 
            //to get whole text data as one event.           
            xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
 
            try {
                XMLEventReader r = null;
                r = xmlif.createXMLEventReader(
                        filename,
                        //new FileInputStream(new File(xmlFileURL.toURI())));
                        new FileInputStream(new File(filename)));
 
 
                //iterate as long as there are more events on the input stream
                while (r.hasNext()) {
                    XMLEvent e = r.nextEvent();
                    Map<String, Object> previousAgency = new HashMap<String, Object>();
                    if (e.isStartElement()) {
                        if (hasStartTagName(e, "AGENCIES")) {
                            System.out.println("Agencies file loaded... ");
                        } else if (hasStartTagName(e, "TITLE")) {
                            System.out.println("TITLE is: " + getCharacters(r));
                            previousAgency.put("title", getCharacters(r));
                        } else if (hasStartTagName(e, "END_DATE_QUAL")) {
                            System.out.println("END_DATE_QUAL is: " + getCharacters(r));
                            previousAgency.put("end_date_qual", getCharacters(r));
                            // save agency
                            getAgency(previousAgency);
                            previousAgency = new HashMap<String, Object>();
                        } else if (hasStartTagName(e, "AGENCY_LINK")) {
                            processAgencyLink(r);
                        } else if (hasStartTagName(e, "AGENCY_LOCATION")) {
                            processAgencyLocation(r);
                        } else if (hasStartTagName(e, "AGENCY_FUNCTION")) {
                            processAgencyFunction(r);
                        } else if (hasStartTagName(e, "AGENCY_STATUS")) {
                            processAgencyStatus(r);
                        } else {
                            System.out.println("Unhandled tag: " + getStartTagName(e) + " content:" + getCharacters(r));
                        }
                    }
                }
            } catch (XMLStreamException ex) {
                System.out.println(ex.getMessage());
 
                if (ex.getNestedException() != null) {
                    ex.getNestedException().printStackTrace();
                }
            }
 
        } catch (FileNotFoundException ex) {
            System.err.println("Error.  Cannot find \"" + filename + "\" in classpath.");
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
 
        inserter.shutdown();
    }
 
    private long getAgency(Map<String, Object> properties) {
        if (agencyIDs.get(properties.get("agency_no").toString()) == null) {
            long agencyID = inserter.createNode(properties, agencyLabel);
            if (properties.values().size() > 2) {
                agencyFullVersion.put(properties.get("agency_no").toString(), true);
            }
            agencyIDs.put(properties.get("agency_no").toString(), agencyID);
            return agencyID;
        } else {
            long agencyID = agencyIDs.get(properties.get("agency_no").toString());
            if (properties.values().size() > 2 && agencyFullVersion.get(properties.get("agency_no")) == null) {
                inserter.setNodeProperties(agencyID, properties);
                agencyFullVersion.put(properties.get("agency_no").toString(), true);
            }
            return agencyID;
        }
    }
 
    private long getLocation(String locationName) {
        if (locationIDs.get(locationName) == null) {
            HashMap properties = new HashMap< String,Object > ();
            properties.put("location_name", locationName);
            long locationID = inserter.createNode(properties, locationLabel);
            locationIDs.put(locationName, locationID);
            return locationID;
        } else {
            return locationIDs.get(locationName);
        }
    }
    private long getFunction(String functionName) {
        if (functionIDs.get(functionName) == null) {
            HashMap properties = new HashMap< String,Object > ();
            properties.put("function_name", functionName);
            long functionID = inserter.createNode(properties, functionLabel);
            functionIDs.put(functionName, functionID);
            return functionID;
        } else {
            return functionIDs.get(functionName);
        }
    }
    private long getStatus(String statusName) {
        if (statusIDs.get(statusName) == null) {
            HashMap properties = new HashMap< String,Object > ();
            properties.put("status_name", statusName);
            long statusID = inserter.createNode(properties, statusLabel);
            statusIDs.put(statusName, statusID);
            return statusID;
        } else {
            return statusIDs.get(statusName);
        }
    }
 
    private void processAgencyLink(XMLEventReader rdr) throws Exception {
        String agency_from_no = null;
        String agency_to_no = null;
        String link_type = null;
        String start_date = null;
        String start_date_qual = null;
        String end_date = null;
        String end_date_qual = null;
 
        while (rdr.hasNext()) {
            XMLEvent e = rdr.nextEvent();
            if (e.isStartElement()) {
                if (hasStartTagName(e, "LINK_AGENCY_NO")) {
                    agency_from_no = getCharacters(rdr);
                } else if (hasStartTagName(e, "LINK_TO_AGENCY_NO")) {
                    agency_to_no = getCharacters(rdr);
                } else if (hasStartTagName(e, "LINK_TYPE")) {
                    link_type = getCharacters(rdr);
                }  else if (hasStartTagName(e, "START_DATE")) {
                    start_date = getCharacters(rdr);
                }else if (hasStartTagName(e, "START_DATE_QUAL")) {
                    start_date_qual = getCharacters(rdr);
                }else if (hasStartTagName(e, "END_DATE")) {
                    end_date = getCharacters(rdr);
                }else if (hasStartTagName(e, "END_DATE_QUAL")) {
                    end_date_qual = getCharacters(rdr);
                }
            }
            if (e.isEndElement()) {
                if (hasEndTagName(e, "AGENCY_LINK")) {
 
                    //System.out.println("Finished processing link:  Name = " + name + "; of = " + of + "; date = " + date);
                    long agencyFromID, agencyToID;
                    Map<String, Object> agencyFromProperties = new HashMap<String, Object>();
                    agencyFromProperties.put("agency_no",agency_from_no);
                    agencyFromID = getAgency(agencyFromProperties);
                    Map<String, Object> agencyToProperties = new HashMap<String, Object>();
                    agencyToProperties.put("agency_no",agency_to_no);
                    agencyToID = getAgency(agencyToProperties);
                    Map<String, Object> relProperties = new HashMap<String, Object>();
                    relProperties.put("link_type", link_type);
                    relProperties.put("start_date", start_date);
                    relProperties.put("start_date_qual", start_date_qual);
                    relProperties.put("end_date", end_date);
                    relProperties.put("end_date_qual", end_date_qual);
                    inserter.createRelationship(agencyFromID, agencyToID,
                            DynamicRelationshipType.withName("IS_LINKED_TO"), relProperties);
 
                    break;
                }
            }
        }
    }
 
    private void processAgencyLocation(XMLEventReader rdr) throws Exception {
        String of = null;
        String name = null;
        String date = null;
 
        while (rdr.hasNext()) {
            XMLEvent e = rdr.nextEvent();
            if (e.isStartElement()) {
                if (hasStartTagName(e, "LOCATION_AGENCY_NO"