Change Log for physical flows should include changed in its specification also
package com.khartec.waltz.service.changelog;
+import com.khartec.waltz.common.CollectionUtilities;
+import com.khartec.waltz.data.DBExecutorPoolInterface;
import com.khartec.waltz.data.changelog.ChangeLogDao;
+import com.khartec.waltz.data.physical_flow.PhysicalFlowDao;
+import com.khartec.waltz.model.EntityKind;
import com.khartec.waltz.model.EntityReference;
import com.khartec.waltz.model.changelog.ChangeLog;
+import com.khartec.waltz.model.physical_flow.PhysicalFlow;
+import org.jooq.lambda.Unchecked;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
+import java.util.ArrayList;
+import java.util.Comparator;
import java.util.List;
import java.util.Optional;
+import java.util.concurrent.Future;
-import static com.khartec.waltz.common.Checks.checkNotEmpty;
-import static com.khartec.waltz.common.Checks.checkNotNull;
+import static com.khartec.waltz.common.Checks.*;
+import static com.khartec.waltz.model.EntityReference.mkRef;
@Service
@@ -39,20 +48,30 @@ public class ChangeLogService {
private static final Logger LOG = LoggerFactory.getLogger(ChangeLogService.class);
private final ChangeLogDao changeLogDao;
+ private final DBExecutorPoolInterface dbExecutorPool;
+ private final PhysicalFlowDao physicalFlowDao;
@Autowired
- public ChangeLogService(ChangeLogDao changeLogDao) {
+ public ChangeLogService(ChangeLogDao changeLogDao,
+ DBExecutorPoolInterface dbExecutorPool,
+ PhysicalFlowDao physicalFlowDao) {
checkNotNull(changeLogDao, "changeLogDao must not be null");
+ checkNotNull(dbExecutorPool, "dbExecutorPool cannot be null");
+ checkNotNull(physicalFlowDao, "physicalFlowDao cannot be null");
this.changeLogDao = changeLogDao;
+ this.dbExecutorPool = dbExecutorPool;
+ this.physicalFlowDao = physicalFlowDao;
}
public List<ChangeLog> findByParentReference(EntityReference ref,
Optional<Integer> limit) {
checkNotNull(ref, "ref must not be null");
-
+ if(ref.kind() == EntityKind.PHYSICAL_FLOW) {
+ return findByParentReferenceForPhysicalFlow(ref, limit);
+ }
return changeLogDao.findByParentReference(ref, limit);
}
@@ -80,4 +99,29 @@ public class ChangeLogService {
return changeLogDao.write(changeLogs);
}
+
+ ////////////////////// PRIVATE HELPERS //////////////////////////////////////////
+
+ private List<ChangeLog> findByParentReferenceForPhysicalFlow(EntityReference ref,
+ Optional<Integer> limit) {
+ checkNotNull(ref, "ref must not be null");
+ checkTrue(ref.kind() == EntityKind.PHYSICAL_FLOW, "ref should refer to a Physical Flow");
+
+ Future<List<ChangeLog>> flowLogsFuture = dbExecutorPool.submit(() -> changeLogDao.findByParentReference(ref, limit));
+
+ Future<List<ChangeLog>> specLogsFuture = dbExecutorPool.submit(() -> {
+ PhysicalFlow flow = physicalFlowDao.getById(ref.id());
+ return changeLogDao.findByParentReference(mkRef(EntityKind.PHYSICAL_SPECIFICATION, flow.specificationId()), limit);
+ });
+
+ return Unchecked.supplier(() -> {
+ List<ChangeLog> flowLogs = flowLogsFuture.get();
+ List<ChangeLog> specLogs = specLogsFuture.get();
+ List<ChangeLog> all = new ArrayList<>();
+ all.addAll(flowLogs);
+ all.addAll(specLogs);
+ return (List<ChangeLog>) CollectionUtilities.sort(all, Comparator.comparing(ChangeLog::createdAt).reversed());
+ }).get();
+ }
+
}
