use of eu.bcvsolutions.idm.core.api.domain.Identifiable in project CzechIdMng by bcvsolutions.
the class DefaultAttachmentManagerIntegrationTest method testAttachmentVersions.
@Test
public void testAttachmentVersions() throws IOException {
Identifiable owner = new TestOwnerEntity(UUID.randomUUID());
String contentOne = "test data 1";
IdmAttachmentDto attachmentOne = prepareAttachment(contentOne);
attachmentOne = attachmentManager.saveAttachment(owner, attachmentOne);
String contentTwo = "test data 2";
IdmAttachmentDto attachmentTwo = prepareAttachment(contentTwo);
attachmentTwo = attachmentManager.saveAttachmentVersion(owner, attachmentTwo, attachmentOne);
String contentThree = "test data 3";
IdmAttachmentDto attachmentThree = prepareAttachment(contentThree);
attachmentThree = attachmentManager.saveAttachmentVersion(owner, attachmentThree, attachmentTwo);
//
Assert.assertNotNull(attachmentManager.get(attachmentOne));
Assert.assertNotNull(attachmentManager.get(attachmentTwo));
Assert.assertNotNull(attachmentManager.get(attachmentThree));
//
// last version only
List<IdmAttachmentDto> attachments = attachmentManager.getAttachments(owner, null).getContent();
Assert.assertEquals(1, attachments.size());
InputStream is = attachmentManager.getAttachmentData(attachments.get(0).getId());
try {
Assert.assertEquals(contentThree, IOUtils.toString(is));
} finally {
IOUtils.closeQuietly(is);
}
//
attachments = attachmentManager.getAttachmentVersions(attachmentOne.getId());
Assert.assertEquals(3, attachments.size());
// three
Assert.assertEquals(Integer.valueOf(3), attachments.get(0).getVersionNumber());
Assert.assertEquals("3.0", attachments.get(0).getVersionLabel());
Assert.assertNull(attachments.get(0).getNextVersion());
Assert.assertEquals(attachmentOne.getId(), attachments.get(0).getParent());
is = attachmentManager.getAttachmentData(attachments.get(0).getId());
try {
Assert.assertEquals(contentThree, IOUtils.toString(is));
} finally {
IOUtils.closeQuietly(is);
}
// two
Assert.assertEquals(Integer.valueOf(2), attachments.get(1).getVersionNumber());
Assert.assertEquals("2.0", attachments.get(1).getVersionLabel());
Assert.assertEquals(attachmentThree.getId(), attachments.get(1).getNextVersion());
Assert.assertEquals(attachmentOne.getId(), attachments.get(1).getParent());
is = attachmentManager.getAttachmentData(attachments.get(1).getId());
try {
Assert.assertEquals(contentTwo, IOUtils.toString(is));
} finally {
IOUtils.closeQuietly(is);
}
// one
Assert.assertEquals(Integer.valueOf(1), attachments.get(2).getVersionNumber());
Assert.assertEquals("1.0", attachments.get(2).getVersionLabel());
Assert.assertEquals(attachmentTwo.getId(), attachments.get(2).getNextVersion());
Assert.assertNull(attachments.get(2).getParent());
is = attachmentManager.getAttachmentData(attachments.get(2).getId());
try {
Assert.assertEquals(contentOne, IOUtils.toString(is));
} finally {
IOUtils.closeQuietly(is);
}
//
attachmentManager.deleteAttachment(attachmentThree);
//
Assert.assertNull(attachmentManager.get(attachmentOne));
Assert.assertNull(attachmentManager.get(attachmentTwo));
Assert.assertNull(attachmentManager.get(attachmentThree));
}
use of eu.bcvsolutions.idm.core.api.domain.Identifiable in project CzechIdMng by bcvsolutions.
the class AbstractReadDtoService method getEntity.
/**
* Returns entity by given id. Returns null, if entity is not exists. For
* AbstractEntity uuid or uuid as string could be given.
*/
protected E getEntity(Serializable id, BasePermission... permission) {
if (id instanceof Identifiable) {
// dto or entity could be given
id = ((Identifiable) id).getId();
}
// resolve identifier
UUID identifier;
if (AbstractEntity.class.isAssignableFrom(getEntityClass()) && (id instanceof String)) {
// different argument type
try {
identifier = UUID.fromString((String) id);
} catch (IllegalArgumentException ex) {
// simply not found
LOG.trace("Identity cannot be found by given identifier [{}]", id);
//
return null;
}
} else {
identifier = (UUID) id;
}
// get entity
E entity = getRepository().findById(identifier).orElse(null);
//
LOG.trace("Entity found [{}], permissions [{}] will be evaluated ...", entity, permission);
entity = checkAccess(entity, permission);
//
LOG.trace("Entity [{}] passed permission check [{}]", entity, permission);
return entity;
}
use of eu.bcvsolutions.idm.core.api.domain.Identifiable in project CzechIdMng by bcvsolutions.
the class IdmLongRunningTaskItemController method loadEmbeddedEntity.
/**
* Fills referenced entity to dto - prevent to load entity for each row
*
* @param dto
*/
@SuppressWarnings("unchecked")
private void loadEmbeddedEntity(Map<UUID, BaseDto> loadedDtos, IdmProcessedTaskItemDto dto) {
UUID entityId = dto.getReferencedEntityId();
try {
BaseDto item = null;
if (!loadedDtos.containsKey(entityId)) {
String referencedDtoType = dto.getReferencedDtoType();
item = getLookupService().lookupDto(referencedDtoType, entityId);
// try to find in audit for deleted entities
if (item == null) {
dto.setDeleted(true);
Object lastPersistedVersion = auditService.findLastPersistedVersion(getLookupService().getEntityClass((Class<? extends Identifiable>) Class.forName(referencedDtoType)), entityId);
if (lastPersistedVersion != null) {
item = getLookupService().toDto((BaseEntity) lastPersistedVersion, null, null);
}
}
loadedDtos.put(entityId, item);
}
dto.getEmbedded().put(IdmProcessedTaskItem_.referencedEntityId.getName(), loadedDtos.get(entityId));
} catch (IllegalArgumentException | ClassNotFoundException ex) {
LOG.debug("Class [{}] not found on classpath (e.g. module was uninstalled)", dto.getReferencedDtoType(), ex);
}
}
use of eu.bcvsolutions.idm.core.api.domain.Identifiable in project CzechIdMng by bcvsolutions.
the class FormValueDeleteBulkActionIntegrationTest method processBulkActionByIds.
@Test
public void processBulkActionByIds() {
Identifiable owner = getHelper().createIdentity((GuardedString) null);
//
// create definition with attribute
IdmFormAttributeDto attribute = new IdmFormAttributeDto();
String attributeName = getHelper().createName();
attribute.setCode(attributeName);
attribute.setName(attribute.getCode());
attribute.setMultiple(true);
attribute.setPersistentType(PersistentType.SHORTTEXT);
IdmFormDefinitionDto formDefinitionOne = formService.createDefinition(IdmIdentity.class, getHelper().createName(), Lists.newArrayList(attribute));
attribute = formDefinitionOne.getMappedAttributeByCode(attribute.getCode());
//
// fill values
formService.saveValues(owner, attribute, Lists.newArrayList(FORM_VALUE_ONE, FORM_VALUE_TWO));
Map<String, List<IdmFormValueDto>> m = formService.getFormInstance(owner, formDefinitionOne).toValueMap();
//
// check value and persistent type
Assert.assertEquals(2, m.get(attributeName).size());
Assert.assertTrue(m.get(attributeName).stream().anyMatch(v -> v.getValue().equals(FORM_VALUE_ONE)));
Assert.assertTrue(m.get(attributeName).stream().anyMatch(v -> v.getValue().equals(FORM_VALUE_TWO)));
//
IdmBulkActionDto bulkAction = findBulkAction(IdmFormValue.class, FormValueDeleteBulkAction.NAME);
Set<UUID> ids = new HashSet<>();
ids.add(m.get(attributeName).stream().filter(v -> v.getValue().equals(FORM_VALUE_ONE)).findFirst().get().getId());
bulkAction.setIdentifiers(ids);
IdmBulkActionDto processAction = bulkActionManager.processAction(bulkAction);
checkResultLrt(processAction, 1l, null, null);
m = formService.getFormInstance(owner, formDefinitionOne).toValueMap();
//
Assert.assertEquals(1, m.get(attributeName).size());
Assert.assertTrue(m.get(attributeName).stream().anyMatch(v -> v.getValue().equals(FORM_VALUE_TWO)));
}
use of eu.bcvsolutions.idm.core.api.domain.Identifiable in project CzechIdMng by bcvsolutions.
the class CodeableEvaluator method lookupEntity.
/**
* Find entity by identifiable object ... this is little strange (we find entity only for adding it to other search)
*
* @param policy
* @return
*/
@SuppressWarnings("unchecked")
private BaseEntity lookupEntity(AuthorizationPolicy policy) {
Object identifier = policy.getEvaluatorProperties().get(PARAMETER_IDENTIFIER);
if (identifier == null || StringUtils.isEmpty(policy.getAuthorizableType())) {
return null;
}
// find entity by identifiable object ... this is little strange (we find entity only for adding it to other search)
BaseEntity entity;
try {
entity = lookupService.lookupEntity((Class<? extends Identifiable>) Class.forName(policy.getAuthorizableType()), identifier.toString());
} catch (ClassNotFoundException ex) {
LOG.warn("Class for name [{}] not found - skipping", policy.getAuthorizableType());
return null;
} catch (IllegalArgumentException ex) {
LOG.warn("Authorizable type [{}] does not support entity lookup - skipping", policy.getAuthorizableType(), ex);
return null;
}
if (entity == null) {
LOG.debug("Entity for type [{}] and code [{}] wasn't found - skipping", policy.getAuthorizableType(), identifier);
return null;
}
return entity;
}
Aggregations