* removed streets from dictionary
[aramzamzam-ru.rentdom.git] / rentdom-web / src / main / java / ru / rentdom / services / coords / CoordsImport.java
blob4ce31cc24edeb2ff40f04590fff40566179a29ca
1 package ru.rentdom.services.coords;
3 import java.io.File;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
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;
31 this.logger = logger;
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();;
54 Town town=new Town();
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);
68 if (country==null)
69 continue;
71 if (!sTown.isEmpty() && !sTown.equalsIgnoreCase((town.getName())))
73 town=new Town(sTown, country);
74 town=allocate(getKey(country.getName(), sTown), town, towns);
77 if (town==null)
78 continue;
80 if (sOrder!="")
82 byte order = Byte.parseByte(sOrder);
83 if (!sCountry.isEmpty() && sTown.isEmpty())
84 country.setOrder(order);
85 else
86 town.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);
96 dataSet.close();
99 private <T extends Coord> T allocate(String key, T entity, Map<String, T> coll)
101 T res = coll.get(key);
102 if (res==null)
104 List<T> l = coordsDao.findByExample(entity, true, "name", "order");
105 if (l.size()>0)
106 res=l.get(0);
107 else
109 logger.debug("new "+entity.getClass().getSimpleName()+": "+entity.getName());
110 coordsDao.makePersistent(entity);
111 res=entity;
113 coll.put(key, res);
115 return res;
118 private String getKey(String... keys)
120 String res="";
121 for (String key : keys) {
122 res+='.'+key.toLowerCase();
124 return res;