Skip to content

Commit

Permalink
Combining migration scripts into one, removing redundant classes, min…
Browse files Browse the repository at this point in the history
…or renaming and imports notation
  • Loading branch information
alex-odysseus committed Dec 17, 2024
1 parent a6d5274 commit 1e36b5c
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 147 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/ohdsi/webapi/tool/Tool.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class Tool extends CommonEntity<Integer> {
@Column(name = "description")
private String description;
@Column(name = "is_enabled")
private Boolean isEnabled;
private Boolean enabled;

@Override
public Integer getId() {
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/ohdsi/webapi/tool/ToolController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
76 changes: 68 additions & 8 deletions src/main/java/org/ohdsi/webapi/tool/ToolServiceImpl.java
Original file line number Diff line number Diff line change
@@ -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<ToolDTO> getTools() {
List<Tool> 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);
}
Expand All @@ -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
Expand All @@ -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);
}

}
82 changes: 0 additions & 82 deletions src/main/java/org/ohdsi/webapi/tool/converter/ToolConvertor.java

This file was deleted.

16 changes: 0 additions & 16 deletions src/main/java/org/ohdsi/webapi/util/DateUtils.java

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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');
) AND sr.name IN ('Atlas users');

0 comments on commit 1e36b5c

Please sign in to comment.