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
protected Set<Class<? extends Exportable>> getRequiredEntities() {
final Set<Class<? extends Exportable>> entities = new HashSet<>();
entities.add(Category.class);
entities.add(ContentSection.class);

View File

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

View File

@ -99,17 +99,20 @@ public abstract class AbstractEntityImExporter<T extends Exportable> {
try {
return objectMapper.writeValueAsString(export);
} catch (JsonProcessingException ex) {
throw new ExportException(String.format(
"Failed to export entity \"%s\" of type \"%s\".",
entity.getUuid(),
getEntityClass().getName()),
ex);
throw new ExportException(
String.format(
"Failed to export entity \"%s\" of type \"%s\".",
entity.getUuid(),
getEntityClass().getName()
),
ex
);
}
}
/**
* 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.
*
* @param entity The entity to reload.

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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