diff --git a/src/main/java/org/ohdsi/webapi/tool/Tool.java b/src/main/java/org/ohdsi/webapi/tool/Tool.java index 46f6385de..93f9d3135 100644 --- a/src/main/java/org/ohdsi/webapi/tool/Tool.java +++ b/src/main/java/org/ohdsi/webapi/tool/Tool.java @@ -35,7 +35,7 @@ public class Tool extends CommonEntity { @Column(name = "description") private String description; @Column(name = "is_enabled") - private Boolean isEnabled; + private Boolean enabled; @Override public Integer getId() { @@ -71,11 +71,11 @@ public void setDescription(String description) { } public Boolean getEnabled() { - return isEnabled; + return enabled; } public void setEnabled(Boolean enabled) { - isEnabled = enabled; + this.enabled = enabled; } @Override diff --git a/src/main/java/org/ohdsi/webapi/tool/ToolController.java b/src/main/java/org/ohdsi/webapi/tool/ToolController.java index cfc38ed96..2b593076c 100644 --- a/src/main/java/org/ohdsi/webapi/tool/ToolController.java +++ b/src/main/java/org/ohdsi/webapi/tool/ToolController.java @@ -3,7 +3,14 @@ import org.ohdsi.webapi.tool.dto.ToolDTO; import org.springframework.stereotype.Controller; -import javax.ws.rs.*; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import java.util.List; diff --git a/src/main/java/org/ohdsi/webapi/tool/ToolServiceImpl.java b/src/main/java/org/ohdsi/webapi/tool/ToolServiceImpl.java index f68d87b96..3dc17dd3b 100644 --- a/src/main/java/org/ohdsi/webapi/tool/ToolServiceImpl.java +++ b/src/main/java/org/ohdsi/webapi/tool/ToolServiceImpl.java @@ -1,41 +1,44 @@ package org.ohdsi.webapi.tool; +import java.text.SimpleDateFormat; +import java.time.Instant; +import java.util.Date; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; import org.apache.shiro.SecurityUtils; import org.ohdsi.webapi.service.AbstractDaoService; import org.ohdsi.webapi.shiro.Entities.UserEntity; -import org.ohdsi.webapi.tool.converter.ToolConvertor; import org.ohdsi.webapi.tool.dto.ToolDTO; import org.springframework.stereotype.Service; @Service public class ToolServiceImpl extends AbstractDaoService implements ToolService { + private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; + private final ToolRepository toolRepository; - private final ToolConvertor toolConvertor; - public ToolServiceImpl(ToolRepository toolRepository, ToolConvertor toolConvertor) { + public ToolServiceImpl(ToolRepository toolRepository) { this.toolRepository = toolRepository; - this.toolConvertor = toolConvertor; } @Override public List getTools() { List tools = (isAdmin() || canManageTools()) ? toolRepository.findAll() : toolRepository.findAllByIsEnabled(true); return tools.stream() - .map(toolConvertor::toDTO).collect(Collectors.toList()); + .map(this::toDTO).collect(Collectors.toList()); } @Override public ToolDTO saveTool(ToolDTO toolDTO) { Tool tool = saveToolFromDTO(toolDTO, getCurrentUser()); - return toolConvertor.toDTO(toolRepository.saveAndFlush(tool)); + return toDTO(toolRepository.saveAndFlush(tool)); } private Tool saveToolFromDTO(ToolDTO toolDTO, UserEntity currentUser) { - Tool tool = toolConvertor.toEntity(toolDTO); + Tool tool = toEntity(toolDTO); if (toolDTO.getId() == null) { tool.setCreatedBy(currentUser); } @@ -45,7 +48,7 @@ private Tool saveToolFromDTO(ToolDTO toolDTO, UserEntity currentUser) { @Override public ToolDTO getById(Integer id) { - return toolConvertor.toDTO(toolRepository.findOne(id)); + return toDTO(toolRepository.findOne(id)); } @Override @@ -57,4 +60,61 @@ private boolean canManageTools() { return Stream.of("tool:put", "tool:post", "tool:*:delete") .allMatch(permission -> SecurityUtils.getSubject().isPermitted(permission)); } + + Tool toEntity(ToolDTO toolDTO) { + boolean isNewTool = toolDTO.getId() == null; + Tool tool = isNewTool ? new Tool() : toolRepository.findOne(toolDTO.getId()); + Instant currentInstant = Instant.now(); + if (isNewTool) { + setCreationDetails(tool, currentInstant); + } else { + setModificationDetails(tool, currentInstant); + } + updateToolFromDTO(tool, toolDTO); + return tool; + } + + private void setCreationDetails(Tool tool, Instant currentInstant) { + tool.setCreatedDate(Date.from(currentInstant)); + tool.setCreatedBy(getCurrentUser()); + } + + private void setModificationDetails(Tool tool, Instant currentInstant) { + tool.setModifiedDate(Date.from(currentInstant)); + tool.setModifiedBy(getCurrentUser()); + } + + private void updateToolFromDTO(Tool tool, ToolDTO toolDTO) { + Optional.ofNullable(toolDTO.getName()).ifPresent(tool::setName); + Optional.ofNullable(toolDTO.getUrl()).ifPresent(tool::setUrl); + Optional.ofNullable(toolDTO.getDescription()).ifPresent(tool::setDescription); + Optional.ofNullable(toolDTO.getEnabled()).ifPresent(tool::setEnabled); + } + + ToolDTO toDTO(Tool tool) { + return Optional.ofNullable(tool) + .map(t -> { + ToolDTO toolDTO = new ToolDTO(); + toolDTO.setId(t.getId()); + toolDTO.setName(t.getName()); + toolDTO.setUrl(t.getUrl()); + toolDTO.setDescription(t.getDescription()); + Optional.ofNullable(tool.getCreatedBy()) + .map(UserEntity::getId) + .map(userRepository::findOne) + .map(UserEntity::getName) + .ifPresent(toolDTO::setCreatedByName); + Optional.ofNullable(tool.getModifiedBy()) + .map(UserEntity::getId) + .map(userRepository::findOne) + .map(UserEntity::getName) + .ifPresent(toolDTO::setModifiedByName); + toolDTO.setCreatedDate(t.getCreatedDate() != null ? new SimpleDateFormat(DATE_TIME_FORMAT).format(t.getCreatedDate()) : null); + toolDTO.setModifiedDate(t.getModifiedDate() != null ? new SimpleDateFormat(DATE_TIME_FORMAT).format(t.getModifiedDate()) : null); + toolDTO.setEnabled(t.getEnabled()); + return toolDTO; + }) + .orElse(null); + } + } \ No newline at end of file diff --git a/src/main/java/org/ohdsi/webapi/tool/converter/ToolConvertor.java b/src/main/java/org/ohdsi/webapi/tool/converter/ToolConvertor.java deleted file mode 100644 index 3b31f6144..000000000 --- a/src/main/java/org/ohdsi/webapi/tool/converter/ToolConvertor.java +++ /dev/null @@ -1,82 +0,0 @@ -package org.ohdsi.webapi.tool.converter; - -import org.ohdsi.webapi.service.AbstractDaoService; -import org.ohdsi.webapi.shiro.Entities.UserEntity; -import org.ohdsi.webapi.shiro.Entities.UserRepository; -import org.ohdsi.webapi.shiro.PermissionManager; -import org.ohdsi.webapi.tool.Tool; -import org.ohdsi.webapi.tool.ToolRepository; -import org.ohdsi.webapi.tool.dto.ToolDTO; -import org.ohdsi.webapi.util.DateUtils; -import org.springframework.stereotype.Component; - -import java.time.Instant; -import java.util.Date; -import java.util.Optional; - -@Component -public class ToolConvertor extends AbstractDaoService { - private final ToolRepository toolRepository; - private final UserRepository userRepository; - - public ToolConvertor(ToolRepository toolRepository, UserRepository userRepository) { - this.toolRepository = toolRepository; - this.userRepository = userRepository; - } - - public Tool toEntity(ToolDTO toolDTO) { - boolean isNewTool = toolDTO.getId() == null; - Tool tool = isNewTool ? new Tool() : toolRepository.findOne(toolDTO.getId()); - Instant currentInstant = Instant.now(); - if (isNewTool) { - setCreationDetails(tool, currentInstant); - } else { - setModificationDetails(tool, currentInstant); - } - updateToolFromDTO(tool, toolDTO); - return tool; - } - - private void setCreationDetails(Tool tool, Instant currentInstant) { - tool.setCreatedDate(Date.from(currentInstant)); - tool.setCreatedBy(getCurrentUser()); - } - - private void setModificationDetails(Tool tool, Instant currentInstant) { - tool.setModifiedDate(Date.from(currentInstant)); - tool.setModifiedBy(getCurrentUser()); - } - - private void updateToolFromDTO(Tool tool, ToolDTO toolDTO) { - Optional.ofNullable(toolDTO.getName()).ifPresent(tool::setName); - Optional.ofNullable(toolDTO.getUrl()).ifPresent(tool::setUrl); - Optional.ofNullable(toolDTO.getDescription()).ifPresent(tool::setDescription); - Optional.ofNullable(toolDTO.getEnabled()).ifPresent(tool::setEnabled); - } - - public ToolDTO toDTO(Tool tool) { - return Optional.ofNullable(tool) - .map(t -> { - ToolDTO toolDTO = new ToolDTO(); - toolDTO.setId(t.getId()); - toolDTO.setName(t.getName()); - toolDTO.setUrl(t.getUrl()); - toolDTO.setDescription(t.getDescription()); - Optional.ofNullable(tool.getCreatedBy()) - .map(UserEntity::getId) - .map(userRepository::findOne) - .map(UserEntity::getName) - .ifPresent(toolDTO::setCreatedByName); - Optional.ofNullable(tool.getModifiedBy()) - .map(UserEntity::getId) - .map(userRepository::findOne) - .map(UserEntity::getName) - .ifPresent(toolDTO::setModifiedByName); - toolDTO.setCreatedDate(DateUtils.dateToString(t.getCreatedDate())); - toolDTO.setModifiedDate(DateUtils.dateToString(t.getModifiedDate())); - toolDTO.setEnabled(t.getEnabled()); - return toolDTO; - }) - .orElse(null); - } -} diff --git a/src/main/java/org/ohdsi/webapi/util/DateUtils.java b/src/main/java/org/ohdsi/webapi/util/DateUtils.java deleted file mode 100644 index addfb0ce0..000000000 --- a/src/main/java/org/ohdsi/webapi/util/DateUtils.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.ohdsi.webapi.util; - -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; - -public class DateUtils { - private static final String DATE_FORMAT = "yyyy-MM-dd"; - private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; - - public static String dateToString(Date date) { - if (date == null) return null; - DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT); - return df.format(date); - } -} diff --git a/src/main/resources/db/migration/postgresql/V2.13.1.20231213141052__create_table_tools.sql b/src/main/resources/db/migration/postgresql/V2.13.1.20231213141052__create_table_tools.sql deleted file mode 100644 index 27f01965a..000000000 --- a/src/main/resources/db/migration/postgresql/V2.13.1.20231213141052__create_table_tools.sql +++ /dev/null @@ -1,18 +0,0 @@ -CREATE TABLE IF NOT EXISTS ${ohdsiSchema}.tool ( - id BIGINT, - name VARCHAR(255) NOT NULL, - url VARCHAR(1000) NOT NULL, - description VARCHAR(1000), - is_enabled BOOLEAN, - created_by_id INTEGER, - modified_by_id INTEGER, - created_date TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT (now()), - modified_date TIMESTAMP WITH TIME ZONE -); - -ALTER TABLE ${ohdsiSchema}.tool ADD CONSTRAINT PK_tool PRIMARY KEY (id); - -ALTER TABLE ${ohdsiSchema}.tool ADD CONSTRAINT fk_tool_ser_user_creator FOREIGN KEY (created_by_id) REFERENCES ${ohdsiSchema}.sec_user(id); -ALTER TABLE ${ohdsiSchema}.tool ADD CONSTRAINT fk_tool_ser_user_updater FOREIGN KEY (modified_by_id) REFERENCES ${ohdsiSchema}.sec_user(id); - -CREATE SEQUENCE ${ohdsiSchema}.tool_seq START WITH 1 INCREMENT BY 1 MAXVALUE 9223372036854775807 NO CYCLE; \ No newline at end of file diff --git a/src/main/resources/db/migration/postgresql/V2.14.0.20240506100635__add_delete_tool_permission_if_not_exists.sql b/src/main/resources/db/migration/postgresql/V2.14.0.20240506100635__add_delete_tool_permission_if_not_exists.sql deleted file mode 100644 index 32480b500..000000000 --- a/src/main/resources/db/migration/postgresql/V2.14.0.20240506100635__add_delete_tool_permission_if_not_exists.sql +++ /dev/null @@ -1,18 +0,0 @@ -INSERT INTO ${ohdsiSchema}.sec_permission (id, value, description) -SELECT nextval('${ohdsiSchema}.sec_permission_id_seq'), 'tool:*:delete', 'Delete Tool' -WHERE NOT EXISTS ( - SELECT NULL FROM ${ohdsiSchema}.sec_permission - WHERE value = 'tool:*:delete' -); - -INSERT INTO ${ohdsiSchema}.sec_role_permission(id, role_id, permission_id) -SELECT nextval('${ohdsiSchema}.sec_role_permission_sequence'), sr.id, sp.id -FROM ${ohdsiSchema}.sec_permission SP, ${ohdsiSchema}.sec_role sr -WHERE sp.value IN ( - 'tool:*:delete' - ) AND sr.name IN ('admin') - AND NOT EXISTS ( - SELECT NULL FROM ${ohdsiSchema}.sec_role_permission - WHERE permission_id = sp.id and role_id = sr.id); - - diff --git a/src/main/resources/db/migration/postgresql/V2.14.0.20240226195200__tools_module_permissions.sql b/src/main/resources/db/migration/postgresql/V2.15.0.20231213141052__create_table_tools_and_permissions.sql similarity index 62% rename from src/main/resources/db/migration/postgresql/V2.14.0.20240226195200__tools_module_permissions.sql rename to src/main/resources/db/migration/postgresql/V2.15.0.20231213141052__create_table_tools_and_permissions.sql index ecd44ce80..765948bd7 100644 --- a/src/main/resources/db/migration/postgresql/V2.14.0.20240226195200__tools_module_permissions.sql +++ b/src/main/resources/db/migration/postgresql/V2.15.0.20231213141052__create_table_tools_and_permissions.sql @@ -1,3 +1,22 @@ +CREATE TABLE IF NOT EXISTS ${ohdsiSchema}.tool ( + id BIGINT, + name VARCHAR(255) NOT NULL, + url VARCHAR(1000) NOT NULL, + description VARCHAR(1000), + is_enabled BOOLEAN, + created_by_id INTEGER, + modified_by_id INTEGER, + created_date TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT (now()), + modified_date TIMESTAMP WITH TIME ZONE +); + +ALTER TABLE ${ohdsiSchema}.tool ADD CONSTRAINT PK_tool PRIMARY KEY (id); + +ALTER TABLE ${ohdsiSchema}.tool ADD CONSTRAINT fk_tool_ser_user_creator FOREIGN KEY (created_by_id) REFERENCES ${ohdsiSchema}.sec_user(id); +ALTER TABLE ${ohdsiSchema}.tool ADD CONSTRAINT fk_tool_ser_user_updater FOREIGN KEY (modified_by_id) REFERENCES ${ohdsiSchema}.sec_user(id); + +CREATE SEQUENCE ${ohdsiSchema}.tool_seq START WITH 1 INCREMENT BY 1 MAXVALUE 9223372036854775807 NO CYCLE; + INSERT INTO ${ohdsiSchema}.sec_permission(id, value, description) VALUES (nextval('${ohdsiSchema}.sec_permission_id_seq'), 'tool:post', 'Create Tool'); INSERT INTO ${ohdsiSchema}.sec_permission(id, value, description) VALUES @@ -26,4 +45,4 @@ FROM ${ohdsiSchema}.sec_permission SP, ${ohdsiSchema}.sec_role sr WHERE sp.value IN ( 'tool:get', 'tool:*:get' - ) AND sr.name IN ('Atlas users'); \ No newline at end of file + ) AND sr.name IN ('Atlas users');