ImExporters update (will not compile yet!)

deploy_packages_to_gitea
Jens Pelzetter 2023-01-11 20:08:27 +01:00
parent 4d4cda372c
commit 3a9a065f50
16 changed files with 1098 additions and 92 deletions

View File

@ -22,6 +22,7 @@ import org.libreccm.imexport.AbstractEntityImExporter;
import org.libreccm.imexport.Processes; import org.libreccm.imexport.Processes;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
@ -29,7 +30,6 @@ import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.NoResultException; import javax.persistence.NoResultException;
import javax.transaction.Transactional;
/** /**
* Exporter/Importer for {@link Categorization} entities. * Exporter/Importer for {@link Categorization} entities.
@ -41,6 +41,10 @@ import javax.transaction.Transactional;
public class CategorizationImExporter public class CategorizationImExporter
extends AbstractEntityImExporter<Categorization> { extends AbstractEntityImExporter<Categorization> {
@Inject
private CategorizationRepository categoriationRepo;
@Inject @Inject
private EntityManager entityManager; private EntityManager entityManager;
@ -60,7 +64,50 @@ public class CategorizationImExporter
} }
@Override @Override
@Transactional(Transactional.TxType.REQUIRED) protected Optional<Categorization> findExistingEntity(final String uuid) {
return categoriationRepo.findByUuid(uuid);
}
@Override
protected void updateExistingEntity(
final Categorization existingEntity, final Categorization importedEntity
) {
if (!Objects.equals(
existingEntity.getCategory(), importedEntity.getCategory()
)) {
existingEntity.setCategory(importedEntity.getCategory());
}
if (!Objects.equals(
existingEntity.getCategorizedObject(),
importedEntity.getCategorizedObject()
)) {
existingEntity.setCategorizedObject(
importedEntity.getCategorizedObject()
);
}
if (existingEntity.isIndexObject() != importedEntity.isIndexObject()) {
existingEntity.setIndexObject(importedEntity.isIndexObject());
}
if (existingEntity.getCategoryOrder() != importedEntity
.getCategoryOrder()) {
existingEntity.setCategoryOrder(importedEntity.getCategoryOrder());
}
if (existingEntity.getObjectOrder() != importedEntity.getObjectOrder()) {
existingEntity.setObjectOrder(importedEntity.getObjectOrder());
}
if (!Objects.equals(existingEntity.getType(), importedEntity.getType())) {
existingEntity.setType(importedEntity.getType());
}
entityManager.merge(existingEntity);
}
@Override
protected void saveImportedEntity(final Categorization entity) { protected void saveImportedEntity(final Categorization entity) {
entityManager.merge(entity); entityManager.merge(entity);
} }

View File

@ -18,18 +18,21 @@
*/ */
package org.libreccm.categorization; package org.libreccm.categorization;
import org.libreccm.core.UnexpectedErrorException;
import org.libreccm.imexport.AbstractEntityImExporter; import org.libreccm.imexport.AbstractEntityImExporter;
import org.libreccm.imexport.Exportable;
import org.libreccm.imexport.Processes; import org.libreccm.imexport.Processes;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.transaction.Transactional;
/** /**
* Exporter/Importer for {@link Category} entities. * Exporter/Importer for {@link Category} entities.
@ -40,6 +43,9 @@ import javax.transaction.Transactional;
@Processes(Category.class) @Processes(Category.class)
public class CategoryImExporter extends AbstractEntityImExporter<Category> { public class CategoryImExporter extends AbstractEntityImExporter<Category> {
@Inject
private CategoryManager categoryManager;
@Inject @Inject
private CategoryRepository categoryRepository; private CategoryRepository categoryRepository;
@ -59,31 +65,146 @@ public class CategoryImExporter extends AbstractEntityImExporter<Category> {
} }
@Override @Override
@Transactional(Transactional.TxType.REQUIRED) protected Optional<Category> findExistingEntity(final String uuid) {
protected void saveImportedEntity(final Category entity) { return categoryRepository.findByUuid(uuid);
final Optional<Category> result = categoryRepository.findByUuid( }
entity.getUuid()
);
final Category category; @Override
if (result.isPresent()) { protected void saveImportedEntity(final Category entity) {
category = result.get(); categoryRepository.save(entity);
category.setAbstractCategory(entity.isAbstractCategory()); }
category.setCategoryOrder(entity.getCategoryOrder());
category.setDescription(entity.getDescription()); @Override
category.setDisplayName(entity.getDisplayName()); protected void updateExistingEntity(
category.setEnabled(entity.isEnabled()); final Category existingEntity,
category.setName(entity.getName()); final Category importedEntity
category.setObjects(entity.getObjects()); ) {
category.setParentCategory(entity.getParentCategory()); if (existingEntity.isAbstractCategory() != importedEntity
category.setSubCategories(entity.getSubCategories()); .isAbstractCategory()) {
category.setTitle(entity.getTitle()); existingEntity.setAbstractCategory(
category.setUniqueId(entity.getUniqueId()); importedEntity.isAbstractCategory()
category.setVisible(entity.isVisible()); );
} else {
category = entity;
} }
categoryRepository.save(category);
if (existingEntity.getCategoryOrder() != importedEntity
.getCategoryOrder()) {
existingEntity.setCategoryOrder(importedEntity.getCategoryOrder());
}
if (!Objects.equals(
existingEntity.getDescription(),
importedEntity.getDescription()
)) {
syncLocalizedStrings(
importedEntity.getDescription(),
existingEntity.getDescription()
);
}
if (!Objects.equals(
existingEntity.getDisplayName(),
importedEntity.getDisplayName()
)) {
existingEntity.setDisplayName(importedEntity.getDisplayName());
}
if (existingEntity.isEnabled() != importedEntity.isEnabled()) {
existingEntity.setEnabled(importedEntity.isEnabled());
}
if (!Objects.equals(
existingEntity.getName(),
importedEntity.getName()
)) {
existingEntity.setName(importedEntity.getName());
}
if (!Objects.equals(
existingEntity.getObjects(),
importedEntity.getObjects()
)) {
final Set<Categorization> categorizationsToRemove = existingEntity
.getObjects()
.stream()
.filter(
categorization -> !importedEntity.getObjects().contains(
categorization
)
)
.collect(Collectors.toSet());
for (Categorization toRemove : categorizationsToRemove) {
try {
categoryManager.removeObjectFromCategory(
toRemove.getCategorizedObject(),
existingEntity
);
} catch (ObjectNotAssignedToCategoryException ex) {
throw new UnexpectedErrorException(ex);
}
}
}
if (!Objects.equals(
existingEntity.getParentCategory(),
importedEntity.getParentCategory()
)) {
final Category oldParentCategory = existingEntity
.getParentCategory();
final Category newParentCategory = importedEntity
.getParentCategory();
categoryManager.removeSubCategoryFromCategory(
existingEntity,
oldParentCategory
);
categoryManager.addSubCategoryToCategory(
existingEntity,
newParentCategory
);
}
if (!Objects.equals(
existingEntity.getSubCategories(),
importedEntity.getSubCategories()
)) {
final Set<Category> subCategoriesToRemove = existingEntity
.getSubCategories()
.stream()
.filter(
subCat -> !importedEntity.getSubCategories().contains(
subCat
)
)
.collect(Collectors.toSet());
for (final Category toRemove : subCategoriesToRemove) {
try {
categoryManager.removeObjectFromCategory(
toRemove, existingEntity
);
} catch (ObjectNotAssignedToCategoryException ex) {
throw new UnexpectedErrorException(ex);
}
}
}
if (!Objects.equals(
existingEntity.getTitle(),
importedEntity.getTitle()
)) {
syncLocalizedStrings(
importedEntity.getTitle(),
existingEntity.getTitle()
);
}
if (existingEntity.isVisible() != importedEntity.isVisible()) {
existingEntity.setVisible(importedEntity.isVisible());
}
categoryRepository.save(existingEntity);
} }
@Override @Override

View File

@ -19,12 +19,14 @@
package org.libreccm.categorization; package org.libreccm.categorization;
import org.libreccm.imexport.AbstractEntityImExporter; import org.libreccm.imexport.AbstractEntityImExporter;
import org.libreccm.imexport.Exportable;
import org.libreccm.imexport.Processes; import org.libreccm.imexport.Processes;
import java.util.Collections; import java.util.Locale;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
@ -39,6 +41,9 @@ import javax.inject.Inject;
@Processes(Domain.class) @Processes(Domain.class)
public class DomainImExporter extends AbstractEntityImExporter<Domain> { public class DomainImExporter extends AbstractEntityImExporter<Domain> {
@Inject
private DomainManager domainManager;
@Inject @Inject
private DomainRepository domainRepository; private DomainRepository domainRepository;
@ -50,16 +55,105 @@ public class DomainImExporter extends AbstractEntityImExporter<Domain> {
@Override @Override
public Class<Domain> getEntityClass() { public Class<Domain> getEntityClass() {
return Domain.class; return Domain.class;
} }
@Override @Override
protected void saveImportedEntity(final Domain entity) { protected Optional<Domain> findExistingEntity(final String uuid) {
return domainRepository.findByUuid(uuid);
}
@Override
protected void saveImportedEntity(final Domain entity) {
domainRepository.save(entity); domainRepository.save(entity);
} }
@Override
protected void updateExistingEntity(
final Domain existingEntity,
final Domain importedEntity
) {
if (!Objects.equals(
existingEntity.getDomainKey(),
importedEntity.getDomainKey()
)) {
existingEntity.setDomainKey(importedEntity.getDomainKey());
}
if (!Objects.equals(existingEntity.getUri(), importedEntity.getUri())) {
existingEntity.setUri(importedEntity.getUri());
}
if (!Objects.equals(
existingEntity.getTitle(),
importedEntity.getTitle()
)) {
syncLocalizedStrings(
importedEntity.getTitle(),
existingEntity.getTitle()
);
}
if (!Objects.equals(
existingEntity.getDescription(),
importedEntity.getDescription()
)) {
for (final Map.Entry<Locale, String> entry : importedEntity
.getDescription().getValues().entrySet()) {
syncLocalizedStrings(
importedEntity.getDescription(),
existingEntity.getDescription()
);
}
}
if (!Objects.equals(
existingEntity.getVersion(),
importedEntity.getVersion()
)) {
existingEntity.setVersion(importedEntity.getVersion());
}
if (!Objects.equals(
existingEntity.getReleased(),
importedEntity.getReleased()
)) {
existingEntity.setReleased(importedEntity.getReleased());
}
if (!Objects.equals(
existingEntity.getOwners(),
importedEntity.getOwners()
)) {
final Set<DomainOwnership> ownersToAdd = importedEntity
.getOwners()
.stream()
.filter(owner -> !existingEntity.getOwners().contains(owner))
.collect(Collectors.toSet());
final Set<DomainOwnership> ownersToRemove = existingEntity
.getOwners()
.stream()
.filter(owner -> !importedEntity.getOwners().contains(owner))
.collect(Collectors.toSet());
for (final DomainOwnership toRemove : ownersToRemove) {
domainManager.removeDomainOwner(
toRemove.getOwner(),
existingEntity
);
}
for (final DomainOwnership toAdd : ownersToAdd) {
domainManager.addDomainOwner(toAdd.getOwner(),
existingEntity,
toAdd.getContext()
);
}
}
domainRepository.save(existingEntity);
}
@Override @Override
protected Domain reloadEntity(final Domain entity) { protected Domain reloadEntity(final Domain entity) {
return domainRepository return domainRepository

View File

@ -19,12 +19,11 @@
package org.libreccm.categorization; package org.libreccm.categorization;
import org.libreccm.imexport.AbstractEntityImExporter; import org.libreccm.imexport.AbstractEntityImExporter;
import org.libreccm.imexport.Exportable;
import org.libreccm.imexport.Processes; import org.libreccm.imexport.Processes;
import org.libreccm.web.CcmApplication; import org.libreccm.web.CcmApplication;
import java.util.HashSet;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
@ -32,7 +31,6 @@ import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.NoResultException; import javax.persistence.NoResultException;
import javax.transaction.Transactional;
/** /**
* Exporter/Importer for {@link DomainOwnership} entities. * Exporter/Importer for {@link DomainOwnership} entities.
@ -45,6 +43,12 @@ import javax.transaction.Transactional;
public class DomainOwnershipImExporter public class DomainOwnershipImExporter
extends AbstractEntityImExporter<DomainOwnership> { extends AbstractEntityImExporter<DomainOwnership> {
@Inject
private DomainManager domainManager;
@Inject
private DomainOwnershipRepository domainOwnershipRepo;
@Inject @Inject
private EntityManager entityManager; private EntityManager entityManager;
@ -65,11 +69,39 @@ public class DomainOwnershipImExporter
} }
@Override @Override
@Transactional(Transactional.TxType.REQUIRED) protected Optional<DomainOwnership> findExistingEntity(final String uuid) {
protected void saveImportedEntity(final DomainOwnership entity) { return domainOwnershipRepo.findByUuid(uuid);
entityManager.persist(entity);
} }
@Override
protected void updateExistingEntity(
final DomainOwnership existingEntity,
final DomainOwnership importedEnity
) {
if (!Objects.equals(
existingEntity.getContext(),
importedEnity.getContext()
)) {
existingEntity.setContext(importedEnity.getContext());
}
if (existingEntity.getOwnerOrder() != importedEnity.getOwnerOrder()) {
existingEntity.setOwnerOrder(importedEnity.getOwnerOrder());
}
if (existingEntity.getDomainOrder() != importedEnity.getDomainOrder()) {
existingEntity.setDomainOrder(importedEnity.getDomainOrder());
}
domainOwnershipRepo.save(existingEntity);
}
@Override
protected void saveImportedEntity(final DomainOwnership entity) {
domainOwnershipRepo.save(entity);
}
@Override @Override
protected DomainOwnership reloadEntity(final DomainOwnership entity) { protected DomainOwnership reloadEntity(final DomainOwnership entity) {
try { try {

View File

@ -19,12 +19,10 @@
package org.libreccm.core; package org.libreccm.core;
import org.libreccm.imexport.AbstractEntityImExporter; import org.libreccm.imexport.AbstractEntityImExporter;
import org.libreccm.imexport.Exportable;
import org.libreccm.imexport.Processes; import org.libreccm.imexport.Processes;
import java.util.Collections;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Optional;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
@ -51,11 +49,66 @@ public class ResourceTypeImExporter
return ResourceType.class; return ResourceType.class;
} }
@Override
protected Optional<ResourceType> findExistingEntity(final String uuid) {
return repository.findByUuid(uuid);
}
@Override @Override
protected void saveImportedEntity(final ResourceType entity) { protected void saveImportedEntity(final ResourceType entity) {
repository.save(entity); repository.save(entity);
} }
@Override
protected void updateExistingEntity(
final ResourceType existingEntity,
final ResourceType importedEntity
) {
if (!Objects.equals(
existingEntity.getTitle(),
importedEntity.getTitle()
)) {
existingEntity.setTitle(importedEntity.getTitle());
}
if (!Objects.equals(
existingEntity.getDescription(),
importedEntity.getDescription()
)) {
syncLocalizedStrings(
importedEntity.getDescription(),
importedEntity.getDescription()
);
}
if (existingEntity.isWorkspaceApplication() != importedEntity
.isWorkspaceApplication()) {
existingEntity.setWorkspaceApplication(
importedEntity.isWorkspaceApplication()
);
}
if (existingEntity.isViewableAsFullPage() != importedEntity
.isViewableAsFullPage()) {
existingEntity.setViewableAsFullPage(
importedEntity.isViewableAsFullPage()
);
}
if (existingEntity.isViewableAsEmbedded() != importedEntity
.isViewableAsEmbedded()) {
existingEntity.setViewableAsEmbedded(
importedEntity.isViewableAsEmbedded()
);
}
if (existingEntity.isSingleton() != importedEntity.isSingleton()) {
existingEntity.setSingleton(importedEntity.isSingleton());
}
repository.save(importedEntity);
}
@Override @Override
protected ResourceType reloadEntity(final ResourceType entity) { protected ResourceType reloadEntity(final ResourceType entity) {
return repository return repository

View File

@ -22,11 +22,15 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.libreccm.l10n.LocalizedString;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Locale;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
@ -124,19 +128,93 @@ public abstract class AbstractEntityImExporter<T extends Exportable> {
this.requiredEntities.addAll(requiredEntities); this.requiredEntities.addAll(requiredEntities);
} }
/**
* Imports an entity from the provided {@code data} and either creates a new
* entity or updates an existing entity.
*
* <strong>This method is not intended to be overwritten by implementations
* of this abstract class.</strong>. Due to technical limitations of the
* Java language it is not possible to mark this method as final.
*
* @param data The entity data to import
*
* @return The imported entity.
*
* @throws ImportExpection If the import fails.
*
* @see #findExistingEntity(java.lang.String)
* @see #saveImportedEntity(org.libreccm.imexport.Exportable)
* @see #updateExistingEntity(org.libreccm.imexport.Exportable,
* org.libreccm.imexport.Exportable)
*/
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public T importEntity(final String data) throws ImportExpection { public T importEntity(final String data) throws ImportExpection {
try { try {
final T entity = objectMapper.readValue(data, getEntityClass()); final T importedEntity = objectMapper.readValue(data,
saveImportedEntity(entity); getEntityClass());
return entity; final Optional<T> existingEntityResult = findExistingEntity(
importedEntity.getUuid()
);
if (existingEntityResult.isPresent()) {
final T existingEntity = existingEntityResult.get();
updateExistingEntity(existingEntity, importedEntity);
return existingEntity;
} else {
saveImportedEntity(importedEntity);
return importedEntity;
}
} catch (IOException ex) { } catch (IOException ex) {
throw new ImportExpection(ex); throw new ImportExpection(ex);
} }
} }
/**
* Retrieve the existing instance of the entity identified by the provided
* UUID.
*
* This method is called by {@link #importEntity(java.lang.String)}.
*
* @param uuid The UUID of the imported entity.
*
* @return An {@link Optional} with the existing entity or an empty
* {@link Optional} if the entity does not yet exist in this
* instance of LibreCCM.
*
* @see #updateExistingEntity(org.libreccm.imexport.Exportable,
* org.libreccm.imexport.Exportable)
* @see #importEntity(java.lang.String)
*/
protected abstract Optional<T> findExistingEntity(final String uuid);
/**
* Save a newly created entity.
*
* This method is called by {@link #importEntity(java.lang.String)}.
*
* @param entity The entity to save.
*
* @see #importEntity(java.lang.String)
*/
protected abstract void saveImportedEntity(T entity); protected abstract void saveImportedEntity(T entity);
/**
* Updates an existing entity. An implementation of this method is expected
* to persist the updated entity. Only properties that differ between the
* imported entity and the existing entity should be updated to match the
* values of the imported entity. Related entities must not be updated by an
* implementation, expect for embedded entities.
*
* @param existingEntity The existing entity.
* @param withImportedEntity The imported entity.
*
* @see #importEntity(java.lang.String)
* @see #findExistingEntity(java.lang.String)
*
*/
protected abstract void updateExistingEntity(
T existingEntity, T withImportedEntity
);
/** /**
* Export an entity (as JSON). There should be no need to overwrite this * Export an entity (as JSON). There should be no need to overwrite this
* method. * method.
@ -167,7 +245,8 @@ public abstract class AbstractEntityImExporter<T extends Exportable> {
/** /**
* Reloads the entity to export. Entities become detacted for several * Reloads the entity to export. Entities become detacted for several
* reasons before they are passed to the null null null null null null null null {@link #exportEntity(org.libreccm.imexport.Exportable) method. The * reasons before they are passed to the null null null null null null null
* null null null null null null {@link #exportEntity(org.libreccm.imexport.Exportable) method. The
* implementation of this should reload the passed entity. * implementation of this should reload the passed entity.
* *
* @param entity The entity to reload. * @param entity The entity to reload.
@ -176,4 +255,32 @@ public abstract class AbstractEntityImExporter<T extends Exportable> {
*/ */
protected abstract T reloadEntity(final T entity); protected abstract T reloadEntity(final T entity);
protected final void syncLocalizedStrings(
final LocalizedString source,
final LocalizedString target
) {
final Set<Locale> localesToAdd = source
.getAvailableLocales()
.stream()
.filter(
locale -> !target.getAvailableLocales().contains(locale)
)
.collect(Collectors.toSet());
final Set<Locale> localesToRemove = target
.getAvailableLocales()
.stream()
.filter(locale -> !source.getAvailableLocales().contains(locale))
.collect(Collectors.toSet());
for(final Locale toRemove : localesToRemove) {
target.removeValue(toRemove);
}
for(final Locale toAdd : localesToAdd) {
target.putValue(toAdd, source.getValue(toAdd));
}
}
} }

View File

@ -61,6 +61,7 @@ import javax.json.JsonObject;
import javax.json.JsonObjectBuilder; import javax.json.JsonObjectBuilder;
import javax.json.JsonReader; import javax.json.JsonReader;
import javax.json.JsonString; import javax.json.JsonString;
import javax.json.JsonValue;
import javax.json.JsonWriter; import javax.json.JsonWriter;
import javax.transaction.Transactional; import javax.transaction.Transactional;
@ -205,7 +206,6 @@ public class ImportExport {
* *
* @see CcmFilesConfiguration#dataPath * @see CcmFilesConfiguration#dataPath
*/ */
@Transactional(Transactional.TxType.REQUIRED)
public void importEntities(final String importName) { public void importEntities(final String importName) {
final String importsPath = String.format("imports/%s", importName); final String importsPath = String.format("imports/%s", importName);
@ -242,15 +242,12 @@ public class ImportExport {
.filter(node -> filterImporters(manifest, node)) .filter(node -> filterImporters(manifest, node))
.collect(Collectors.toList()); .collect(Collectors.toList());
importers for(EntityImExporterTreeNode importer : importers) {
.stream() importEntitiesOfType(
.map(EntityImExporterTreeNode::getEntityImExporter) importName,
.forEach( importer.getEntityImExporter()
imExporter -> importEntitiesOfType(
importName,
imExporter
)
); );
}
} catch (DependencyException ex) { } catch (DependencyException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} }
@ -287,14 +284,14 @@ public class ImportExport {
final JsonObject toc = tocReader.readObject(); final JsonObject toc = tocReader.readObject();
final JsonArray files = toc.getJsonArray("files"); final JsonArray files = toc.getJsonArray("files");
files.forEach( for(JsonValue value : files) {
value -> importEntity( importEntity(
importName, importName,
type, type,
((JsonString) value).getString(), ((JsonString) value).getString(),
entityImExporter entityImExporter
) );
); }
} catch (IOException } catch (IOException
| FileDoesNotExistException | FileDoesNotExistException
| FileAccessException | FileAccessException

View File

@ -19,18 +19,17 @@
package org.libreccm.security; package org.libreccm.security;
import org.libreccm.imexport.AbstractEntityImExporter; import org.libreccm.imexport.AbstractEntityImExporter;
import org.libreccm.imexport.Exportable;
import org.libreccm.imexport.Processes; import org.libreccm.imexport.Processes;
import java.util.Collections;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.transaction.Transactional;
/** /**
* Exporter/Importer for {@link Group}s. * Exporter/Importer for {@link Group}s.
@ -44,6 +43,9 @@ public class GroupImExporter extends AbstractEntityImExporter<Group> {
@Inject @Inject
private EntityManager entityManager; private EntityManager entityManager;
@Inject
private GroupManager groupManager;
@Inject @Inject
private GroupRepository groupRepository; private GroupRepository groupRepository;
@ -59,12 +61,69 @@ public class GroupImExporter extends AbstractEntityImExporter<Group> {
} }
@Override @Override
@Transactional(Transactional.TxType.REQUIRED) protected Optional<Group> findExistingEntity(final String uuid) {
return groupRepository.findByUuid(uuid);
}
@Override
protected void saveImportedEntity(final Group entity) { protected void saveImportedEntity(final Group entity) {
entity.setPartyId(0); entity.setPartyId(0);
entityManager.persist(entity); entityManager.persist(entity);
} }
@Override
protected void updateExistingEntity(
final Group existingEntity,
final Group importedEntity
) {
if (!Objects.equals(
existingEntity.getName(),
importedEntity.getName()
)) {
existingEntity.setName(importedEntity.getName());
}
if (!Objects.equals(
existingEntity.getMemberships(),
importedEntity.getMemberships()
)) {
final Set<GroupMembership> membershipsToRemove = existingEntity
.getMemberships()
.stream()
.filter(
membership -> !importedEntity.getMemberships().contains(
membership
)
)
.collect(Collectors.toSet());
final Set<GroupMembership> membershipsToAdd = importedEntity
.getMemberships()
.stream()
.filter(
membership -> !existingEntity.getMemberships().contains(
membership
)
)
.collect(Collectors.toSet());
for (final GroupMembership membership : membershipsToRemove) {
groupManager.removeMemberFromGroup(
membership.getMember(),
existingEntity
);
}
for(final GroupMembership membership: membershipsToAdd) {
groupManager.addMemberToGroup(
membership.getMember(),
existingEntity
);
}
}
groupRepository.save(existingEntity);
}
@Override @Override
protected Group reloadEntity(final Group entity) { protected Group reloadEntity(final Group entity) {
return groupRepository return groupRepository

View File

@ -22,13 +22,13 @@ import org.libreccm.imexport.AbstractEntityImExporter;
import org.libreccm.imexport.Processes; import org.libreccm.imexport.Processes;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.NoResultException; import javax.persistence.NoResultException;
import javax.transaction.Transactional;
/** /**
* Exporter/Importer for {@link GroupMembership} entities. * Exporter/Importer for {@link GroupMembership} entities.
@ -40,6 +40,9 @@ import javax.transaction.Transactional;
public class GroupMembershipImExporter public class GroupMembershipImExporter
extends AbstractEntityImExporter<GroupMembership> { extends AbstractEntityImExporter<GroupMembership> {
@Inject
private GroupMembershipRepository groupMembershipRepo;
@Inject @Inject
private EntityManager entityManager; private EntityManager entityManager;
@ -60,12 +63,26 @@ public class GroupMembershipImExporter
} }
@Override @Override
@Transactional(Transactional.TxType.REQUIRED) protected Optional<GroupMembership> findExistingEntity(
final String uuid
) {
return groupMembershipRepo.findByUuid(uuid);
}
@Override
protected void saveImportedEntity(final GroupMembership entity) { protected void saveImportedEntity(final GroupMembership entity) {
entity.setMembershipId(0); entity.setMembershipId(0);
entityManager.persist(entity); entityManager.persist(entity);
} }
@Override
protected void updateExistingEntity(
final GroupMembership existingEntity,
final GroupMembership importedEntity
) {
// No update, update is handled by GroupImporter
}
@Override @Override
protected GroupMembership reloadEntity(final GroupMembership entity) { protected GroupMembership reloadEntity(final GroupMembership entity) {
try { try {

View File

@ -22,6 +22,7 @@ import org.libreccm.imexport.AbstractEntityImExporter;
import org.libreccm.imexport.Processes; import org.libreccm.imexport.Processes;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
@ -55,11 +56,65 @@ public class PermissionImExporter extends AbstractEntityImExporter<Permission> {
return Permission.class; return Permission.class;
} }
@Override
protected Optional<Permission> findExistingEntity(final String uuid) {
return permissionRepository.findByUuid(uuid);
}
@Override @Override
protected void saveImportedEntity(final Permission entity) { protected void saveImportedEntity(final Permission entity) {
permissionRepository.save(entity); permissionRepository.save(entity);
} }
@Override
protected void updateExistingEntity(
final Permission existingEntity,
final Permission importedEntity
) {
if (!Objects.equals(
existingEntity.getGrantedPrivilege(),
importedEntity.getGrantedPrivilege()
)) {
existingEntity.setGrantedPrivilege(
importedEntity.getGrantedPrivilege()
);
}
if (!Objects.equals(
existingEntity.getCreationUser(),
importedEntity.getCreationUser()
)) {
existingEntity.setCreationUser(importedEntity.getCreationUser());
}
if (!Objects.equals(
existingEntity.getCreationDate(),
importedEntity.getCreationDate()
)) {
existingEntity.setCreationDate(importedEntity.getCreationDate());
}
if (!Objects.equals(
existingEntity.getCreationIp(),
importedEntity.getCreationIp()
)) {
existingEntity.setCreationIp(importedEntity.getCreationIp());
}
if (existingEntity.isInherited() != importedEntity.isInherited()) {
existingEntity.setInherited(importedEntity.isInherited());
}
if (!Objects.equals(
existingEntity.getInheritedFrom(),
importedEntity.getInheritedFrom()
)) {
existingEntity.setInheritedFrom(importedEntity.getInheritedFrom());
}
permissionRepository.save(existingEntity);
}
@Override @Override
protected Permission reloadEntity(final Permission entity) { protected Permission reloadEntity(final Permission entity) {
return permissionRepository return permissionRepository

View File

@ -20,21 +20,22 @@ package org.libreccm.security;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.libreccm.core.UnexpectedErrorException;
import org.libreccm.imexport.AbstractEntityImExporter; import org.libreccm.imexport.AbstractEntityImExporter;
import org.libreccm.imexport.ExportException; import org.libreccm.imexport.ExportException;
import org.libreccm.imexport.Exportable; import org.libreccm.imexport.Exportable;
import org.libreccm.imexport.Processes; import org.libreccm.imexport.Processes;
import org.libreccm.workflow.AssignableTaskManager;
import org.libreccm.workflow.TaskAssignment;
import java.util.Collections;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.enterprise.context.Dependent; import javax.enterprise.context.Dependent;
import javax.inject.Inject; import javax.inject.Inject;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.transaction.Transactional;
/** /**
* Exporter/Importer for {@link Role}s. * Exporter/Importer for {@link Role}s.
@ -46,14 +47,24 @@ import javax.transaction.Transactional;
public class RoleImExporter extends AbstractEntityImExporter<Role> { public class RoleImExporter extends AbstractEntityImExporter<Role> {
private static final Logger LOGGER = LogManager.getLogger( private static final Logger LOGGER = LogManager.getLogger(
RoleImExporter.class); RoleImExporter.class
);
@Inject @Inject
private EntityManager entityManager; private EntityManager entityManager;
@Inject
private RoleManager roleManager;
@Inject @Inject
private RoleRepository roleRepository; private RoleRepository roleRepository;
@Inject
private PermissionManager permissionManager;
@Inject
private AssignableTaskManager taskManager;
@PostConstruct @PostConstruct
@Override @Override
protected void init() { protected void init() {
@ -65,7 +76,11 @@ public class RoleImExporter extends AbstractEntityImExporter<Role> {
return Role.class; return Role.class;
} }
@Transactional(Transactional.TxType.REQUIRED) @Override
protected Optional<Role> findExistingEntity(final String uuid) {
return roleRepository.findByUuid(uuid);
}
@Override @Override
public String exportEntity(final Exportable entity) throws ExportException { public String exportEntity(final Exportable entity) throws ExportException {
final Role role = roleRepository final Role role = roleRepository
@ -89,6 +104,155 @@ public class RoleImExporter extends AbstractEntityImExporter<Role> {
roleRepository.save(entity); roleRepository.save(entity);
} }
@Override
protected void updateExistingEntity(
final Role existingEntity,
final Role importedEntity
) {
if (!Objects.equals(
existingEntity.getName(),
importedEntity.getName()
)) {
existingEntity.setName(importedEntity.getName());
}
if (!Objects.equals(
existingEntity.getDescription(),
importedEntity.getDescription()
)) {
syncLocalizedStrings(
importedEntity.getDescription(),
existingEntity.getDescription()
);
}
roleRepository.save(existingEntity);
if (!Objects.equals(
existingEntity.getMemberships(),
importedEntity.getMemberships()
)) {
final Set<RoleMembership> membershipsToRemove = existingEntity
.getMemberships()
.stream()
.filter(
membership -> !importedEntity.getMemberships().contains(
membership
)
).collect(Collectors.toSet());
final Set<RoleMembership> membershipsToAdd = importedEntity
.getMemberships()
.stream()
.filter(
membership -> !existingEntity.getMemberships().contains(
membership
)
)
.collect(Collectors.toSet());
for (final RoleMembership membership : membershipsToRemove) {
roleManager.removeRoleFromParty(
existingEntity,
membership.getMember()
);
}
for (final RoleMembership membership : membershipsToAdd) {
roleManager.assignRoleToParty(
existingEntity,
membership.getMember()
);
}
}
if (!Objects.equals(
existingEntity.getPermissions(),
importedEntity.getPermissions()
)) {
final Set<Permission> permissionsToRemove = existingEntity
.getPermissions()
.stream()
.filter(
permission -> !importedEntity.getPermissions().contains(
permission
)
)
.collect(Collectors.toSet());
final Set<Permission> permissionToAdd = importedEntity
.getPermissions()
.stream()
.filter(
permission -> !existingEntity.getPermissions().contains(
permission
)
).collect(Collectors.toSet());
for (final Permission permission : permissionsToRemove) {
if (permission.getObject() == null) {
permissionManager.revokePrivilege(
permission.getGrantedPrivilege(),
existingEntity
);
} else {
permissionManager.revokePrivilege(
permission.getGrantedPrivilege(),
existingEntity,
permission.getObject()
);
}
}
for (final Permission permission : permissionToAdd) {
if (permission.getObject() == null) {
permissionManager.grantPrivilege(
permission.getGrantedPrivilege(),
existingEntity
);
} else {
permissionManager.grantPrivilege(
permission.getGrantedPrivilege(),
existingEntity,
permission.getObject()
);
}
}
}
if (!Objects.equals(
existingEntity.getAssignedTasks(),
importedEntity.getAssignedTasks()
)) {
final Set<TaskAssignment> assignmentsToRemove = existingEntity
.getAssignedTasks()
.stream()
.filter(
assignment -> !importedEntity.getAssignedTasks().contains(
assignment
)
)
.collect(Collectors.toSet());
final Set<TaskAssignment> assignmentsToAdd = importedEntity
.getAssignedTasks()
.stream()
.filter(
assignment -> !existingEntity.getAssignedTasks().contains(
assignment
)
)
.collect(Collectors.toSet());
for(final TaskAssignment assignment : assignmentsToRemove) {
taskManager.retractTask(assignment.getTask(), existingEntity);
}
for(final TaskAssignment assignment : assignmentsToAdd) {
taskManager.assignTask(assignment.getTask(), existingEntity);
}
}
}
@Override @Override
protected Role reloadEntity(final Role entity) { protected Role reloadEntity(final Role entity) {
return roleRepository return roleRepository
@ -104,3 +268,4 @@ public class RoleImExporter extends AbstractEntityImExporter<Role> {
} }
} }

View File

@ -19,18 +19,16 @@
package org.libreccm.security; package org.libreccm.security;
import org.libreccm.imexport.AbstractEntityImExporter; import org.libreccm.imexport.AbstractEntityImExporter;
import org.libreccm.imexport.Exportable;
import org.libreccm.imexport.Processes; import org.libreccm.imexport.Processes;
import java.util.HashSet;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.NoResultException; import javax.persistence.NoResultException;
import javax.transaction.Transactional;
/** /**
* Exporter/Importer for {@link RoleMembership}s. * Exporter/Importer for {@link RoleMembership}s.
@ -44,6 +42,9 @@ public class RoleMembershipImExporter
@Inject @Inject
private EntityManager entityManager; private EntityManager entityManager;
@Inject
private RoleMembershipRepository membershipRepo;
@PostConstruct @PostConstruct
@Override @Override
protected void init() { protected void init() {
@ -62,7 +63,11 @@ public class RoleMembershipImExporter
} }
@Override @Override
@Transactional(Transactional.TxType.REQUIRED) protected Optional<RoleMembership> findExistingEntity(final String uuid) {
return membershipRepo.findByUuid(uuid);
}
@Override
protected void saveImportedEntity(final RoleMembership entity) { protected void saveImportedEntity(final RoleMembership entity) {
if (entity.getMembershipId() == 0) { if (entity.getMembershipId() == 0) {
@ -72,6 +77,14 @@ public class RoleMembershipImExporter
} }
} }
@Override
protected void updateExistingEntity(
final RoleMembership existingEntity,
final RoleMembership importedEntity
) {
// Nothing
}
@Override @Override
protected RoleMembership reloadEntity(final RoleMembership entity) { protected RoleMembership reloadEntity(final RoleMembership entity) {
try { try {

View File

@ -19,12 +19,10 @@
package org.libreccm.security; package org.libreccm.security;
import org.libreccm.imexport.AbstractEntityImExporter; import org.libreccm.imexport.AbstractEntityImExporter;
import org.libreccm.imexport.Exportable;
import org.libreccm.imexport.Processes; import org.libreccm.imexport.Processes;
import java.util.Collections;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Optional;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
@ -58,15 +56,65 @@ public class UserImExporter extends AbstractEntityImExporter<User> {
return User.class; return User.class;
} }
@Override
protected Optional<User> findExistingEntity(final String uuid) {
return userRepository.findByUuid(uuid);
}
@Override @Override
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
protected void saveImportedEntity(final User entity) { protected void saveImportedEntity(final User entity) {
// Reset partyId. // Reset partyId.
entity.setPartyId(0); entity.setPartyId(0);
// userRepository.save(entity);
entityManager.persist(entity); entityManager.persist(entity);
} }
@Override
protected void updateExistingEntity(
final User existingEntity,
final User importedEntity
) {
if (!Objects.equals(
existingEntity.getName(),
importedEntity.getName()
)) {
existingEntity.setName(importedEntity.getName());
}
if (!Objects.equals(
existingEntity.getGivenName(),
importedEntity.getGivenName()
)) {
existingEntity.setGivenName(importedEntity.getGivenName());
}
if (!Objects.equals(
existingEntity.getFamilyName(),
importedEntity.getFamilyName()
)) {
existingEntity.setFamilyName(importedEntity.getFamilyName());
}
if (!Objects.equals(
existingEntity.getEmailAddresses(),
importedEntity.getEmailAddresses()
)) {
existingEntity.setEmailAddresses(importedEntity.getEmailAddresses());
}
if (existingEntity.isBanned() != importedEntity.isBanned()) {
existingEntity.setBanned(importedEntity.isBanned());
}
if (existingEntity.isPasswordResetRequired() != importedEntity.isPasswordResetRequired()) {
existingEntity.setPasswordResetRequired(
importedEntity.isPasswordResetRequired()
);
}
userRepository.save(existingEntity);
}
@Override @Override
protected User reloadEntity(final User entity) { protected User reloadEntity(final User entity) {
return userRepository return userRepository

View File

@ -19,17 +19,14 @@
package org.libreccm.web; package org.libreccm.web;
import org.libreccm.imexport.AbstractEntityImExporter; import org.libreccm.imexport.AbstractEntityImExporter;
import org.libreccm.imexport.Exportable;
import org.libreccm.imexport.Processes; import org.libreccm.imexport.Processes;
import java.util.Collections;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Optional;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.transaction.Transactional;
/** /**
* Exporter/Importer for application instances. * Exporter/Importer for application instances.
@ -56,11 +53,68 @@ public class ApplicationImExporter
} }
@Override @Override
@Transactional(Transactional.TxType.REQUIRED) protected Optional<CcmApplication> findExistingEntity(
final String uuid
) {
return applicationRepository.findByUuid(uuid);
}
@Override
protected void saveImportedEntity(final CcmApplication entity) { protected void saveImportedEntity(final CcmApplication entity) {
applicationRepository.save(entity); applicationRepository.save(entity);
} }
@Override
protected void updateExistingEntity(
final CcmApplication existingEntity,
final CcmApplication importedEntity
) {
if (!Objects.equals(
existingEntity.getTitle(),
importedEntity.getTitle()
)) {
syncLocalizedStrings(
importedEntity.getTitle(),
existingEntity.getTitle()
);
}
if (!Objects.equals(
existingEntity.getDescription(),
importedEntity.getDescription()
)) {
syncLocalizedStrings(
importedEntity.getDescription(),
existingEntity.getDescription()
);
}
if (!Objects.equals(
existingEntity.getCreated(),
importedEntity.getCreated()
)) {
existingEntity.setCreated(importedEntity.getCreated());
}
if(!Objects.equals(
existingEntity.getApplicationType(),
importedEntity.getApplicationType()
)) {
existingEntity.setApplicationType(
importedEntity.getApplicationType()
);
}
if (!Objects.equals(
existingEntity.getPrimaryUrl(),
importedEntity.getPrimaryUrl()
)) {
existingEntity.setPrimaryUrl(importedEntity.getPrimaryUrl());
}
applicationRepository.save(existingEntity);
}
@Override @Override
protected CcmApplication reloadEntity(final CcmApplication entity) { protected CcmApplication reloadEntity(final CcmApplication entity) {
return applicationRepository return applicationRepository

View File

@ -19,16 +19,15 @@
package org.libreccm.workflow; package org.libreccm.workflow;
import org.libreccm.imexport.AbstractEntityImExporter; import org.libreccm.imexport.AbstractEntityImExporter;
import org.libreccm.imexport.Exportable;
import org.libreccm.imexport.Processes; import org.libreccm.imexport.Processes;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.transaction.Transactional;
/** /**
* Exporter/Importer for {@link AssignableTask}s. * Exporter/Importer for {@link AssignableTask}s.
@ -45,6 +44,12 @@ public class AssignableTaskImExporter
@Inject @Inject
private AssignableTaskRepository assignableTaskRepository; private AssignableTaskRepository assignableTaskRepository;
@Inject
private TaskManager taskManager;
@Inject
private AssignableTaskManager assignableTaskManager;
@PostConstruct @PostConstruct
@Override @Override
protected void init() { protected void init() {
@ -62,11 +67,61 @@ public class AssignableTaskImExporter
} }
@Override @Override
@Transactional(Transactional.TxType.REQUIRED) protected Optional<AssignableTask> findExistingEntity(final String uuid) {
return assignableTaskRepository.findByUuid(uuid);
}
@Override
protected void saveImportedEntity(final AssignableTask entity) { protected void saveImportedEntity(final AssignableTask entity) {
assignableTaskRepository.save(entity); assignableTaskRepository.save(entity);
} }
@Override
protected void updateExistingEntity(
final AssignableTask existingEntity,
final AssignableTask importedEntity
) {
if(!Objects.equals(
existingEntity.getLabel(),
importedEntity.getLabel()
)) {
syncLocalizedStrings(
importedEntity.getLabel(),
existingEntity.getLabel()
);
}
if(!Objects.equals(
existingEntity.getDescription(),
importedEntity.getDescription()
)) {
syncLocalizedStrings(
importedEntity.getDescription(),
existingEntity.getDescription()
);
}
if (existingEntity.isActive() != importedEntity.isActive()) {
existingEntity.setActive(importedEntity.isActive());
}
if (!Objects.equals(
existingEntity.getWorkflow(),
importedEntity.getWorkflow()
)) {
taskManager.removeTask(
existingEntity.getWorkflow(),
existingEntity
);
taskManager.addTask(
importedEntity.getWorkflow(),
existingEntity
);
// ToDo
}
}
@Override @Override
protected AssignableTask reloadEntity(final AssignableTask entity) { protected AssignableTask reloadEntity(final AssignableTask entity) {
return assignableTaskRepository return assignableTaskRepository

View File

@ -19,17 +19,16 @@
package org.libreccm.workflow; package org.libreccm.workflow;
import org.libreccm.imexport.AbstractEntityImExporter; import org.libreccm.imexport.AbstractEntityImExporter;
import org.libreccm.imexport.Exportable;
import org.libreccm.imexport.Processes; import org.libreccm.imexport.Processes;
import java.util.Collections;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.transaction.Transactional;
/** /**
* Importer/Exporter for {@link Workflow}s. * Importer/Exporter for {@link Workflow}s.
@ -40,6 +39,9 @@ import javax.transaction.Transactional;
@Processes(Workflow.class) @Processes(Workflow.class)
public class WorkflowImExporter extends AbstractEntityImExporter<Workflow> { public class WorkflowImExporter extends AbstractEntityImExporter<Workflow> {
@Inject
private TaskManager taskManager;
@Inject @Inject
private WorkflowRepository workflowRepository; private WorkflowRepository workflowRepository;
@ -51,16 +53,103 @@ public class WorkflowImExporter extends AbstractEntityImExporter<Workflow> {
@Override @Override
public Class<Workflow> getEntityClass() { public Class<Workflow> getEntityClass() {
return Workflow.class; return Workflow.class;
} }
@Override @Override
@Transactional(Transactional.TxType.REQUIRED) protected Optional<Workflow> findExistingEntity(final String uuid) {
return workflowRepository.findByUuid(uuid);
}
@Override
protected void saveImportedEntity(final Workflow entity) { protected void saveImportedEntity(final Workflow entity) {
workflowRepository.save(entity); workflowRepository.save(entity);
} }
@Override
protected void updateExistingEntity(
final Workflow existingEntity,
final Workflow importedEntity
) {
if (existingEntity.isAbstractWorkflow() != importedEntity.isAbstractWorkflow()) {
existingEntity.setAbstractWorkflow(
importedEntity.isAbstractWorkflow()
);
}
if (!Objects.equals(
existingEntity.getTemplate(),
importedEntity.getTemplate()
)) {
existingEntity.setTemplate(importedEntity.getTemplate());
}
if (!Objects.equals(
existingEntity.getName(),
importedEntity.getName()
)) {
syncLocalizedStrings(
importedEntity.getName(),
existingEntity.getName()
);
}
if (!Objects.equals(
existingEntity.getDescription(),
importedEntity.getDescription()
)) {
syncLocalizedStrings(
importedEntity.getDescription(),
existingEntity.getDescription()
);
}
if (existingEntity.getState() != importedEntity.getState()) {
existingEntity.setState(importedEntity.getState());
}
if (existingEntity.isActive() != importedEntity.isActive()) {
existingEntity.setActive(importedEntity.isActive());
}
if (existingEntity.getTasksState() != importedEntity.getTasksState()) {
existingEntity.setTasksState(importedEntity.getTasksState());
}
if (!Objects.equals(
existingEntity.getObject(),
importedEntity.getObject()
)) {
existingEntity.setObject(importedEntity.getObject());
}
workflowRepository.save(importedEntity);
if (!Objects.equals(
existingEntity.getTasks(),
importedEntity.getTasks()
)) {
final Set<Task> tasksToRemove = existingEntity
.getTasks()
.stream()
.filter(task -> !importedEntity.getTasks().contains(task))
.collect(Collectors.toSet());
final Set<Task> tasksToAdd = importedEntity
.getTasks()
.stream( )
.filter(task -> !existingEntity.getTasks().contains(task))
.collect(Collectors.toSet());
for(final Task task : tasksToRemove) {
taskManager.removeTask(existingEntity, task);
}
for(final Task task : tasksToAdd) {
taskManager.addTask(existingEntity, task);
}
}
}
@Override @Override
protected Workflow reloadEntity(final Workflow entity) { protected Workflow reloadEntity(final Workflow entity) {
return workflowRepository return workflowRepository