-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TACKLE-192] - Bulk copy Assessment/Review (#142)
* Add first template * Add native tests * Add utils * Add tests * Add audit data
- Loading branch information
1 parent
2af484e
commit e03f379
Showing
12 changed files
with
687 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/io/tackle/applicationinventory/dto/BulkReviewDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.tackle.applicationinventory.dto; | ||
|
||
import javax.validation.constraints.NotEmpty; | ||
import javax.validation.constraints.NotNull; | ||
import java.util.List; | ||
|
||
public class BulkReviewDto { | ||
|
||
private Long id; | ||
|
||
@NotNull | ||
private Long sourceReview; | ||
|
||
@NotEmpty | ||
private List<Long> targetApplications; | ||
|
||
private boolean completed; | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public Long getSourceReview() { | ||
return sourceReview; | ||
} | ||
|
||
public void setSourceReview(Long sourceReview) { | ||
this.sourceReview = sourceReview; | ||
} | ||
|
||
public List<Long> getTargetApplications() { | ||
return targetApplications; | ||
} | ||
|
||
public void setTargetApplications(List<Long> targetApplications) { | ||
this.targetApplications = targetApplications; | ||
} | ||
|
||
public boolean isCompleted() { | ||
return completed; | ||
} | ||
|
||
public void setCompleted(boolean completed) { | ||
this.completed = completed; | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/io/tackle/applicationinventory/dto/utils/EntityToDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.tackle.applicationinventory.dto.utils; | ||
|
||
import io.tackle.applicationinventory.dto.BulkReviewDto; | ||
import io.tackle.applicationinventory.entities.BulkCopyReview; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
public class EntityToDTO { | ||
|
||
public static BulkReviewDto toDTO(BulkCopyReview entity) { | ||
BulkReviewDto dto = new BulkReviewDto(); | ||
|
||
dto.setId(entity.id); | ||
dto.setCompleted(entity.completed); | ||
dto.setSourceReview(entity.sourceReview.id); | ||
dto.setTargetApplications(entity.targetApplications.stream() | ||
.map(f -> f.application.id) | ||
.collect(Collectors.toList()) | ||
); | ||
|
||
return dto; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/io/tackle/applicationinventory/entities/BulkCopyReview.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.tackle.applicationinventory.entities; | ||
|
||
import io.tackle.commons.entities.AbstractEntity; | ||
import org.hibernate.annotations.ResultCheckStyle; | ||
import org.hibernate.annotations.SQLDelete; | ||
import org.hibernate.annotations.Where; | ||
|
||
import javax.persistence.*; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
@Entity | ||
@Table(name = "bulk_copy_review") | ||
@SQLDelete(sql = "UPDATE bulk_copy_review SET deleted = true WHERE id = ?", check = ResultCheckStyle.COUNT) | ||
@Where(clause = "deleted = false") | ||
public class BulkCopyReview extends AbstractEntity { | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(foreignKey = @ForeignKey, name = "review_id") | ||
public Review sourceReview; | ||
|
||
@OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL}, orphanRemoval = true, mappedBy = "bulkCopyReview") | ||
public Set<BulkCopyReviewDetails> targetApplications = new HashSet<>(); | ||
|
||
public boolean completed = false; | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/io/tackle/applicationinventory/entities/BulkCopyReviewDetails.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.tackle.applicationinventory.entities; | ||
|
||
import io.tackle.commons.entities.AbstractEntity; | ||
import org.hibernate.annotations.ResultCheckStyle; | ||
import org.hibernate.annotations.SQLDelete; | ||
import org.hibernate.annotations.Where; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
@Table(name = "bulk_copy_review_details") | ||
@SQLDelete(sql = "UPDATE bulk_copy_review_details SET deleted = true WHERE id = ?", check = ResultCheckStyle.COUNT) | ||
@Where(clause = "deleted = false") | ||
public class BulkCopyReviewDetails extends AbstractEntity { | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(foreignKey = @ForeignKey, name = "bulk_copy_review_id") | ||
public BulkCopyReview bulkCopyReview; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(foreignKey = @ForeignKey, name = "application_id") | ||
public Application application; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/main/java/io/tackle/applicationinventory/resources/ReviewBulkCopyResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package io.tackle.applicationinventory.resources; | ||
|
||
import io.tackle.applicationinventory.dto.BulkReviewDto; | ||
import io.tackle.applicationinventory.dto.utils.EntityToDTO; | ||
import io.tackle.applicationinventory.entities.Application; | ||
import io.tackle.applicationinventory.entities.BulkCopyReview; | ||
import io.tackle.applicationinventory.entities.BulkCopyReviewDetails; | ||
import io.tackle.applicationinventory.entities.Review; | ||
import io.tackle.applicationinventory.services.BulkCopyReviewService; | ||
import io.vertx.core.eventbus.EventBus; | ||
|
||
import javax.inject.Inject; | ||
import javax.transaction.NotSupportedException; | ||
import javax.transaction.*; | ||
import javax.validation.Valid; | ||
import javax.validation.constraints.NotNull; | ||
import javax.ws.rs.*; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
@Path("/review/bulk") | ||
@Produces("application/json") | ||
@Consumes("application/json") | ||
public class ReviewBulkCopyResource { | ||
|
||
@Inject | ||
UserTransaction transaction; | ||
|
||
@Inject | ||
EventBus eventBus; | ||
|
||
private void handleSimpleRollback(UserTransaction transaction) { | ||
try { | ||
transaction.rollback(); | ||
} catch (SystemException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
@POST | ||
public BulkReviewDto createBulkCopyReview( | ||
@NotNull @Valid BulkReviewDto bulkReviewInput | ||
) throws SystemException, NotSupportedException, HeuristicRollbackException, HeuristicMixedException, RollbackException { | ||
transaction.begin(); | ||
|
||
// Find source review | ||
Long sourceReviewId = bulkReviewInput.getSourceReview(); | ||
Review sourceReview = Review.<Review>findByIdOptional(sourceReviewId) | ||
.orElseThrow(() -> { | ||
handleSimpleRollback(transaction); | ||
return new BadRequestException("Source review not valid"); | ||
}); | ||
|
||
// Find target applications | ||
List<Application> applicationsTarget = bulkReviewInput.getTargetApplications().stream() | ||
.map(appId -> Application.<Application>findById(appId)) | ||
.collect(Collectors.toList()); | ||
if (applicationsTarget.stream().anyMatch(Objects::isNull)) { | ||
handleSimpleRollback(transaction); | ||
throw new BadRequestException("One or more target applications is not valid"); | ||
} | ||
|
||
// Prepare JPA object to be persisted | ||
BulkCopyReview bulkCopyReviewOutput = new BulkCopyReview(); | ||
|
||
bulkCopyReviewOutput.sourceReview = sourceReview; | ||
bulkCopyReviewOutput.targetApplications = applicationsTarget.stream() | ||
.map(application -> { | ||
BulkCopyReviewDetails detail = new BulkCopyReviewDetails(); | ||
detail.bulkCopyReview = bulkCopyReviewOutput; | ||
detail.application = application; | ||
|
||
return detail; | ||
}) | ||
.collect(Collectors.toSet()); | ||
bulkCopyReviewOutput.completed = false; | ||
|
||
bulkCopyReviewOutput.persist(); | ||
transaction.commit(); | ||
|
||
// Fire bus event | ||
eventBus.send(BulkCopyReviewService.BUS_EVENT, bulkCopyReviewOutput.id); | ||
|
||
// Generate response | ||
return EntityToDTO.toDTO(bulkCopyReviewOutput); | ||
} | ||
|
||
@GET | ||
@Path("/{id}") | ||
public BulkReviewDto getBulkCopyReview(@PathParam("id") Long id) { | ||
BulkCopyReview entity = BulkCopyReview | ||
.<BulkCopyReview>findByIdOptional(id) | ||
.orElseThrow(NotFoundException::new); | ||
|
||
return EntityToDTO.toDTO(entity); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/io/tackle/applicationinventory/services/BulkCopyReviewService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.tackle.applicationinventory.services; | ||
|
||
import io.quarkus.vertx.ConsumeEvent; | ||
import io.smallrye.common.annotation.Blocking; | ||
import io.tackle.applicationinventory.entities.Application; | ||
import io.tackle.applicationinventory.entities.BulkCopyReview; | ||
import io.tackle.applicationinventory.entities.Review; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.transaction.Transactional; | ||
|
||
@ApplicationScoped | ||
public class BulkCopyReviewService { | ||
|
||
public static final String BUS_EVENT = "process-bulk-review-copy"; | ||
|
||
@Transactional | ||
@ConsumeEvent(BUS_EVENT) | ||
@Blocking | ||
public void processBulkCopyReview(Long bulkId) { | ||
BulkCopyReview bulk = BulkCopyReview.findById(bulkId); | ||
|
||
// Copy review to all apps | ||
bulk.targetApplications.forEach(detail -> { | ||
Application application = Application.findById(detail.application.id); | ||
|
||
Review oldReview = Review.find("application.id", detail.application.id).firstResult(); | ||
if (oldReview != null) { | ||
oldReview = copyReviewFromSourceToTarget(bulk.sourceReview, oldReview); | ||
oldReview.persist(); | ||
} else { | ||
Review newReview = copyReviewFromSourceToTarget(bulk.sourceReview, new Review()); | ||
newReview.application = application; | ||
newReview.persist(); | ||
} | ||
}); | ||
|
||
bulk.completed = true; | ||
bulk.persist(); | ||
} | ||
|
||
private Review copyReviewFromSourceToTarget(Review source, Review target) { | ||
target.proposedAction = source.proposedAction; | ||
target.effortEstimate = source.effortEstimate; | ||
target.businessCriticality = source.businessCriticality; | ||
target.workPriority = source.workPriority; | ||
target.comments = source.comments; | ||
|
||
target.copiedFromReviewId = source.id; | ||
return target; | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/main/resources/db/migration/V20211008.1__add_bulk_copy_review.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
create table bulk_copy_review | ||
( | ||
id int8 not null, | ||
|
||
createTime timestamp, | ||
createUser varchar(255), | ||
deleted boolean, | ||
updateTime timestamp, | ||
updateUser varchar(255), | ||
|
||
completed boolean, | ||
review_id int8 not null, | ||
primary key (id) | ||
); | ||
|
||
create table bulk_copy_review_details | ||
( | ||
id int8 not null, | ||
|
||
createTime timestamp, | ||
createUser varchar(255), | ||
deleted boolean, | ||
updateTime timestamp, | ||
updateUser varchar(255), | ||
|
||
bulk_copy_review_id int8 not null, | ||
application_id int8 not null, | ||
primary key (id) | ||
); | ||
|
||
alter table if exists bulk_copy_review_details | ||
add constraint fk_bulk_copy_review_details_application | ||
foreign key (application_id) | ||
references application; | ||
|
||
-- Add audit data | ||
|
||
alter table if exists review | ||
add column copiedFromReviewId int8; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.