siddur.solidtrust.site.APIController.java Source code

Java tutorial

Introduction

Here is the source code for siddur.solidtrust.site.APIController.java

Source

package siddur.solidtrust.site;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.util.Version;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.FullTextQuery;
import org.hibernate.search.jpa.Search;
import org.hibernate.search.query.dsl.BooleanJunction;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import siddur.solidtrust.FreePersister;
import siddur.solidtrust.SolidtrustConstants;
import siddur.solidtrust.anw.ANWBautotests;
import siddur.solidtrust.anw.ANWService;
import siddur.solidtrust.autorola.Autorola;
import siddur.solidtrust.autoscout.AutoscoutNl;
import siddur.solidtrust.autoscout.AutoscoutPriceAnalyzer;
import siddur.solidtrust.autoscoutde.AutoscoutDe;
import siddur.solidtrust.autoscoutde.AutoscoutDePriceAnalyzer;
import siddur.solidtrust.autotrader.Autotrader;
import siddur.solidtrust.autotrader.TraderService;
import siddur.solidtrust.azure.AzureCarConstants;
import siddur.solidtrust.azure.AzureCarService;
import siddur.solidtrust.bi.mileage.TopMileageService;
import siddur.solidtrust.classic.PriceCalculator;
import siddur.solidtrust.classic.SynonymService;
import siddur.solidtrust.cost.CarCost;
import siddur.solidtrust.cost.CarCostService;
import siddur.solidtrust.entity.AccessItem;
import siddur.solidtrust.entity.AzureCar;
import siddur.solidtrust.entity.Car;
import siddur.solidtrust.entity.ClassicCarItem;
import siddur.solidtrust.entity.ClassicCarPrice;
import siddur.solidtrust.entity.Fault;
import siddur.solidtrust.entity.ReviewInfo;
import siddur.solidtrust.entity.Tire;
import siddur.solidtrust.euroncap.Euroncap;
import siddur.solidtrust.fault.FaultService;
import siddur.solidtrust.logo.LogoService;
import siddur.solidtrust.manual.Manual;
import siddur.solidtrust.manual.ManualItem;
import siddur.solidtrust.manual.ManualRepository;
import siddur.solidtrust.marktplaats.MarktplaatsService;
import siddur.solidtrust.marktplaats.info.CarInfoPage;
import siddur.solidtrust.marktplaats.info.CarInfoService;
import siddur.solidtrust.newprice.NewpricePersister;
import siddur.solidtrust.owners.CarOwner;
import siddur.solidtrust.owners.NewCarOwnerService;
import siddur.solidtrust.priceAnalysis.NewMarktplaats;
import siddur.solidtrust.priceAnalysis.PriceAnalyzer;
import siddur.solidtrust.priceAnalysis.SortedMarktplaats;
import siddur.solidtrust.rdw.RDWService;
import siddur.solidtrust.rdw.RDWStatus;
import siddur.solidtrust.review.ReviewService;
import siddur.solidtrust.site.ProductAPI.Product;
import siddur.solidtrust.util.DateUtil;
import siddur.solidtrust.util.LuceneUtil;

@Controller
@RequestMapping("/api")
public class APIController {
    private final static Logger log4j = Logger.getLogger(APIController.class);

    @PersistenceContext
    private EntityManager em;

    @Autowired
    private NewpricePersister carService;

    @Autowired
    private AzureCarService azureService;

    @Autowired
    private FaultService faultService;

    @Autowired
    private FreePersister free;

    @Autowired
    private PriceCalculator calculator;

    @Autowired
    private RDWService rdwService;

    @Autowired
    private MarktplaatsService mService;

    @Autowired
    private SynonymService ss;

    @Autowired
    private PriceAnalyzer analyzer;

    @Autowired
    private AutoscoutPriceAnalyzer autoScountanalyzer;

    @Autowired
    private AutoscoutDePriceAnalyzer autoscoutDeAnalyzer;

    @Autowired
    private TopMileageService topMileage;

    @Autowired
    private NewCarOwnerService cos;

    @Autowired
    private ReviewService rs;

    @Autowired
    private ManualRepository manualRep;

    @Autowired
    private CarInfoService carinfoService;

    @Autowired
    private ANWService anwService;

    @Autowired
    private TraderService traderService;

    @Autowired
    private CarCostService carCostService;

    @Autowired
    private LogoService logoService;

    @RequestMapping(value = "/visits")
    public @ResponseBody long visitsApi(@RequestParam("productNO") int productNO, HttpServletRequest request) {
        String q = "select count(a) from AccessItem a where a.service = ? and a.username = ? and month='"
                + DateUtil.date2String2(new Date()) + "'";
        String userId = request.getAttribute(SolidtrustConstants.CLIENT_ID) + "";
        List<Long> list = em.createQuery(q, Long.class).setParameter(1, productNO).setParameter(2, userId)
                .getResultList();
        if (list.isEmpty()) {
            return 0;
        }
        return list.get(0);
    }

    @RequestMapping(value = "/roadtax")
    @Transactional(readOnly = false)
    public @ResponseBody String raodTax(@RequestParam("id") String id, HttpServletRequest request) {
        AccessItem ai = new AccessItem();
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.ROADTAX.getId());
        ai.setRequest(id);

        Car car = null;
        ;
        try {
            car = carService.findCar(id);
        } catch (Exception e) {
            ai.setStatus(-1);
            log4j.error(e.getMessage(), e);
            ai.setResponse(e.getMessage());
        }
        String tax = null;

        if (car != null) {
            tax = car.getRoadTax();
            ai.setResponse(tax);
        } else {
            ai.setResponse("no data");
            ai.setStatus(0);
        }
        free.save(ai);
        return tax;
    }

    @RequestMapping(value = "/roadtax2")
    @Transactional(readOnly = false)
    public @ResponseBody Object raodTax2(@RequestParam("id") String id, HttpServletRequest request) {
        AccessItem ai = new AccessItem();
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.ROADTAX.getId());
        ai.setRequest(id);

        Car car = null;
        ;
        try {
            car = carService.findCar(id);
        } catch (Exception e) {
            ai.setStatus(-1);
            log4j.error(e.getMessage(), e);
            ai.setResponse(e.getMessage());
        }
        String tax = null;

        if (car != null) {
            tax = car.getRoadTax();
            ai.setResponse(tax);
        } else {
            ai.setResponse("no data");
            ai.setStatus(0);
        }
        free.save(ai);
        Map<String, String> map = new HashMap<String, String>();
        map.put("tax", tax);
        return map;
    }

    @RequestMapping(value = "/newprice")
    public @ResponseBody Object findCar(@RequestParam("id") String id, HttpServletRequest request) {
        AccessItem ai = new AccessItem();
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.NEW_PRICE.getId());
        ai.setRequest(id);

        Car car = carService.findCar(id);

        if (car == null) {
            ai.setStatus(0);
            ai.setResponse("Not found:" + id);
        } else {
            ai.setResponse(car.toString());
        }
        free.save(ai);
        return car;
    }

    @RequestMapping(value = "/rdw")
    public @ResponseBody Object findAzureCar(@RequestParam("id") String id, HttpServletRequest request) {
        AccessItem ai = new AccessItem();
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.RDW.getId());
        ai.setRequest(id);

        Map<String, Object> carMap = null;
        if (!StringUtils.isEmpty(id)) {
            try {
                carMap = azureService.findEntityMapByLicensePlate(id);
            } catch (Exception e) {
                ai.setStatus(-1);
                log4j.error(e.getMessage(), e);
                ai.setResponse(e.getMessage());
            }
        }

        if (carMap != null) {
            ai.setResponse("[bpm=" + carMap.get(AzureCarConstants.BPM) + "];" + "[brand="
                    + carMap.get(AzureCarConstants.MERK) + "];" + "[type="
                    + carMap.get(AzureCarConstants.HANDELSBENAMING) + "];" + "[arrangement="
                    + carMap.get(AzureCarConstants.INRICHTING) + "];");
        } else {
            ai.setStatus(0);
            ai.setResponse(null);
        }
        free.save(ai);
        return carMap == null ? "no data" : carMap;
    }

    @RequestMapping(value = "/rdw_status")
    public @ResponseBody Callable<Object> findRDWStatus(@RequestParam("id") final String id,
            HttpServletRequest request) {
        final AccessItem ai = new AccessItem();
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.RDW_STATUS.getId());
        ai.setRequest(id);
        return new Callable<Object>() {
            @Override
            public Object call() throws Exception {

                RDWStatus rdw = rdwService.findStatus(id);

                String result = null;
                if (rdw == null) {
                    result = "no data";
                    ai.setResponse(result);
                    ai.setStatus(0);
                    free.save(ai);
                    return result;
                }

                ai.setResponse(rdw.toString());
                free.save(ai);
                return rdw;
            }
        };

    }

    @RequestMapping(value = "/extenddata")
    public @ResponseBody Object extendCarData(@RequestParam("id") String id, HttpServletRequest request) {
        AccessItem ai = new AccessItem();
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.EXTEND_CAR_DATA.getId());
        ai.setRequest(id);

        Car car = null;
        try {
            car = carService.findCar(id);
        } catch (Exception e) {
            ai.setStatus(-1);
            log4j.error(e.getMessage(), e);
            ai.setResponse(e.getMessage());
        }
        Map<String, String> map = null;
        if (car != null) {
            map = new HashMap<String, String>(2);
            String rear = car.getRearTiresize();
            String front = car.getFrontTiresize();
            String si = car.getServiceIntervals();

            map.put("frontTiresize", StringUtils.isEmpty(si) ? "no-info" : front);
            map.put("rearTiresize", StringUtils.isEmpty(rear) ? "no-info" : rear);
            map.put("serviceIntervals", StringUtils.isEmpty(si) ? "no-info" : si);
            ai.setResponse(rear + "," + si);
        } else {
            ai.setResponse("no data");
        }
        free.save(ai);
        if (map == null) {
            return "no data";
        }
        return map;
    }

    @RequestMapping(value = "/fault")
    public @ResponseBody Object fault(@RequestParam("id") String id,
            @RequestParam(value = "rate", defaultValue = "false", required = false) boolean rate,
            HttpServletRequest request) {
        AccessItem ai = new AccessItem();
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.FAULT.getId());
        ai.setRequest(id);

        AzureCar entity = null;
        if (!StringUtils.isEmpty(id)) {
            try {
                entity = azureService.findByLicensePlate(id.trim());
            } catch (Exception e) {
                ai.setStatus(-1);
                log4j.error(e.getMessage(), e);
                ai.setResponse(e.getMessage());
            }
        }

        Fault fault = null;
        if (entity != null) {
            Fault f = faultService.entity2Fault(entity);
            List<Object[]> list = null;
            try {
                list = faultService.search(1, f, "2");
            } catch (Exception e) {
                log4j.error(e.getMessage(), e);
                ai.setStatus(-1);
                ai.setResponse(e.getMessage());
            }
            fault = (list == null || list.isEmpty()) ? null : (Fault) list.get(0)[1];
            boolean needOldVersion = false;
            boolean needMerge = false;

            if (fault != null) {
                ai.setResponse(fault.getId() + "");
                log4j.info("find fault with id:" + ai.getResponse());
                if (StringUtils.isEmpty(fault.getChassis())) {
                    needOldVersion = true;
                    needMerge = true;
                }
            } else {
                needOldVersion = true;
                needMerge = false;
            }
            if (needOldVersion) {
                try {
                    list = faultService.search(1, f, "1");
                } catch (Exception e) {
                    log4j.error(e.getMessage(), e);
                    ai.setStatus(-1);
                    ai.setResponse(e.getMessage());
                }
                Fault old = (list == null || list.isEmpty()) ? null : (Fault) list.get(0)[1];
                if (old != null) {
                    if (needMerge) {
                        fault.merge(old);
                        ai.setResponse(ai.getResponse() + "," + old.getId());
                    } else {
                        fault = old;
                        ai.setResponse(fault.getId() + "");
                    }

                    log4j.info("find fault with ids:" + ai.getResponse());
                }
            }

            if (fault == null) {
                fault = f;
            }
        }

        //clean up
        if (fault != null) {
            fault.format();
            if (!rate) {
                fault.setBodywork(null);
                fault.setBrakeSystem(null);
                fault.setChassis(null);
                fault.setElectrics(null);
                fault.setEngine(null);
                fault.setFaultIndex(null);
            } else {
                if (fault.getBodywork() == null) {
                    fault.setBodywork("");
                }
                if (fault.getBrakeSystem() == null) {
                    fault.setBrakeSystem("");
                }
                if (fault.getChassis() == null) {
                    fault.setChassis("");
                }
                if (fault.getElectrics() == null) {
                    fault.setElectrics("");
                }
                if (fault.getEngine() == null) {
                    fault.setEngine("");
                }
                if (fault.getFaultIndex() == null) {
                    fault.setFaultIndex("");
                }
            }
        } else {
            ai.setStatus(0);
        }
        free.save(ai);
        return fault;
    }

    @RequestMapping(value = "/tire")
    public @ResponseBody Object fault(
            @RequestParam(value = "width", required = false, defaultValue = "0") float width,
            @RequestParam(value = "height", required = false, defaultValue = "0") float height,
            @RequestParam(value = "diameter", required = false, defaultValue = "0") float diameter,
            HttpServletRequest request) {
        AccessItem ai = new AccessItem();
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.TIRE.getId());
        ai.setRequest(width + ", " + height + ", " + diameter);

        if (width * height * diameter == 0) {
            String result = null;
            if (width == 0) {
                result = "Width cannot be empty";
            }
            if (height == 0) {
                result = "Height cannot be empty";
            }
            if (diameter == 0) {
                result = "Diameter cannot be empty";
            }
            ai.setStatus(-1);
            ai.setResponse(result);
            free.save(ai);
            return result;
        }

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

        List<Tire> yearRound = em.createQuery(query, Tire.class).setParameter("season", 0)
                .setParameter("width", width).setParameter("height", height).setParameter("diameter", diameter)
                .setMaxResults(max).getResultList();

        List<Tire> summer = em.createQuery(query, Tire.class).setParameter("season", 2).setParameter("width", width)
                .setParameter("height", height).setParameter("diameter", diameter).setMaxResults(max)
                .getResultList();

        List<Tire> winter = em.createQuery(query, Tire.class).setParameter("season", 4).setParameter("width", width)
                .setParameter("height", height).setParameter("diameter", diameter).setMaxResults(max)
                .getResultList();

        List<Tire> result = new ArrayList<Tire>();
        result.addAll(yearRound);
        result.addAll(summer);
        result.addAll(winter);

        ai.setResponse(
                "year-round:" + yearRound.size() + ", summer:" + summer.size() + ", winter:" + winter.size());
        free.save(ai);

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(result.size());
        for (Tire tire : result) {
            Map<String, Object> item = new HashMap<String, Object>();
            item.put("name", tire.getName());
            item.put("price", tire.getPrice());
            item.put("season", tire.getSeasonName());
            item.put("productURL", tire.getProductURL());
            item.put("imageURL", tire.getImageURL());
            list.add(item);
        }
        return list;
    }

    @RequestMapping(value = "/mileage")
    public @ResponseBody Object mileage(@RequestParam("key") String key,
            @RequestParam(value = "type", required = false, defaultValue = "0") int type,
            HttpServletRequest request) {
        AccessItem ai = new AccessItem();
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.MILE_AGE.getId());
        ai.setRequest(key);

        int i = 0;
        key = key.replace("-", "");
        Map<String, Object> result = new HashMap<String, Object>();
        if (key.length() == 6) {
            if (type == 0) { //both autorola and marktplaats
                String jpql = "from Autorola a where a.licensePlate='" + key + "'";
                List<Autorola> list = em.createQuery(jpql, Autorola.class).getResultList();
                for (Autorola a : list) {
                    if (i == 0) {
                        result.put("brand", a.getBrand());
                        result.put("model", a.getModel());
                        result.put("licensePlate", a.getLicensePlate());
                        result.put("mileage", a.getMileAge());
                        result.put("source", "AR");
                        result.put("date", a.getScrapedAt());
                    } else {
                        result.put("mileage" + i, a.getMileAge());
                        result.put("source" + i, "AR");
                        result.put("date" + i, a.getScrapedAt());
                    }
                    i++;
                }
                jpql = "from NewMarktplaats m where m.licensePlate='" + key + "'";
                List<NewMarktplaats> list1 = em.createQuery(jpql, NewMarktplaats.class).getResultList();
                for (NewMarktplaats a : list1) {
                    if (i == 0) {
                        result.put("brand", a.getBrand());
                        result.put("model", a.getModel());
                        result.put("licensePlate", a.getLicensePlate());
                        result.put("mileage", a.getMileage());
                        result.put("source", "MP");
                        result.put("date", a.getDateScraped());
                    } else {
                        result.put("mileage" + i, a.getMileage());
                        result.put("source" + i, "MP");
                        result.put("date" + i, a.getDateScraped());
                    }
                    i++;
                }
            } else if (type == 1) { //Autorola
                String jpql = "from Autorola a where a.licensePlate='" + key + "'";
                List<Autorola> list = em.createQuery(jpql, Autorola.class).getResultList();
                for (Autorola a : list) {
                    if (i == 0) {
                        result.put("brand", a.getBrand());
                        result.put("model", a.getModel());
                        result.put("licensePlate", a.getLicensePlate());
                        result.put("mileage", a.getMileAge());
                        result.put("source", "AR");
                        result.put("date", a.getScrapedAt());
                    } else {
                        result.put("mileage" + i, a.getMileAge());
                        result.put("source" + i, "AR");
                        result.put("date" + i, a.getScrapedAt());
                    }
                    i++;
                }
            } else if (type == 2) {//NewMarktplaats
                String jpql = "from NewMarktplaats m where m.licensePlate='" + key + "'";
                List<NewMarktplaats> list1 = em.createQuery(jpql, NewMarktplaats.class).getResultList();
                for (NewMarktplaats a : list1) {
                    if (i == 0) {
                        result.put("brand", a.getBrand());
                        result.put("model", a.getModel());
                        result.put("licensePlate", a.getLicensePlate());
                        result.put("mileage", a.getMileage());
                        result.put("source", "MP");
                        result.put("date", a.getDateScraped());
                    } else {
                        result.put("mileage" + i, a.getMileage());
                        result.put("source" + i, "MP");
                        result.put("date" + i, a.getDateScraped());
                    }
                    i++;
                }
            }
        } else if (type == 0 || type == 1) {
            String jpql = "from Autorola a where a.chassisNumber='" + key + "'";
            List<Autorola> list = em.createQuery(jpql, Autorola.class).getResultList();
            for (Autorola a : list) {
                if (i == 0) {
                    result.put("brand", a.getBrand());
                    result.put("model", a.getModel());
                    result.put("licensePlate", a.getLicensePlate());
                    result.put("mileage", a.getMileAge());
                    result.put("source", "AR");
                    result.put("date", a.getScrapedAt());
                } else {
                    result.put("mileage" + i, a.getMileAge());
                    result.put("source" + i, "AR");
                    result.put("date" + i, a.getScrapedAt());
                }
                i++;
            }
        }
        if (result.size() > 0) {
            ai.setResponse("brand=" + result.get("brand") + ",model=" + result.get("model") + ",mileage="
                    + result.get("mileage") + ",licnese plate=" + result.get("licensePlate"));
            free.save(ai);
            return result;
        }
        String r = "Not found:" + key;
        ai.setResponse(r);
        ai.setStatus(0);
        free.save(ai);
        return r;
    }

    @SuppressWarnings("deprecation")
    @RequestMapping(value = "/classic_price")
    public @ResponseBody Object classicPrice(
            @RequestParam(value = "type", required = false, defaultValue = "2") int searchType,
            @RequestParam("key") String key,
            @RequestParam(value = "enableBuild", required = false, defaultValue = "true") boolean enableBuild,
            HttpServletRequest request) throws Exception {
        AccessItem ai = new AccessItem();
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.CLASSIC_PRICE.getId());
        ai.setRequest(key);
        Map<String, Object> map = new HashMap<String, Object>();

        if (!StringUtils.isEmpty(key)) {
            key = key.trim();
            if (key.length() > 4) {
                String brand, model;
                int year = 0;
                if (searchType == 2) {
                    AzureCar entity = azureService.findByLicensePlate(key);
                    year = (entity.getDateOfBuild().getYear() + 1900);
                    brand = entity.getBrand();
                    model = entity.getType();
                } else {
                    try {
                        year = Integer.parseInt(key.substring(0, 4));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    brand = model = key.substring(4);
                }

                List<ClassicCarItem> list = calculator.search(enableBuild ? year : 0, brand, model, false, true);
                if (searchType == 2 && list.size() < 5) {
                    log4j.info("Found results are less than 5, enable to search with build year range");
                    list = calculator.search(enableBuild ? year : 0, brand, model, true, true);
                }
                if (!list.isEmpty()) {
                    ClassicCarPrice price = calculator.calc(list);
                    map.put("Average", price.getAveragePrice());
                    map.put("Highest", price.getHighestPrice());
                    map.put("Lowest", price.getLowestPrice());
                    map.put("count", list.size());
                    map.put("brand", brand);
                    map.put("model", model);
                    map.put("year", year);
                }
            }
        }

        Object result = null;
        if (map.isEmpty()) {
            result = "not data";
            ai.setStatus(0);
        } else {
            result = map;
        }
        free.save(ai);
        return result;
    }

    @SuppressWarnings("deprecation")
    @RequestMapping(value = "/marktplaats_price")
    public @ResponseBody Object marktplaatsPrice(@RequestParam(value = "id", required = false) String key,
            @RequestParam(value = "brand", required = false) String brand,
            @RequestParam(value = "model", required = false) String model,
            @RequestParam(value = "build", required = false) String build,
            @RequestParam(value = "mileage", required = false) Integer mileage, HttpServletRequest request)
            throws Exception {
        AccessItem ai = new AccessItem();
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.MARKTPLAATS_PRICE.getId());
        Map<String, Object> map = new HashMap<String, Object>();

        if (key != null) {
            AzureCar entity = azureService.findByLicensePlate(key);
            build = (entity.getDateOfBuild().getYear() + 1900) + "";
            brand = entity.getBrand();
            model = entity.getType();
        }
        ai.setRequest(brand + " " + model + " " + build + " " + mileage);
        List<SortedMarktplaats> list = analyzer.search2(build, brand, model, mileage);
        if (!list.isEmpty()) {
            ClassicCarPrice price = analyzer.calc2(list);
            map.put("Average", price.getAveragePrice());
            map.put("Highest", price.getHighestPrice());
            map.put("Lowest", price.getLowestPrice());
            map.put("count", list.size());
            map.put("brand", brand);
            map.put("model", model);
            map.put("year", build);
        }

        Object result = null;
        if (map.isEmpty()) {
            result = "not data";
            ai.setStatus(0);
            ai.setResponse(null);
        } else {
            result = map;
            ai.setResponse(map.get("Average") + "");
        }
        free.save(ai);
        return result;
    }

    @SuppressWarnings("deprecation")
    @RequestMapping(value = "/autoscout_price")
    public @ResponseBody Object autoscoutPrice(@RequestParam(value = "id", required = false) String key,
            @RequestParam(value = "brand", required = false) String brand,
            @RequestParam(value = "model", required = false) String model,
            @RequestParam(value = "build", required = false) String build,
            @RequestParam(value = "mileage", required = false) Integer mileage, HttpServletRequest request)
            throws Exception {
        AccessItem ai = new AccessItem();
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.AUTOSCOUT_PRICE.getId());
        ai.setRequest(brand + " " + model + " " + mileage);
        Map<String, Object> map = new HashMap<String, Object>();

        if (key != null) {
            AzureCar entity = azureService.findByLicensePlate(key);
            build = (entity.getDateOfBuild().getYear() + 1900) + "";
            brand = entity.getBrand();
            model = entity.getType();
        }
        List<AutoscoutNl> list = autoScountanalyzer.search(build, brand, model, mileage);
        if (!list.isEmpty()) {
            ClassicCarPrice price = autoScountanalyzer.calc(list);
            map.put("Average", price.getAveragePrice());
            map.put("Highest", price.getHighestPrice());
            map.put("Lowest", price.getLowestPrice());
            map.put("count", list.size());
            map.put("brand", brand);
            map.put("model", model);
            map.put("year", build);
        }

        Object result = null;
        if (map.isEmpty()) {
            result = "not data";
            ai.setStatus(0);
            ai.setResponse(null);
        } else {
            result = map;
            ai.setResponse(map.get("Average") + "");
        }
        free.save(ai);
        return result;
    }

    //it is using German car data
    @RequestMapping(value = "/scout_de_price")
    public @ResponseBody Object autoscoutPrice2(@RequestParam(value = "brand") String brand,
            @RequestParam(value = "model") String model,
            @RequestParam(value = "build", required = false) String build,
            @RequestParam(value = "fuelType", required = false) String fuelType,
            @RequestParam(value = "mileage", required = false) Integer mileage,
            @RequestParam(value = "horsepower", required = false) Integer horsepower,
            @RequestParam(value = "engineSize", required = false) Integer engineSize, HttpServletRequest request)
            throws Exception {
        AccessItem ai = new AccessItem();
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.AUTOSCOUT_DE_PRICE.getId());
        ai.setRequest(brand + " " + model + " " + mileage);
        Map<String, Object> map = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(fuelType)) {
            fuelType = fuelType.toLowerCase();
            if ("petrol".equals(fuelType) || "gasoline".equals(fuelType) || "benzine".equals(fuelType)) {
                fuelType = "Benzin";
            }
        }

        List<AutoscoutDe> list = autoscoutDeAnalyzer.search(build, brand, model, engineSize, engineSize, fuelType,
                null, mileage, horsepower, null, null);
        if (list.isEmpty() && (horsepower != null || engineSize != null)) {
            list = autoscoutDeAnalyzer.search(build, brand, model, null, null, fuelType, null, mileage, null, null,
                    null);
        }
        if (!list.isEmpty()) {
            ClassicCarPrice price = autoscoutDeAnalyzer.calc(list);
            map.put("Average", price.getAveragePrice());
            map.put("Highest", price.getHighestPrice());
            map.put("Lowest", price.getLowestPrice());
            map.put("count", list.size());
            map.put("brand", brand);
            map.put("model", model);
        }

        Object result = null;
        if (map.isEmpty()) {
            result = "not data";
            ai.setStatus(0);
            ai.setResponse(null);
        } else {
            result = map;
            ai.setResponse(map.get("Average") + "");
        }
        free.save(ai);
        return result;
    }

    @RequestMapping(value = "/stadagen")
    public @ResponseBody Object stadagen(@RequestParam("id") String id, HttpServletRequest request)
            throws Exception {
        AccessItem ai = new AccessItem();
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.STADAGEN.getId());

        AzureCar entity = azureService.findByLicensePlate(id);
        String brand = entity.getBrand();
        String model = entity.getType();

        @SuppressWarnings("deprecation")
        String build = (entity.getDateOfBuild().getYear() + 1900) + "";

        ai.setRequest(id);
        int avgDays = mService.averageTimeOnSaleByLucene2(brand, model, build);
        if (avgDays == 0) {
            ai.setStatus(0);
        }
        ai.setResponse(avgDays + " days");
        free.save(ai);
        return avgDays;
    }

    @RequestMapping(value = "/daysOnSale")
    public @ResponseBody Object daysOnSale(@RequestParam("id") String id, HttpServletRequest request)
            throws Exception {

        AccessItem ai = new AccessItem();
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.DAYS_ON_SALE.getId());
        ai.setRequest(id);

        List<NewMarktplaats> list = mService.findEntityByLicensePlate(id);

        List<NewMarktplaats> unremoved = new ArrayList<NewMarktplaats>();
        for (NewMarktplaats marktplaats : list) {
            if (marktplaats.getDateRemoved().equals("0000-00-00")) {
                unremoved.add(marktplaats);
            }
        }

        Object result = null;
        if (!unremoved.isEmpty()) {
            Collections.sort(unremoved, new Comparator<NewMarktplaats>() {

                @Override
                public int compare(NewMarktplaats o1, NewMarktplaats o2) {
                    return o2.getAdDate().compareTo(o1.getAdDate());
                }

            });
            int msPerDay = 24 * 60 * 60 * 1000;
            int days = (int) ((new Date().getTime() - DateUtil.string2Date1(unremoved.get(0).getAdDate()).getTime())
                    / msPerDay);
            result = days;
            ai.setResponse(days + " days");
        } else {
            ai.setStatus(0);
            result = "no data";
        }

        free.save(ai);
        return result;
    }

    @Transactional(readOnly = true)
    @RequestMapping(value = "/euroncap")
    public @ResponseBody Object euroncap(@RequestParam("id") String id, HttpServletRequest request)
            throws Exception {
        AccessItem ai = new AccessItem();
        ai.setRequest(id);
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.EURONCAP.getId());

        AzureCar entity = null;

        if (!StringUtils.isEmpty(id)) {
            entity = azureService.findByLicensePlate(id.trim());
        }

        Object result = null;
        if (entity != null) {

            String brand = entity.getBrand();
            String model = entity.getType();
            @SuppressWarnings("deprecation")
            String build = (entity.getDateOfBuild().getYear() + 1900) + "";

            Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
            FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
            QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Euroncap.class)
                    .get();

            String s = "0, 0, 0";
            String[] boostArray = null;
            boostArray = s.split(",");

            @SuppressWarnings("rawtypes")
            BooleanJunction bj = qb.bool();
            if (brand != null) {
                QueryParser parser = new QueryParser(Version.LUCENE_36, "brand", analyzer);
                LuceneUtil.setBoost(0, boostArray, parser.parse(brand), bj);
            }

            if (model != null) {
                try {
                    model = ss.find(model, "1");
                } catch (Exception e1) {
                    log4j.warn(e1.getMessage(), e1);
                }
                QueryParser parser = new QueryParser(Version.LUCENE_36, "model", analyzer);
                LuceneUtil.setBoost(1, boostArray, parser.parse(model), bj);
            }

            if (build != null) {
                @SuppressWarnings("rawtypes")
                BooleanJunction rangebj = qb.bool();
                Query range = rangebj.must(new TermRangeQuery("buildFrom", "0000", build, true, true))
                        .must(new TermRangeQuery("buildTo", build, "9999", true, true)).createQuery();
                LuceneUtil.setBoost(2, boostArray, range, bj);
            }

            Query query = bj.createQuery();
            FullTextQuery persistenceQuery = fullTextEntityManager.createFullTextQuery(query, Euroncap.class);
            persistenceQuery.setMaxResults(15);

            @SuppressWarnings("unchecked")
            List<Euroncap> list = persistenceQuery.getResultList();

            if (list.isEmpty()) {
                result = "no data";
                ai.setStatus(0);
                ai.setResponse(null);
            } else {

                Euroncap nearestEu = list.get(0);

                if (entity.getDateOfBuild() != null) {
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(entity.getDateOfBuild());
                    int year = cal.get(Calendar.YEAR);
                    int distance = 3000;
                    for (Euroncap eu : list) {
                        int y = 0;
                        try {
                            y = Integer.parseInt(eu.getBuild());
                        } catch (Exception e) {
                        }

                        int d = Math.abs(year - y);
                        if (d < distance) {
                            distance = d;
                            nearestEu = eu;
                        }
                    }
                }
                result = nearestEu;
                ai.setResponse(nearestEu.toString());

            }
        } else {
            String r = "Not found:" + id;
            ai.setResponse(r);
            result = r;
            ai.setStatus(0);
        }

        free.save(ai);
        return result;
    }

    /*
     * Average Mileage of Marktplaat
     * see MarktplaatsController.mileage
     */
    @Transactional(readOnly = true)
    @RequestMapping(value = "/marktplaats_mileage")
    public @ResponseBody Object mileage(@RequestParam("id") String id, HttpServletRequest request)
            throws Exception {
        AccessItem ai = new AccessItem();
        ai.setRequest(id);
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.MARKTPLAATS_MILEAPGE.getId());

        AzureCar entity = null;
        Object result = null;
        Integer avg = null;

        if (!StringUtils.isEmpty(id)) {
            entity = azureService.findByLicensePlate(id.trim());
        }
        if (entity != null) {
            String brand = entity.getBrand();
            String _model = entity.getType();

            @SuppressWarnings("deprecation")
            String build = (entity.getDateOfBuild().getYear() + 1900) + "";
            String fuelType = entity.getFuelType();

            avg = mService.mileageByLuceneForAPI2(brand, _model, build, fuelType);
        }

        if (avg == null || avg == 0) {
            String r = "no data";
            ai.setResponse(r);
            result = r;
            ai.setStatus(0);
        } else {
            result = avg;
            ai.setResponse(result + "");
        }

        free.save(ai);
        return result;
    }

    /*
     * brand top mileage
     */
    @RequestMapping(value = "/brand_mileage")
    public @ResponseBody Object brandMileage(@RequestParam("id") String id, HttpServletRequest request)
            throws Exception {
        AccessItem ai = new AccessItem();
        ai.setRequest(id);
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.BRAND_MILEAPGE.getId());

        AzureCar entity = null;
        Object result = null;
        Integer avg = null;

        if (!StringUtils.isEmpty(id)) {
            entity = azureService.findByLicensePlate(id.trim());
        }
        if (entity != null) {
            String brand = entity.getBrand();

            @SuppressWarnings("deprecation")
            int build = (entity.getDateOfBuild().getYear() + 1900);
            if (build >= Calendar.getInstance().get(Calendar.YEAR) - 16) {
                String fuelType = entity.getFuelType();
                avg = topMileage.topMileage(brand, fuelType);
            }
        }

        if (avg == null) {
            String r = "Not found:" + id;
            ai.setResponse(r);
            result = r;
            ai.setStatus(0);
        } else {
            result = avg.intValue();
            ai.setResponse(result + "");
        }

        free.save(ai);
        return result;
    }

    /*
     * car owner history
     */
    @RequestMapping(value = "/car_owner")
    public @ResponseBody Object carOwner(@RequestParam("id") String id, HttpServletRequest request) {
        AccessItem ai = new AccessItem();
        ai.setRequest(id);
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.CAR_OWNER_HISTORY.getId());
        String result = null;

        List<CarOwner> results = null;
        try {
            results = cos.search(id);
            if (results.size() > 0) {
                CarOwner co = results.get(0);
                if (co != null && co.getScrapedName() != null) {
                    List<ReviewInfo> list = rs.search(co.getScrapedName(), co.getCity());
                    rs.assemble1(list, co);
                }
            }
        } catch (Exception e) {
            ai.setStatus(-1);
            log4j.info(e.getMessage(), e);
        }
        if (results == null || results.isEmpty()) {
            result = "no data";
            ai.setStatus(0);
        } else {
            result = results.get(0).getName() + "...";
        }
        ai.setResponse(result);
        free.save(ai);
        return results;
    }

    /*
     * car review
     */
    @RequestMapping(value = "/car_company_review")
    public @ResponseBody Object review(@RequestParam("company") String company, @RequestParam("city") String city,
            HttpServletRequest request) {
        AccessItem ai = new AccessItem();
        ai.setRequest(company + " " + city);
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.CAR_COMPANY_REVIEW_BY_COMPANY.getId());

        Object result = null;
        try {
            List<ReviewInfo> list = rs.search(company, city);
            result = rs.assemble(list);
        } catch (Exception e) {
            ai.setStatus(-1);
            log4j.info(e.getMessage(), e);
        }
        if (result == null) {
            result = "no data";
            ai.setStatus(0);
        }
        ai.setResponse("");
        free.save(ai);
        return result;
    }

    /*
     * car review by license plate
     */
    @RequestMapping(value = "/car_company_review2")
    public @ResponseBody Object review(@RequestParam("id") String id, HttpServletRequest request) {
        AccessItem ai = new AccessItem();
        ai.setRequest(id);
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.CAR_COMPANY_REVIEW_BY_LICENSEPLATE.getId());

        Object result = null;
        try {
            CarOwner co = cos.searchLatestOne(id);
            if (co != null && co.getScrapedName() != null) {
                List<ReviewInfo> list = rs.search(co.getScrapedName(), co.getCity());
                rs.assemble1(list, co);
                result = co;
            }
        } catch (Exception e) {
            ai.setStatus(-1);
            log4j.info(e.getMessage(), e);
        }
        if (result == null) {
            result = "no data";
            ai.setStatus(0);
        }
        ai.setResponse(result + "");
        free.save(ai);
        return result;
    }

    /*
     * car review by license plate
     */
    @RequestMapping(value = "/car_manual")
    public @ResponseBody Object carManual(@RequestParam("brand") String brand, @RequestParam("model") String model,
            HttpServletRequest request, HttpServletResponse resp) {
        resp.setHeader("Access-Control-Allow-Origin", "*");
        AccessItem ai = new AccessItem();
        ai.setRequest(brand + " " + model);
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.CAR_MANUAL.getId());

        final String address = request.getLocalAddr();
        final int port = request.getLocalPort();
        String portStr = port == 80 ? "" : ":" + port;
        String prex = "http://" + address + portStr + "/solidtrust/images/";

        Object result = null;
        try {
            List<Manual> list = manualRep.findByBrandAndModel(brand, model);
            if (list.size() > 0) {
                siddur.solidtrust.manual.Car car = new siddur.solidtrust.manual.Car();
                Manual m = list.get(0);
                car.setAcceleration(m.getAcceleration());
                car.setAverageMileage(m.getAverageMileage());
                car.setBrand(m.getBrand());
                car.setBuildFrom(m.getBuildFrom());
                car.setBuildTo(m.getBuildTo());
                car.setImage(prex + m.getImage() + "_1.jpg");
                car.setModel(m.getModel());
                car.setNewPrice(m.getNewPrice());
                car.setRoadTax(m.getRoadTax());
                car.setTireSize(m.getTireSize());
                car.setTopspeed(m.getTopspeed());
                for (Manual manual : list) {
                    String[] files = manual.getManual().split(";");
                    for (String f : files) {
                        ManualItem mi = new ManualItem();
                        mi.setBuildYear(manual.getBuild());
                        mi.setImage(prex + manual.getImage() + "_1.jpg");
                        mi.setUrl(f);
                        car.getManual().add(mi);
                    }
                }
                result = car;
            }
        } catch (Exception e) {
            ai.setStatus(-1);
            log4j.info(e.getMessage(), e);
        }
        if (result == null) {
            result = "no data";
            ai.setStatus(0);
        }
        ai.setResponse(result + "");
        free.save(ai);
        return result;
    }

    @RequestMapping("/car_info")
    public @ResponseBody Object carinfo(@RequestParam(value = "startId", required = true) Integer startId,
            @RequestParam(value = "pageSize", required = false, defaultValue = "12") Integer pageSize,
            HttpServletRequest request) {
        AccessItem ai = new AccessItem();
        ai.setRequest(request.getQueryString());
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.CAR_INFO.getId());
        CarInfoPage result = carinfoService.sync(startId, pageSize);
        if (result.getResults().isEmpty()) {
            ai.setStatus(0);
        } else {
            ai.setResponse(result.getResults().size() + "");
        }
        free.save(ai);
        return result;
    }

    @RequestMapping("/anw")
    public @ResponseBody Object anw(@RequestParam(value = "brand", required = true) String brand,
            @RequestParam(value = "model", required = false) String model,
            @RequestParam(value = "build", required = false) String build, HttpServletRequest request)
            throws Exception {
        AccessItem ai = new AccessItem();
        ai.setRequest(request.getQueryString());
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.ANW.getId());
        List<ANWBautotests> result = anwService.searchByLucene(brand, model, build);
        if (result.isEmpty()) {
            ai.setStatus(0);
        } else {
            ai.setResponse(result.size() + "");
        }
        free.save(ai);
        return result;
    }

    @RequestMapping("/trader")
    public @ResponseBody Object trader(@RequestParam(value = "brand", required = true) String brand,
            @RequestParam(value = "model", required = false) String model, HttpServletRequest request)
            throws Exception {
        AccessItem ai = new AccessItem();
        ai.setRequest(request.getQueryString());
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.AUTOTRADER.getId());
        List<Autotrader> result = traderService.searchByLucene(brand, model);
        if (result.isEmpty()) {
            ai.setStatus(0);
        } else {
            ai.setResponse(result.size() + "");
        }
        free.save(ai);
        return result;
    }

    @RequestMapping("/cost")
    public @ResponseBody Object cost(String id, HttpServletRequest request) throws Exception {
        AccessItem ai = new AccessItem();
        ai.setRequest(request.getQueryString());
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.CAR_COST.getId());

        CarCost cc = carCostService.search(id);
        Object result = null;
        if (cc == null) {
            result = "no data";
            ai.setResponse("no data");
        } else {
            result = cc;
            ai.setResponse(cc.getBrand() + ", " + cc.getModel() + ", " + cc.getBuild());
        }
        free.save(ai);
        return result;
    }

    @RequestMapping("/logo")
    public @ResponseBody Object logo(String key, String type, HttpServletRequest request) throws Exception {
        AccessItem ai = new AccessItem();
        ai.setRequest(request.getQueryString());
        ai.setIp(request.getRemoteHost());
        ai.setUsername(request.getAttribute(SolidtrustConstants.CLIENT_ID) + "");
        ai.setService(Product.LOGO.getId());

        String logo = logoService.find(key, "plate".equals(type));
        ai.setResponse(logo);
        free.save(ai);
        return logo;
    }
}