@Override public SiteEntity create(SiteEntity entity) { Preconditions.checkNotNull(entity.getSiteId(), "SiteId is null: " + entity.getSiteId()); List<SiteEntity> entities = new ArrayList<>(1); entity.ensureDefault(); entities.add(entity); try { queryService.insert(insertSql, entities, new SiteEntityToRelation()); } catch (SQLException e) { LOGGER.error("Error to insert SiteEntity: {}", entity, e); throw new IllegalArgumentException("Error to insert SiteEntity: " + entity + e); } return entity; } }
@Override public SiteEntity create(SiteEntity entity) { Preconditions.checkNotNull(entity.getSiteId(), "SiteId is null: " + entity.getSiteId()); if (siteId2EntityMap.containsKey(entity.getSiteId())) { throw new IllegalArgumentException("Duplicated siteId: " + entity.getSiteId()); } entity.ensureDefault(); siteId2EntityMap.put(entity.getSiteId(), entity); return entity; }
@Override public SiteEntity update(SiteEntity siteEntity) throws EntityNotFoundException { if (siteEntity.getSiteId() == null && siteEntity.getUuid() == null) { throw new IllegalArgumentException("siteId and UUID are both null, don't know how to update"); } SiteEntity oldEntity = (siteEntity.getSiteId() != null) ? getBySiteId(siteEntity.getSiteId()) : getByUUID(siteEntity.getUuid()); siteEntity.setUuid(oldEntity.getUuid()); siteEntity.setCreatedTime(oldEntity.getCreatedTime()); siteEntity.ensureDefault(); siteId2EntityMap.put(siteEntity.getSiteId(), siteEntity); return siteEntity; } }
@Override public SiteEntity update(SiteEntity siteEntity) throws EntityNotFoundException { if (siteEntity.getSiteId() == null && siteEntity.getUuid() == null) { throw new IllegalArgumentException("siteId and UUID are both null, don't know how to update"); } int result; try { SiteEntity oldEntity = getBySiteId(siteEntity.getSiteId()); siteEntity.setUuid(oldEntity.getUuid()); siteEntity.setCreatedTime(oldEntity.getCreatedTime()); siteEntity.ensureDefault(); result = queryService.update(updateSqlByUUID, siteEntity, new SiteEntityToRelation()); } catch (SQLException e) { LOGGER.error("Error to update SiteEntity: {}", siteEntity, e); throw new EntityNotFoundException(e); } if (result == 0) { throw new EntityNotFoundException("update " + siteEntity + "Not Found"); } return siteEntity; }