Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package data.services; import data.dao.CarColorValueDao; import data.dao.CarDao; import data.dao.CarOptionValueDao; import data.dao.FeatureDao; import data.dao.FreeOptionDao; import data.dao.PropertyNameDao; import data.entity.Car; import data.entity.CarColorValue; import data.entity.CarOptionValue; import data.entity.Feature; import data.entity.FreeOption; import data.entity.Mark; import data.entity.Model; import data.entity.PropertyName; import data.services.parent.PrimService; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.ScopedProxyMode; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import support.StringAdapter; /** * * @author bezdatiuzer */ @Service @Transactional @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS) public class CarService extends PrimService { @Autowired private CarDao carDao; @Autowired private CarOptionValueDao carOptionValueDao; @Autowired private CarColorValueDao carColorValueDao; @Autowired private FeatureDao featureDao; @Autowired private FreeOptionDao freeOptionDao; @Autowired private PropertyNameDao propertyNameDao; public List<Car> getCars() { return carDao.getAllAsc("title"); } public void create(Car car) throws Exception { Car newCar = new Car(); Double price = car.getCmPrice(); Long mediaId = car.getMediaId(); Long cmgqId = car.getCmgqId(); Long cmsqId = car.getCmsqId(); Long cmsgqId = car.getCmsgqId(); Long qutoId = car.getCmqId(); List<CarColorValue> ccvs = car.getCarColorValues(); if (ccvs == null) { ccvs = new ArrayList(); } List<CarOptionValue> covs = car.getCarOptionValues(); if (covs == null) { covs = new ArrayList(); } List<PropertyName> pns = car.getPropertyNames(); if (pns == null) { pns = new ArrayList(); } Model m = car.getModel(); String url = car.getUrl(); if (url == null) { url = ""; } String title = car.getTitle(); if (title == null || title.equals("")) { title = "? ?"; } String complTitle = car.getCompletionTitle(); if (complTitle == null || complTitle.equals("")) { complTitle = "? ?"; } List<FreeOption> fos = car.getFreeOptions(); if (fos == null) { fos = new ArrayList(); } List<Feature> fs = car.getFeatures(); if (fs == null) { fs = new ArrayList(); } newCar.setCarColorValues(ccvs); newCar.setCarOptionValues(covs); newCar.setCmgqId(cmgqId); newCar.setCmsgqId(cmsgqId); newCar.setCmsqId(cmsqId); newCar.setCompletionTitle(complTitle); newCar.setFeatures(fs); newCar.setFreeOptions(fos); newCar.setIsNew(car.isIsNew()); newCar.setMediaId(mediaId); newCar.setPropertyNames(pns); newCar.setModel(m); newCar.setTitle(title); newCar.setUrl(url); newCar.setCmqId(qutoId); newCar.setCmPrice(price); if (carDao.find(newCar).isEmpty()) { if (validate(car)) { carDao.save(car); } } } public void updateFeaturesInCar(List<Feature> flist, Long oldCarId) { Car car = new Car(); car.setCmqId(oldCarId); List<Car> clist = carDao.find(car); if (!flist.isEmpty()) { if (!clist.isEmpty()) { Car newCar = clist.get(0); newCar.setFeatures(flist); carDao.update(newCar); } else { addError(" ? QUTO- " + oldCarId + " "); } } } public void updatePropertiesInCar(List<PropertyName> propertyNames, Long oldCarId) { Car car = new Car(); car.setCmqId(oldCarId); List<Car> clist = carDao.find(car); if (!clist.isEmpty()) { Car newCar = clist.get(0); newCar.setPropertyNames(propertyNames); carDao.update(newCar); } else { addError(" ? QUTO- " + oldCarId + " "); } } public List<Car> getCarsWithBrandsLesserPrice(List<Mark> mlist, BigDecimal price) { if (mlist.isEmpty()) { return carDao.getCarLesserPrice(price); } else { List<Car> clist = new ArrayList(); for (Mark m : mlist) { for (Model mod : m.getModels()) { for (Car c : mod.getCars()) { if (BigDecimal.valueOf(c.getCmPrice()).compareTo(price) <= 0 && BigDecimal.valueOf(c.getCmPrice()).compareTo(BigDecimal.valueOf(0)) > 0) { clist.add(c); } } } } return clist; } } public Car findByOldId(Long oldId) throws Exception { List<Car> list = carDao.findById(oldId); if (!list.isEmpty()) { //if(list.size()==1){ return list.get(0); /*}else{ addError(" quto: "+oldId); }*/ } else { addError(" "); } throw new Exception(" ? : " + oldId + " "); //return null; } public void create(CarOptionValue cov) throws Exception { if (cov != null) { CarOptionValue newCov = new CarOptionValue(); newCov.setOldId(cov.getOldId()); if (carOptionValueDao.find(newCov).isEmpty()) { if (validate(cov)) { carOptionValueDao.save(cov); } else { throw new Exception("id=" + cov.getOldId() + "ccoid=" + cov.getCcoOldId() + "error: " + StringAdapter.getStringFromList(getResult().getErrors())); } } } else { addError("cov=null!"); } } public void update(CarOptionValue cov) throws Exception { if (cov != null) { CarOptionValue supCov = new CarOptionValue(); supCov.setOldId(cov.getOldId()); List<CarOptionValue> suplist = carOptionValueDao.find(supCov); if (!suplist.isEmpty()) { if (validate(cov)) { CarOptionValue newCov = suplist.get(0); newCov.setPrice(cov.getPrice()); newCov.setTitle(cov.getTitle()); carOptionValueDao.update(newCov); } /*else{ throw new Exception("id="+cov.getOldId()+"ccoid="+cov.getCcoOldId()+"error: "+StringAdapter.getStringFromList(getResult().getErrors())); }*/ } } } public void delete(Car car) { for (CarColorValue ccv : car.getCarColorValues()) { carColorValueDao.delete(ccv); } for (CarOptionValue cov : car.getCarOptionValues()) { carOptionValueDao.delete(cov); } for (FreeOption fo : car.getFreeOptions()) { freeOptionDao.delete(fo); } for (PropertyName pn : car.getPropertyNames()) { propertyNameDao.delete(pn); } car.setFeatures(new ArrayList()); carDao.update(car); carDao.delete(car); } /* public List<Feature> getFeatures(Car car){ List<Feature> res = car.getFeatures(); Feature f = new Feature(); f.setCmgId(car.getCmsqId()); List<Feature> fl = featureDao.find(f); if(!fl.isEmpty()){ for(Feature f1:fl){ if(!res.contains(f1)){ res.add(f1); } } } return res; }*/ /*public List<Feature> getFeatures(Car car){ List<Feature> res = car.getFeatures(); ArrayList<String> uidList = new ArrayList(); for(Feature f:res){ uidList.add(f.getUid()); } //HashSet<String> uidSet = new HashSet(); //Feature f = new Feature(); //f.setCmgId(car.getCmsqId()); //List<Feature> fl = featureDao.find(f); List<Feature> fl = featureDao.getByCmgId(car.getCmsqId()); if(!fl.isEmpty()){ for(Feature f1:fl){ if(!uidList.contains(f1.getUid())){ res.add(f1); uidList.add(f1.getUid()); } } } return res; }*/ //public void updatePropertiesInCar () }