Skip to content

Usage ImmobiliareIT

Andreas Rudolph edited this page Aug 16, 2021 · 19 revisions

How to use immobiliare.it format

Reading XML in immobiliare.it format

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

import java.io.File;
import org.openestate.io.immobiliare_it.ImmobiliareItDocument;
import org.openestate.io.immobiliare_it.ImmobiliareItUtils;
import org.openestate.io.immobiliare_it.xml.Descriptions;
import org.openestate.io.immobiliare_it.xml.Feed;
import org.openestate.io.immobiliare_it.xml.Property;

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

        // convert ImmobiliareItDocument into a Java object
        Feed feed = doc.toObject();

        // now we can access the XML content through ordinary Java objects
        if (feed.getProperties() != null) {
            for (Property object : feed.getProperties().getProperty()) {
                // get object nr
                String objectNr = object.getUniqueId();

                // get object description
                Descriptions descriptions = (object.getFeatures() != null) ?
                        object.getFeatures().getDescriptions() :
                        null;
                String objectInfo = (descriptions != null && !descriptions.getDescription().isEmpty()) ?
                        descriptions.getDescription().get(0).getContent() : null;

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

See a full example at ImmobiliareItReadingExample.java.

Accessing XML data in immobiliare.it format

The class org.openestate.io.immobiliare_it.xml.Feed is equivalent to a <feed> root element in a immobiliare.it document. For example the following code creates a immobiliare.it document programmatically:

import com.thedeanda.lorem.Lorem;
import com.thedeanda.lorem.LoremIpsum;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Calendar;
import java.util.Currency;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
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;

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

    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());
        }

        // now make something useful with the object
    }

    /**
     * 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;
    }
}

See a full example at ImmobiliareItWritingExample.java.

Writing XML in immobiliare.it format

After a org.openestate.io.immobiliare_it.xml.Feed object was created, it can be converted into a org.openestate.io.immobiliare_it.ImmobiliareItDocument with the static newDocument() function.

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

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

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

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

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

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

See a full example at ImmobiliareItWritingExample.java.