@Override @Transactional public void updatePart(PartModel partModel, String modelNumber) { log.debug("Update Part model: {}", partModel); if (partModel == null) { throw new IllegalArgumentException("Cannot update null Part"); } try { Part part = bindPart(partModel); log.debug("Update part: part: {}", part); if (modelNumber != null) { try { HeliParts heliParts = null; Heli heli = entityManager.find(Heli.class, modelNumber); if (heli != null) { heliParts = heliPartsService.getHeliPart(modelNumber, part.getPartNumber()); if (heliParts == null) { log.debug("Update part heli: {}", heli); log.debug("Update part helipart: {}", heliParts); heliPartsService.createHeliPart(heli, part); } } } catch (NoResultException e) { log.debug("Update part error: {}", e.getMessage()); } } log.debug("Part updated in database: {}", part); entityManager.merge(part); } catch (Exception e) { throw new IllegalArgumentException(e.getMessage()); } }
private PartModel bindPartModel(Part part) { PartModel model = null; try { if (part != null && part.getPartNumber().length() > 0) { model = new PartModel(); model.setCountNeeded(part.getCountNeeded()); model.setName(part.getName()); model.setPartNumber(part.getPartNumber()); if (part.getPhoto() != null) { model.setPhoto(part.getPhoto()); } model.setStockCount(part.getStockCount()); } } catch (NullPointerException e) { throw new IllegalArgumentException(e.getMessage()); } return model; }
private Part bindPart(PartModel model) { Part part = null; try { if (model != null && model.getPartNumber().length() > 0) { part = entityManager.find(Part.class, model.getPartNumber()); if (part != null) { part.setName(model.getName()); part.setCountNeeded(model.getCountNeeded()); part.setStockCount(model.getStockCount()); if (model.getPhoto() != null) { part.setPhoto(model.getPhoto()); } part.setPhoto(model.getPhoto()); } else if (part == null) { part = new Part(); part.setName(model.getName()); part.setPartNumber(model.getPartNumber()); part.setCountNeeded(model.getCountNeeded()); part.setStockCount(model.getStockCount()); if (model.getPhoto() != null) { part.setPhoto(model.getPhoto()); } part.setPhoto(model.getPhoto()); } } } catch (NullPointerException e) { throw new IllegalArgumentException(e.getMessage()); } return part; }