Skip to content

Usage ImmobiliareIT

Andreas Rudolph edited this page Jul 6, 2019 · 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.Feed;
import org.openestate.io.immobiliare_it.xml.Feed.Properties.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
                String objectInfo = (object.getFeatures() != null && !object.getFeatures().getDescription().isEmpty()) ?
                        object.getFeatures().getDescription().get(0).getValue() : 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.ImmobiliareItUtils;
import org.openestate.io.immobiliare_it.xml.Box;
import org.openestate.io.immobiliare_it.xml.BusinessElement.BusinessElementCategory;
import org.openestate.io.immobiliare_it.xml.ClassEnergy;
import org.openestate.io.immobiliare_it.xml.Clazz;
import org.openestate.io.immobiliare_it.xml.Feed;
import org.openestate.io.immobiliare_it.xml.Feed.Properties.Property;
import org.openestate.io.immobiliare_it.xml.Floor;
import org.openestate.io.immobiliare_it.xml.Furniture;
import org.openestate.io.immobiliare_it.xml.Garden;
import org.openestate.io.immobiliare_it.xml.Heat;
import org.openestate.io.immobiliare_it.xml.Kitchen;
import org.openestate.io.immobiliare_it.xml.LocationStructure.Locality.Neighbourhood.LocationNeighbourhoodType;
import org.openestate.io.immobiliare_it.xml.ObjectFactory;
import org.openestate.io.immobiliare_it.xml.Operation;
import org.openestate.io.immobiliare_it.xml.OwnershipType;
import org.openestate.io.immobiliare_it.xml.PictureExtended;
import org.openestate.io.immobiliare_it.xml.PictureProject;
import org.openestate.io.immobiliare_it.xml.PropertyType;
import org.openestate.io.immobiliare_it.xml.PropertyTypeBusiness;
import org.openestate.io.immobiliare_it.xml.PropertyTypeSimple;
import org.openestate.io.immobiliare_it.xml.Rental;
import org.openestate.io.immobiliare_it.xml.Status;
import org.openestate.io.immobiliare_it.xml.TerrainType;
import org.openestate.io.immobiliare_it.xml.VideoProject;
import org.openestate.io.immobiliare_it.xml.VideoType;
import org.openestate.io.immobiliare_it.xml.YesNoReady;
import org.openestate.io.immobiliare_it.xml.types.Category;
import org.openestate.io.immobiliare_it.xml.types.EnergyUnit;
import org.openestate.io.immobiliare_it.xml.types.LandSizeUnit;
import org.openestate.io.immobiliare_it.xml.types.SizeUnit;

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.createFeedProperties());
        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.createFeedPropertiesProperty();
        obj.setBuildingStatus(randomValue(Status.values()));
        obj.setCategory(randomValue(Category.values()));
        obj.setDateExpiration(Calendar.getInstance());
        obj.setDateUpdated(Calendar.getInstance());
        obj.setOperation(randomValue(Operation.values()));
        obj.setUniqueId(RandomStringUtils.randomAlphanumeric(5));

        obj.setAgent(FACTORY.createFeedPropertiesPropertyAgent());
        obj.getAgent().setEmail(RANDOMIZER.getEmail());
        obj.getAgent().setOfficeName(RANDOMIZER.getName());

        obj.setBlueprints(FACTORY.createFeedPropertiesPropertyBlueprints());
        int blueprintCount = RandomUtils.nextInt(2, 5);
        for (int i = 0; i < blueprintCount; i++) {
            obj.getBlueprints().getBlueprint().add(createPictureExtended(i));
        }

        obj.setBuilding(FACTORY.createBuilding());
        obj.getBuilding().setCategory(randomValue(Category.values()));
        obj.getBuilding().setClazz(randomValue(Clazz.values()));
        obj.getBuilding().setDetail(randomValue(PropertyTypeBusiness.values()));
        obj.getBuilding().setStatus(randomValue(Status.values()));
        obj.getBuilding().setType(randomValue(PropertyType.values()));

        obj.setExtraFeatures(FACTORY.createFeedPropertiesPropertyExtraFeatures());
        obj.getExtraFeatures().setAirConditioning(RandomUtils.nextBoolean());
        obj.getExtraFeatures().setBalcony(RandomUtils.nextBoolean());
        obj.getExtraFeatures().setBathrooms(BigInteger.valueOf(RandomUtils.nextLong(1, 5)));
        obj.getExtraFeatures().setBeamHeight(BigInteger.valueOf(RandomUtils.nextLong(1, 10)));
        obj.getExtraFeatures().setBedrooms(BigInteger.valueOf(RandomUtils.nextLong(1, 5)));
        obj.getExtraFeatures().setBuildYear(RandomUtils.nextInt(1900, 2000));
        obj.getExtraFeatures().setDocDescription(RANDOMIZER.getWords(5, 10));
        obj.getExtraFeatures().setDocSpecification(RANDOMIZER.getWords(5, 10));
        obj.getExtraFeatures().setElevator(RandomUtils.nextBoolean());
        obj.getExtraFeatures().setFloorplannerUrl("http://floorplanner-url.it/" + RandomStringUtils.randomAlphanumeric(5));
        obj.getExtraFeatures().setFreeConditions(RANDOMIZER.getWords(5, 10));
        obj.getExtraFeatures().setFurniture(randomValue(Furniture.values()));
        obj.getExtraFeatures().setGarden(randomValue(Garden.values()));
        obj.getExtraFeatures().setHeating(randomValue(Heat.values()));
        obj.getExtraFeatures().setKitchen(randomValue(Kitchen.values()));
        obj.getExtraFeatures().setNet(RandomUtils.nextBoolean());
        obj.getExtraFeatures().setNumFloors(BigInteger.valueOf(RandomUtils.nextLong(1, 5)));
        obj.getExtraFeatures().setOverheadCrane(randomValue(YesNoReady.values()));
        obj.getExtraFeatures().setReception(RandomUtils.nextBoolean());
        obj.getExtraFeatures().setRentContract(randomValue(Rental.values()));
        obj.getExtraFeatures().setSecurityAlarm(RandomUtils.nextBoolean());
        obj.getExtraFeatures().setTerrace(RandomUtils.nextBoolean());
        obj.getExtraFeatures().setVirtualTour(RANDOMIZER.getWords(5, 10));

        obj.getExtraFeatures().setAdditionalCosts(FACTORY.createAdditionalCostsType());
        obj.getExtraFeatures().getAdditionalCosts().setCurrency(Currency.getInstance("EUR"));
        obj.getExtraFeatures().getAdditionalCosts().setValue(BigInteger.valueOf(RandomUtils.nextLong(0, 5000)));

        obj.getExtraFeatures().setExternalArea(FACTORY.createLandSizeType());
        obj.getExtraFeatures().getExternalArea().setUnit(randomValue(LandSizeUnit.values()));
        obj.getExtraFeatures().getExternalArea().setValue(BigInteger.valueOf(RandomUtils.nextLong(50, 5000)));

        obj.getExtraFeatures().setFloor(FACTORY.createFloor());
        obj.getExtraFeatures().getFloor().setType(randomValue(Floor.FloorType.values()));
        obj.getExtraFeatures().getFloor().setValue(BigInteger.valueOf(RandomUtils.nextLong(0, 10)));

        obj.getExtraFeatures().setGarage(FACTORY.createBox());
        obj.getExtraFeatures().getGarage().setType(randomValue(Box.BoxType.values()));
        obj.getExtraFeatures().getGarage().setValue(BigInteger.valueOf(RandomUtils.nextLong(0, 10)));

        obj.getExtraFeatures().setOfficeSize(FACTORY.createSizeType());
        obj.getExtraFeatures().getOfficeSize().setUnit(randomValue(SizeUnit.values()));
        obj.getExtraFeatures().getOfficeSize().setValue(BigInteger.valueOf(RandomUtils.nextLong(5, 50)));

        obj.setFeatures(FACTORY.createFeedPropertiesPropertyFeatures());
        obj.getFeatures().setEnergyClass(randomValue(ClassEnergy.values()));
        obj.getFeatures().setRooms(RandomUtils.nextInt(1, 5));

        obj.getFeatures().setEnergyPerformance(FACTORY.createClassEnergyPerformance());
        obj.getFeatures().getEnergyPerformance().setCertified(RandomUtils.nextBoolean());
        obj.getFeatures().getEnergyPerformance().setUnit(randomValue(EnergyUnit.values()));
        obj.getFeatures().getEnergyPerformance().setValue(RANDOMIZER.getWords(3, 10));

        obj.getFeatures().setPrice(FACTORY.createPriceType());
        obj.getFeatures().getPrice().setCurrency(Currency.getInstance("EUR"));
        obj.getFeatures().getPrice().setReserved(RandomUtils.nextBoolean());
        obj.getFeatures().getPrice().setValue(BigInteger.valueOf(RandomUtils.nextLong(500, 5000000)));

        obj.getFeatures().setSize(FACTORY.createSizeType());
        obj.getFeatures().getSize().setUnit(randomValue(SizeUnit.values()));
        obj.getFeatures().getSize().setValue(BigInteger.valueOf(RandomUtils.nextLong(50, 5000)));

        obj.setLocation(FACTORY.createLocationStructure());
        obj.getLocation().setAdministrativeArea(RANDOMIZER.getWords(3, 5));
        obj.getLocation().setCountryCode(randomValue(new String[]{"DE", "IT", "AT", "CH"}));

        obj.getLocation().setCity(FACTORY.createLocationStructureCity());
        obj.getLocation().getCity().setCode(BigInteger.valueOf(RandomUtils.nextLong(1, 1000)));
        obj.getLocation().getCity().setValue(RANDOMIZER.getCity());

        obj.getLocation().setLocality(FACTORY.createLocationStructureLocality());
        obj.getLocation().getLocality().setLatitude(BigDecimal.valueOf(RandomUtils.nextDouble(0, 90)));
        obj.getLocation().getLocality().setLongitude(BigDecimal.valueOf(RandomUtils.nextDouble(0, 90)));
        obj.getLocation().getLocality().setPostalCode(RANDOMIZER.getZipCode());

        obj.getLocation().getLocality().setNeighbourhood(FACTORY.createLocationStructureLocalityNeighbourhood());
        obj.getLocation().getLocality().getNeighbourhood().setId(BigInteger.valueOf(RandomUtils.nextInt(0, 10)));
        obj.getLocation().getLocality().getNeighbourhood().setType(randomValue(LocationNeighbourhoodType.values()));
        obj.getLocation().getLocality().getNeighbourhood().setValue(RANDOMIZER.getWords(3, 10));

        obj.getLocation().getLocality().setThoroughfare(FACTORY.createLocationStructureLocalityThoroughfare());
        obj.getLocation().getLocality().getThoroughfare().setDisplay(RandomUtils.nextBoolean());
        obj.getLocation().getLocality().getThoroughfare().setValue(RANDOMIZER.getWords(3, 10));

        obj.getLocation().setSubAdministrativeArea(FACTORY.createLocationStructureSubAdministrativeArea());
        obj.getLocation().getSubAdministrativeArea().setCode(RandomStringUtils.randomAlphanumeric(5));
        obj.getLocation().getSubAdministrativeArea().setValue(RANDOMIZER.getCity());

        obj.setPictures(FACTORY.createFeedPropertiesPropertyPictures());
        int pictureCount = RandomUtils.nextInt(2, 10);
        for (int i = 0; i < pictureCount; i++) {
            obj.getPictures().getPictureUrlAndPicture().add(createPicture(i));
        }

        obj.setPropertyType(FACTORY.createProptype());
        obj.getPropertyType().setBusinessType(FACTORY.createBusinessElement());
        obj.getPropertyType().getBusinessType().setCategory(randomValue(BusinessElementCategory.values()));
        obj.getPropertyType().getBusinessType().setValue(randomValue(PropertyTypeBusiness.values()));
        obj.getPropertyType().setTerrains(FACTORY.createTerrains());
        obj.getPropertyType().getTerrains().getTerrain().add(randomValue(TerrainType.values()));
        obj.getPropertyType().setType(randomValue(PropertyTypeSimple.values()));

        obj.setTransactionType(FACTORY.createTransactionType());
        obj.getTransactionType().setAuction(RandomUtils.nextBoolean());
        obj.getTransactionType().setOwnership(randomValue(OwnershipType.values()));
        obj.getTransactionType().setValue(RANDOMIZER.getWords(3, 10));

        obj.setVideos(FACTORY.createFeedPropertiesPropertyVideos());
        int videoCount = RandomUtils.nextInt(1, 3);
        for (int i = 0; i < videoCount; i++) {
            obj.getVideos().getVideo().add(createVideo());
        }

        return obj;
    }

    /**
     * Create a {@link PictureProject} with some example data.
     *
     * @return created example object
     */
    private static PictureProject createPicture(int position) {
        PictureProject pic = FACTORY.createPictureProject();
        pic.setPosition(BigInteger.valueOf(position));
        pic.setValue("https://www.example.com/image-" + position + ".jpg");
        return pic;
    }

    /**
     * Create a {@link PictureExtended} with some example data.
     *
     * @param position image position
     * @return created example object
     */
    private static PictureExtended createPictureExtended(int position) {
        PictureExtended pic = FACTORY.createPictureExtended();
        pic.setPosition(BigInteger.valueOf(position));
        pic.setValue("image-" + position + ".jpg");
        pic.setUrl("https://www.example.com/" + pic.getValue());
        return pic;
    }

    /**
     * Create a {@link VideoProject} with some example data.
     *
     * @return created example object
     */
    private static VideoProject createVideo() {
        VideoProject video = FACTORY.createVideoProject();
        video.setType(VideoType.REMOTE);
        video.setValue("https://www.example.com/video-" + RandomUtils.nextInt(0, 999) + ".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.

Clone this wiki locally