siddur.solidtrust.azure.AzureConnector.java Source code

Java tutorial

Introduction

Here is the source code for siddur.solidtrust.azure.AzureConnector.java

Source

package siddur.solidtrust.azure;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.time.DateUtils;
import org.core4j.Enumerable;
import org.joda.time.LocalDateTime;
import org.odata4j.consumer.ODataClientRequest;
import org.odata4j.consumer.ODataConsumer;
import org.odata4j.consumer.ODataConsumers;
import org.odata4j.consumer.behaviors.OClientBehavior;
import org.odata4j.core.OEntity;
import org.odata4j.core.OProperty;
import org.odata4j.core.OQueryRequest;
import org.odata4j.edm.EdmSimpleType;
import org.odata4j.repack.org.apache.commons.codec.binary.Base64;

import siddur.solidtrust.entity.AzureCar;
import siddur.solidtrust.util.DateUtil;

@Deprecated
public class AzureConnector {

    public static final int pageSize = 5000;

    // pageIndex from 0
    public static Enumerable<OEntity> updateDateField(int days, int pageIndex, String field) throws Exception {
        int top = pageSize;
        int skip = pageIndex * pageSize;

        Date d = new Date();
        d = DateUtils.addDays(d, -days);
        String day = DateUtil.date2String(d);

        ODataConsumer c = ODataConsumers.create(AzureCarConstants.API_URL);
        OQueryRequest<OEntity> request = c.getEntities("KENT_VRTG_O_DAT").skip(skip).top(top);

        if (field.equals(AzureCarConstants.DATUMAANVANGTENAAMSTELLING)) {
            request.filter("Datumaanvangtenaamstelling eq datetime'" + day + "'")
                    .select("Datumaanvangtenaamstelling");
        } else if (field.equals(AzureCarConstants.VERVALDATUMAPK)) {
            request.filter("VervaldatumAPK gt datetime'" + day + "'").select("VervaldatumAPK");
        }

        Enumerable<OEntity> entityList = request.execute();
        return entityList;
    }

    @Deprecated
    //cannot filter with Wachtopkeuren
    public static Enumerable<OEntity> updateWOK(int pageIndex) {
        int top = pageSize;
        int skip = pageIndex * pageSize;

        ODataConsumer c = ODataConsumers.create(AzureCarConstants.API_URL);
        Enumerable<OEntity> entityList = c.getEntities("KENT_VRTG_O_DAT").skip(skip).top(top)
                .orderBy("Wachtopkeuren").select("Kenteken").execute();

        return entityList;
    }

    public static OEntity findByLicensePlate(String lp) throws Exception {
        ODataConsumer c = ODataConsumers.create(AzureCarConstants.API_URL);
        OEntity entity = c.getEntity("KENT_VRTG_O_DAT", lp).execute();
        return entity;
    }

    @Deprecated
    public static Enumerable<OEntity> queryEachPage(int pageSize, int pageIndex) {
        int top = pageSize;
        int skip = pageIndex * pageSize;
        ODataConsumer c = ODataConsumers.create(AzureCarConstants.API_URL);
        Enumerable<OEntity> entityList = c.getEntities("KENT_VRTG_O_DAT").top(top).skip(skip).execute();
        return entityList;
    }

    @Deprecated
    public static Enumerable<OEntity> updateSince(final String lp) {
        ODataConsumer c = ODataConsumers.newBuilder(AzureCarConstants.API_URL)
                .setClientBehaviors(new OClientBehavior() {

                    @Override
                    public ODataClientRequest transform(ODataClientRequest req) {
                        if (req.getUrl().endsWith("KENT_VRTG_O_DAT")) {
                            req.getQueryParams().put("$skiptoken", "'" + lp + "'");
                        }
                        return req;
                    }
                }).build();

        Enumerable<OEntity> entityList = c.getEntities("KENT_VRTG_O_DAT").top(1000).execute();
        return entityList;
    }

    public static AzureCar update(AzureCar azureCar, OEntity entity) {
        for (@SuppressWarnings("rawtypes")
        OProperty p : entity.getProperties()) {
            Object v = p.getValue();
            if (v != null) {
                if (p.getType().equals(EdmSimpleType.BINARY)) {
                    v = Base64.encodeBase64String((byte[]) v).trim();
                }
                populate(azureCar, p.getName(), v);
            }
        }
        return azureCar;
    }

    public static AzureCar convert(OEntity entity) {
        AzureCar azureCar = new AzureCar();
        for (@SuppressWarnings("rawtypes")
        OProperty p : entity.getProperties()) {
            Object v = p.getValue();
            if (v != null) {
                if (p.getType().equals(EdmSimpleType.BINARY)) {
                    v = Base64.encodeBase64String((byte[]) v).trim();
                }
                populate(azureCar, p.getName(), v);
            }
        }
        return azureCar;
    }

    public static Map<String, Object> convert(AzureCar azureCar) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(AzureCarConstants.BPM, getNumber(azureCar.getBpm()));
        map.put(AzureCarConstants.KENTEKEN, azureCar.getLicensePlate());
        map.put(AzureCarConstants.CATALOGUSPRIJS, getNumber(azureCar.getNewPrice()));
        map.put(AzureCarConstants.CO2UITSTOOTGECOMBINEERD, getNumber(azureCar.getCo2()));
        map.put(AzureCarConstants.DATUMAANVANGTENAAMSTELLING, toLocalDateTime(azureCar.getDateRegistedOnName()));
        map.put(AzureCarConstants.DATUMEERSTEAFGIFTENEDERLAND,
                toLocalDateTime(azureCar.getDateRegistedOnHolland()));
        map.put(AzureCarConstants.DATUMEERSTETOELATING, toLocalDateTime(azureCar.getDateOfBuild()));
        map.put(AzureCarConstants.HANDELSBENAMING, azureCar.getType());
        map.put(AzureCarConstants.EERSTEKLEUR, azureCar.getFirstColor());
        map.put(AzureCarConstants.HOOFDBRANDSTOF, azureCar.getFuelType());
        map.put(AzureCarConstants.INRICHTING, azureCar.getArrangement());
        map.put(AzureCarConstants.MERK, azureCar.getBrand());
        map.put(AzureCarConstants.MILIEUCLASSIFICATIE, azureCar.getEnviromentClassification());
        map.put(AzureCarConstants.NEVENBRANDSTOF, azureCar.getSecondFuel());
        map.put(AzureCarConstants.RETROFITROETFILTER, azureCar.getDieselFilter());
        map.put(AzureCarConstants.TWEEDEKLEUR, azureCar.getSecondColor());
        map.put(AzureCarConstants.VERVALDATUMAPK, toLocalDateTime(azureCar.getDateOfNextInspection()));
        map.put(AzureCarConstants.VOERTUIGSOORT, azureCar.getVehicleType());
        map.put(AzureCarConstants.ZUINIGHEIDSLABEL, azureCar.getEconomicalLabel());
        map.put(AzureCarConstants.AANTALCILINDERS, getNumber(azureCar.getNumOfCylinders()));
        map.put(AzureCarConstants.AANTALSTAANPLAATSEN, getNumber(azureCar.getNumOfStandingPlace()));
        map.put(AzureCarConstants.AANTALZITPLAATSEN, getNumber(azureCar.getNumOfSittingPlace()));
        map.put(AzureCarConstants.BRANDSTOFVERBRUIKBUITENWEG, getNumber(azureCar.getFuelUseOfHignway()));
        map.put(AzureCarConstants.BRANDSTOFVERBRUIKSTAD, getNumber(azureCar.getFuelUseOfCity()));
        map.put(AzureCarConstants.BRANDSTOFVERBRUIKGECOMBINEERD, getNumber(azureCar.getFuelUseOfCombined()));
        map.put(AzureCarConstants.CILINDERINHOUD, getNumber(azureCar.getVolumeOfCylinder()));
        map.put(AzureCarConstants.G3INSTALLATIE, azureCar.getG3GassInstallation());
        map.put(AzureCarConstants.LAADVERMOGEN, getNumber(azureCar.getLoadCapacity()));
        map.put(AzureCarConstants.MASSALEEGVOERTUIG, getNumber(azureCar.getMassOfEmptyVehicle()));
        map.put(AzureCarConstants.MASSARIJKLAAR, getNumber(azureCar.getMassOfDriveReady()));
        map.put(AzureCarConstants.MAXIMALECONSTRUCTIESNELHEID, getNumber(azureCar.getMaximumConstructionSpeed()));
        map.put(AzureCarConstants.MAXIMUMTETREKKENMASSAAUTONOOMGEREMD,
                getNumber(azureCar.getMaximumMassOfPullingAutoBrake()));
        map.put(AzureCarConstants.MAXIMUMTETREKKENMASSAGEREMD, getNumber(azureCar.getMaximumMassOfPullingBrake()));
        map.put(AzureCarConstants.MAXIMUMTETREKKENMASSAMIDDENASGEREMD,
                getNumber(azureCar.getMaximumMassOfPullingMidaxelBrake()));
        map.put(AzureCarConstants.MAXIMUMTETREKKENMASSAONGEREMD,
                getNumber(azureCar.getMaximumMassOfPullingNotBrake()));
        map.put(AzureCarConstants.MAXIMUMTETREKKENMASSAOPLEGGERGEREMD,
                getNumber(azureCar.getMaximumMassOfPullingTrailer()));
        map.put(AzureCarConstants.TOEGESTANEMAXIMUMMASSAVOERTUIG,
                getNumber(azureCar.getMaximumMassOfPullingAllowed()));
        map.put(AzureCarConstants.VERMOGEN, getNumber(azureCar.getPower()));
        map.put(AzureCarConstants.WACHTOPKEUREN, azureCar.getWaitingForMot());
        map.put(AzureCarConstants.WAMVERZEKERDGEREGISTREERD, azureCar.getRegisteredInsurance());

        map.put("Vermogenbromsnorfiets", null);
        return map;
    }

    private static void populate(AzureCar azureCar, String n, Object v) {
        if (n.equals(AzureCarConstants.BPM)) {
            azureCar.setBpm((int) v);
        } else if (n.equals(AzureCarConstants.KENTEKEN)) {
            azureCar.setLicensePlate((String) v);
        } else if (n.equals(AzureCarConstants.CATALOGUSPRIJS)) {
            azureCar.setNewPrice((int) v);
        } else if (n.equals(AzureCarConstants.CO2UITSTOOTGECOMBINEERD)) {
            azureCar.setCo2((short) v);
        } else if (n.equals(AzureCarConstants.DATUMAANVANGTENAAMSTELLING)) {
            azureCar.setDateRegistedOnName(toDate((LocalDateTime) v));
        } else if (n.equals(AzureCarConstants.DATUMEERSTEAFGIFTENEDERLAND)) {
            azureCar.setDateRegistedOnHolland(toDate((LocalDateTime) v));
        } else if (n.equals(AzureCarConstants.DATUMEERSTETOELATING)) {
            azureCar.setDateOfBuild(toDate((LocalDateTime) v));
        } else if (n.equals(AzureCarConstants.HANDELSBENAMING)) {
            azureCar.setType((String) v);
        } else if (n.equals(AzureCarConstants.EERSTEKLEUR)) {
            azureCar.setFirstColor((String) v);
        } else if (n.equals(AzureCarConstants.HOOFDBRANDSTOF)) {
            azureCar.setFuelType((String) v);
        } else if (n.equals(AzureCarConstants.INRICHTING)) {
            azureCar.setArrangement((String) v);
        } else if (n.equals(AzureCarConstants.MERK)) {
            azureCar.setBrand((String) v);
        } else if (n.equals(AzureCarConstants.MILIEUCLASSIFICATIE)) {
            azureCar.setEnviromentClassification((String) v);
        } else if (n.equals(AzureCarConstants.NEVENBRANDSTOF)) {
            azureCar.setSecondFuel((String) v);
        } else if (n.equals(AzureCarConstants.RETROFITROETFILTER)) {
            azureCar.setDieselFilter((String) v);
        } else if (n.equals(AzureCarConstants.TWEEDEKLEUR)) {
            azureCar.setSecondColor((String) v);
        } else if (n.equals(AzureCarConstants.VERVALDATUMAPK)) {
            azureCar.setDateOfNextInspection(toDate((LocalDateTime) v));
        } else if (n.equals(AzureCarConstants.VOERTUIGSOORT)) {
            azureCar.setVehicleType((String) v);
        } else if (n.equals(AzureCarConstants.ZUINIGHEIDSLABEL)) {
            azureCar.setEconomicalLabel((String) v);
        } else if (n.equals(AzureCarConstants.AANTALCILINDERS)) {
            azureCar.setNumOfCylinders((short) v);
        } else if (n.equals(AzureCarConstants.AANTALSTAANPLAATSEN)) {
            azureCar.setNumOfStandingPlace((short) v);
        } else if (n.equals(AzureCarConstants.AANTALZITPLAATSEN)) {
            azureCar.setNumOfSittingPlace((short) v);
        } else if (n.equals(AzureCarConstants.BRANDSTOFVERBRUIKBUITENWEG)) {
            azureCar.setFuelUseOfHignway(((BigDecimal) v).floatValue());
        } else if (n.equals(AzureCarConstants.BRANDSTOFVERBRUIKSTAD)) {
            azureCar.setFuelUseOfCity(((BigDecimal) v).floatValue());
        } else if (n.equals(AzureCarConstants.BRANDSTOFVERBRUIKGECOMBINEERD)) {
            azureCar.setFuelUseOfCombined(((BigDecimal) v).floatValue());
        } else if (n.equals(AzureCarConstants.CILINDERINHOUD)) {
            azureCar.setVolumeOfCylinder((int) v);
        } else if (n.equals(AzureCarConstants.G3INSTALLATIE)) {
            azureCar.setG3GassInstallation((String) v);
        } else if (n.equals(AzureCarConstants.LAADVERMOGEN)) {
            azureCar.setLoadCapacity((int) v);
        } else if (n.equals(AzureCarConstants.MASSALEEGVOERTUIG)) {
            azureCar.setMassOfEmptyVehicle((int) v);
        } else if (n.equals(AzureCarConstants.MASSARIJKLAAR)) {
            azureCar.setMassOfDriveReady((int) v);
        } else if (n.equals(AzureCarConstants.MAXIMALECONSTRUCTIESNELHEID)) {
            azureCar.setMaximumConstructionSpeed((short) v);
        } else if (n.equals(AzureCarConstants.MAXIMUMTETREKKENMASSAAUTONOOMGEREMD)) {
            azureCar.setMaximumMassOfPullingAutoBrake((int) v);
        } else if (n.equals(AzureCarConstants.MAXIMUMTETREKKENMASSAGEREMD)) {
            azureCar.setMaximumMassOfPullingBrake((int) v);
        } else if (n.equals(AzureCarConstants.MAXIMUMTETREKKENMASSAMIDDENASGEREMD)) {
            azureCar.setMaximumMassOfPullingMidaxelBrake((int) v);
        } else if (n.equals(AzureCarConstants.MAXIMUMTETREKKENMASSAONGEREMD)) {
            azureCar.setMaximumMassOfPullingNotBrake((int) v);
        } else if (n.equals(AzureCarConstants.MAXIMUMTETREKKENMASSAOPLEGGERGEREMD)) {
            azureCar.setMaximumMassOfPullingTrailer((int) v);
        } else if (n.equals(AzureCarConstants.TOEGESTANEMAXIMUMMASSAVOERTUIG)) {
            azureCar.setMaximumMassOfPullingAllowed((int) v);
        } else if (n.equals(AzureCarConstants.VERMOGEN)) {
            azureCar.setPower((short) v);
        } else if (n.equals(AzureCarConstants.WACHTOPKEUREN)) {
            azureCar.setWaitingForMot((String) v);
        } else if (n.equals(AzureCarConstants.WAMVERZEKERDGEREGISTREERD)) {
            azureCar.setRegisteredInsurance((String) v);
        }
    }

    private static Date toDate(LocalDateTime l) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, l.getYear());
        c.set(Calendar.DAY_OF_YEAR, l.getDayOfYear());
        c.set(Calendar.HOUR_OF_DAY, l.getHourOfDay());
        c.set(Calendar.MINUTE, l.getMinuteOfHour());
        c.set(Calendar.SECOND, l.getSecondOfMinute());
        c.set(Calendar.MILLISECOND, l.getMillisOfSecond());
        return c.getTime();
    }

    private static LocalDateTime toLocalDateTime(Date d) {
        if (d == null) {
            return null;
        }
        LocalDateTime l = new LocalDateTime(d.getTime());
        return l;
    }

    private static Object getNumber(float n) {
        if (n == 0) {
            return null;
        }
        return n;
    }

    private static Object getNumber(int n) {
        if (n == 0) {
            return null;
        }
        return n;
    }

    private static Object getNumber(short n) {
        if (n == 0) {
            return null;
        }
        return n;
    }

    public static void main(String[] args) throws Exception {
        OEntity entity = findByLicensePlate("GT455V");
        System.out.println(entity);
    }
}