Mercurial > lada > lada-server
comparison src/main/java/de/intevation/lada/factory/OrtFactory.java @ 1261:dc7dde59bb54
Moved rules to create an ort object to ort factory.
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Fri, 27 Jan 2017 15:00:31 +0100 |
parents | a81de70d321d |
children | a851fc69130c |
comparison
equal
deleted
inserted
replaced
1260:201cc62a1c57 | 1261:dc7dde59bb54 |
---|---|
25 import com.vividsolutions.jts.geom.GeometryFactory; | 25 import com.vividsolutions.jts.geom.GeometryFactory; |
26 import com.vividsolutions.jts.geom.Point; | 26 import com.vividsolutions.jts.geom.Point; |
27 | 27 |
28 import de.intevation.lada.importer.ReportItem; | 28 import de.intevation.lada.importer.ReportItem; |
29 import de.intevation.lada.model.stammdaten.Ort; | 29 import de.intevation.lada.model.stammdaten.Ort; |
30 import de.intevation.lada.model.stammdaten.Staat; | |
31 import de.intevation.lada.model.stammdaten.Verwaltungseinheit; | |
30 import de.intevation.lada.util.annotation.RepositoryConfig; | 32 import de.intevation.lada.util.annotation.RepositoryConfig; |
33 import de.intevation.lada.util.data.QueryBuilder; | |
31 import de.intevation.lada.util.data.Repository; | 34 import de.intevation.lada.util.data.Repository; |
32 import de.intevation.lada.util.data.RepositoryType; | 35 import de.intevation.lada.util.data.RepositoryType; |
33 | 36 |
34 public class OrtFactory { | 37 public class OrtFactory { |
35 | 38 |
42 | 45 |
43 private List<ReportItem> errors; | 46 private List<ReportItem> errors; |
44 | 47 |
45 public void transformCoordinates(Ort ort) { | 48 public void transformCoordinates(Ort ort) { |
46 errors = new ArrayList<ReportItem>(); | 49 errors = new ArrayList<ReportItem>(); |
47 int kda = ort.getKdaId(); | 50 Integer kda = ort.getKdaId(); |
48 String epsg = null; | 51 String epsg = null; |
49 String xCoord = null; | 52 String xCoord = null; |
50 String yCoord = null; | 53 String yCoord = null; |
51 switch(kda) { | 54 switch(kda) { |
52 case 4: epsg = "EPSG:4326"; | 55 case 4: epsg = "EPSG:4326"; |
92 errors.add(err); | 95 errors.add(err); |
93 return; | 96 return; |
94 } | 97 } |
95 } | 98 } |
96 | 99 |
100 /** | |
101 * Use given attribute to try to add other attributes. | |
102 * To set futher attributes at least one of the following attribute set | |
103 * need to be present: | |
104 * - kda, x, y | |
105 * - gemId | |
106 * - staat | |
107 * | |
108 * @param kda The koordinatenart | |
109 * @param x The x coordinate | |
110 * @param y The y coordinate | |
111 * @param gemId The gemeinde id | |
112 * @param staat The staat id | |
113 */ | |
114 public Ort completeOrt(Ort ort) { | |
115 QueryBuilder<Ort> builder = | |
116 new QueryBuilder<Ort>( | |
117 repository.entityManager("stamm"), | |
118 Ort.class); | |
119 logger.debug("try to make a complete ort"); | |
120 if (ort.getKdaId() != null && | |
121 ort.getKoordXExtern() != null && | |
122 ort.getKoordYExtern() != null | |
123 ) { | |
124 logger.debug("has koordinates"); | |
125 builder.and("kdaId", ort.getKdaId()); | |
126 builder.and("koordXExtern", ort.getKoordXExtern()); | |
127 builder.and("koordYExtern", ort.getKoordYExtern()); | |
128 builder.and("ozId", ort.getOzId()); | |
129 builder.and("netzbetreiberId", ort.getNetzbetreiberId()); | |
130 List<Ort> orte = repository.filterPlain(builder.getQuery(), "stamm"); | |
131 if (orte != null && orte.size() > 0) { | |
132 return orte.get(0); | |
133 } | |
134 } | |
135 else if (ort.getGemId() != null) { | |
136 logger.debug("has gemid"); | |
137 builder.and("gemId", ort.getGemId()); | |
138 builder.and("ozId", ort.getOzId()); | |
139 builder.and("netzbetreiberId", ort.getNetzbetreiberId()); | |
140 List<Ort> orte = repository.filterPlain(builder.getQuery(), "stamm"); | |
141 if (orte != null && orte.size() > 0) { | |
142 logger.debug("found ort: " + orte.get(0).getId()); | |
143 return orte.get(0); | |
144 } | |
145 } | |
146 else if (ort.getStaatId() != null && | |
147 ort.getStaatId() != 0 | |
148 ) { | |
149 logger.debug("has staat"); | |
150 builder.and("staatId", ort.getGemId()); | |
151 builder.and("ozId", ort.getOzId()); | |
152 builder.and("netzbetreiberId", ort.getNetzbetreiberId()); | |
153 List<Ort> orte = repository.filterPlain(builder.getQuery(), "stamm"); | |
154 if (orte != null && orte.size() > 0) { | |
155 return orte.get(0); | |
156 } | |
157 } | |
158 | |
159 logger.debug("no ort found"); | |
160 return createOrt(ort); | |
161 } | |
162 | |
163 private Ort createOrt(Ort ort) { | |
164 boolean hasKoord = false; | |
165 boolean hasGem = false; | |
166 boolean hasStaat = false; | |
167 if (ort.getKdaId() != null && | |
168 ort.getKoordXExtern() != null && | |
169 ort.getKoordYExtern() != null | |
170 ) { | |
171 logger.debug("transformCoordinates"); | |
172 transformCoordinates(ort); | |
173 hasKoord = true; | |
174 } | |
175 if (ort.getGemId() == null && hasKoord) { | |
176 logger.debug("findVerwaltungseinheit"); | |
177 findVerwaltungseinheit(ort); | |
178 } | |
179 if (ort.getGemId() != null){ | |
180 if (ort.getStaatId() == null) { | |
181 ort.setStaatId(0); | |
182 } | |
183 Verwaltungseinheit v = repository.getByIdPlain( | |
184 Verwaltungseinheit.class, | |
185 ort.getGemId(), | |
186 "stamm"); | |
187 if (!hasKoord) { | |
188 logger.debug("add coordinates"); | |
189 ort.setMpArt("V"); | |
190 ort.setKdaId(4); | |
191 ort.setKoordYExtern(String.valueOf(v.getMittelpunkt().getY())); | |
192 ort.setKoordXExtern(String.valueOf(v.getMittelpunkt().getX())); | |
193 } | |
194 if (ort.getKurztext() == null) { | |
195 ort.setKurztext(v.getBezeichnung()); | |
196 } | |
197 if (ort.getLangtext() == null) { | |
198 ort.setLangtext(v.getBezeichnung()); | |
199 } | |
200 if (ort.getBerichtstext() == null) { | |
201 ort.setBerichtstext(v.getBezeichnung()); | |
202 } | |
203 transformCoordinates(ort); | |
204 hasGem = true; | |
205 } | |
206 if (ort.getStaatId() != null && | |
207 ort.getStaatId() != 0 && | |
208 !hasKoord && | |
209 !hasGem | |
210 ) { | |
211 Staat staat = | |
212 repository.getByIdPlain(Staat.class, ort.getStaatId(), "stamm"); | |
213 ort.setKdaId(staat.getKdaId()); | |
214 ort.setKoordXExtern(staat.getKoordXExtern()); | |
215 ort.setKoordYExtern(staat.getKoordYExtern()); | |
216 ort.setKurztext(staat.getStaat()); | |
217 ort.setLangtext(staat.getStaat()); | |
218 if (staat.getStaatIso() != null) { | |
219 ort.setOrtId("Staat_" + staat.getStaatIso()); | |
220 } | |
221 ort.setBerichtstext(staat.getStaat()); | |
222 transformCoordinates(ort); | |
223 hasStaat = true; | |
224 } | |
225 return ort; | |
226 } | |
227 | |
228 /** | |
229 * Use the geom of an ort object to determine the verwaltungseinheit. | |
230 * If verwaltungseinheit was found the gemId is used as reference in the ort | |
231 * object. | |
232 * | |
233 * @param ort The ort object | |
234 */ | |
97 public void findVerwaltungseinheit(Ort ort) { | 235 public void findVerwaltungseinheit(Ort ort) { |
98 if (ort.getGeom() == null) { | 236 if (ort.getGeom() == null) { |
99 return; | 237 return; |
100 } | 238 } |
101 Query q = repository.entityManager("stamm") | 239 Query q = repository.entityManager("stamm") |
130 public List<ReportItem> getErrors() { | 268 public List<ReportItem> getErrors() { |
131 return errors; | 269 return errors; |
132 } | 270 } |
133 | 271 |
134 public boolean hasErrors() { | 272 public boolean hasErrors() { |
135 return !errors.isEmpty(); | 273 return !(errors == null) && !errors.isEmpty(); |
136 } | 274 } |
137 } | 275 } |