Java tutorial
package siddur.solidtrust.autoscout.price; import java.util.Arrays; import java.util.Date; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.apache.commons.lang3.time.DateUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.ui.Model; import siddur.solidtrust.autoscout.AutoscoutNl; import siddur.solidtrust.autoscout.AutoscoutPriceAnalyzer; import siddur.solidtrust.autoscout.IAutoscout; import siddur.solidtrust.autoscoutEs.AutoscoutEs; import siddur.solidtrust.autoscoutFr.AutoscoutFr; import siddur.solidtrust.autoscoutde.AutoscoutDe; import siddur.solidtrust.azure.AzureCarService; import siddur.solidtrust.entity.AzureCar; import siddur.solidtrust.util.DateUtil; @Service public class AutoscoutPriceService { private static final Logger log4j = Logger.getLogger(AutoscoutPriceService.class); private static final Integer[] MILEAGE_RANGE = { 0, 5000, 20000, 50000, 100000, 150000, 200000, 300000, Integer.MAX_VALUE }; @PersistenceContext private EntityManager em; @Autowired private AzureCarService azureService; @Autowired private AutoscoutPriceAnalyzer autoScountanalyzer; // @Transactional(readOnly = true) // public AutoscoutPrice compare(String lp) { // String ql = "from AutoscoutNl where licensePlate = :lp order by id desc"; // List<AutoscoutNl> list = em.createQuery(ql, AutoscoutNl.class).setParameter("lp", lp).setMaxResults(1).getResultList(); // if(!list.isEmpty()) { // AutoscoutNl nl = list.get(0); // AutoscoutPrice ap = new AutoscoutPrice(); // String from = DateUtil.date2String(DateUtils.addDays(new Date(), -90)); // ap.setLicensePlate(lp); // ap.setBrand(nl.getBrand()); // ap.setModel(nl.getModel()); // ap.setBuildYear(nl.getBuild()); // ap.setFuelType(nl.getFuel()); // ap.setEnginesize(nl.getEnginesize()); // ap.setPriceNl(nl.getPrice()); // ap.setMileage(nl.getMileage()); // // int mileage = nl.getMileage(); // int i = 0; // for (int j = 1; j < MILEAGE_RANGE.length; j++) { // if(MILEAGE_RANGE[j] >= mileage){ // i = j - 1; // break; // } // } // // ap.setPriceDe(getPrice(ap, "AutoscoutDe", from, MILEAGE_RANGE[i], MILEAGE_RANGE[i + 1])); // ap.setPriceFr(getPrice(ap, "AutoscoutFr", from, MILEAGE_RANGE[i], MILEAGE_RANGE[i + 1])); // ap.setPriceEs(getPrice(ap, "AutoscoutEs", from, MILEAGE_RANGE[i], MILEAGE_RANGE[i + 1])); // return ap; // } // return null; // } @Transactional(readOnly = true) public void compareList(String lp, Integer mileage, Model model) { long start = System.currentTimeMillis(); AutoscoutNl nl = getByPlate(lp, mileage); log4j.info("1>" + (System.currentTimeMillis() - start) / 1000); if (nl != null) { AutoscoutPrice ap = new AutoscoutPrice(); String from = DateUtil.date2String(DateUtils.addDays(new Date(), -90)); ap.setLicensePlate(lp); ap.setBrand(nl.getBrand()); ap.setModel(nl.getModel()); ap.setBuildYear(nl.getBuild()); ap.setFuelType(nl.getFuel()); ap.setEnginesize(nl.getEnginesize()); ap.setMileage(mileage); log4j.info("brand=" + nl.getBrand() + ", model=" + nl.getModel() + ", build=" + nl.getBuild() + ", fuel=" + nl.getFuel() + ", engine=" + nl.getEnginesize() + ", mileage=" + nl.getMileage()); int i = 0; for (int j = 1; j < MILEAGE_RANGE.length; j++) { if (MILEAGE_RANGE[j] >= mileage) { i = j - 1; break; } } model.addAttribute("item", ap); ap.setPriceNl(getAutoscoutNl(model, ap, from, MILEAGE_RANGE[i], MILEAGE_RANGE[i + 1])); log4j.info("2>" + (System.currentTimeMillis() - start) / 1000); ap.setPriceDe(getAutoscout(model, ap, AutoscoutDe.class, from, MILEAGE_RANGE[i], MILEAGE_RANGE[i + 1])); log4j.info("3>" + (System.currentTimeMillis() - start) / 1000); ap.setPriceFr(getAutoscout(model, ap, AutoscoutFr.class, from, MILEAGE_RANGE[i], MILEAGE_RANGE[i + 1])); log4j.info("4>" + (System.currentTimeMillis() - start) / 1000); ap.setPriceEs(getAutoscout(model, ap, AutoscoutEs.class, from, MILEAGE_RANGE[i], MILEAGE_RANGE[i + 1])); log4j.info("5>" + (System.currentTimeMillis() - start) / 1000); } } @SuppressWarnings("deprecation") private AutoscoutNl getByPlate(String lp, Integer mileage) { String ql = "from AutoscoutNl where licensePlate = :lp order by id desc"; List<AutoscoutNl> list = em.createQuery(ql, AutoscoutNl.class).setParameter("lp", lp).setMaxResults(1) .getResultList(); if (!list.isEmpty()) { return list.get(0); } //in AzureCar AzureCar entity = null; try { entity = azureService.findByLicensePlate(lp); } catch (Exception e) { log4j.error(e.getMessage(), e); } if (entity != null) { String build = (entity.getDateOfBuild().getYear() + 1900) + ""; String brand = entity.getBrand(); String model = entity.getType(); try { list = autoScountanalyzer.search(build, brand, model, mileage, entity.getFuelType(), entity.getVolumeOfCylinder(), 3, true); if (!list.isEmpty()) { return list.get(0); } } catch (Exception e) { log4j.error(e.getMessage(), e); } } return null; } // private int getPrice(AutoscoutPrice ap, String table, String from, int mileFrom, int mileTo) { // List<String> fuels = FuelTypeMap.getQueryString(ap.getFuelType(), table); // log4j.info(ap.getFuelType() + "==>" + fuels.size()); // if(fuels.isEmpty()) { // return -1; // } // String ql = "select average(price) from " + table + " where dateScraped > :dateScraped" // + " and brand = :brand" // + " and model = :model" // + " and build = :build" // + " and mileage between :mileFrom and :mileTo" // + " and fuel in :fuels" // + " and enginesize = :enginesize order by id desc"; // List<Integer> list = em.createQuery(ql, Integer.class) // .setParameter("brand", ap.getBrand()) // .setParameter("model", ap.getModel()) // .setParameter("build", ap.getBuildYear()) // .setParameter("mileFrom", mileFrom) // .setParameter("mileTo", mileTo) // .setParameter("fuels", fuels) // .setParameter("enginesize", ap.getEnginesize()) // .setParameter("dateScraped", from) // .setMaxResults(1) // .getResultList(); // if(list.size() > 0) { // return list.get(0); // } // return -1; // } private int getAutoscoutNl(Model model, AutoscoutPrice ap, String from, int mileFrom, int mileTo) { String ql = "from AutoscoutNl where dateScraped > :dateScraped" + " and brand = :brand" + " and model = :model" + " and build = :build" + " and mileage between :mileFrom and :mileTo" + " and fuel in :fuels" + " and enginesize = :enginesize order by id desc"; log4j.info(ql); log4j.info(mileFrom + "-" + mileTo); log4j.info(from); List<?> list = em.createQuery(ql, AutoscoutNl.class).setParameter("brand", ap.getBrand()) .setParameter("model", ap.getModel()).setParameter("build", ap.getBuildYear()) .setParameter("mileFrom", mileFrom).setParameter("mileTo", mileTo) .setParameter("fuels", ap.getFuelType()).setParameter("enginesize", ap.getEnginesize()) .setParameter("dateScraped", from).getResultList(); if (list.size() > 0) { log4j.info(list.get(0)); model.addAttribute(AutoscoutNl.class.getSimpleName(), list); int count = 0; int total = 0; for (Object obj : list) { IAutoscout a = (IAutoscout) obj; count++; total += a.getPrice(); } int price = total / count; model.addAttribute(AutoscoutNl.class.getSimpleName() + "_price", price); return price; } return 0; } private int getAutoscout(Model model, AutoscoutPrice ap, Class<?> entity, String from, int mileFrom, int mileTo) { List<String> fuels = FuelTypeMap.getQueryString(ap.getFuelType(), entity.getSimpleName()); log4j.info(ap.getFuelType() + "==>" + Arrays.toString(fuels.toArray())); if (fuels.isEmpty()) { return 0; } String ql = "from " + entity.getSimpleName() + " where dateScraped > :dateScraped" + " and brand = :brand" + " and model = :model" + " and build = :build" + " and mileage between :mileFrom and :mileTo" + " and fuel in :fuels" + " and enginesize = :enginesize order by id desc"; log4j.info(ql); log4j.info(mileFrom + "-" + mileTo); log4j.info(from); List<?> list = em.createQuery(ql, entity).setParameter("brand", ap.getBrand()) .setParameter("model", ap.getModel()).setParameter("build", ap.getBuildYear()) .setParameter("mileFrom", mileFrom).setParameter("mileTo", mileTo).setParameter("fuels", fuels) .setParameter("enginesize", ap.getEnginesize()).setParameter("dateScraped", from).getResultList(); if (list.size() > 0) { model.addAttribute(entity.getSimpleName(), list); int count = 0; int total = 0; for (Object obj : list) { IAutoscout a = (IAutoscout) obj; count++; total += a.getPrice(); } int price = total / count; model.addAttribute(entity.getSimpleName() + "_price", price); return price; } return 0; } }