Skip to content
Andreas Rudolph edited this page Aug 16, 2021 · 3 revisions

How to use immobar.it format

Reading XML in immobar.it format

The class org.openestate.io.immobar_it.ImmobarItUtils provides a static createDocument() function to read XML data in immobar.it format from a java.io.File, java.io.InputStream, java.lang.String or org.w3c.dom.Document into a org.openestate.io.immobar_it.ImmobarItDocument.

import java.io.File;
import org.openestate.io.immobar_it.ImmobarItDocument;
import org.openestate.io.immobar_it.ImmobarItUtils;
import org.openestate.io.immobar_it.xml.CompanyType;
import org.openestate.io.immobar_it.xml.PropertyType;
import org.openestate.io.immobar_it.xml.Realestate;

public class ImmobarItReadingExample {
    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 ImmobarItDocument
        ImmobarItDocument doc = ImmobarItUtils.createDocument(new File(args[0]));

        // convert ImmobarItDocument into a Java object
        Realestate realestate = doc.toObject();

        // now we can access the XML content through ordinary Java objects
        for (CompanyType company : realestate.getCompany()) {

            // get german company name
            String companyNameDe = company.getCompanyNameDe();

            // get italian company name
            String companyNameIt = company.getCompanyNameIt();

            // print company information to console
            System.out.println("found company " +
                    "'" + companyNameDe + "' / '" + companyNameIt + "'");

            // process company properties
            for (PropertyType property : company.getProperty()) {
                // get object nr
                String objectNr = property.getId();

                // get german title
                String titleDe = property.getTitleDe();

                // get italian title
                String titleIt = property.getTitleIt();

                // print object information to console
                System.out.println("> found object " +
                        "'" + objectNr + "': '" + titleDe + "' / '" + titleIt + "'");
            }
        }
    }
}

See a full example at ImmobarItReadingExample.java.

Accessing XML data in immobar.it format

The class org.openestate.io.immobar_it.xml.Realestate is equivalent to a <realestate> root element in a immobar.it document. For example the following code creates a immobar.it document programmatically:

import com.thedeanda.lorem.Lorem;
import com.thedeanda.lorem.LoremIpsum;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Calendar;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.openestate.io.immobar_it.ImmobarItUtils;
import org.openestate.io.immobar_it.xml.CompanyType;
import org.openestate.io.immobar_it.xml.ObjectFactory;
import org.openestate.io.immobar_it.xml.PropertyType;
import org.openestate.io.immobar_it.xml.Realestate;
import org.openestate.io.immobar_it.xml.types.ImmotypeValue;
import org.openestate.io.immobar_it.xml.types.RevenuetypeValue;

public class ImmobarItWritingExample {
    private final static ObjectFactory FACTORY = ImmobarItUtils.getFactory();
    private final static Lorem RANDOMIZER = new LoremIpsum();

    public static void main(String[] args) throws Exception {
        // create a Realestate object with some example data
        // this object corresponds to the <realestate> element in XML
        Realestate realestate = FACTORY.createRealestate();

        // append some example companies to the transfer
        int companyCount = RandomUtils.nextInt(1, 3);
        for (int i = 0; i < companyCount; i++) {
            realestate.getCompany().add(createCompany());
        }

        // now make something useful with the object
    }

    /**
     * Create a {@link CompanyType} with some example data.
     *
     * @return created example object
     */
    private static CompanyType createCompany() {
        // create an example company
        CompanyType company = FACTORY.createCompanyType();

        company.setCompanyNameDe(RANDOMIZER.getName());
        company.setCompanyAddressDe(RANDOMIZER.getWords(1, 3));
        company.setCompanyLocationDe(RANDOMIZER.getCity());

        company.setCompanyNameIt(RANDOMIZER.getName());
        company.setCompanyAddressIt(RANDOMIZER.getWords(1, 3));
        company.setCompanyLocationIt(RANDOMIZER.getCity());

        company.setCompanyPostalcode(RANDOMIZER.getZipCode());
        company.setCompanyProvince(RANDOMIZER.getStateAbbr());
        company.setCompanyCountry("IT");

        company.setCompanyEmail(RANDOMIZER.getEmail());
        company.setCompanyPhone(RANDOMIZER.getPhone());
        company.setCompanyMobile(RANDOMIZER.getPhone());
        company.setCompanyFax(RANDOMIZER.getPhone());

        company.setCompanyISTAT(RandomStringUtils.randomNumeric(5));

        //noinspection CatchMayIgnoreException
        try {
            company.setCompanyWebsite(new URI("https://www.example.com"));
        } catch (URISyntaxException ex) {
        }

        // append some example properties to the company
        int propertyCount = RandomUtils.nextInt(3, 5);
        for (int i = 0; i < propertyCount; i++) {
            company.getProperty().add(createProperty());
        }

        return company;
    }

    /**
     * Create an {@link PropertyType} with some example data.
     *
     * @return created example object
     */
    private static PropertyType createProperty() {
        // create an example real estate
        PropertyType property = FACTORY.createPropertyType();

        property.setId(RandomStringUtils.randomAlphanumeric(5));
        property.setReferencenumber(RandomStringUtils.randomAlphanumeric(5));
        property.setISTATcode(RandomStringUtils.randomNumeric(5));

        property.setAddressDe(RANDOMIZER.getWords(1, 3));
        property.setLocationDe(RANDOMIZER.getCity());
        property.setDistrictDe(RANDOMIZER.getCity());

        property.setAddressIt(RANDOMIZER.getWords(1, 3));
        property.setLocationIt(RANDOMIZER.getCity());
        property.setDistrictIt(RANDOMIZER.getCity());

        property.setPostalcode(RandomStringUtils.randomNumeric(5));
        property.setProvince(RANDOMIZER.getStateAbbr());
        property.setCountry("IT");

        property.setLatitude(BigDecimal.valueOf(RandomUtils.nextDouble(0, 180) - 90));
        property.setLongitude(BigDecimal.valueOf(RandomUtils.nextDouble(0, 360) - 180));

        property.setTitleDe(RANDOMIZER.getWords(1, 3));
        property.setDescriptionDe(RANDOMIZER.getWords(10, 50));
        property.setHeatingDe(RANDOMIZER.getWords(1, 3));
        property.setKitchentypeDe(RANDOMIZER.getWords(1, 10));
        property.setParkinglottypeDe(RANDOMIZER.getWords(1, 10));

        property.setTitleIt(RANDOMIZER.getWords(1, 3));
        property.setDescriptionIt(RANDOMIZER.getWords(10, 50));
        property.setHeatingIt(RANDOMIZER.getWords(1, 3));
        property.setKitchentypeIt(RANDOMIZER.getWords(1, 10));
        property.setParkinglottypeIt(RANDOMIZER.getWords(1, 10));

        property.setCellar(RandomUtils.nextBoolean());
        property.setCommission(RandomUtils.nextBoolean());
        property.setContactpersonname(RANDOMIZER.getName());
        property.setConvention(RandomUtils.nextBoolean());
        property.setCountbathrooms(BigInteger.valueOf(RandomUtils.nextLong(0, 10)));
        property.setCountgarage(BigInteger.valueOf(RandomUtils.nextLong(0, 10)));
        property.setCountparkinglot(BigInteger.valueOf(RandomUtils.nextLong(0, 10)));
        property.setCountrooms(BigInteger.valueOf(RandomUtils.nextLong(0, 10)));
        property.setCountterrace(BigInteger.valueOf(RandomUtils.nextLong(0, 10)));
        property.setCreationdate(Calendar.getInstance());
        property.setDuplex(RandomUtils.nextBoolean());
        property.setElevator(RandomUtils.nextBoolean());
        property.setEnergyclass(randomValue(new String[]{"A", "B", "C", "D"}));
        property.setFloor(BigInteger.valueOf(RandomUtils.nextLong(0, 10)));
        property.setFloorisground(RandomUtils.nextBoolean());
        property.setFlooristop(RandomUtils.nextBoolean());
        property.setFurnished(RandomUtils.nextBoolean());
        property.setGarden(RandomUtils.nextBoolean());
        property.setImmotype(randomValue(ImmotypeValue.values()));
        property.setIpe(BigDecimal.valueOf(RandomUtils.nextDouble(0, 1000)));
        property.setLastmoddate(Calendar.getInstance());
        property.setPriceNet(BigDecimal.valueOf(RandomUtils.nextDouble(0, 10000)));
        property.setPriceOnRequest(RandomUtils.nextBoolean());
        property.setRevenuetype(randomValue(RevenuetypeValue.values()));
        property.setSqmCellar(BigDecimal.valueOf(RandomUtils.nextDouble(0, 100)));
        property.setSqmGarden(BigDecimal.valueOf(RandomUtils.nextDouble(0, 100)));
        property.setSqmGross(BigDecimal.valueOf(RandomUtils.nextDouble(0, 100)));
        property.setSqmNet(BigDecimal.valueOf(RandomUtils.nextDouble(0, 100)));
        property.setSqmSale(BigDecimal.valueOf(RandomUtils.nextDouble(0, 100)));
        property.setUsed(RandomUtils.nextBoolean());
        property.setUtilities(BigDecimal.valueOf(RandomUtils.nextDouble(0, 100)));

        int imageCount = RandomUtils.nextInt(1, 9);
        for (int i = 0; i < imageCount; i++) {
            try {
                property.getImage().add(
                        new URI("https://www.example.com/image-" + i + ".jpg"));
            } catch (Exception ex) {
            }
        }

        int planimetryCount = RandomUtils.nextInt(1, 3);
        for (int i = 0; i < planimetryCount; i++) {
            try {
                property.getPlanimetry().add(
                        new URI("https://www.example.com/plan-" + i + ".jpg"));
            } catch (Exception ex) {
            }
        }

        return property;
    }

    /**
     * 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 ImmobarItWritingExample.java.

Writing XML in immobar.it format

After a org.openestate.io.immobar_it.xml.Realestate object was created, it can be converted into a org.openestate.io.immobar_it.ImmobarItDocument with the static newDocument() function.

The class org.openestate.io.immobar_it.ImmobarItDocument provides a toXml() function, that finally writes the contents of the Realestate 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.immobar_it.ImmobarItDocument;
import org.openestate.io.immobar_it.xml.Realestate;

public class ImmobarItWritingExample {
    private final static boolean PRETTY_PRINT = true;

    /**
     * Convert a {@link Realestate} to XML and write it into a {@link File}.
     *
     * @param realestate 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(Realestate realestate, File file) throws Exception {
        ImmobarItDocument
                .newDocument(realestate)
                .toXml(file, PRETTY_PRINT);
    }

    /**
     * Convert a {@link Realestate} to XML and write it into an {@link OutputStream}.
     *
     * @param realestate 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(Realestate realestate, OutputStream output) throws Exception {
        ImmobarItDocument
                .newDocument(realestate)
                .toXml(output, PRETTY_PRINT);
    }

    /**
     * Convert a {@link Realestate} to XML and write it into a {@link Writer}.
     *
     * @param realestate 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(Realestate realestate, Writer output) throws Exception {
        ImmobarItDocument
                .newDocument(realestate)
                .toXml(output, PRETTY_PRINT);
    }

    /**
     * Convert a {@link Realestate} to XML and print the results to the console.
     *
     * @param realestate Java object representing the XML root element
     * @throws Exception if the document can't be written
     */
    private static void writeToConsole(Realestate realestate) throws Exception {
        System.out.println(
            ImmobarItDocument
                .newDocument(realestate)
                .toXmlString(PRETTY_PRINT)
        );
    }
}

See a full example at ImmobarItWritingExample.java.