1 package nl.b3p.brmo.loader.entity;
2
3 import java.io.IOException;
4 import java.io.StringReader;
5 import java.text.ParseException;
6 import java.text.SimpleDateFormat;
7 import java.util.Date;
8 import javax.xml.parsers.DocumentBuilder;
9 import javax.xml.parsers.DocumentBuilderFactory;
10 import javax.xml.parsers.ParserConfigurationException;
11 import javax.xml.xpath.XPath;
12 import javax.xml.xpath.XPathExpression;
13 import javax.xml.xpath.XPathExpressionException;
14 import javax.xml.xpath.XPathFactory;
15 import nl.b3p.brmo.loader.BrmoFramework;
16 import org.apache.commons.lang3.StringUtils;
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.w3c.dom.Document;
20 import org.xml.sax.InputSource;
21 import org.xml.sax.SAXException;
22
23
24
25
26 public class BrkBericht extends Bericht {
27
28 private final String soort = BrmoFramework.BR_BRK;
29 private boolean xpathEvaluated = false;
30
31 private static final Log log = LogFactory.getLog(BrkBericht.class);
32
33 public BrkBericht(String brXml) {
34 super(brXml);
35 }
36
37 public void setVervallenInfo(String objectRef, Date datum) {
38 this.objectRef = objectRef;
39 this.datum = datum;
40
41 xpathEvaluated = true;
42 }
43
44 private void evaluateXPath() {
45 if (xpathEvaluated) {
46 return;
47 }
48
49 xpathEvaluated = true;
50
51 if (objectRef != null && datum != null) {
52
53 return;
54 }
55
56
57 try {
58
59 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
60 DocumentBuilder builder = factory.newDocumentBuilder();
61 Document doc = builder.parse(new InputSource(new StringReader(getBrXml())));
62
63 XPathFactory xPathfactory = XPathFactory.newInstance();
64 XPath xpath = xPathfactory.newXPath();
65
66 XPathExpression expr =
67 xpath.compile(
68 "/KadastraalObjectSnapshot/*[local-name()= 'Perceel' or local-name()='Appartementsrecht']/identificatie/namespace/text()");
69 objectRef = expr.evaluate(doc);
70
71 expr =
72 xpath.compile(
73 "/KadastraalObjectSnapshot/*[local-name()= 'Perceel' or local-name()='Appartementsrecht']/identificatie/lokaalId/text()");
74 objectRef += ":" + expr.evaluate(doc);
75
76 expr =
77 xpath.compile(
78 "/KadastraalObjectSnapshot/*[local-name()= 'toestandsdatum' or local-name()='toestandsdatum']/text()");
79 setDatumAsString(expr.evaluate(doc));
80 } catch (Exception e) {
81 log.error("Error while getting brk referentie", e);
82 }
83 }
84
85 public void setDatumAsString(String d) {
86 if (d == null || d.isEmpty()) {
87 return;
88 }
89
90 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
91 try {
92 datum = sdf.parse(d);
93 } catch (ParseException pe) {
94 log.error("Error while parsing date: " + datum, pe);
95 }
96 }
97
98 @Override
99 public String getObjectRef() {
100 evaluateXPath();
101
102 return objectRef;
103 }
104
105 @Override
106 public Date getDatum() {
107 evaluateXPath();
108
109 return datum;
110 }
111
112 public String getRestoredFileName(Date bestanddatum, Integer volgordenummer) {
113 try {
114 final SimpleDateFormat output = new SimpleDateFormat("yyyyMMdd");
115
116 String prefix = "BKE-MUTBX01";
117 String kadGemCode;
118 String perceelnummer;
119 String brkdatum = output.format(bestanddatum);
120 String sectie;
121 String appartementsrechtVolgnummer;
122
123 String basePath =
124 "/KadastraalObjectSnapshot/*[local-name()= 'Perceel' or local-name()='Appartementsrecht']/kadastraleAanduiding/";
125 XPathFactory xPathfactory = XPathFactory.newInstance();
126 XPath xpath = xPathfactory.newXPath();
127 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
128 DocumentBuilder builder = factory.newDocumentBuilder();
129 Document doc = builder.parse(new InputSource(new StringReader(this.getBrXml())));
130
131
132 if (this.getBrXml().contains("<empty/>") && StringUtils.isNotBlank(this.getBrOrgineelXml())) {
133
134 basePath = "/Mutatie/kadastraalObject/AanduidingKadastraalObject/kadastraleAanduiding/";
135 doc = builder.parse(new InputSource(new StringReader(this.getBrOrgineelXml())));
136 }
137
138 XPathExpression expr = xpath.compile(basePath + "AKRKadastraleGemeenteCode/waarde/text()");
139 kadGemCode = expr.evaluate(doc);
140
141 expr = xpath.compile(basePath + "sectie/text()");
142 sectie = expr.evaluate(doc);
143
144 expr = xpath.compile(basePath + "perceelnummer/text()");
145 perceelnummer = expr.evaluate(doc);
146
147 expr = xpath.compile(basePath + "appartementsrechtVolgnummer/text()");
148 appartementsrechtVolgnummer = expr.evaluate(doc);
149 if (StringUtils.isNotBlank(appartementsrechtVolgnummer)) {
150 appartementsrechtVolgnummer = "A" + appartementsrechtVolgnummer;
151 }
152
153 String aanduiding = kadGemCode + sectie + perceelnummer + appartementsrechtVolgnummer;
154 log.debug("gevonden aanduiding voor herstelde bestandsnaam: " + aanduiding);
155 String filename = "bestandsnaam kon niet worden hersteld";
156 if (StringUtils.isNotBlank(aanduiding)) {
157 filename =
158 prefix + "-" + aanduiding + "-" + brkdatum + "-" + volgordenummer.toString() + ".zip";
159 }
160 return filename;
161 } catch (ParserConfigurationException
162 | XPathExpressionException
163 | SAXException
164 | IOException ex) {
165 log.error("Cannot create filename from xml: ", ex);
166 return "";
167 }
168 }
169 }