Skip to content

Commit

Permalink
Merge pull request RADAR-base#286 from RADAR-base/fix-source-transfer
Browse files Browse the repository at this point in the history
Fix source transfer
  • Loading branch information
nivemaham authored Jul 13, 2018
2 parents 67acfd7 + 5273a3b commit 2c7de29
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 26 deletions.
16 changes: 15 additions & 1 deletion src/main/java/org/radarcns/management/service/SourceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ public Optional<SourceDTO> findOneByName(String sourceName) {
.map(sourceMapper::sourceToSourceDTO);
}


/**
* Get one source by id.
*
* @param id the id of the source
* @return the entity
*/
@Transactional(readOnly = true)
public Optional<SourceDTO> findOneById(Long id) {
log.debug("Request to get Source by id: {}", id);
return Optional.ofNullable(sourceRepository.findOne(id))
.map(sourceMapper::sourceToSourceDTO);
}

/**
* Delete the device by id.
*
Expand Down Expand Up @@ -142,7 +156,7 @@ public List<MinimalSourceDetailsDTO> findAllMinimalSourceDetailsByProjectAndAssi
* @param attributes value to update
* @return Updated {@link MinimalSourceDetailsDTO} of source
*/
public MinimalSourceDetailsDTO safeUpdate(Source sourceToUpdate,
public MinimalSourceDetailsDTO safeUpdateOfAttributes(Source sourceToUpdate,
Map<String, String> attributes) {

// update source attributes
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/org/radarcns/management/web/rest/SourceResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import javax.validation.Valid;
import org.radarcns.auth.config.Constants;
import org.radarcns.auth.exception.NotAuthorizedException;
import org.radarcns.management.domain.SourceType;
import org.radarcns.management.repository.ProjectRepository;
import org.radarcns.management.repository.SourceRepository;
import org.radarcns.management.service.SourceService;
import org.radarcns.management.service.dto.SourceDTO;
Expand Down Expand Up @@ -60,6 +62,9 @@ public class SourceResource {
@Autowired
private HttpServletRequest servletRequest;

@Autowired
private ProjectRepository projectRepository;

/**
* POST /sources : Create a new source.
*
Expand Down Expand Up @@ -114,6 +119,38 @@ public ResponseEntity<SourceDTO> updateSource(@Valid @RequestBody SourceDTO sour
return createSource(sourceDto);
}
checkPermission(getJWT(servletRequest), SOURCE_UPDATE);
Optional<SourceDTO> sourceToUpdateDto = sourceService
.findOneById(sourceDto.getId());

if (!sourceToUpdateDto.isPresent()) {
return ResponseEntity.notFound().headers(HeaderUtil.createFailureAlert(ENTITY_NAME,
"sourceNotFound",
"Cannot find a source by sourceName " + sourceDto.getSourceName())).build();
}
SourceDTO sourceToUpdate = sourceToUpdateDto.get();

// if the source is being transferred to another project.
if (!sourceToUpdate.getProject().getId().equals(sourceDto.getProject().getId())) {
if (sourceToUpdate.getAssigned()) {
return ResponseEntity.badRequest().headers(
HeaderUtil.createFailureAlert(ENTITY_NAME, "sourceIsAssigned",
"Cannot transfer an assigned source")).build();
}
// check whether source-type of the device is assigned to the new project
// to be transferred.
Optional<SourceType> sourceType = projectRepository
.findSourceTypeByProjectIdAndSourceTypeId(sourceDto.getProject().getId(),
sourceToUpdate.getSourceType().getId());
if (!sourceType.isPresent()) {
return ResponseEntity.badRequest().headers(HeaderUtil
.createFailureAlert(ENTITY_NAME,
"invalidTransfer", "Cannot transfer a source to a project which doesn't "
+ "have compatible source-type")).build();
}
// set old source-type, ensures compatibility
sourceDto.setSourceType(sourceToUpdate.getSourceType());

}
SourceDTO result = sourceService.save(sourceDto);
return ResponseEntity.ok()
.headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, sourceDto.getSourceName()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ public ResponseEntity<List<SubjectDTO>> getAllSubjects(@ApiParam Pageable pageab
INACTIVE_PARTICIPANT) : Collections.singletonList(PARTICIPANT);

if (projectName != null && externalId != null) {
Optional<Subject> subject = Optional.empty();
subjectRepository
Optional<Subject> subject = subjectRepository
.findOneByProjectNameAndExternalIdAndAuthoritiesIn(projectName, externalId,
authoritiesToInclude);

Expand Down Expand Up @@ -515,6 +514,7 @@ public ResponseEntity<MinimalSourceDetailsDTO> updateSubjectSource(@PathVariable
}

// there should be only one source under a source-name.
return ResponseEntity.ok().body(sourceService.safeUpdate(sources.get(0), attributes));
return ResponseEntity.ok().body(sourceService.safeUpdateOfAttributes(sources.get(0),
attributes));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,12 @@ <h4 class="modal-title" id="myProjectLabel" jhiTranslate="managementPortalApp.pr
<div class="form-group">
<label class="form-control-label" jhiTranslate="managementPortalApp.project.prettyProjectName" for="field_humanReadableProjectName">Human readable Name</label>
<input type="text" class="form-control" name="prettyProjectName" id="field_humanReadableProjectName"
[(ngModel)]="project.humanReadableProjectName" value="{{projectIdAsPrettyValue? project.projectName : ''}}" required/>
[(ngModel)]="project.humanReadableProjectName" value="{{projectIdAsPrettyValue? project.projectName : ''}}"/>
<small id="prettyProjectNameHelp" class="form-text text-muted">Human readable project name that doesn't have to follow url pattern.</small>
<small>
<input type="checkbox" class="form-control-check" name="grantTypes" [checked]="projectIdAsPrettyValue" (change)="projectIdAsPrettyValue = !projectIdAsPrettyValue" />
Do you want to use the project-id as the human readable name?
</small>
<div [hidden]="!(editForm.controls.humanReadableProjectName?.dirty && editForm.controls.humanReadableProjectName?.invalid)">
<small class="form-text text-danger"
[hidden]="!editForm.controls.humanReadableProjectName?.errors?.required" jhiTranslate="entity.validation.required">
This field is required.
</small>
</div>
</div>

<div class="form-group">
Expand Down
5 changes: 3 additions & 2 deletions src/main/webapp/i18n/en/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
"error": {
"sourceIdExists": "A new source cannot already have a Source ID",
"sourceNameExists": "Source Name is already registered.",
"sourceIsAssigned": "Cannot edit/delete a currently assigned source.",
"sourceIsAssigned": "Cannot transfer/delete a currently assigned source.",
"sourceAssignedRequired": "A new source must have the 'assigned' field specified.",
"validation": "Validation error, check the entered values in the fields."
"validation": "Validation error, check the entered values in the fields.",
"invalidTransfer" : "Cannot transfer a source to a project which doesn't have compatible source-type"
}
}
5 changes: 3 additions & 2 deletions src/main/webapp/i18n/nl/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
"error": {
"sourceIdExists": "Een nieuwe source mag geen ID toegewezen hebben.",
"sourceNameExists": "Huidige source naam is al geregistreerd",
"sourceIsAssigned": "Kan een momenteel toegewezen source niet bewerken / verwijderen.",
"sourceIsAssigned": "Kan een momenteel toegewezen bron niet overdragen / verwijderen.",
"sourceAssignedRequired": "Het 'assigned' veld is verplicht voor een nieuwe Source.",
"validation": "Validatie fout, controleer de ingevoerde waarden."
"validation": "Validatie fout, controleer de ingevoerde waarden.",
"invalidTransfer" : "Cannot transfer a source to a project which doesn't have compatible source-type"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.List;
import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.junit.Before;
Expand Down Expand Up @@ -101,9 +100,6 @@ public class ProjectResourceIntTest {
@Autowired
private ExceptionTranslator exceptionTranslator;

@Autowired
private EntityManager em;

@Autowired
private HttpServletRequest servletRequest;

Expand Down Expand Up @@ -135,7 +131,7 @@ public void setUp() throws ServletException {
* <p>This is a static method, as tests for other entities might also need it,
* if they test an entity which requires the current entity.</p>
*/
public static Project createEntity(EntityManager em) {
public static Project createEntity() {
Project project = new Project()
.projectName(DEFAULT_PROJECT_NAME)
.description(DEFAULT_DESCRIPTION)
Expand All @@ -150,7 +146,7 @@ public static Project createEntity(EntityManager em) {

@Before
public void initTest() {
project = createEntity(em);
project = createEntity();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@

import java.util.List;
import java.util.UUID;
import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.radarcns.management.ManagementPortalTestApp;
import org.radarcns.management.domain.Project;
import org.radarcns.management.domain.Source;
import org.radarcns.management.repository.ProjectRepository;
import org.radarcns.management.repository.SourceRepository;
import org.radarcns.management.security.JwtAuthenticationFilter;
import org.radarcns.management.service.SourceService;
Expand Down Expand Up @@ -89,22 +90,25 @@ public class SourceResourceIntTest {
private ExceptionTranslator exceptionTranslator;

@Autowired
private EntityManager em;
private HttpServletRequest servletRequest;

@Autowired
private HttpServletRequest servletRequest;
private ProjectRepository projectRepository;

private MockMvc restDeviceMockMvc;

private Source source;

private Project project;

@Before
public void setUp() throws ServletException {
MockitoAnnotations.initMocks(this);
SourceResource sourceResource = new SourceResource();
ReflectionTestUtils.setField(sourceResource, "servletRequest", servletRequest);
ReflectionTestUtils.setField(sourceResource, "sourceService", sourceService);
ReflectionTestUtils.setField(sourceResource, "sourceRepository", sourceRepository);
ReflectionTestUtils.setField(sourceResource, "projectRepository", projectRepository);

JwtAuthenticationFilter filter = OAuthHelper.createAuthenticationFilter();
filter.init(new MockFilterConfig());
Expand All @@ -123,7 +127,7 @@ public void setUp() throws ServletException {
* <p>This is a static method, as tests for other entities might also need it,
* if they test an entity which requires the current entity.</p>
*/
public static Source createEntity(EntityManager em) {
public static Source createEntity() {
Source source = new Source()
.assigned(DEFAULT_ASSIGNED)
.sourceName(DEFAULT_SOURCE_NAME);
Expand All @@ -132,10 +136,12 @@ public static Source createEntity(EntityManager em) {

@Before
public void initTest() {
source = createEntity(em);
source = createEntity();
List<SourceTypeDTO> sourceTypeDtos = sourceTypeService.findAll();
assertThat(sourceTypeDtos.size()).isGreaterThan(0);
source.setSourceType(sourceTypeMapper.sourceTypeDTOToSourceType(sourceTypeDtos.get(0)));
project = projectRepository.findOne(1L);
source.project(project);
}

@Test
Expand All @@ -156,6 +162,7 @@ public void createSource() throws Exception {
Source testSource = sourceList.get(sourceList.size() - 1);
assertThat(testSource.isAssigned()).isEqualTo(DEFAULT_ASSIGNED);
assertThat(testSource.getSourceName()).isEqualTo(DEFAULT_SOURCE_NAME);
assertThat(testSource.getProject().getProjectName()).isEqualTo(project.getProjectName());
}

@Test
Expand Down

0 comments on commit 2c7de29

Please sign in to comment.