public void mergeAttributes(ProductRelease product, ProductRelease templateProduct) { if (product.getAttributes() != null) { if ((templateProduct.getAttributes() != null) && (!templateProduct.getAttributes().isEmpty())) { for (Attribute attribute : product.getAttributes()) { String key = attribute.getKey(); boolean notExistInTemplate = templateProduct.getAttribute(key) == null; if (notExistInTemplate) { templateProduct.addAttribute(attribute); } else { templateProduct.getAttribute(key).setValue(attribute.getValue()); } } } else { templateProduct.setAttributes(product.getAttributes()); } } }
public ProductRelease loadFromSDCAndCreate(String name, ClaudiaData data) throws EntityNotFoundException { log.info("Sync product release " + name); ProductRelease productRelease; String product = name.split("-")[0]; String version = name.split("-")[1]; try { log.info("Loading from sdc " + product + " " + version); ProductRelease pRelease = productReleaseSdcDao.load(product, version, data); try { productRelease = productReleaseDao.load(name); HashSet<Attribute> attributesCopy = new HashSet<Attribute>(productRelease.getAttributes()); HashSet<Metadata> metatadasCopy = new HashSet<Metadata>(productRelease.getMetadatas()); productReleaseDao.removeAttributesAndMetadatas(productRelease); // remove for (Attribute attribute : attributesCopy) { attributeDao.remove(attribute); } for (Metadata metadata : metatadasCopy) { metadataDao.remove(metadata); } // add new attributes and metadatas for (Attribute attribute : pRelease.getAttributes()) { Attribute newAttribute = attributeDao.create(attribute); productRelease.addAttribute(newAttribute); } for (Metadata metadata : pRelease.getMetadatas()) { Metadata newMetadata = metadataDao.create(metadata); productRelease.addMetadata(newMetadata); } productReleaseDao.update(productRelease); } catch (Exception ex) { log.info("Product don't exist in database: creates"); productRelease = create(pRelease); } } catch (EntityNotFoundException e5) { String msg = "Product release NOT found in SDC: " + name; log.warn(msg); throw new EntityNotFoundException(ProductRelease.class, msg, name); } catch (SdcException e6) { String msg = "Failure at loading ProductRelease from SDC" + name + " " + e6.getMessage(); log.warn(msg); throw new EntityNotFoundException(ProductRelease.class, msg, name); } catch (InvalidEntityException e7) { String msg = "Failure creating new ProductRelease " + name + " " + e7.getMessage(); log.warn(msg); throw new EntityNotFoundException(ProductRelease.class, msg, name); } catch (AlreadyExistsEntityException e8) { String msg = "Failure creating ProductRelease " + name + " " + e8.getMessage(); log.warn(msg); throw new EntityNotFoundException(ProductRelease.class, msg, name); } return productRelease; }