@Override public GearBoxOil fillPrices(GearBoxOil oil) { GearBoxOil result = new GearBoxOil(); GearBoxOil currentOil = getGearBoxOilByName(oil.getName()); if (currentOil.getId() > 0) { String sqlUpdate = "update gearboxoils set price=:price where id=:id"; MapSqlParameterSource params = new MapSqlParameterSource(); params.addValue("id", oil.getId()); params.addValue("price", oil.getPrice()); KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(sqlUpdate, params, keyHolder); try { if (keyHolder.getKey() != null) { result = getGearBoxOil(keyHolder.getKey().intValue()); } } catch (EmptyResultDataAccessException e) { result = new GearBoxOil(); } } return result; }
@Override public GearBoxOil createGearBoxOil(GearBoxOil oil) { GearBoxOil result = new GearBoxOil(); GearBoxOil currentOil = null; if (oil.getId() > 0) { currentOil = getGearBoxOil( oil .getId()); // если это редактирование, в структуре уже будет Id. ТОгда // удостоверимся, что такой элемент есть в БД } else { currentOil = getGearBoxOilByName(oil.getName()); } String sqlUpdate = "insert into gearboxoils (name, description, gearboxtype, oilstuff, manufacturer, judgement, photo, price" + ", specification, value, viscosity, discount, instock) Values (:name, :description, :gearboxtype, :oilstuff, :manufacturer, :judgement, :photo, :price" + ", :specification, :value, :viscosity, :discount, :instock)"; MapSqlParameterSource params = new MapSqlParameterSource(); if (currentOil.getId() > 0) { // В БД есть такой элемент sqlUpdate = "update gearboxoils set name=:name, description=:description, gearboxtype=:gearboxtype, oilstuff=:oilstuff, judgement=:judgement" + ", manufacturer=:manufacturer, photo=:photo, price=:price, specification=:specification, value=:value" + ", viscosity=:viscosity, discount=:discount, instock=:instock where id=:id"; params.addValue("id", oil.getId()); } params.addValue("name", oil.getName()); params.addValue("description", oil.getDescription()); params.addValue("gearboxtype", ((GearBoxType) oil.getGearBoxType()).getId()); params.addValue("oilstuff", ((OilStuff) oil.getOilStuff()).getId()); params.addValue("judgement", oil.getJudgement()); params.addValue("manufacturer", ((Manufacturer) oil.getManufacturer()).getId()); params.addValue("photo", oil.getPhoto()); params.addValue("price", oil.getPrice()); params.addValue("specification", oil.getSpecification()); params.addValue("value", oil.getValue()); params.addValue("viscosity", oil.getViscosity()); params.addValue("discount", oil.getDiscount()); params.addValue("instock", oil.getInStock()); KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(sqlUpdate, params, keyHolder); try { if (keyHolder.getKey() != null) { result = getGearBoxOil(keyHolder.getKey().intValue()); } } catch (EmptyResultDataAccessException e) { result = new GearBoxOil(); } return result; }