Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
[RW-183] Create WorkspaceService to handle research purpose review. (#…
…239) * Factor out WorkspaceService (wrapping WorkspaceDao). * Combine copies of 'get workspace and throw if it doesn not exist'. * Add methods for research purpose review, and associated tests. * Incidental fix of string formatting in exception messages (in WS files only). TODOs: * `@Version` to prevent conflicting edits. RW-215 * Fix remaining string formatting in exceptions. RW-217 * Remove remaining redundant Java index declarations. RW-220
- Loading branch information
Mark Fickett
committed
Oct 30, 2017
1 parent
fecd6e1
commit 9a0c14f633213a8b31db453a9c83aee6fc35d2bb
Showing
12 changed files
with
299 additions
and
66 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
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<databaseChangeLog | ||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9 | ||
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd"> | ||
<changeSet author="markfickett" id="changelog-8-ws-index"> | ||
<createIndex | ||
indexName="idx_workspace_rp" | ||
tableName="workspace" | ||
unique="true"> | ||
<column name="rp_review_requested"/> | ||
<column name="rp_approved"/> | ||
</createIndex> | ||
</changeSet> | ||
</databaseChangeLog> |
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
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
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
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
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
@@ -1,2 +1,6 @@ | ||
DAOs' query implementations are | ||
[automatically derived](https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.details). | ||
|
||
Services implement custom behavior which can't be auto-generated. Where both a | ||
Service and a DAO are available, inject the Service and use its public dao field | ||
where necessary. [Design doc](https://docs.google.com/document/d/1uNf6_5TZxnQt8BP_wWoGxPcGwaIZAhZswW3bXwbswHU). |
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
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
@@ -0,0 +1,68 @@ | ||
package org.pmiops.workbench.db.dao; | ||
|
||
import java.util.List; | ||
import java.util.logging.Logger; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import org.pmiops.workbench.db.model.Workspace; | ||
import org.pmiops.workbench.exceptions.BadRequestException; | ||
import org.pmiops.workbench.exceptions.NotFoundException; | ||
|
||
|
||
/** | ||
* Workspace manipulation and shared business logic which can't be represented by automatic query | ||
* generation in WorkspaceDao, or convenience aliases. | ||
* | ||
* TODO(RW-215) Add versioning to detect/prevent concurrent edits. | ||
*/ | ||
@Service | ||
public class WorkspaceService { | ||
private static final Logger log = Logger.getLogger(WorkspaceService.class.getName()); | ||
|
||
/** | ||
* Clients wishing to use the auto-generated methods from the DAO interface may directly access | ||
* it here. | ||
*/ | ||
public final WorkspaceDao dao; | ||
|
||
@Autowired | ||
public WorkspaceService(WorkspaceDao workspaceDao) { | ||
this.dao = workspaceDao; | ||
} | ||
|
||
public Workspace get(String ns, String id) { | ||
return dao.findByWorkspaceNamespaceAndFirecloudName(ns, id); | ||
} | ||
|
||
public Workspace getRequired(String ns, String id) { | ||
Workspace workspace = get(ns, id); | ||
if (workspace == null) { | ||
throw new NotFoundException(String.format("Workspace %s/%s not found.", ns, id)); | ||
} | ||
return workspace; | ||
} | ||
|
||
public List<Workspace> findForReview() { | ||
return dao.findByApprovedIsNullAndReviewRequestedTrueOrderByTimeRequested(); | ||
} | ||
|
||
// FIXME @Version instead? Bean instantiation v. @Transactional? | ||
//@Transactional | ||
public void setResearchPurposeApproved(String ns, String id, boolean approved) { | ||
Workspace workspace = getRequired(ns, id); | ||
if (workspace.getReviewRequested() == null || !workspace.getReviewRequested()) { | ||
throw new BadRequestException(String.format( | ||
"No review requested for workspace %s/%s.", ns, id)); | ||
} | ||
if (workspace.getApproved() != null) { | ||
throw new BadRequestException(String.format( | ||
"Workspace %s/%s already %s.", | ||
ns, id, workspace.getApproved() ? "approved" : "rejected")); | ||
} | ||
workspace.setApproved(approved); | ||
dao.save(workspace); | ||
} | ||
} |
Oops, something went wrong.