@Override public synchronized DashboardEntity update(DashboardEntity entity, User user) throws EntityNotFoundException { Preconditions.checkNotNull(entity, "Entity should not be null"); Preconditions.checkNotNull(entity.getUuid(), "uuid should not be null"); DashboardEntity current = getByUUID(entity.getUuid()); || current.getAuthor().equals(user.getName()), "UPDATE operation is not allowed"); if (entity.getName() != null) { current.setName(entity.getName()); if (entity.getDescription() != null) { current.setDescription(entity.getDescription()); if (entity.getAuthor() != null) { current.setAuthor(entity.getAuthor()); if (entity.getCharts() != null) { current.setCharts(entity.getCharts()); if (entity.getSettings() != null) { current.setSettings(entity.getSettings()); if (entity.getCreatedTime() > 0) { LOGGER.warn("createdTime is not updatable but provided: {}, ignore", current.getCreatedTime()); if (entity.getModifiedTime() > 0) { LOGGER.warn("modifiedTime is not updatable but provided: {}, ignore", current.getModifiedTime()); current.ensureDefault();
@Override public synchronized DashboardEntity create(DashboardEntity entity) { Preconditions.checkNotNull(entity, "DashboardEntity is null"); Preconditions.checkArgument(entity.getUuid() == null, "Dashboard Entity uuid should be null"); entity.ensureDefault(); try { Preconditions.checkArgument(getByUUIDOrName(entity.getUuid(), entity.getName()) == null, "Duplicated dashboard name"); } catch (EntityNotFoundException e) { // ignore } dashboardEntityMap.put(entity.getUuid(), entity); return entity; }
default DashboardEntity createOrUpdate(DashboardEntity dashboardEntity, User user) throws EntityNotFoundException { Preconditions.checkNotNull(dashboardEntity, "DashboardEntity should not be null"); if (dashboardEntity.getUuid() == null) { Preconditions.checkArgument(dashboardEntity.getAuthor() == null, "author is immutable"); dashboardEntity.setAuthor(user.getName()); return create(dashboardEntity); } else { return update(dashboardEntity, user); } } }
@Override public DashboardEntity deleteByUUID(String uuid, User user) throws EntityNotFoundException { Preconditions.checkNotNull(uuid, "uuid should not be null"); DashboardEntity entity = this.getByUUID(uuid); Preconditions.checkArgument(user.isInRole(User.Role.ADMINISTRATOR) || entity.getAuthor().equals(user.getName()), "DELETE operation is not allowed"); try { queryService.execute(String.format(DELETE_BY_UUID_SQL_FORMAT, uuid)); } catch (SQLException e) { LOGGER.error("Error to execute {}: {}", String.format(DELETE_BY_UUID_SQL_FORMAT, uuid), uuid, e.getMessage(), e); throw new IllegalArgumentException("SQL execution error: " + e.getMessage(), e); } return entity; } }
@Override public synchronized DashboardEntity getByUUIDOrName(String uuid, String name) throws EntityNotFoundException { if (uuid != null) { return getByUUID(uuid); } else if (name != null) { return dashboardEntityMap.values().stream() .filter((dashboardEntity -> dashboardEntity.getName().equals(name))).findAny() .orElseThrow(() -> new EntityNotFoundException("Dashboard named: " + name + " not found")); } throw new IllegalArgumentException("Both uuid and name are null"); }
@Override public DashboardEntity create(DashboardEntity entity) { Preconditions.checkNotNull(entity, "Entity should not be null"); entity.ensureDefault(); try { int retCode = queryService.insert(CREATE_SQL, Collections.singletonList(entity), DASHBOARD_SERIALIZER); if (retCode > 0) { return entity; } else { throw new SQLException("Insertion returned: " + retCode); } } catch (SQLException e) { LOGGER.error("Error to insert entity {} (entity: {}): {}", CREATE_SQL, entity.toString(), e.getMessage(), e); throw new IllegalArgumentException("SQL execution error:" + e.getMessage(), e); } }
@Override public synchronized DashboardEntity deleteByUUID(String uuid, User user) throws EntityNotFoundException { Preconditions.checkNotNull(uuid, "UUID should not be null"); DashboardEntity current = this.getByUUID(uuid); Preconditions.checkArgument(user.isInRole(User.Role.ADMINISTRATOR) || current.getAuthor().equals(user.getName()), "DELETE operation is not allowed for user " + user.getName()); dashboardEntityMap.remove(uuid); return current; } }
@Override public DashboardEntity update(DashboardEntity entity, User user) throws EntityNotFoundException { Preconditions.checkNotNull(entity, "Entity should not be null"); Preconditions.checkNotNull(entity.getUuid(), "uuid should not be null"); DashboardEntity current = getByUUID(entity.getUuid()); || current.getAuthor().equals(user.getName()), "UPDATE operation is not allowed"); if (entity.getName() != null) { current.setName(entity.getName()); if (entity.getDescription() != null) { current.setDescription(entity.getDescription()); if (entity.getAuthor() != null) { current.setAuthor(entity.getAuthor()); if (entity.getCharts() != null) { current.setCharts(entity.getCharts()); if (entity.getSettings() != null) { current.setSettings(entity.getSettings()); if (entity.getCreatedTime() > 0) { LOGGER.warn("createdTime is not updatable but provided: {}, ignore", current.getCreatedTime()); if (entity.getModifiedTime() > 0) { LOGGER.warn("modifiedTime is not updatable but provided: {}, ignore", current.getModifiedTime()); current.ensureDefault();