Provide default methods to prevent repository classes having to override the methods

This commit is contained in:
sureshms 2021-10-24 08:14:09 -07:00
parent a30fc96fa9
commit 363262a699
14 changed files with 33 additions and 155 deletions

View File

@ -48,7 +48,6 @@ public class CatalogGenericExceptionMapper implements ExceptionMapper<Throwable>
@Override
public Response toResponse(Throwable ex) {
ex.printStackTrace();
if (ex instanceof ProcessingException || ex instanceof IllegalArgumentException) {
final Response response = BadRequestException.of().getResponse();
return Response.fromResponse(response)

View File

@ -81,11 +81,6 @@ public class BotsRepository extends EntityRepository<Bots>{
@Override
public void storeRelationships(Bots entity) throws IOException { }
@Override
public EntityUpdater getUpdater(Bots original, Bots updated, boolean patchOperation) throws IOException {
return null;
}
static class BotsEntityInterface implements EntityInterface<Bots> {
private final Bots entity;

View File

@ -104,12 +104,6 @@ public class ChartRepository extends EntityRepository<Chart> {
applyTags(chart);
}
@Override
public EntityUpdater getUpdater(Chart original, Chart updated, boolean patchOperation) throws IOException {
return new ChartUpdater(original, updated, patchOperation);
}
private void applyTags(Chart chart) throws IOException {
// Add chart level tags by adding tag to chart relationship
EntityUtil.applyTags(dao.tagDAO(), chart.getTags(), chart.getFullyQualifiedName());
@ -270,18 +264,4 @@ public class ChartRepository extends EntityRepository<Chart> {
entity.setTags(tags);
}
}
/**
* Handles entity updated from PUT and POST operation.
*/
public class ChartUpdater extends EntityUpdater {
public ChartUpdater(Chart original, Chart updated, boolean patchOperation) {
super(original, updated, patchOperation);
}
@Override
public void entitySpecificUpdate() throws IOException {
// No special updates beyond
}
}
}

View File

@ -341,6 +341,11 @@ public class DashboardRepository extends EntityRepository<Dashboard> {
super(original, updated, patchOperation);
}
@Override
public void entitySpecificUpdate() throws IOException {
updateCharts();
}
private void updateCharts() {
String dashboardId = updated.getId().toString();
@ -358,10 +363,5 @@ public class DashboardRepository extends EntityRepository<Dashboard> {
List<UUID> updatedChartIds = EntityUtil.getIDList(updated.getEntity().getCharts());
recordChange("charts", origChartIds, updatedChartIds);
}
@Override
public void entitySpecificUpdate() throws IOException {
updateCharts();
}
}
}

View File

@ -111,12 +111,6 @@ public class DashboardServiceRepository extends EntityRepository<DashboardServic
public void storeRelationships(DashboardService entity) throws IOException {
}
@Override
public EntityUpdater getUpdater(DashboardService original, DashboardService updated, boolean patchOperation)
throws IOException {
return null;
}
public static class DashboardServiceEntityInterface implements EntityInterface<DashboardService> {
private final DashboardService entity;

View File

@ -100,15 +100,9 @@ public class DatabaseRepository extends EntityRepository<Database> {
EntityUtil.setOwner(dao.relationshipDAO(), database.getId(), Entity.DATABASE, database.getOwner());
}
@Override
public EntityUpdater getUpdater(Database original, Database updated, boolean patchOperation) throws IOException {
return new DatabaseUpdater(original, updated, patchOperation);
}
public EntityReference getOwner(Database database) throws IOException {
return database != null ? EntityUtil.populateOwner(database.getId(), dao.relationshipDAO(), dao.userDAO(),
dao.teamDAO())
: null;
dao.teamDAO()) : null;
}
private List<EntityReference> getTables(Database database) throws IOException {
@ -241,18 +235,4 @@ public class DatabaseRepository extends EntityRepository<Database> {
@Override
public void setTags(List<TagLabel> tags) { }
}
/**
* Handles entity updated from PUT and POST operation.
*/
public class DatabaseUpdater extends EntityUpdater {
public DatabaseUpdater(Database original, Database updated, boolean patchOperation) {
super(original, updated, patchOperation);
}
@Override
public void entitySpecificUpdate() throws IOException {
// Nothing special to do
}
}
}

View File

@ -111,11 +111,6 @@ public class DatabaseServiceRepository extends EntityRepository<DatabaseService>
public void storeRelationships(DatabaseService entity) throws IOException {
}
@Override
public EntityUpdater getUpdater(DatabaseService original, DatabaseService updated, boolean patchOperation) throws IOException {
return null;
}
public static class DatabaseServiceEntityInterface implements EntityInterface<DatabaseService> {
private final DatabaseService entity;

View File

@ -36,6 +36,25 @@ public abstract class EntityRepository<T> {
private final Fields patchFields;
private final Fields putFields;
/**
* Entity related operations that should be implemented or overridden by entities
*/
public abstract String getFullyQualifiedName(T entity);
public abstract ResultList<T> getResultList(List<T> entities, String beforeCursor, String afterCursor,
int total) throws GeneralSecurityException, UnsupportedEncodingException;
public abstract EntityInterface<T> getEntityInterface(T entity);
public abstract T setFields(T entity, Fields fields) throws IOException, ParseException;
public abstract void restorePatchAttributes(T original, T updated) throws IOException, ParseException;
public abstract void validate(T entity) throws IOException;
public abstract void store(T entity, boolean update) throws IOException;
public abstract void storeRelationships(T entity) throws IOException;
public EntityUpdater getUpdater(T original, T updated, boolean patchOperation) throws IOException {
return new EntityUpdater(original, updated, patchOperation);
}
EntityRepository(Class<T> entityClass, EntityDAO<T> entityDAO, CollectionDAO collectionDAO,
Fields patchFields, Fields putFields) {
this.entityClass = entityClass;
@ -129,8 +148,8 @@ public abstract class EntityRepository<T> {
updatedEntity.setUpdatedBy(user);
updatedEntity.setUpdatedAt(new Date());
restorePatchAttributes(original, updated);
validate(updated);
restorePatchAttributes(original, updated);
EntityUpdater entityUpdater = getUpdater(original, updated, true);
entityUpdater.update();
entityUpdater.store();
@ -144,25 +163,11 @@ public abstract class EntityRepository<T> {
return entity;
}
// TODO clean up all the exceptions and check if they are really thrown
/**
* Entity related operations
* Class that performs PUT and PATCH UPDATE operation. Override {@code entitySpecificUpdate()} to add
* additional entity specific fields to be updated.
*/
public abstract String getFullyQualifiedName(T entity);
public abstract ResultList<T> getResultList(List<T> entities, String beforeCursor, String afterCursor,
int total) throws GeneralSecurityException, UnsupportedEncodingException;
public abstract EntityInterface<T> getEntityInterface(T entity);
public abstract T setFields(T entity, Fields fields) throws IOException, ParseException;
public abstract void restorePatchAttributes(T original, T updated) throws IOException, ParseException;
public abstract void validate(T entity) throws IOException;
public abstract void store(T entity, boolean update) throws IOException;
public abstract void storeRelationships(T entity) throws IOException;
public abstract EntityUpdater getUpdater(T original, T updated, boolean patchOperation) throws IOException;
// TODO better name
public abstract class EntityUpdater {
public class EntityUpdater {
protected final EntityInterface<T> original;
protected final EntityInterface<T> updated;
protected final boolean patchOperation;
@ -186,6 +191,10 @@ public abstract class EntityRepository<T> {
entitySpecificUpdate();
}
public void entitySpecificUpdate() throws IOException {
// Default implementation. Override this to add any entity specific field updates
}
private void updateDescription() {
if (!patchOperation &&
original.getDescription() != null && !original.getDescription().isEmpty()) {
@ -229,7 +238,6 @@ public abstract class EntityRepository<T> {
EntityUtil.applyTags(daoCollection.tagDAO(), updated.getTags(), updated.getFullyQualifiedName());
}
public abstract void entitySpecificUpdate() throws IOException;
public final Double getNewVersion(Double oldVersion) {
Double newVersion = oldVersion;

View File

@ -112,12 +112,6 @@ public class MessagingServiceRepository extends EntityRepository<MessagingServic
@Override
public void storeRelationships(MessagingService entity) throws IOException { }
@Override
public EntityUpdater getUpdater(MessagingService original, MessagingService updated, boolean patchOperation)
throws IOException {
return null;
}
public static class MessagingServiceEntityInterface implements EntityInterface<MessagingService> {
private final MessagingService entity;

View File

@ -114,11 +114,6 @@ public class MetricsRepository extends EntityRepository<Metrics> {
applyTags(metrics);
}
@Override
public EntityUpdater getUpdater(Metrics original, Metrics updated, boolean patchOperation) throws IOException {
return null;
}
private EntityReference getService(Metrics metrics) throws IOException { // Get service by metrics Id
EntityReference ref = EntityUtil.getService(dao.relationshipDAO(), metrics.getId(), Entity.DASHBOARD_SERVICE);
return getService(ref);
@ -228,18 +223,4 @@ public class MetricsRepository extends EntityRepository<Metrics> {
entity.setTags(tags);
}
}
/**
* Handles entity updates from PUT and POST operation.
*/
public class MetricsUpdater extends EntityUpdater {
public MetricsUpdater(Metrics original, Metrics updated, boolean patchOperation) {
super(original, updated, patchOperation);
}
@Override
public void entitySpecificUpdate() throws IOException {
// No entity specific update
}
}
}

View File

@ -114,11 +114,6 @@ public class PipelineServiceRepository extends EntityRepository<PipelineService>
}
@Override
public EntityUpdater getUpdater(PipelineService original, PipelineService updated, boolean patchOperation) throws IOException {
return null;
}
public static class PipelineServiceEntityInterface implements EntityInterface<PipelineService> {
private final PipelineService entity;

View File

@ -97,11 +97,6 @@ public class ReportRepository extends EntityRepository<Report> {
// TODO
}
@Override
public EntityUpdater getUpdater(Report original, Report updated, boolean patchOperation) throws IOException {
return null;
}
private EntityReference getService(Report report) {
return report == null ? null : getService(EntityUtil.getService(dao.relationshipDAO(), report.getId()));
}

View File

@ -105,11 +105,6 @@ public class TaskRepository extends EntityRepository<Task> {
applyTags(task);
}
@Override
public EntityUpdater getUpdater(Task original, Task updated, boolean patchOperation) throws IOException {
return new TaskUpdater(original, updated, patchOperation);
}
private void applyTags(Task task) throws IOException {
// Add task level tags by adding tag to task relationship
EntityUtil.applyTags(dao.tagDAO(), task.getTags(), task.getFullyQualifiedName());
@ -266,18 +261,4 @@ public class TaskRepository extends EntityRepository<Task> {
entity.setTags(tags);
}
}
/**
* Handles entity updated from PUT and POST operation.
*/
public class TaskUpdater extends EntityUpdater {
public TaskUpdater(Task original, Task updated, boolean patchOperation) {
super(original, updated, patchOperation);
}
@Override
public void entitySpecificUpdate() throws IOException {
// No specific updates for tasks
}
}
}

View File

@ -108,11 +108,6 @@ public class TopicRepository extends EntityRepository<Topic> {
applyTags(topic);
}
@Override
public EntityUpdater getUpdater(Topic original, Topic updated, boolean patchOperation) throws IOException {
return new TopicUpdater(original, updated, patchOperation);
}
private void applyTags(Topic topic) throws IOException {
// Add topic level tags by adding tag to topic relationship
EntityUtil.applyTags(dao.tagDAO(), topic.getTags(), topic.getFullyQualifiedName());
@ -281,18 +276,4 @@ public class TopicRepository extends EntityRepository<Topic> {
entity.setTags(tags);
}
}
/**
* Handles entity updated from PUT and POST operation.
*/
public class TopicUpdater extends EntityUpdater {
public TopicUpdater(Topic original, Topic updated, boolean patchOperation) {
super(original, updated, patchOperation);
}
@Override
public void entitySpecificUpdate() throws IOException {
// No specific update for Topic
}
}
}