@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 mapRow(ResultSet rs, int rowNum) throws SQLException { GearBoxOil oil = new GearBoxOil(); oil.setId(rs.getInt("id")); oil.setName(rs.getString("name")); oil.setDescription(rs.getString("description")); oil.setSpecification(rs.getString("Specification")); oil.setJudgement(rs.getDouble("judgement")); Manufacturer manufacturer = new Manufacturer(); manufacturer.setId(rs.getInt("manufacturer")); manufacturer.setName(rs.getString("man_name")); oil.setManufacturer(manufacturer); OilStuff oilStuff = new OilStuff(); oilStuff.setId(rs.getInt("oilStuff")); oilStuff.setName(rs.getString("os_name")); oil.setOilStuff(oilStuff); GearBoxType gearBoxType = new GearBoxType(); gearBoxType.setId(rs.getInt("gearBoxType")); gearBoxType.setName(rs.getString("gbt_name")); oil.setGearBoxType(gearBoxType); oil.setPhoto(rs.getString("photo")); oil.setPrice(rs.getDouble("price")); oil.setValue(rs.getDouble("value")); oil.setViscosity(rs.getString("viscosity")); oil.setDiscount(rs.getDouble("discount")); oil.setInStock(rs.getInt("instock")); return oil; }
@Override public GearBoxOil createGearBoxOilWithoutPrice(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" + ", specification, value, viscosity, discount, instock, manufacturer_code) " + " Values (:name, :description, :gearboxtype, :oilstuff, :manufacturer, :judgement, :photo" + ", :specification, :value, :viscosity, :discount, :instock, :manufacturer_code)"; 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, specification=:specification, value=:value" + ", viscosity=:viscosity, discount=:discount, instock=:instock, manufacturer_code=:manufacturer_code 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("specification", oil.getSpecification()); params.addValue("value", oil.getValue()); params.addValue("viscosity", oil.getViscosity()); params.addValue("discount", oil.getDiscount()); params.addValue("instock", oil.getInStock()); params.addValue("manufacturer_code", oil.getManufacturerCode()); 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; }