|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+package se.citerus.dddsample.util;
|
|
|
2
|
+
|
|
|
3
|
+import org.apache.commons.io.IOUtils;
|
|
|
4
|
+import org.apache.commons.io.LineIterator;
|
|
|
5
|
+import org.apache.commons.logging.Log;
|
|
|
6
|
+import org.apache.commons.logging.LogFactory;
|
|
|
7
|
+import org.hibernate.CacheMode;
|
|
|
8
|
+import org.hibernate.FlushMode;
|
|
|
9
|
+import org.hibernate.SessionFactory;
|
|
|
10
|
+import org.hibernate.classic.Session;
|
|
|
11
|
+import org.springframework.beans.factory.BeanFactoryUtils;
|
|
|
12
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
13
|
+import org.springframework.transaction.PlatformTransactionManager;
|
|
|
14
|
+import org.springframework.transaction.TransactionStatus;
|
|
|
15
|
+import org.springframework.transaction.support.TransactionCallback;
|
|
|
16
|
+import org.springframework.transaction.support.TransactionTemplate;
|
|
|
17
|
+import org.springframework.web.context.WebApplicationContext;
|
|
|
18
|
+import org.springframework.web.context.support.WebApplicationContextUtils;
|
|
|
19
|
+import se.citerus.dddsample.domain.Location;
|
|
|
20
|
+import se.citerus.dddsample.domain.UnLocode;
|
|
|
21
|
+
|
|
|
22
|
+import javax.servlet.ServletContextEvent;
|
|
|
23
|
+import javax.servlet.ServletContextListener;
|
|
|
24
|
+import java.io.IOException;
|
|
|
25
|
+import java.io.InputStream;
|
|
|
26
|
+import java.util.zip.ZipEntry;
|
|
|
27
|
+import java.util.zip.ZipFile;
|
|
|
28
|
+
|
|
|
29
|
+/**
|
|
|
30
|
+ * Imports about 55 000 locations from an official UN Locode CSV export.
|
|
|
31
|
+ *
|
|
|
32
|
+ */
|
|
|
33
|
+public class LocationsImporter implements ServletContextListener {
|
|
|
34
|
+ private static final String ZIP_FILE_NAME = "unlocodes.zip";
|
|
|
35
|
+ private static final String ZIP_ENTRY_NAME = "2006-2 UNLOCODE CodeList.txt";
|
|
|
36
|
+ private static final int BATCH_SIZE = 1000;
|
|
|
37
|
+ private static final Log logger = LogFactory.getLog(LocationsImporter.class);
|
|
|
38
|
+
|
|
|
39
|
+ protected int importLocations(Session session) throws IOException {
|
|
|
40
|
+ ZipFile zipFile = new ZipFile(new ClassPathResource(ZIP_FILE_NAME).getFile());
|
|
|
41
|
+ ZipEntry zipEntry = zipFile.getEntry(ZIP_ENTRY_NAME);
|
|
|
42
|
+ InputStream inputStream = zipFile.getInputStream(zipEntry);
|
|
|
43
|
+ LineIterator iterator = IOUtils.lineIterator(inputStream, "ISO-8859-1");
|
|
|
44
|
+ session.setCacheMode(CacheMode.IGNORE);
|
|
|
45
|
+ session.setFlushMode(FlushMode.MANUAL);
|
|
|
46
|
+
|
|
|
47
|
+ int insertCount = 1;
|
|
|
48
|
+ while (iterator.hasNext()) {
|
|
|
49
|
+ String line = iterator.nextLine();
|
|
|
50
|
+ Location location = parseLocation(line);
|
|
|
51
|
+ if (location != null) {
|
|
|
52
|
+ session.save(location);
|
|
|
53
|
+ if (insertCount % BATCH_SIZE == 0) {
|
|
|
54
|
+ session.flush();
|
|
|
55
|
+ session.clear();
|
|
|
56
|
+ }
|
|
|
57
|
+ insertCount++;
|
|
|
58
|
+ }
|
|
|
59
|
+ }
|
|
|
60
|
+ session.flush();
|
|
|
61
|
+
|
|
|
62
|
+ return insertCount;
|
|
|
63
|
+ }
|
|
|
64
|
+
|
|
|
65
|
+ private Location parseLocation(String line) {
|
|
|
66
|
+ String countryCode = line.substring(3, 5);
|
|
|
67
|
+ String locationCode = line.substring(6, 9);
|
|
|
68
|
+ if (locationCode.trim().length() == 3) {
|
|
|
69
|
+ String name = line.substring(10, 46).trim();
|
|
|
70
|
+ UnLocode unlocode = new UnLocode(countryCode, locationCode);
|
|
|
71
|
+ return new Location(unlocode, name);
|
|
|
72
|
+ } else {
|
|
|
73
|
+ return null;
|
|
|
74
|
+ }
|
|
|
75
|
+ }
|
|
|
76
|
+
|
|
|
77
|
+ public void contextInitialized(ServletContextEvent event) {
|
|
|
78
|
+ final WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
|
|
|
79
|
+ final PlatformTransactionManager ptm = (PlatformTransactionManager) BeanFactoryUtils.beanOfType(context, PlatformTransactionManager.class);
|
|
|
80
|
+ final SessionFactory sf = (SessionFactory) BeanFactoryUtils.beanOfType(context, SessionFactory.class);
|
|
|
81
|
+ final TransactionTemplate tt = new TransactionTemplate(ptm);
|
|
|
82
|
+
|
|
|
83
|
+ long t = System.currentTimeMillis();
|
|
|
84
|
+ Integer count = (Integer) tt.execute(new TransactionCallback() {
|
|
|
85
|
+ public Object doInTransaction(TransactionStatus status) {
|
|
|
86
|
+ try {
|
|
|
87
|
+ return importLocations(sf.getCurrentSession());
|
|
|
88
|
+ } catch (IOException e) {
|
|
|
89
|
+ throw new RuntimeException(e);
|
|
|
90
|
+ }
|
|
|
91
|
+ }
|
|
|
92
|
+ });
|
|
|
93
|
+ logger.info("Imported " + count + " locations in " + (System.currentTimeMillis() - t)/1000.0 + " seconds.");
|
|
|
94
|
+ }
|
|
|
95
|
+
|
|
|
96
|
+ public void contextDestroyed(ServletContextEvent event) {}
|
|
|
97
|
+}
|