@Override @Transactional public void save(ResourcePropEntity entity) { if (StringUtils.isBlank(entity.getId())) { resourcePropDao.save(entity); } else { resourcePropDao.merge(entity); } }
private void mergeAttribute(final ResourceEntity bean, final ResourceEntity dbObject) { /* * if the incoming bean is from the database, there is no reason to do any merging * This was written to avoid merging of attributes when you call findById on the resourceService, * and then save the same object (see ManagedSystemServiceImpl.updateMangagedSys) */ if (bean.getResourceProps() != null && bean.getResourceProps() instanceof PersistentCollection) { return; } Set<ResourcePropEntity> beanProps = (bean.getResourceProps() != null) ? bean.getResourceProps() : new HashSet<ResourcePropEntity>(); Set<ResourcePropEntity> dbProps = (dbObject.getResourceProps() != null) ? new HashSet<ResourcePropEntity>(dbObject.getResourceProps()) : new HashSet<ResourcePropEntity>(); /* update */ Iterator<ResourcePropEntity> dbIteroator = dbProps.iterator(); while (dbIteroator.hasNext()) { final ResourcePropEntity dbProp = dbIteroator.next(); boolean contains = false; for (final ResourcePropEntity beanProp : beanProps) { if (StringUtils.equals(dbProp.getId(), beanProp.getId())) { dbProp.setValue(beanProp.getValue()); dbProp.setElement(getEntity(beanProp.getElement())); dbProp.setName(beanProp.getName()); dbProp.setIsMultivalued(beanProp.getIsMultivalued()); // renewedProperties.add(dbProp); contains = true; break; } } /* remove */ if (!contains) { dbIteroator.remove(); } } /* add */ final Set<ResourcePropEntity> toAdd = new HashSet<>(); for (final ResourcePropEntity beanProp : beanProps) { boolean contains = false; dbIteroator = dbProps.iterator(); while (dbIteroator.hasNext()) { final ResourcePropEntity dbProp = dbIteroator.next(); if (StringUtils.equals(dbProp.getId(), beanProp.getId())) { contains = true; } } if (!contains) { beanProp.setResource(bean); beanProp.setElement(getEntity(beanProp.getElement())); toAdd.add(beanProp); } } dbProps.addAll(toAdd); bean.setResourceProps(dbProps); }