QualityAdeUnmarshaller.java 14.6 KB
Newer Older
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
/*-
 * Copyright 2020 Hochschule für Technik Stuttgart
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.hft.stuttgart.quality.marshaller;

import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

import javax.xml.bind.JAXBElement;

import org.citygml4j.builder.jaxb.unmarshal.citygml.ade.ADEUnmarshallerHelper;
import org.citygml4j.model.citygml.ade.binding.ADEModelObject;
import org.citygml4j.model.citygml.ade.binding.ADEUnmarshaller;
import org.citygml4j.util.mapper.CheckedTypeMapper;
import org.citygml4j.xml.io.reader.MissingADESchemaException;

import de.hft.stuttgart.quality.model.AllPolygonsWrongOrientation;
import de.hft.stuttgart.quality.model.AttributeMissing;
import de.hft.stuttgart.quality.model.AttributeWrongValue;
import de.hft.stuttgart.quality.model.ConsecutivePointsSame;
import de.hft.stuttgart.quality.model.Edge;
import de.hft.stuttgart.quality.model.HoleOutside;
import de.hft.stuttgart.quality.model.InnerRingsNested;
import de.hft.stuttgart.quality.model.InteriorDisconnected;
import de.hft.stuttgart.quality.model.IntersectingRings;
import de.hft.stuttgart.quality.model.MultipleConnectedComponents;
import de.hft.stuttgart.quality.model.NonManifoldEdge;
import de.hft.stuttgart.quality.model.NonManifoldVertex;
import de.hft.stuttgart.quality.model.NonPlanarDistancePlane;
import de.hft.stuttgart.quality.model.NonPlanarNormalsDeviation;
import de.hft.stuttgart.quality.model.OrientationRingsSame;
import de.hft.stuttgart.quality.model.PolygonWrongOrientation;
import de.hft.stuttgart.quality.model.RingNotClosed;
import de.hft.stuttgart.quality.model.RingSelfIntersection;
import de.hft.stuttgart.quality.model.SolidNotClosed;
import de.hft.stuttgart.quality.model.SolidSelfIntersection;
import de.hft.stuttgart.quality.model.TooFewPoints;
import de.hft.stuttgart.quality.model.TooFewPolygons;
import de.hft.stuttgart.quality.model.Validation;
import de.hft.stuttgart.quality.model.ValidationError;
import de.hft.stuttgart.quality.model.ValidationResult;
import de.hft.stuttgart.quality.model.jaxb.AllPolygonsWrongOrientationType;
import de.hft.stuttgart.quality.model.jaxb.AttributeMissingType;
import de.hft.stuttgart.quality.model.jaxb.AttributeWrongValueType;
import de.hft.stuttgart.quality.model.jaxb.ConsecutivePointsSameType;
import de.hft.stuttgart.quality.model.jaxb.EdgeType;
import de.hft.stuttgart.quality.model.jaxb.HoleOutsideType;
import de.hft.stuttgart.quality.model.jaxb.InnerRingsNestedType;
import de.hft.stuttgart.quality.model.jaxb.InteriorDisconnectedType;
import de.hft.stuttgart.quality.model.jaxb.IntersectingRingsType;
import de.hft.stuttgart.quality.model.jaxb.MultipleConnectedComponentsType;
import de.hft.stuttgart.quality.model.jaxb.NonManifoldEdgeType;
import de.hft.stuttgart.quality.model.jaxb.NonManifoldVertexType;
import de.hft.stuttgart.quality.model.jaxb.NonPlanarDistancePlaneType;
import de.hft.stuttgart.quality.model.jaxb.NonPlanarNormalsDeviationType;
import de.hft.stuttgart.quality.model.jaxb.OrientationRingsSameType;
import de.hft.stuttgart.quality.model.jaxb.PolygonWrongOrientationType;
import de.hft.stuttgart.quality.model.jaxb.RingNotClosedType;
import de.hft.stuttgart.quality.model.jaxb.RingSelfIntersectionType;
import de.hft.stuttgart.quality.model.jaxb.SolidNotClosedType;
import de.hft.stuttgart.quality.model.jaxb.SolidSelfIntersectionType;
import de.hft.stuttgart.quality.model.jaxb.TooFewPointsType;
import de.hft.stuttgart.quality.model.jaxb.TooFewPolygonsType;
import de.hft.stuttgart.quality.model.jaxb.ValidationErrorType;
import de.hft.stuttgart.quality.model.jaxb.ValidationResultType;
import de.hft.stuttgart.quality.model.jaxb.ValidationType;
import de.hft.stuttgart.quality.util.UncheckedMissingADESchemaException;

public class QualityAdeUnmarshaller implements ADEUnmarshaller {

	private final ReentrantLock lock = new ReentrantLock();
	private CheckedTypeMapper<ADEModelObject> typeMapper;
	private ADEUnmarshallerHelper helper;

	private CheckedTypeMapper<ADEModelObject> getTypeMapper() {
		if (typeMapper == null) {
			lock.lock();
			try {
				if (typeMapper == null) {
					typeMapper = CheckedTypeMapper.<ADEModelObject>create()
							.with(ValidationType.class, this::unmarshalValidationType)
							.with(ValidationResultType.class, this::umarshalValidationResultType)
							.with(ConsecutivePointsSameType.class, this::unmarshalConsecutivePointsSameType)
							.with(TooFewPointsType.class, this::unmarshalTooFewPointsType)
							.with(RingSelfIntersectionType.class, this::unmarshalRingSelfIntersectionType)
							.with(RingNotClosedType.class, this::unmarshalRingNotClosedType)
							.with(InteriorDisconnectedType.class, this::unmarshalInteriorDisconnectedType)
							.with(IntersectingRingsType.class, this::unmarshalIntersectingRingsType)
							.with(NonPlanarDistancePlaneType.class, this::unmarshalNonPlanarDistancePlaneType)
							.with(InnerRingsNestedType.class, this::unmarshalInnerRingsType)
							.with(HoleOutsideType.class, this::unmarshalHoleOutsideType)
							.with(OrientationRingsSameType.class, this::unmarshalOrientationRingsSameType)
							.with(NonPlanarNormalsDeviationType.class, this::unmarshalNonPlanarNormalsDeviationType)
							.with(AllPolygonsWrongOrientationType.class, this::unmarshalAllPolygonsWrongOrientationType)
							.with(PolygonWrongOrientationType.class, this::unmarshalPolygonWrongOrientationType)
							.with(SolidSelfIntersectionType.class, this::unmarshalSolidSelfIntersectionType)
							.with(NonManifoldVertexType.class, this::unmarshalNonManifoldVertexType)
							.with(NonManifoldEdgeType.class, this::unmarshalNonManifoldEdgeType)
							.with(SolidNotClosedType.class, this::unmarshalSolidNotClosedType)
							.with(TooFewPolygonsType.class, this::unmarshalTooFewPolygonsType)
							.with(MultipleConnectedComponentsType.class, this::unmarshalMultipleConnectedComponentsType)
							.with(AttributeWrongValueType.class, this::unmarshalAttributeWrongValueType)
							.with(AttributeMissingType.class, this::unmarshalAttributeMissingType);

				}
			} finally {
				lock.unlock();
			}
		}

		return typeMapper;
	}

	@Override
	public void setADEUnmarshallerHelper(ADEUnmarshallerHelper helper) {
		this.helper = helper;
	}

	@Override
	public ADEModelObject unmarshal(JAXBElement<?> src) throws MissingADESchemaException {
		return unmarshal(src.getValue());
	}

	@Override
	public ADEModelObject unmarshal(Object src) throws MissingADESchemaException {
		return getTypeMapper().apply(src);
	}

	private Validation unmarshalValidationType(ValidationType src) {
		Validation dest = new Validation();
		dest.setValidationDate(src.getValidationDate());
		dest.setValidationSoftware(src.getValidationSoftware());
		dest.setValidationPlan(src.getValidationPlan());
		dest.setStatistics(src.getStatistics());
		return dest;
	}

	private ValidationResult umarshalValidationResultType(ValidationResultType src) throws MissingADESchemaException {
		ValidationResult dest = new ValidationResult();
		dest.setResult(src.getResult());
		if (src.isSetErrors()) {
			try {
				dest.getErrors()
						.addAll(src.getErrors().stream().map(this::unmarshalError).collect(Collectors.toList()));
			} catch (UncheckedMissingADESchemaException e) {
				throw e.getCause();
			}
		}
		return dest;
	}

	private ValidationError unmarshalError(ValidationErrorType err) {
		try {
			return (ValidationError) getTypeMapper().apply(err);
		} catch (MissingADESchemaException e) {
			throw new UncheckedMissingADESchemaException(e);
		}
	}

	private ConsecutivePointsSame unmarshalConsecutivePointsSameType(ConsecutivePointsSameType src) {
		ConsecutivePointsSame dest = new ConsecutivePointsSame();
		dest.setLinearRingId(src.getLinearRingId());
		dest.setVertex1(helper.getGMLUnmarshaller().unmarshalDirectPosition(src.getVertex1()));
		dest.setVertex2(helper.getGMLUnmarshaller().unmarshalDirectPosition(src.getVertex2()));
		return dest;
	}

	private TooFewPoints unmarshalTooFewPointsType(TooFewPointsType src) {
		TooFewPoints dest = new TooFewPoints();
		dest.setLinearRingId(src.getLinearRingId());
		return dest;
	}

	private RingSelfIntersection unmarshalRingSelfIntersectionType(RingSelfIntersectionType src) {
		RingSelfIntersection dest = new RingSelfIntersection();
		if (src.getEdge1() != null) {
			dest.setEdge1(unmarshalEdge(src.getEdge1()));
		}

		if (src.getEdge2() != null) {
			dest.setEdge2(unmarshalEdge(src.getEdge2()));
		}

		dest.setLinearRingId(src.getLinearRingId());
		dest.setType(src.getType());

		if (src.getVertex1() != null) {
			dest.setVertex1(helper.getGMLUnmarshaller().unmarshalDirectPosition(src.getVertex1()));
		}

		if (src.getVertex2() != null) {
			dest.setVertex2(helper.getGMLUnmarshaller().unmarshalDirectPosition(src.getVertex2()));
		}
		return dest;
	}

	private RingNotClosed unmarshalRingNotClosedType(RingNotClosedType src) {
		RingNotClosed dest = new RingNotClosed();
		dest.setLinearRingId(src.getLinearRingId());
		return dest;
	}

	private Edge unmarshalEdge(EdgeType src) {
		Edge dest = new Edge();
		if (src.getFrom() != null) {
			dest.setFrom(helper.getGMLUnmarshaller().unmarshalDirectPosition(src.getFrom()));
		}
		if (src.getTo() != null) {
			dest.setTo(helper.getGMLUnmarshaller().unmarshalDirectPosition(src.getTo()));
		}
		return dest;
	}

	private InteriorDisconnected unmarshalInteriorDisconnectedType(InteriorDisconnectedType src) {
		InteriorDisconnected dest = new InteriorDisconnected();
		dest.setPolygonId(src.getPolygonId());
		return dest;
	}

	private IntersectingRings unmarshalIntersectingRingsType(IntersectingRingsType src) {
		IntersectingRings dest = new IntersectingRings();
		dest.setPolygonId(src.getPolygonId());
		dest.setLinearRingId1(src.getLinearRingId1());
		dest.setLinearRingId2(src.getLinearRingId2());
		return dest;
	}

	private InnerRingsNested unmarshalInnerRingsType(InnerRingsNestedType src) {
		InnerRingsNested dest = new InnerRingsNested();
		dest.setPolygonId(src.getPolygonId());
		dest.setLinearRingId1(src.getLinearRingId1());
		dest.setLinearRingId2(src.getLinearRingId2());
		return dest;
	}

	private NonPlanarDistancePlane unmarshalNonPlanarDistancePlaneType(NonPlanarDistancePlaneType src) {
		NonPlanarDistancePlane dest = new NonPlanarDistancePlane();
		dest.setPolygonId(src.getPolygonId());
		if (src.getVertex() != null) {
			dest.setVertex(helper.getGMLUnmarshaller().unmarshalDirectPosition(src.getVertex()));
		}
		if (src.getDistance() != null) {
			dest.setDistance(helper.getGMLUnmarshaller().unmarshalLength(src.getDistance()));
		}
		return dest;
	}

	private HoleOutside unmarshalHoleOutsideType(HoleOutsideType src) {
		HoleOutside dest = new HoleOutside();
		dest.setPolygonId(src.getPolygonId());
		dest.setLinearRingId(src.getLinearRingId());
		return dest;
	}

	private NonPlanarNormalsDeviation unmarshalNonPlanarNormalsDeviationType(NonPlanarNormalsDeviationType src) {
		NonPlanarNormalsDeviation dest = new NonPlanarNormalsDeviation();
		dest.setPolygonId(src.getPolygonId());
		if (src.getDeviation() != null) {
			dest.setDeviation(helper.getGMLUnmarshaller().unmarshalAngle(src.getDeviation()));
		}
		return dest;
	}

	private OrientationRingsSame unmarshalOrientationRingsSameType(OrientationRingsSameType src) {
		OrientationRingsSame dest = new OrientationRingsSame();
		dest.setPolygonId(src.getPolygonId());
		dest.setLinearRingId(src.getLinearRingId());
		return dest;
	}

	private AllPolygonsWrongOrientation unmarshalAllPolygonsWrongOrientationType(AllPolygonsWrongOrientationType src) {
		AllPolygonsWrongOrientation dest = new AllPolygonsWrongOrientation();
		dest.setGeometryId(src.getGeometryId());
		return dest;
	}

	private PolygonWrongOrientation unmarshalPolygonWrongOrientationType(PolygonWrongOrientationType src) {
		PolygonWrongOrientation dest = new PolygonWrongOrientation();
		dest.setGeometryId(src.getGeometryId());
		if (src.isSetEdges()) {
			List<Edge> edges = dest.getEdges();
			for (EdgeType e : src.getEdges()) {
				edges.add(unmarshalEdge(e));
			}
		}
		return dest;
	}

	private SolidSelfIntersection unmarshalSolidSelfIntersectionType(SolidSelfIntersectionType src) {
		SolidSelfIntersection dest = new SolidSelfIntersection();
		dest.setGeometryId(src.getGeometryId());
		dest.setPolygonId1(src.getPolygonId1());
		dest.setPolygonId2(src.getPolygonId2());
		return dest;
	}

	private NonManifoldVertex unmarshalNonManifoldVertexType(NonManifoldVertexType src) {
		NonManifoldVertex dest = new NonManifoldVertex();
		dest.setGeometryId(src.getGeometryId());
		if (src.getVertex() != null) {
			dest.setVertex(helper.getGMLUnmarshaller().unmarshalDirectPosition(src.getVertex()));
		}
		return dest;
	}

	private NonManifoldEdge unmarshalNonManifoldEdgeType(NonManifoldEdgeType src) {
		NonManifoldEdge dest = new NonManifoldEdge();
		dest.setGeometryId(src.getGeometryId());
		if (src.isSetEdges()) {
			List<Edge> edges = dest.getEdges();
			for (EdgeType e : src.getEdges()) {
				edges.add(unmarshalEdge(e));
			}
		}
		return dest;
	}

	private SolidNotClosed unmarshalSolidNotClosedType(SolidNotClosedType src) {
		SolidNotClosed dest = new SolidNotClosed();
		dest.setGeometryId(src.getGeometryId());
		if (src.isSetEdges()) {
			List<Edge> edges = dest.getEdges();
			for (EdgeType e : src.getEdges()) {
				edges.add(unmarshalEdge(e));
			}
		}
		return dest;
	}

	private TooFewPolygons unmarshalTooFewPolygonsType(TooFewPolygonsType src) {
		TooFewPolygons dest = new TooFewPolygons();
		dest.setGeometryId(src.getGeometryId());
		return dest;
	}

	private MultipleConnectedComponents unmarshalMultipleConnectedComponentsType(MultipleConnectedComponentsType src) {
		MultipleConnectedComponents dest = new MultipleConnectedComponents();
		dest.setGeometryId(src.getGeometryId());
		if (src.isSetComponents()) {
			dest.getComponents().addAll(src.getComponents());
		}
		return dest;
	}

	private AttributeWrongValue unmarshalAttributeWrongValueType(AttributeWrongValueType src) {
		AttributeWrongValue dest = new AttributeWrongValue();
		dest.setAttributeName(src.getAttributeName());
		dest.setChildId(src.getChildId());
		dest.setGeneric(src.isGeneric());
		return dest;
	}

	private AttributeMissing unmarshalAttributeMissingType(AttributeMissingType src) {
		AttributeMissing dest = new AttributeMissing();
		dest.setAttributeName(src.getAttributeName());
		dest.setChildId(src.getChildId());
		dest.setGeneric(src.isGeneric());
		return dest;
	}

}