Skip to content

Commit

Permalink
SHIBUI-1968
Browse files Browse the repository at this point in the history
updated custom entity attribute with resource id and changed the APIs to
use the resource id instead of name for id of the CEAD
  • Loading branch information
chasegawa committed Jun 23, 2021
1 parent 329c3c8 commit 32ee29e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,34 @@ public ResponseEntity<?> getAll() {
return ResponseEntity.ok(caService.getAllDefinitions());
}

@GetMapping("/attribute/{name}")
@GetMapping("/attribute/{resourceId}")
@Transactional(readOnly = true)
public ResponseEntity<?> getOne(@PathVariable String name) {
CustomEntityAttributeDefinition cad = caService.find(name);
public ResponseEntity<?> getOne(@PathVariable String resourceId) {
CustomEntityAttributeDefinition cad = caService.find(resourceId);
if (cad == null) {
HttpHeaders headers = new HttpHeaders();
headers.setLocation(
ServletUriComponentsBuilder.fromCurrentServletMapping().path("/api/custom/entity/attribute/" + name).build().toUri());
ServletUriComponentsBuilder.fromCurrentServletMapping().path("/api/custom/entity/attribute/" + resourceId).build().toUri());

return ResponseEntity.status(HttpStatus.NOT_FOUND).headers(headers)
.body(new ErrorResponse(String.valueOf(HttpStatus.NOT_FOUND.value()),
String.format("The custom attribute definition with name: [%s] does not already exist.", name)));
String.format("The custom attribute definition with resource id: [%s] does not already exist.", resourceId)));
}
return ResponseEntity.ok(cad);
}

@DeleteMapping("/attribute/{name}")
@DeleteMapping("/attribute/{resourceId}")
@Transactional
public ResponseEntity<?> delete(@PathVariable String name) {
CustomEntityAttributeDefinition cad = caService.find(name);
public ResponseEntity<?> delete(@PathVariable String resourceId) {
CustomEntityAttributeDefinition cad = caService.find(resourceId);
if (cad == null) {
HttpHeaders headers = new HttpHeaders();
headers.setLocation(
ServletUriComponentsBuilder.fromCurrentServletMapping().path("/api/custom/entity/attribute/" + name).build().toUri());
ServletUriComponentsBuilder.fromCurrentServletMapping().path("/api/custom/entity/attribute/" + resourceId).build().toUri());

return ResponseEntity.status(HttpStatus.NOT_FOUND).headers(headers)
.body(new ErrorResponse(String.valueOf(HttpStatus.NOT_FOUND.value()),
String.format("The custom attribute definition with name: [%s] does not already exist.", name)));
String.format("The custom attribute definition with resource id: [%s] does not already exist.", resourceId)));
}
caService.deleteDefinition(cad);
return ResponseEntity.noContent().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.PostLoad;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
Expand Down Expand Up @@ -53,7 +52,6 @@ public class CustomEntityAttributeDefinition implements IRelyingPartyOverridePro
@Column(name = "invert", nullable = true)
String invert;

@Id
@Column(nullable = false)
String name;

Expand All @@ -62,6 +60,10 @@ public class CustomEntityAttributeDefinition implements IRelyingPartyOverridePro

@Column(name = "persist_value", nullable = true)
String persistValue;

@Id
@Column(name = "resource_id", nullable = false)
String resourceId = UUID.randomUUID().toString();

@Override
public String getAttributeName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface CustomEntityAttributeDefinitionRepository extends JpaRepository

CustomEntityAttributeDefinition findByName(String name);

CustomEntityAttributeDefinition findByResourceId(String resourceId);

@SuppressWarnings("unchecked")
CustomEntityAttributeDefinition save(CustomEntityAttributeDefinition attribute);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface CustomEntityAttributesDefinitionService {

void deleteDefinition(CustomEntityAttributeDefinition definition);

CustomEntityAttributeDefinition find(String name);
CustomEntityAttributeDefinition find(String resourceId);

List<CustomEntityAttributeDefinition> getAllDefinitions();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public CustomEntityAttributeDefinition createOrUpdateDefinition(CustomEntityAttr
@Override
@Transactional
public void deleteDefinition(CustomEntityAttributeDefinition definition) {
CustomEntityAttributeDefinition entityToRemove = repository.findByName(definition.getName());
CustomEntityAttributeDefinition entityToRemove = repository.findByResourceId(definition.getResourceId());
repository.delete(entityToRemove);
notifyListeners();
}

@Override
public CustomEntityAttributeDefinition find(String name) {
return repository.findByName(name);
public CustomEntityAttributeDefinition find(String resourceId) {
return repository.findByResourceId(resourceId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,7 @@ class CustomEntityAttributeDefinitionRepositoryTests extends Specification {

// delete tests
when:
def delByName = new CustomEntityAttributeDefinition().with {
it.name = "ca-name"
it
}
repo.delete(delByName)
repo.delete(caFromDb1)
entityManager.flush()
entityManager.clear()

Expand All @@ -159,24 +155,12 @@ class CustomEntityAttributeDefinitionRepositoryTests extends Specification {
def setItems2 = new HashSet<String>(["val2", "val1"])
def setItems3 = new HashSet<String>(["val1", "val2", "val3"])
def setItems4 = new HashSet<String>(["val1", "val2", "val3", "val4"])
def ca2 = new CustomEntityAttributeDefinition().with {
it.name = "ca-name"
it.attributeType = "SELECTION_LIST"
it.customAttrListDefinitions = setItems2
it
}
def ca3 = new CustomEntityAttributeDefinition().with {
it.name = "ca-name"
it.attributeType = "SELECTION_LIST"
it.customAttrListDefinitions = setItems3
it
}
def ca4 = new CustomEntityAttributeDefinition().with {
it.name = "ca-name"
it.attributeType = "SELECTION_LIST"
it.customAttrListDefinitions = setItems4
it
}

when:
repo.save(ca3)
Expand All @@ -186,31 +170,39 @@ class CustomEntityAttributeDefinitionRepositoryTests extends Specification {
then:
def cas = repo.findAll()
cas.size() == 1
def caFromDb = cas.get(0).asType(CustomEntityAttributeDefinition)
caFromDb.equals(ca3) == true
def ca3FromDb = cas.get(0).asType(CustomEntityAttributeDefinition)
ca3FromDb.equals(ca3) == true

// now update the attribute list items
caFromDb.with {
ca3FromDb.with {
it.customAttrListDefinitions = setItems4
it
}
repo.save(caFromDb)
repo.save(ca3FromDb)
entityManager.flush()
entityManager.clear()

def caFromDb4 = repo.findAll().get(0).asType(CustomEntityAttributeDefinition)
def ca4 = new CustomEntityAttributeDefinition().with {
it.name = "ca-name"
it.attributeType = "SELECTION_LIST"
it.customAttrListDefinitions = setItems4
it.resourceId = ca3FromDb.resourceId
it
}
caFromDb4.equals(ca4) == true

// now remove items
caFromDb.with {
ca3FromDb.with {
it.customAttrListDefinitions = setItems2
it
}
repo.save(caFromDb)
repo.save(ca3FromDb)
entityManager.flush()
entityManager.clear()

def caFromDb2 = repo.findAll().get(0).asType(CustomEntityAttributeDefinition)
caFromDb2.equals(ca2) == true
ca3FromDb.resourceId == caFromDb2.resourceId
ca3FromDb.customAttrListDefinitions.equals(caFromDb2.customAttrListDefinitions)
}
}

0 comments on commit 32ee29e

Please sign in to comment.