-
Notifications
You must be signed in to change notification settings - Fork 16
Usage OpenImmo
OpenImmo specifies two types of XML documents:
-
A document with a
<openimmo>
root element contains real estate data, that is for example transferred between an agency and a real estate portal. This type of document is represented by theorg.openestate.io.openimmo.OpenImmoTransferDocument
class. -
A document with a
<openimmo_feedback>
root element contains data of a contact request by a customer. Those files are sent from a real estate portal to the agency. This type of document is represented by theorg.openestate.io.openimmo.OpenImmoFeedbackDocument
class.
Both document classes are extending the general / abstract org.openestate.io.openimmo.OpenImmoDocument
class.
The class org.openestate.io.openimmo.OpenImmoUtils
provides a static createDocument()
function to read XML data in OpenImmo format from a java.io.File
, java.io.InputStream
, java.lang.String
or org.w3c.dom.Document
into a org.openestate.io.openimmo.OpenImmoDocument
.
The createDocument()
function returns either a org.openestate.io.openimmo.OpenImmoTransferDocument
or a org.openestate.io.openimmo.OpenImmoFeedbackDocument
- depending on the type of OpenImmo document.
import java.io.File;
import org.openestate.io.openimmo.OpenImmoTransferDocument;
import org.openestate.io.openimmo.OpenImmoUtils;
import org.openestate.io.openimmo.xml.Anbieter;
import org.openestate.io.openimmo.xml.Immobilie;
import org.openestate.io.openimmo.xml.Openimmo;
public class OpenImmoReadingExample {
public static void main(String[] args) throws Exception {
if (args.length < 1) {
System.err.println("No file was specified!");
System.exit(1);
}
// read file into a OpenImmoTransferDocument
OpenImmoTransferDocument doc = (OpenImmoTransferDocument)
OpenImmoUtils.createDocument(new File(args[0]));
// convert OpenImmoTransferDocument into a Java object
Openimmo openimmo = doc.toObject();
// now we can access the XML content through ordinary Java objects
for (Anbieter anbieter : openimmo.getAnbieter()) {
System.out.println("found agency '" + anbieter.getAnbieternr() + "'");
// process real estates of the agency
for (Immobilie immobilie : anbieter.getImmobilie()) {
// get object nr
String objectNr = (immobilie.getVerwaltungTechn() != null) ?
immobilie.getVerwaltungTechn().getObjektnrIntern() :
null;
// get object title
String objectTitle = (immobilie.getFreitexte() != null) ?
immobilie.getFreitexte().getObjekttitel() :
null;
// print object information to console
System.out.println("> found object " +
"'" + objectNr + "': " + objectTitle);
}
}
}
}
See a full example at OpenImmoReadingExample.java
.
The class org.openestate.io.openimmo.xml.Openimmo
is equivalent to a <openimmo>
root element in a OpenImmo document. For example the following code creates a OpenImmo document programmatically:
import com.thedeanda.lorem.Lorem;
import com.thedeanda.lorem.LoremIpsum;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Locale;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.openestate.io.openimmo.OpenImmoUtils;
import org.openestate.io.openimmo.xml.Aktion;
import org.openestate.io.openimmo.xml.Anbieter;
import org.openestate.io.openimmo.xml.Anhang;
import org.openestate.io.openimmo.xml.Haus;
import org.openestate.io.openimmo.xml.Immobilie;
import org.openestate.io.openimmo.xml.ObjectFactory;
import org.openestate.io.openimmo.xml.Openimmo;
import org.openestate.io.openimmo.xml.Uebertragung;
public class OpenImmoWritingExample {
private final static ObjectFactory FACTORY = OpenImmoUtils.getFactory();
private final static Lorem RANDOMIZER = new LoremIpsum();
public static void main(String[] args) {
// create an Openimmo object with some example data
// this object corresponds to the <openimmo> root element in XML
Openimmo openimmo = OpenImmoUtils.getFactory().createOpenimmo();
openimmo.setUebertragung(createUebertragung());
int anbieterCount = RandomUtils.nextInt(1, 5);
for (int i = 0; i < anbieterCount; i++) {
openimmo.getAnbieter().add(createAnbieter());
}
// now make something useful with the object
}
/**
* Create an {@link Anbieter} with some example data.
*
* @return created example object
*/
private static Anbieter createAnbieter() {
// create an example agency
Anbieter anbieter = FACTORY.createAnbieter();
anbieter.setAnbieternr(RandomStringUtils.randomAlphanumeric(5));
anbieter.setFirma(RANDOMIZER.getName());
anbieter.setOpenimmoAnid(RandomStringUtils.randomAlphanumeric(5));
// add some real estates to the agency
int immobilieCount = RandomUtils.nextInt(1, 6);
for (int i = 0; i < immobilieCount; i++) {
anbieter.getImmobilie().add(createImmobilie());
}
return anbieter;
}
/**
* Create an {@link Immobilie} with some example data.
*
* @return created example object
*/
private static Immobilie createImmobilie() {
// create an example real estate
Immobilie immobilie = FACTORY.createImmobilie();
// add some administrative information
immobilie.setVerwaltungTechn(FACTORY.createVerwaltungTechn());
immobilie.getVerwaltungTechn().setAktion(FACTORY.createAktion());
immobilie.getVerwaltungTechn().getAktion().setAktionart(randomValue(Aktion.AktionArt.values()));
immobilie.getVerwaltungTechn().setObjektnrIntern(RandomStringUtils.randomNumeric(10));
// set categorization
immobilie.setObjektkategorie(FACTORY.createObjektkategorie());
immobilie.getObjektkategorie().setNutzungsart(FACTORY.createNutzungsart());
immobilie.getObjektkategorie().getNutzungsart().setANLAGE(RandomUtils.nextBoolean());
immobilie.getObjektkategorie().getNutzungsart().setGEWERBE(RandomUtils.nextBoolean());
immobilie.getObjektkategorie().getNutzungsart().setWAZ(RandomUtils.nextBoolean());
immobilie.getObjektkategorie().getNutzungsart().setWOHNEN(RandomUtils.nextBoolean());
immobilie.getObjektkategorie().setVermarktungsart(FACTORY.createVermarktungsart());
immobilie.getObjektkategorie().getVermarktungsart().setERBPACHT(RandomUtils.nextBoolean());
immobilie.getObjektkategorie().getVermarktungsart().setKAUF(RandomUtils.nextBoolean());
immobilie.getObjektkategorie().getVermarktungsart().setLEASING(RandomUtils.nextBoolean());
immobilie.getObjektkategorie().getVermarktungsart().setMIETEPACHT(RandomUtils.nextBoolean());
immobilie.getObjektkategorie().setObjektart(FACTORY.createObjektart());
Haus singleFamilyHouse = FACTORY.createHaus();
singleFamilyHouse.setHaustyp(randomValue(Haus.Haustyp.values()));
immobilie.getObjektkategorie().getObjektart().getHaus().add(singleFamilyHouse);
// add some information about the location
immobilie.setGeo(FACTORY.createGeo());
immobilie.getGeo().setPlz(RANDOMIZER.getZipCode());
immobilie.getGeo().setOrt(RANDOMIZER.getCity());
immobilie.getGeo().setLand(FACTORY.createLand());
immobilie.getGeo().getLand().setIsoLand(Locale.GERMANY.getISO3Country());
// add some information about prices
immobilie.setPreise(FACTORY.createPreise());
immobilie.getPreise().setHeizkosten(BigDecimal.valueOf(RandomUtils.nextDouble(100, 1000)));
immobilie.getPreise().setKaufpreis(FACTORY.createKaufpreis());
immobilie.getPreise().getKaufpreis().setAufAnfrage(RandomUtils.nextBoolean());
immobilie.getPreise().getKaufpreis().setValue(BigDecimal.valueOf(RandomUtils.nextDouble(10000, 999999)));
// add some information about features
immobilie.setAusstattung(FACTORY.createAusstattung());
immobilie.getAusstattung().setGaestewc(RandomUtils.nextBoolean());
immobilie.getAusstattung().setGartennutzung(RandomUtils.nextBoolean());
immobilie.getAusstattung().setHeizungsart(FACTORY.createHeizungsart());
immobilie.getAusstattung().getHeizungsart().setZENTRAL(RandomUtils.nextBoolean());
immobilie.getAusstattung().getHeizungsart().setFUSSBODEN(RandomUtils.nextBoolean());
// add some descriptions
immobilie.setFreitexte(FACTORY.createFreitexte());
immobilie.getFreitexte().setObjekttitel(RANDOMIZER.getWords(1, 10));
immobilie.getFreitexte().setObjektbeschreibung(RANDOMIZER.getWords(10, 50));
// set the contact person
immobilie.setKontaktperson(FACTORY.createKontaktperson());
immobilie.getKontaktperson().setName(RANDOMIZER.getName());
immobilie.getKontaktperson().setEmailDirekt(RANDOMIZER.getEmail());
immobilie.getKontaktperson().setTelDurchw(RANDOMIZER.getPhone());
immobilie.getKontaktperson().setPlz(RANDOMIZER.getZipCode());
immobilie.getKontaktperson().setOrt(RANDOMIZER.getCity());
immobilie.getKontaktperson().setLand(FACTORY.createLand());
immobilie.getKontaktperson().getLand().setIsoLand(Locale.GERMANY.getISO3Country());
// add some attachments
immobilie.setAnhaenge(FACTORY.createAnhaenge());
int attachmentCount = RandomUtils.nextInt(3, 10);
for (int i = 0; i < attachmentCount; i++) {
immobilie.getAnhaenge().getAnhang().add(createAnhang());
}
return immobilie;
}
/**
* Create an {@link Anhang} with some example data.
*
* @return created example object
*/
private static Anhang createAnhang() {
// create an example transfer
Anhang anhang = FACTORY.createAnhang();
anhang.setAnhangtitel(RANDOMIZER.getWords(2, 5));
anhang.setLocation(Anhang.Location.EXTERN);
anhang.setFormat("image/jpeg");
anhang.setDaten(FACTORY.createDaten());
anhang.getDaten().setPfad("image" + RandomStringUtils.randomAlphabetic(3) + ".jpg");
return anhang;
}
/**
* Create an {@link Uebertragung} with some example data.
*
* @return created example object
*/
private static Uebertragung createUebertragung() {
// create an example transfer
Uebertragung uebertragung = FACTORY.createUebertragung();
uebertragung.setArt(randomValue(Uebertragung.Art.values()));
uebertragung.setModus(randomValue(Uebertragung.Modus.values()));
uebertragung.setSendersoftware(RANDOMIZER.getName());
uebertragung.setSenderversion(RandomStringUtils.randomNumeric(1, 3));
uebertragung.setTechnEmail(RANDOMIZER.getEmail());
uebertragung.setTimestamp(Calendar.getInstance());
uebertragung.setUmfang(randomValue(Uebertragung.Umfang.values()));
return uebertragung;
}
/**
* 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;
}
}
See a full example at OpenImmoWritingExample.java
.
After a org.openestate.io.openimmo.xml.Openimmo
object was created, it can be converted into a org.openestate.io.openimmo.OpenImmoTransferDocument
with the static newDocument()
function.
The class org.openestate.io.openimmo.OpenImmoTransferDocument
provides a toXml()
function, that finally writes the contents of the Openimmo
object as XML into a java.io.File
, java.io.OutputStream
or java.io.Writer
.
import java.io.File;
import java.io.OutputStream;
import java.io.Writer;
import org.openestate.io.openimmo.OpenImmoTransferDocument;
import org.openestate.io.openimmo.xml.Openimmo;
public class OpenImmoWritingExample {
private final static boolean PRETTY_PRINT = true;
/**
* Convert a {@link Openimmo} to XML and write it into a {@link File}.
*
* @param openimmo Java object representing the XML root element
* @param file the file, where the document is written to
* @throws Exception if the document can't be written
*/
private static void write(Openimmo openimmo, File file) throws Exception {
OpenImmoTransferDocument
.newDocument(openimmo)
.toXml(file, PRETTY_PRINT);
}
/**
* Convert a {@link Openimmo} to XML and write it into an {@link OutputStream}.
*
* @param openimmo Java object representing the XML root element
* @param output the stream, where the document is written to
* @throws Exception if the document can't be written
*/
private static void write(Openimmo openimmo, OutputStream output) throws Exception {
OpenImmoTransferDocument
.newDocument(openimmo)
.toXml(output, PRETTY_PRINT);
}
/**
* Convert a {@link Openimmo} to XML and write it into a {@link Writer}.
*
* @param openimmo Java object representing the XML root element
* @param output the writer, where the document is written to
* @throws Exception if the document can't be written
*/
private static void write(Openimmo openimmo, Writer output) throws Exception {
OpenImmoTransferDocument
.newDocument(openimmo)
.toXml(output, PRETTY_PRINT);
}
/**
* Convert a {@link Openimmo} to XML and print the results to the console.
*
* @param openimmo Java object representing the XML root element
* @throws Exception if the document can't be written
*/
private static void writeToConsole(Openimmo openimmo) throws Exception {
System.out.println(
OpenImmoTransferDocument
.newDocument(openimmo)
.toXmlString(PRETTY_PRINT)
);
}
}
See a full example at OpenImmoWritingExample.java
.
OpenEstate-IO-OpenImmo supports all versions of the OpenImmo format, that were ever released to public. The Java classes in the org.openestate.io.openimmo.xml
package are bound to version 1.2.7b of the OpenImmo format.
If a document in OpenImmo format is imported in any desired version, a call to the toObject()
function will automatically update the document to the latest version 1.2.7b before the Java object is created.
In order to write a OpenImmo document with an earlier version then 1.2.7b, you can call the downgrade()
function on the document before calling the toXml()
function:
import java.io.File;
import org.openestate.io.openimmo.OpenImmoTransferDocument;
import org.openestate.io.openimmo.OpenImmoVersion;
import org.openestate.io.openimmo.xml.Openimmo;
public class OpenImmoWritingExample {
private final static boolean PRETTY_PRINT = true;
/**
* Downgrade an {@link Openimmo} element to an earlier version
* and write the resulting XML into a {@link File}.
*
* @param openimmo the document to write
* @param file the file, where the document is written to
* @param version the XML version to write
* @throws Exception if the document can't be written
*/
private static void write(Openimmo openimmo, File file, OpenImmoVersion version) throws Exception {
OpenImmoTransferDocument doc = OpenImmoTransferDocument.newDocument(openimmo);
doc.downgrade(version);
doc.toXml(file, PRETTY_PRINT);
}
}