1 package ru
.rentdom
.services
.coords
;
4 import java
.util
.HashMap
;
8 import org
.slf4j
.Logger
;
10 import ru
.rentdom
.dao
.CoordsDAO
;
11 import ru
.rentdom
.entities
.coords
.Coord
;
12 import ru
.rentdom
.entities
.coords
.Country
;
13 import ru
.rentdom
.entities
.coords
.District
;
14 import ru
.rentdom
.entities
.coords
.MetroStation
;
15 import ru
.rentdom
.entities
.coords
.Town
;
16 import dk
.eobjects
.metamodel
.DataContext
;
17 import dk
.eobjects
.metamodel
.DataContextFactory
;
18 import dk
.eobjects
.metamodel
.data
.DataSet
;
19 import dk
.eobjects
.metamodel
.query
.Query
;
20 import dk
.eobjects
.metamodel
.schema
.Column
;
21 import dk
.eobjects
.metamodel
.schema
.Schema
;
22 import dk
.eobjects
.metamodel
.schema
.Table
;
24 public class CoordsImport
{
25 private final CoordsDAO coordsDao
;
26 private final Logger logger
;
28 public CoordsImport(CoordsDAO coordsDAO
, Logger logger
)
30 coordsDao
= coordsDAO
;
34 public void process(File excelFile
)
36 DataContext dataContext
= DataContextFactory
.createExcelDataContext(excelFile
, false);
37 Schema schema
= dataContext
.getDefaultSchema();
38 // A simple way of getting the table object is just to pick the first one (the first sheet in the spreadsheet)
39 Table table
= schema
.getTables()[0];
40 Column orderColumn
= table
.getColumnByName("order");
41 Column countryColumn
= table
.getColumnByName("country");
42 Column townColumn
= table
.getColumnByName("town");
43 Column districtColumn
= table
.getColumnByName("district");
44 Column metroColumn
= table
.getColumnByName("metro");
45 Query q
= new Query().select(orderColumn
, countryColumn
, townColumn
, districtColumn
, metroColumn
).from(table
);
46 DataSet dataSet
= dataContext
.executeQuery(q
);
48 Map
<String
, Country
> countries
=new HashMap
<String
, Country
>();
49 Map
<String
, Town
> towns
=new HashMap
<String
, Town
>();
50 Map
<String
, District
> districts
=new HashMap
<String
, District
>();
51 Map
<String
, MetroStation
> metroStations
=new HashMap
<String
, MetroStation
>();
53 Country country
=new Country();;
55 while (dataSet
.next()) {
56 String sOrder
=((String
) dataSet
.getRow().getValue(orderColumn
)).trim();
57 String sCountry
=((String
) dataSet
.getRow().getValue(countryColumn
)).trim();
58 String sTown
=((String
) dataSet
.getRow().getValue(townColumn
)).trim();
59 String sDistrict
=((String
) dataSet
.getRow().getValue(districtColumn
)).trim();
60 String sMetro
=((String
) dataSet
.getRow().getValue(metroColumn
)).trim();
62 if (!sCountry
.isEmpty() && !sCountry
.equalsIgnoreCase((country
.getName())))
64 country
=new Country(sCountry
);
65 country
=allocate(getKey(sCountry
), country
, countries
);
71 if (!sTown
.isEmpty() && !sTown
.equalsIgnoreCase((town
.getName())))
73 town
=new Town(sTown
, country
);
74 town
=allocate(getKey(country
.getName(), sTown
), town
, towns
);
82 byte order
= Byte
.parseByte(sOrder
);
83 if (!sCountry
.isEmpty() && sTown
.isEmpty())
84 country
.setOrder(order
);
90 if (!sDistrict
.isEmpty())
91 allocate(getKey(country
.getName(), town
.getName(), sDistrict
), new District(sDistrict
, town
), districts
);
92 if (!sMetro
.isEmpty())
93 allocate(getKey(country
.getName(), town
.getName(), sMetro
), new MetroStation(sMetro
, town
), metroStations
);
99 private <T
extends Coord
> T
allocate(String key
, T entity
, Map
<String
, T
> coll
)
101 T res
= coll
.get(key
);
104 List
<T
> l
= coordsDao
.findByExample(entity
, true, "name", "order");
109 logger
.debug("new "+entity
.getClass().getSimpleName()+": "+entity
.getName());
110 coordsDao
.makePersistent(entity
);
118 private String
getKey(String
... keys
)
121 for (String key
: keys
) {
122 res
+='.'+key
.toLowerCase();