siddur.solidtrust.cost.CarCostService.java Source code

Java tutorial

Introduction

Here is the source code for siddur.solidtrust.cost.CarCostService.java

Source

package siddur.solidtrust.cost;

import java.text.NumberFormat;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import siddur.solidtrust.azure.AzureCarPersister;
import siddur.solidtrust.entity.AzureCar;
import siddur.solidtrust.entity.Car;
import siddur.solidtrust.entity.Tire;
import siddur.solidtrust.newprice.NewpriceService;
import siddur.solidtrust.priceAnalysis.NewMarktplaats;
import siddur.solidtrust.wok.Wachtopkeuren;

@Service
public class CarCostService {

    private static final Logger log4j = Logger.getLogger(CarCostService.class);

    @PersistenceContext
    private EntityManager em;

    @Autowired
    private AzureCarPersister azurePersister;

    @Autowired
    private NewpriceService ss;

    @SuppressWarnings("deprecation")
    public CarCost search(String lp) {
        String ql = "from NewMarktplaats m where m.licensePlate = '" + lp + "' order by id desc";
        List<NewMarktplaats> list = em.createQuery(ql, NewMarktplaats.class).setMaxResults(1).getResultList();
        AzureCar a = null;
        if (list.size() == 0) {
            a = azurePersister.find(lp);
            if (a == null) {
                return null;
            }
            ql = "select n from AzureCar b, NewMarktplaats n where b.licensePlate != '" + lp + "' and b.brand = '"
                    + a.getBrand() + "' and b.type = '" + a.getType() + "' and n.licensePlate = b.licensePlate";
            list = em.createQuery(ql, NewMarktplaats.class).setMaxResults(1).getResultList();
        }
        if (list.size() == 1) {
            NewMarktplaats m = list.get(0);

            CarCost cc = new CarCost();
            cc.setBrand(m.getBrand());
            cc.setModel(m.getModel());
            cc.setBuild(m.getBuild());
            if (StringUtils.isNotEmpty(m.getImage())) {
                cc.setImage(m.getImage().replace("marktplaats", "afbeelding"));
            }

            cc.setRoadTax(m.getRoadTax());
            cc.setFuelUsage(m.getFuelUsage());
            cc.setSetTransmission(m.getTransmission());

            cc.setRepairCost(tranfer(m.getRepairCost()));
            cc.setTiresCost(tranfer(m.getTiresCost()));
            cc.setFuelCost(tranfer(m.getFuelCost()));

            cc.setWok(isWok(lp));
            if (a != null) {
                cc.setImported(a.getDateOfBuild().getYear() != a.getDateRegistedOnHolland().getYear());
            } else {
                cc.setImported(m.getNotImported() != Boolean.TRUE);
            }

            AzureCar entity = null;
            try {
                entity = azurePersister.find(lp);
            } catch (Exception e) {
                log4j.error(e.getMessage(), e);
            }

            if (entity != null) {
                Car c = getNewpriceCar(entity);
                if (c != null) {
                    cc.setNewprice(c.getNewPrice());
                    cc.setAcceleration(c.getAcceleration());
                    cc.setTireSize(c.getFrontTiresize());

                    String tireSize = cc.getTireSize();
                    if (StringUtils.isNotBlank(tireSize)) {
                        try {
                            setTireData(tireSize.trim(), cc);
                        } catch (Exception e) {
                            log4j.error(e.getMessage(), e);
                        }
                    }
                }
            }
            return cc;
        }
        return null;
    }

    private static String tranfer(String value) {
        //Marktplaats is based on 1250 kilometers per month. I want to recalculate this for 1083 kilometers
        float rate = 1083f / 1250f;
        String v = value.replace(",", ".");
        if (StringUtils.isNumeric(v)) {
            float rc = Float.parseFloat(value.replace(",", ".")) * rate;
            NumberFormat nf = NumberFormat.getIntegerInstance();
            nf.setMaximumFractionDigits(2);
            String _v = nf.format(rc);
            return _v.replace(".", ",");
        }
        return value;
    }

    private boolean isWok(String lp) {
        return null != em.find(Wachtopkeuren.class, lp);
    }

    //205/50R15
    //205/50VR15
    //145/70SR12Prijzen
    private void setTireData(String tireSize, CarCost cc) {
        int p1 = tireSize.indexOf("/");
        int p2 = tireSize.indexOf("VR");
        if (p2 == -1) {
            p2 = tireSize.indexOf("SR");
        }
        if (p2 == -1) {
            p2 = tireSize.indexOf("R");
        }

        float width = Integer.parseInt(tireSize.substring(0, p1));
        float height = Integer.parseInt(tireSize.substring(p1 + 1, p2));
        p2 = tireSize.indexOf("R");
        float diameter = Integer.parseInt(tireSize.replace("Prijzen", "").substring(p2 + 1));

        String query = "from Tire t where t.width = :width and t.height = :height and t.diameter = :diameter order by t.price asc";

        List<Tire> yearRound = em.createQuery(query, Tire.class).setParameter("width", width)
                .setParameter("height", height).setParameter("diameter", diameter).getResultList();
        if (!yearRound.isEmpty()) {
            double sum = 0;
            for (Tire tire : yearRound) {
                sum += tire.getPrice();
            }
            cc.setAverageTireCost((float) (sum / yearRound.size()));

            cc.setTiresNumber(yearRound.size());
            Tire frt = yearRound.get(0);
            cc.setCheapestTireName(frt.getBrand());
            cc.setCheapestTirePrice(frt.getPrice());
            cc.setCheapestTireUrl(frt.getProductURL());
        }
    }

    private Car getNewpriceCar(AzureCar entity) {
        List<Object[]> list = null;
        try {
            list = ss.search(1, ss.entity2Car(entity));
        } catch (Exception e) {
            log4j.error(e.getMessage(), e);
        }
        if (list.isEmpty()) {
            return null;
        }
        Car car = (Car) list.get(0)[1];
        return car;
    }

}