コード例 #1
0
  @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;
  }
コード例 #2
0
  @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;
  }