Code cleanup

deploy_packages_to_gitea
Jens Pelzetter 2022-09-24 11:26:17 +02:00
parent c77dc2f991
commit a72945501d
11 changed files with 135 additions and 108 deletions

View File

@ -42,7 +42,6 @@ public abstract class AbstractContentItemImExporter<T extends ContentItem>
@Override @Override
protected Set<Class<? extends Exportable>> getRequiredEntities() { protected Set<Class<? extends Exportable>> getRequiredEntities() {
final Set<Class<? extends Exportable>> entities = new HashSet<>(); final Set<Class<? extends Exportable>> entities = new HashSet<>();
entities.add(Category.class); entities.add(Category.class);
entities.add(ContentSection.class); entities.add(ContentSection.class);

View File

@ -76,13 +76,16 @@ public class CategorizationImExporter
@Override @Override
protected Categorization reloadEntity(final Categorization entity) { protected Categorization reloadEntity(final Categorization entity) {
try { try {
return entityManager.createNamedQuery( return entityManager
"Categorization.findById", .createNamedQuery(
Categorization.class "Categorization.findById",
).setParameter( Categorization.class
"categorizationId", )
Objects.requireNonNull(entity).getCategorizationId() .setParameter(
).getSingleResult(); "categorizationId",
Objects.requireNonNull(entity).getCategorizationId()
)
.getSingleResult();
} catch (NoResultException ex) { } catch (NoResultException ex) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
String.format( String.format(

View File

@ -99,17 +99,20 @@ public abstract class AbstractEntityImExporter<T extends Exportable> {
try { try {
return objectMapper.writeValueAsString(export); return objectMapper.writeValueAsString(export);
} catch (JsonProcessingException ex) { } catch (JsonProcessingException ex) {
throw new ExportException(String.format( throw new ExportException(
"Failed to export entity \"%s\" of type \"%s\".", String.format(
entity.getUuid(), "Failed to export entity \"%s\" of type \"%s\".",
getEntityClass().getName()), entity.getUuid(),
ex); getEntityClass().getName()
),
ex
);
} }
} }
/** /**
* 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 {@link #exportEntity(org.libreccm.imexport.Exportable) method. The * reasons before they are passed to the 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.

View File

@ -46,8 +46,9 @@ import java.util.stream.Collectors;
*/ */
final class EntityImExporterTreeManager { final class EntityImExporterTreeManager {
private static final Logger LOGGER = LogManager private static final Logger LOGGER = LogManager.getLogger(
.getLogger(EntityImExporterTreeManager.class); EntityImExporterTreeManager.class
);
/** /**
* Initialises the tree with the provided list of * Initialises the tree with the provided list of
@ -63,9 +64,8 @@ final class EntityImExporterTreeManager {
* cycle is detected in the dependency tree. * cycle is detected in the dependency tree.
*/ */
public List<EntityImExporterTreeNode> generateTree( public List<EntityImExporterTreeNode> generateTree(
final List<AbstractEntityImExporter<?>> imExporters) final List<AbstractEntityImExporter<?>> imExporters
throws DependencyException { ) throws DependencyException {
LOGGER.info("Starting to generate dependency tree..."); LOGGER.info("Starting to generate dependency tree...");
//Create the tree nodes. A HashMap is used to avoid duplicates and //Create the tree nodes. A HashMap is used to avoid duplicates and
@ -73,12 +73,15 @@ final class EntityImExporterTreeManager {
final Map<String, EntityImExporterTreeNode> nodes = imExporters final Map<String, EntityImExporterTreeNode> nodes = imExporters
.stream() .stream()
.map(EntityImExporterTreeNode::new) .map(EntityImExporterTreeNode::new)
.collect(Collectors.toMap( .collect(
node -> node Collectors.toMap(
.getEntityImExporter() node -> node
.getEntityClass() .getEntityImExporter()
.getName(), .getEntityClass()
node -> node)); .getName(),
node -> node
)
);
//Add the dependency relations to the nodes //Add the dependency relations to the nodes
for (final AbstractEntityImExporter<?> imExporter : imExporters) { for (final AbstractEntityImExporter<?> imExporter : imExporters) {
@ -89,7 +92,6 @@ final class EntityImExporterTreeManager {
final List<EntityImExporterTreeNode> nodeList = new ArrayList<>(); final List<EntityImExporterTreeNode> nodeList = new ArrayList<>();
for (final Map.Entry<String, EntityImExporterTreeNode> entry for (final Map.Entry<String, EntityImExporterTreeNode> entry
: nodes.entrySet()) { : nodes.entrySet()) {
nodeList.add(entry.getValue()); nodeList.add(entry.getValue());
} }
@ -120,16 +122,18 @@ final class EntityImExporterTreeManager {
final List<EntityImExporterTreeNode> orderedNodes = new ArrayList<>(); final List<EntityImExporterTreeNode> orderedNodes = new ArrayList<>();
final List<EntityImExporterTreeNode> resolvedNodes = new ArrayList<>(); final List<EntityImExporterTreeNode> resolvedNodes = new ArrayList<>();
LOGGER.info("Looking for EntityImExporters which do not depend on any " LOGGER.info(
+ "other EntityImExporters."); "Looking for EntityImExporters which do not depend on any "
+ "other EntityImExporters."
);
//Find all nodes which do not depend on any other nodes. These //Find all nodes which do not depend on any other nodes. These
//nodes are used as starting point for the sorting. //nodes are used as starting point for the sorting.
for (final EntityImExporterTreeNode node : nodes) { for (final EntityImExporterTreeNode node : nodes) {
if (node.getDependsOn().isEmpty()) { if (node.getDependsOn().isEmpty()) {
LOGGER.info( LOGGER.info(
"\tNode \"{}\" does not depend on any other module", "\tNode \"{}\" does not depend on any other module",
node.getEntityImExporter().getClass().getName()); node.getEntityImExporter().getClass().getName()
);
resolvedNodes.add(node); resolvedNodes.add(node);
} }
} }
@ -139,9 +143,10 @@ final class EntityImExporterTreeManager {
//Remove the first node from the resolved nodes list //Remove the first node from the resolved nodes list
final EntityImExporterTreeNode current = resolvedNodes.remove(0); final EntityImExporterTreeNode current = resolvedNodes.remove(0);
LOGGER.info("\tProcessing node for EntityImExporter \"{}\"...", LOGGER.info(
current "\tProcessing node for EntityImExporter \"{}\"...",
.getEntityImExporter().getClass().getName()); current.getEntityImExporter().getClass().getName()
);
//Add the node to the ordered modules list. //Add the node to the ordered modules list.
orderedNodes.add(current); orderedNodes.add(current);
@ -149,7 +154,6 @@ final class EntityImExporterTreeManager {
//Remove the edges to the current node. //Remove the edges to the current node.
for (final EntityImExporterTreeNode dependent for (final EntityImExporterTreeNode dependent
: current.getDependentImExporters()) { : current.getDependentImExporters()) {
dependent.removeDependsOn(current); dependent.removeDependsOn(current);
//If the dependent node has no more dependsOn relations put //If the dependent node has no more dependsOn relations put
@ -163,22 +167,26 @@ final class EntityImExporterTreeManager {
//Check if all nodes have been ordered. If not the tree has at least on //Check if all nodes have been ordered. If not the tree has at least on
//on cycle and can't be processed. //on cycle and can't be processed.
if (orderedNodes.size() == nodes.size()) { if (orderedNodes.size() == nodes.size()) {
LOGGER.info(
LOGGER.info("EntityImExporter dependency graph processed " "EntityImExporter dependency graph processed "
+ "successfully. EntityImExporters in order:"); + "successfully. EntityImExporters in order:"
);
for (final EntityImExporterTreeNode node : orderedNodes) { for (final EntityImExporterTreeNode node : orderedNodes) {
LOGGER.info("\t{}", LOGGER.info(
node.getEntityImExporter().getClass().getName()); "\t{}",
node.getEntityImExporter().getClass().getName()
);
} }
return orderedNodes; return orderedNodes;
} else { } else {
LOGGER.fatal(
LOGGER.fatal("The EntityImExporter dependency graph has at least " "The EntityImExporter dependency graph has at least one cycle."
+ "one cycle."); );
throw new DependencyException("The EntityImExporter dependency " throw new DependencyException(
+ "graph has at least one cycle."); "The EntityImExporter dependency graph has at least one cycle."
);
} }
} }
@ -194,38 +202,45 @@ final class EntityImExporterTreeManager {
*/ */
private void addDependencyRelations( private void addDependencyRelations(
final AbstractEntityImExporter<?> imExporter, final AbstractEntityImExporter<?> imExporter,
final Map<String, EntityImExporterTreeNode> nodes) final Map<String, EntityImExporterTreeNode> nodes
)
throws DependencyException { throws DependencyException {
//Get the name of the module from the module info. //Get the name of the module from the module info.
final String className = imExporter.getEntityClass().getName(); final String className = imExporter.getEntityClass().getName();
LOGGER LOGGER
.info("Adding dependency relations for EntityImExporter \"{}\"...", .info(
className); "Adding dependency relations for EntityImExporter \"{}\"...",
className
);
//Check if the nodes map has an entry for the EntityImExporter. //Check if the nodes map has an entry for the EntityImExporter.
if (!nodes.containsKey(className)) { if (!nodes.containsKey(className)) {
LOGGER.fatal(
LOGGER.fatal("EntityImExporter nodes map does contain an entry for " "EntityImExporter nodes map does contain an entry for "
+ "\"{}\". That should not happen.", + "\"{}\". That should not happen.",
className); className
throw new IllegalArgumentException(String.format( );
"The nodes map does not contain a node for " throw new IllegalArgumentException(
+ "EntityImExporter \"%s\"." String.format(
+ "This should not happen.", "The nodes map does not contain a node for "
className)); + "EntityImExporter \"%s\"."
+ "This should not happen.",
className
)
);
} }
//Get the node from the map //Get the node from the map
final EntityImExporterTreeNode node = nodes.get(className); final EntityImExporterTreeNode node = nodes.get(className);
LOGGER LOGGER.info(
.info("Processing required modules for EntityImExporter \"{}\"...", "Processing required modules for EntityImExporter \"{}\"...",
className); className
);
//Process the EntityImExporter required by the current module and add //Process the EntityImExporter required by the current module and add
//the dependency relations. //the dependency relations.
for (final Class<? extends Exportable> clazz for (final Class<? extends Exportable> clazz
: imExporter.getRequiredEntities()) { : imExporter.getRequiredEntities()) {
addDependencyRelation(nodes, node, clazz); addDependencyRelation(nodes, node, clazz);
} }
} }
@ -247,25 +262,34 @@ final class EntityImExporterTreeManager {
Class<? extends Exportable> requiredClass) Class<? extends Exportable> requiredClass)
throws DependencyException { throws DependencyException {
LOGGER.info("\tEntityImExporter for \"{}\" requires " LOGGER.info(
+ "EntityImExporter for \"{}\".", "\tEntityImExporter for \"{}\" requires EntityImExporter "
node.getEntityImExporter().getClass().getName(), + "for \"{}\".",
requiredClass.getName()); node.getEntityImExporter().getClass().getName(),
requiredClass.getName()
);
//Check if the nodes list has an entry for the required module. //Check if the nodes list has an entry for the required module.
if (!nodes.containsKey(requiredClass.getName())) { if (!nodes.containsKey(requiredClass.getName())) {
LOGGER.fatal("Required EntityImExporter for \"{}\" no found.", LOGGER.fatal(
requiredClass.getName()); "Required EntityImExporter for \"{}\" no found.",
throw new DependencyException(String.format( requiredClass.getName()
"EntityImExporter for type \"%s\" depends on type \"%s\" " );
+ "but no EntityImExporter for type \"%s\" is available.", throw new DependencyException(
node.getEntityImExporter().getEntityClass(), String.format(
requiredClass.getName(), "EntityImExporter for type \"%s\" depends on type \"%s\" "
requiredClass.getName())); + "but no EntityImExporter for type \"%s\" is "
+ "available.",
node.getEntityImExporter().getEntityClass(),
requiredClass.getName(),
requiredClass.getName()
)
);
} }
final EntityImExporterTreeNode dependencyNode = nodes final EntityImExporterTreeNode dependencyNode = nodes.get(
.get(requiredClass.getName()); requiredClass.getName()
);
//Create the dependencies relations. //Create the dependencies relations.
node.addDependsOn(dependencyNode); node.addDependsOn(dependencyNode);

View File

@ -45,7 +45,8 @@ public final class EntityImExporterTreeNode {
} }
public EntityImExporterTreeNode( public EntityImExporterTreeNode(
final AbstractEntityImExporter<?> entityImExporter) { final AbstractEntityImExporter<?> entityImExporter
) {
this(); this();
this.entityImExporter = entityImExporter; this.entityImExporter = entityImExporter;
@ -73,42 +74,35 @@ public final class EntityImExporterTreeNode {
} }
void addDependentImExporter(final EntityImExporterTreeNode node) { void addDependentImExporter(final EntityImExporterTreeNode node) {
dependentImExporters.add(node); dependentImExporters.add(node);
} }
void removeDependentImExporter(final EntityImExporterTreeNode node) { void removeDependentImExporter(final EntityImExporterTreeNode node) {
dependentImExporters.remove(node); dependentImExporters.remove(node);
} }
public List<EntityImExporterTreeNode> getDependsOn() { public List<EntityImExporterTreeNode> getDependsOn() {
return Collections.unmodifiableList(dependsOn); return Collections.unmodifiableList(dependsOn);
} }
void setDependsOn(final List<EntityImExporterTreeNode> dependsOn) { void setDependsOn(final List<EntityImExporterTreeNode> dependsOn) {
this.dependsOn = new ArrayList<>(dependsOn); this.dependsOn = new ArrayList<>(dependsOn);
} }
void addDependsOn(final EntityImExporterTreeNode node) { void addDependsOn(final EntityImExporterTreeNode node) {
dependsOn.add(node); dependsOn.add(node);
} }
void removeDependsOn(final EntityImExporterTreeNode node) { void removeDependsOn(final EntityImExporterTreeNode node) {
dependsOn.remove(node); dependsOn.remove(node);
} }
@Override @Override
public int hashCode() { public int hashCode() {
int hash = 7; int hash = 7;
hash = 47 * hash hash = 47 * hash + Objects.hashCode(
+ Objects.hashCode( this.entityImExporter.getClass().getName()
this.entityImExporter.getClass().getName() );
);
return hash; return hash;
} }
@ -126,7 +120,8 @@ public final class EntityImExporterTreeNode {
final EntityImExporterTreeNode other = (EntityImExporterTreeNode) obj; final EntityImExporterTreeNode other = (EntityImExporterTreeNode) obj;
return Objects.equals( return Objects.equals(
this.entityImExporter.getClass().getName(), this.entityImExporter.getClass().getName(),
other.getEntityImExporter().getClass().getName()); other.getEntityImExporter().getClass().getName()
);
} }
@Override @Override

View File

@ -16,7 +16,6 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA * MA 02110-1301 USA
*/ */
package org.libreccm.imexport; package org.libreccm.imexport;
/** /**
@ -29,15 +28,16 @@ public class ImportExpection extends Exception {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** /**
* Creates a new instance of <code>ImportExpection</code> without detail message. * Creates a new instance of <code>ImportExpection</code> without detail
* message.
*/ */
public ImportExpection() { public ImportExpection() {
super(); super();
} }
/** /**
* Constructs an instance of <code>ImportExpection</code> with the specified detail message. * Constructs an instance of <code>ImportExpection</code> with the specified
* detail message.
* *
* @param msg The detail message. * @param msg The detail message.
*/ */
@ -46,23 +46,24 @@ public class ImportExpection extends Exception {
} }
/** /**
* Constructs an instance of <code>ImportExpection</code> which wraps the * Constructs an instance of <code>ImportExpection</code> which wraps the
* specified exception. * specified exception.
* *
* @param exception The exception to wrap. * @param exception The exception to wrap.
*/ */
public ImportExpection(final Exception exception) { public ImportExpection(final Exception exception) {
super(exception); super(exception);
} }
/** /**
* Constructs an instance of <code>ImportExpection</code> with the specified message which also wraps the * Constructs an instance of <code>ImportExpection</code> with the specified
* specified exception. * message which also wraps the specified exception.
* *
* @param msg The detail message. * @param msg The detail message.
* @param exception The exception to wrap. * @param exception The exception to wrap.
*/ */
public ImportExpection(final String msg, final Exception exception) { public ImportExpection(final String msg, final Exception exception) {
super(msg, exception); super(msg, exception);
} }
} }

View File

@ -518,7 +518,6 @@ public class ImportExport {
public Class<? extends Exportable> value() { public Class<? extends Exportable> value() {
return value; return value;
} }
} }
} }

View File

@ -67,11 +67,13 @@ public class RoleImExporter extends AbstractEntityImExporter<Role> {
() -> new IllegalArgumentException( () -> new IllegalArgumentException(
String.format( String.format(
"Provided entity %d does not exist in database.", "Provided entity %d does not exist in database.",
entity) entity
)
) )
); );
role.getDescription().getValues().forEach((locale, value) -> LOGGER role.getDescription().getValues().forEach((locale, value) -> LOGGER
.info("{}: {}", locale, value)); .info("{}: {}", locale, value)
);
return super.exportEntity(entity); return super.exportEntity(entity);
} }

View File

@ -74,7 +74,8 @@ public class RoleMembershipImExporter
.setParameter( .setParameter(
"membershipId", "membershipId",
Objects.requireNonNull(entity).getMembershipId() Objects.requireNonNull(entity).getMembershipId()
).getSingleResult(); )
.getSingleResult();
} catch (NoResultException ex) { } catch (NoResultException ex) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
String.format( String.format(

View File

@ -60,7 +60,6 @@ public class TaskAssignmentImExporter
@Override @Override
protected Set<Class<? extends Exportable>> getRequiredEntities() { protected Set<Class<? extends Exportable>> getRequiredEntities() {
final Set<Class<? extends Exportable>> classes = new HashSet<>(); final Set<Class<? extends Exportable>> classes = new HashSet<>();
classes.add(AssignableTask.class); classes.add(AssignableTask.class);

View File

@ -75,7 +75,8 @@ public class TaskDependencyImExporter
.setParameter( .setParameter(
"dependencyId", "dependencyId",
Objects.requireNonNull(entity).getTaskDependencyId() Objects.requireNonNull(entity).getTaskDependencyId()
).getSingleResult(); )
.getSingleResult();
} catch (NoResultException ex) { } catch (NoResultException ex) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
String.format( String.format(