Create placeholders for parent categories that have not been imported

when a child category is imported, and update them when that category is
imported.
deploy_packages_to_gitea
Jens Pelzetter 2022-10-23 10:55:28 +02:00
parent bf0a449419
commit 4e8d1929b8
2 changed files with 59 additions and 9 deletions

View File

@ -23,7 +23,9 @@ import com.fasterxml.jackson.annotation.ObjectIdResolver;
import org.libreccm.cdi.utils.CdiUtil;
import javax.enterprise.context.RequestScoped;
import java.io.Serializable;
import java.util.Optional;
/**
* Used by the {@link CategorizationImExporter} to resolve categories based on
@ -49,18 +51,41 @@ public class CategoryIdResolver implements Serializable, ObjectIdResolver {
@Override
public Object resolveId(final ObjectIdGenerator.IdKey id) {
return CdiUtil
final Optional<Category> result = CdiUtil
.createCdiUtil()
.findBean(CategoryRepository.class)
.findByUuid(id.key.toString())
.orElseThrow(
() -> new IllegalArgumentException(
String.format(
"No Category with UUID %s in the database.",
id.key.toString()
)
.findByUuid(id.key.toString());
if (result.isPresent()) {
return result.get();
} else {
final Category category = new Category();
category.setUuid(id.key.toString());
category.setUniqueId(id.key.toString());
category.setName(
String.format(
"placeholder-%s",
id.key.toString()
)
);
CdiUtil
.createCdiUtil()
.findBean(CategoryRepository.class)
.save(category);
return category;
}
//
// return CdiUtil
// .createCdiUtil()
// .findBean(CategoryRepository.class)
// .findByUuid(id.key.toString())
// .orElseThrow(
// () -> new IllegalArgumentException(
// String.format(
// "No Category with UUID %s in the database.",
// id.key.toString()
// )
// )
// );
}
@Override

View File

@ -24,12 +24,15 @@ import org.libreccm.imexport.Processes;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
import static com.ctc.wstx.shaded.msv_core.datatype.xsd.NumberType.save;
/**
* Exporter/Importer for {@link Category} entities.
*
@ -58,7 +61,29 @@ public class CategoryImExporter extends AbstractEntityImExporter<Category> {
@Override
@Transactional(Transactional.TxType.REQUIRED)
protected void saveImportedEntity(final Category entity) {
categoryRepository.save(entity);
final Optional<Category> result = categoryRepository.findByUuid(
entity.getUuid()
);
final Category category;
if (result.isPresent()) {
category = result.get();
category.setAbstractCategory(entity.isAbstractCategory());
category.setCategoryOrder(entity.getCategoryOrder());
category.setDescription(entity.getDescription());
category.setDisplayName(entity.getDisplayName());
category.setEnabled(entity.isEnabled());
category.setName(entity.getName());
category.setObjects(entity.getObjects());
category.setParentCategory(entity.getParentCategory());
category.setSubCategories(entity.getSubCategories());
category.setTitle(entity.getTitle());
category.setUniqueId(entity.getUniqueId());
category.setVisible(entity.isVisible());
} else {
category = entity;
}
categoryRepository.save(category);
}
@Override