-
Notifications
You must be signed in to change notification settings - Fork 16
/
ImmobiliareItWritingExample.java
382 lines (351 loc) · 15.9 KB
/
ImmobiliareItWritingExample.java
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
375
376
377
378
379
380
381
382
/*
* Copyright 2015-2021 OpenEstate.org.
*
* 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 org.openestate.io.examples;
import com.thedeanda.lorem.Lorem;
import com.thedeanda.lorem.LoremIpsum;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Calendar;
import java.util.Currency;
import org.apache.commons.io.output.NullOutputStream;
import org.apache.commons.io.output.NullWriter;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.openestate.io.immobiliare_it.ImmobiliareItDocument;
import org.openestate.io.immobiliare_it.ImmobiliareItUtils;
import org.openestate.io.immobiliare_it.xml.ClassType;
import org.openestate.io.immobiliare_it.xml.DescriptionType;
import org.openestate.io.immobiliare_it.xml.Document;
import org.openestate.io.immobiliare_it.xml.Extended;
import org.openestate.io.immobiliare_it.xml.Feed;
import org.openestate.io.immobiliare_it.xml.FurnitureType;
import org.openestate.io.immobiliare_it.xml.GardenType;
import org.openestate.io.immobiliare_it.xml.KitchenType;
import org.openestate.io.immobiliare_it.xml.NationCodeType;
import org.openestate.io.immobiliare_it.xml.ObjectFactory;
import org.openestate.io.immobiliare_it.xml.OperationType;
import org.openestate.io.immobiliare_it.xml.OwnershipType;
import org.openestate.io.immobiliare_it.xml.Picture;
import org.openestate.io.immobiliare_it.xml.Property;
import org.openestate.io.immobiliare_it.xml.RentalType;
import org.openestate.io.immobiliare_it.xml.StatusType;
import org.openestate.io.immobiliare_it.xml.TerrainType;
import org.openestate.io.immobiliare_it.xml.Transaction;
import org.openestate.io.immobiliare_it.xml.Video;
import org.openestate.io.immobiliare_it.xml.YesNoReadyType;
import org.openestate.io.immobiliare_it.xml.types.SizeUnitType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Example for writing XML files for <a href="https://www.immobiliare.it/">immobiliare.it</a>.
* <p>
* This example illustrates the programmatic creation of documents for
* <a href="https://www.immobiliare.it/">immobiliare.it</a> and how they are written into XML.
*
* @author Andreas Rudolph
* @since 1.0
*/
public class ImmobiliareItWritingExample {
@SuppressWarnings("unused")
private final static Logger LOGGER = LoggerFactory.getLogger(ImmobiliareItWritingExample.class);
private final static ObjectFactory FACTORY = ImmobiliareItUtils.getFactory();
private final static Lorem RANDOMIZER = new LoremIpsum();
private final static boolean PRETTY_PRINT = true;
/**
* Start the example application.
*
* @param args command line arguments
*/
@SuppressWarnings("Duplicates")
public static void main(String[] args) {
// create a Feed object with some example data
// this object corresponds to the <feed> root element in XML
Feed feed = FACTORY.createFeed();
// append some example objects to the Feed object
feed.setProperties(FACTORY.createProperties());
int propertyCount = RandomUtils.nextInt(3, 5);
for (int i = 0; i < propertyCount; i++) {
feed.getProperties().getProperty().add(createProperty());
}
// convert the Feed object into an XML document
ImmobiliareItDocument doc = null;
try {
doc = ImmobiliareItDocument.newDocument(feed);
} catch (Exception ex) {
LOGGER.error("Can't create XML document!");
LOGGER.error("> " + ex.getLocalizedMessage(), ex);
System.exit(1);
}
// write XML document into a java.io.File
try {
write(doc, File.createTempFile("output-", ".xml"));
} catch (IOException ex) {
LOGGER.error("Can't create temporary file!");
LOGGER.error("> " + ex.getLocalizedMessage(), ex);
System.exit(1);
}
// write XML document into a java.io.OutputStream
write(doc, NullOutputStream.NULL_OUTPUT_STREAM);
// write XML document into a java.io.Writer
write(doc, new NullWriter());
// write XML document into a string and send it to the console
writeToConsole(doc);
}
/**
* Create a {@link Property} with some example data.
*
* @return created example object
*/
private static Property createProperty() {
// create an example real estate for rent
Property obj = FACTORY.createProperty();
obj.setUniqueId(RandomStringUtils.randomAlphanumeric(5));
obj.setDateUpdated(Calendar.getInstance());
obj.setDateExpiration(Calendar.getInstance());
obj.setOperation(randomValue(OperationType.values()));
// add agent
obj.setAgent(FACTORY.createAgent());
obj.getAgent().setEmail(RANDOMIZER.getEmail());
obj.getAgent().setOfficeName(RANDOMIZER.getName());
// add a transaction
obj.setTransactions(FACTORY.createTransactions());
Transaction transaction = FACTORY.createTransaction();
transaction.setAuction(RandomUtils.nextBoolean());
transaction.setForRevenue(RandomUtils.nextBoolean());
transaction.setOwnership(randomValue(OwnershipType.values()));
transaction.setPrice(FACTORY.createPriceType());
transaction.getPrice().setCurrency(Currency.getInstance("EUR"));
transaction.getPrice().setReserved(RandomUtils.nextBoolean());
transaction.getPrice().setValue(BigInteger.valueOf(RandomUtils.nextLong(100, 10000)));
transaction.setType(RANDOMIZER.getWords(1, 3));
obj.getTransactions().getTransaction().add(transaction);
// add information about the building
obj.setBuilding(FACTORY.createBuilding());
obj.getBuilding().setClazz(randomValue(ClassType.values()));
obj.getBuilding().setStatus(randomValue(StatusType.values()));
// add some terrains to the building
obj.getBuilding().setTerrains(FACTORY.createTerrains());
int terrainCount = RandomUtils.nextInt(2, 5);
for (int i = 0; i < terrainCount; i++) {
obj.getBuilding().getTerrains().getTerrain().add(randomValue(TerrainType.values()));
}
// add some features
obj.setFeatures(FACTORY.createFeaturesProperty());
obj.getFeatures().setDescriptions(FACTORY.createDescriptions());
for (NationCodeType lang : NationCodeType.values()) {
obj.getFeatures().getDescriptions().getDescription().add(createDescription(lang));
}
obj.getFeatures().setSize(FACTORY.createSizeType());
obj.getFeatures().getSize().setUnit(randomValue(SizeUnitType.values()));
obj.getFeatures().getSize().setValue(BigInteger.valueOf(RandomUtils.nextLong(100, 1000)));
// add some extra features
obj.setExtraFeatures(FACTORY.createExtraFeatures());
obj.getExtraFeatures().setAdditionalCosts(FACTORY.createAdditionalCostsType());
obj.getExtraFeatures().getAdditionalCosts().setCurrency(Currency.getInstance("EUR"));
obj.getExtraFeatures().getAdditionalCosts().setValue(BigDecimal.valueOf(RandomUtils.nextDouble(50, 1000)));
obj.getExtraFeatures().setBalcony(RandomUtils.nextBoolean());
obj.getExtraFeatures().setBedrooms(BigInteger.valueOf(RandomUtils.nextLong(1, 5)));
obj.getExtraFeatures().setBuildYear(RandomUtils.nextInt(1990, 2020));
obj.getExtraFeatures().setElevator(RandomUtils.nextBoolean());
obj.getExtraFeatures().setFloorplannerUrl("https://floorplanner-url.it/" + RandomStringUtils.randomAlphanumeric(5));
obj.getExtraFeatures().setFreeConditions(RANDOMIZER.getWords(5, 10));
obj.getExtraFeatures().setFurniture(randomValue(FurnitureType.values()));
obj.getExtraFeatures().setGarden(randomValue(GardenType.values()));
obj.getExtraFeatures().setKitchen(randomValue(KitchenType.values()));
obj.getExtraFeatures().setNet(RandomUtils.nextBoolean());
obj.getExtraFeatures().setNumFloors(BigInteger.valueOf(RandomUtils.nextLong(1, 5)));
obj.getExtraFeatures().setOverheadCrane(randomValue(YesNoReadyType.values()));
obj.getExtraFeatures().setReception(RandomUtils.nextBoolean());
obj.getExtraFeatures().setRentContract(randomValue(RentalType.values()));
obj.getExtraFeatures().setSecurityAlarm(RandomUtils.nextBoolean());
obj.getExtraFeatures().setTerrace(RandomUtils.nextBoolean());
// add some extended attributes
obj.setExtended(FACTORY.createExtended());
obj.getExtended().setAmountBalcony(BigInteger.valueOf(RandomUtils.nextLong(1, 5)));
obj.getExtended().setAmountBedrooms(BigInteger.valueOf(RandomUtils.nextLong(1, 5)));
obj.getExtended().setAmountOtherRooms(BigInteger.valueOf(RandomUtils.nextLong(1, 5)));
obj.getExtended().setAmountParking(BigInteger.valueOf(RandomUtils.nextLong(1, 5)));
obj.getExtended().setAmountTerrace(BigInteger.valueOf(RandomUtils.nextLong(1, 5)));
obj.getExtended().setBasement(RandomUtils.nextBoolean());
obj.getExtended().setCloset(RandomUtils.nextBoolean());
obj.getExtended().setExternalFrames(randomValue(Extended.ExternalFrames.values()));
obj.getExtended().setFireplace(RandomUtils.nextBoolean());
obj.getExtended().setSwimmingPool(RandomUtils.nextBoolean());
obj.getExtended().setTv(randomValue(Extended.Tv.values()));
// add some pictures
obj.setPictures(FACTORY.createPictures());
int pictureCount = RandomUtils.nextInt(2, 5);
for (int i = 0; i < pictureCount; i++) {
obj.getPictures().getPicture().add(createPicture(i));
}
// add some blueprints
obj.setBlueprints(FACTORY.createBlueprints());
int blueprintCount = RandomUtils.nextInt(2, 5);
for (int i = 0; i < blueprintCount; i++) {
obj.getBlueprints().getBlueprint().add(createPicture(i));
}
// add some videos
obj.setVideos(FACTORY.createVideos());
int videoCount = RandomUtils.nextInt(2, 5);
for (int i = 0; i < videoCount; i++) {
obj.getVideos().getVideo().add(createVideo(i));
}
// add some documents
obj.setDocuments(FACTORY.createDocuments());
int documentCount = RandomUtils.nextInt(2, 5);
for (int i = 0; i < documentCount; i++) {
obj.getDocuments().getDocument().add(createDocument(i));
}
return obj;
}
/**
* Create a {@link DescriptionType} with some example data.
*
* @return created example object
*/
private static DescriptionType createDescription(NationCodeType lang) {
DescriptionType description = FACTORY.createDescriptionType();
description.setNative(RandomUtils.nextBoolean());
description.setLanguage(lang);
description.setContent(RANDOMIZER.getWords(10, 100));
description.setTitle(RANDOMIZER.getWords(2, 10));
return description;
}
/**
* Create a {@link Document} with some example data.
*
* @return created example object
*/
private static Document createDocument(int position) {
Document doc = FACTORY.createDocument();
doc.setPosition(BigInteger.valueOf(position));
doc.setUrl("https://www.example.com/document-" + position + ".pdf");
doc.setMimetype("application/pdf");
return doc;
}
/**
* Create a {@link Picture} with some example data.
*
* @return created example object
*/
private static Picture createPicture(int position) {
Picture pic = FACTORY.createPicture();
pic.setPosition(BigInteger.valueOf(position));
pic.setUrl("https://www.example.com/image-" + position + ".jpg");
return pic;
}
/**
* Create a {@link Video} with some example data.
*
* @return created example object
*/
private static Video createVideo(int position) {
Video video = FACTORY.createVideo();
video.setType(Video.Type.REMOTE);
video.setValue("https://www.example.com/video-" + position + ".mp4");
return video;
}
/**
* Get a random value from an array.
*
* @param values array containing values to select from
* @param <T> type of contained values
* @return randomly selected value
*/
private static <T> T randomValue(T[] values) {
return (values != null && values.length > 0) ?
values[RandomUtils.nextInt(0, values.length)] :
null;
}
/**
* Write an {@link ImmobiliareItDocument} into a {@link File}.
*
* @param doc the document to write
* @param file the file, where the document is written to
*/
@SuppressWarnings("Duplicates")
private static void write(ImmobiliareItDocument doc, File file) {
LOGGER.info("writing document with version " + doc.getDocumentVersion());
try {
doc.toXml(file, PRETTY_PRINT);
LOGGER.info("> written to: " + file.getAbsolutePath());
} catch (Exception ex) {
LOGGER.error("Can't write document into a file!");
LOGGER.error("> " + ex.getLocalizedMessage(), ex);
System.exit(1);
}
}
/**
* Write an {@link ImmobiliareItDocument} into an {@link OutputStream}.
*
* @param doc the document to write
* @param output the stream, where the document is written to
*/
@SuppressWarnings({"Duplicates", "SameParameterValue"})
private static void write(ImmobiliareItDocument doc, OutputStream output) {
LOGGER.info("writing document with version " + doc.getDocumentVersion());
try {
doc.toXml(output, PRETTY_PRINT);
LOGGER.info("> written to a java.io.OutputStream");
} catch (Exception ex) {
LOGGER.error("Can't write document into an OutputStream!");
LOGGER.error("> " + ex.getLocalizedMessage(), ex);
System.exit(1);
}
}
/**
* Write an {@link ImmobiliareItDocument} into a {@link Writer}.
*
* @param doc the document to write
* @param output the writer, where the document is written to
*/
@SuppressWarnings("Duplicates")
private static void write(ImmobiliareItDocument doc, Writer output) {
LOGGER.info("writing document with version " + doc.getDocumentVersion());
try {
doc.toXml(output, PRETTY_PRINT);
LOGGER.info("> written to a java.io.Writer");
} catch (Exception ex) {
LOGGER.error("Can't write document into a Writer!");
LOGGER.error("> " + ex.getLocalizedMessage(), ex);
System.exit(1);
}
}
/**
* Write an {@link ImmobiliareItDocument} into a {@link String} and print the
* results to the console.
*
* @param doc the document to write
*/
@SuppressWarnings("Duplicates")
private static void writeToConsole(ImmobiliareItDocument doc) {
LOGGER.info("writing document with version " + doc.getDocumentVersion());
try {
String xml = doc.toXmlString(PRETTY_PRINT);
LOGGER.info(StringUtils.repeat("-", 50)
+ System.lineSeparator() + xml);
} catch (Exception ex) {
LOGGER.error("Can't write document into a string!");
LOGGER.error("> " + ex.getLocalizedMessage(), ex);
System.exit(1);
}
}
}