Skip to content

Commit

Permalink
Sonar cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed May 10, 2022
1 parent f717efe commit ff47075
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 99 deletions.
Expand Up @@ -27,7 +27,6 @@
import de.fraunhofer.iosb.ilt.configurable.editor.EditorSubclass;
import de.fraunhofer.iosb.ilt.frostserver.model.EntityType;
import de.fraunhofer.iosb.ilt.frostserver.model.ModelRegistry;
import de.fraunhofer.iosb.ilt.frostserver.model.core.EntityValidator;
import de.fraunhofer.iosb.ilt.frostserver.model.core.annotations.Annotation;
import java.util.ArrayList;
import java.util.List;
Expand Down
Expand Up @@ -125,12 +125,16 @@ public boolean addOrderBy(OrderBy orderby) {
Object lastValue = last.getProperty(obPath);
Object nextValue = next.getProperty(obPath);
if (lastValue == null || nextValue == null) {
LOGGER.debug("Order expression value is null, using normal nextLink: {}", orderExpression.toUrl());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Order expression value is null, using normal nextLink: {}", orderExpression.toUrl());
}
return setFailed();
}
Constant valueConstant = ConstantFactory.of(lastValue);
if (valueConstant == null) {
LOGGER.debug("Order expression value can not be made a constant, using normal nextLink: {}", orderExpression.toUrl());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Order expression value can not be made a constant, using normal nextLink: {}", orderExpression.toUrl());
}
return setFailed();
}
if (lastValue.equals(nextValue)) {
Expand Down Expand Up @@ -242,12 +246,10 @@ public static String generateSelfLink(Query query, ResourcePath path, Entity ent
}

/**
* Generate a navigation link for the given entity, using the given path and
* parent entities.
* Generate a navigation link for the given entity, using the given path and parent entities.
*
* @param query The query used.
* @param path The path for the current page that relative links are
* relative to.
* @param path The path for the current page that relative links are relative to.
* @param parent The parent of the entity to generate a navlink for.
* @param entity The entity to generate a navlink for.
* @param absolute If true, the generated link is absolute.
Expand All @@ -266,12 +268,10 @@ public static String generateNavLink(Query query, ResourcePath path, Entity pare
}

/**
* Generate a navigation link for the given EntitySet, using the given path
* and parent entities.
* Generate a navigation link for the given EntitySet, using the given path and parent entities.
*
* @param query The query used.
* @param path The path for the current page that relative links are
* relative to.
* @param path The path for the current page that relative links are relative to.
* @param parent The parent of the entity to generate a navlink for.
* @param es The EntitySet to generate a navlink for.
* @param absolute If true, the generated link is absolute.
Expand Down
Expand Up @@ -19,8 +19,8 @@

import de.fraunhofer.iosb.ilt.frostserver.query.expression.ExpressionVisitor;
import de.fraunhofer.iosb.ilt.frostserver.util.StringHelper;
import java.text.ParseException;
import net.time4j.ZonalDateTime;
import net.time4j.engine.ChronoException;
import net.time4j.format.expert.Iso8601Format;
import net.time4j.tz.Timezone;
import static net.time4j.tz.ZonalOffset.UTC;
Expand All @@ -37,7 +37,7 @@ public DateTimeConstant(ZonalDateTime value) {
super(value);
}

public DateTimeConstant(String value) throws ParseException {
public DateTimeConstant(String value) {
if (value.lastIndexOf('-') <= 0) {
// We do not want simple integers be interpreted as a year.
throw new IllegalArgumentException("Not a date: " + value);
Expand All @@ -58,7 +58,7 @@ public <O> O accept(ExpressionVisitor<O> visitor) {
public static DateTimeConstant parse(String value) {
try {
return new DateTimeConstant(value);
} catch (ParseException ex) {
} catch (ChronoException ex) {
throw new IllegalArgumentException("Failed to parse PlainTimestamp " + StringHelper.cleanForLogging(value), ex);
}
}
Expand Down
Expand Up @@ -22,7 +22,6 @@
import de.fraunhofer.iosb.ilt.frostserver.settings.CoreSettings;
import static de.fraunhofer.iosb.ilt.frostserver.settings.CoreSettings.TAG_CORE_SETTINGS;
import de.fraunhofer.iosb.ilt.frostserver.util.LiquibaseUser;
import de.fraunhofer.iosb.ilt.frostserver.util.LiquibaseUtils;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.UpgradeFailedException;
import java.io.IOException;
import java.io.PrintWriter;
Expand Down Expand Up @@ -73,12 +72,13 @@ protected void processGetRequest(HttpServletRequest request, HttpServletResponse
out.println("</form></p>");
out.println("<p><a href='.'>Back...</a></p>");

PersistenceManager pm = PersistenceManagerFactory.getInstance(coreSettings).create();
if (pm instanceof LiquibaseUser) {
checkForUpgrades(out, (LiquibaseUser) pm);
}
for (LiquibaseUser user : coreSettings.getLiquibaseUsers()) {
checkForUpgrades(out, user);
try (PersistenceManager pm = PersistenceManagerFactory.getInstance(coreSettings).create()) {
if (pm instanceof LiquibaseUser) {
checkForUpgrades(out, (LiquibaseUser) pm);
}
for (LiquibaseUser user : coreSettings.getLiquibaseUsers()) {
checkForUpgrades(out, user);
}
}

out.println("<p>Done. Click the button to execute the listed updates.</p>");
Expand Down Expand Up @@ -117,12 +117,13 @@ protected void processPostRequest(HttpServletRequest request, HttpServletRespons
out.println("<body>");
out.println("<h1>Servlet DatabaseStatus at " + request.getContextPath() + "</h1><p>Updating Database</p>");

PersistenceManager pm = PersistenceManagerFactory.getInstance(coreSettings).create();
if (pm instanceof LiquibaseUser) {
processUpgrade(out, (LiquibaseUser) pm);
}
for (LiquibaseUser user : coreSettings.getLiquibaseUsers()) {
processUpgrade(out, user);
try (PersistenceManager pm = PersistenceManagerFactory.getInstance(coreSettings).create()) {
if (pm instanceof LiquibaseUser) {
processUpgrade(out, (LiquibaseUser) pm);
}
for (LiquibaseUser user : coreSettings.getLiquibaseUsers()) {
processUpgrade(out, user);
}
}

out.println("<p>Done. <a href='DatabaseStatus'>Back...</a></p>");
Expand Down
Expand Up @@ -46,7 +46,6 @@
import java.util.Set;
import java.util.regex.Pattern;
import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils;
import org.jooq.AggregateFunction;
import org.jooq.DSLContext;
import org.jooq.Delete;
Expand All @@ -56,12 +55,9 @@
import org.jooq.Record;
import org.jooq.Record1;
import org.jooq.ResultQuery;
import org.jooq.SQL;
import org.jooq.SelectConditionStep;
import org.jooq.SelectIntoStep;
import org.jooq.SelectJoinStep;
import org.jooq.SelectSelectStep;
import org.jooq.Table;
import org.jooq.conf.ParamType;
import org.jooq.impl.DSL;
import org.jooq.impl.SQLDataType;
Expand Down
Expand Up @@ -69,6 +69,7 @@ public class ResultBuilder implements ResourcePathVisitor {
* The logger for this class.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(ResultBuilder.class);
private static final String ESTIMATE_COUNT = "Estimate: {}, Count: {}";

private final PostgresPersistenceManager pm;
private final PersistenceSettings persistenceSettings;
Expand Down Expand Up @@ -342,7 +343,7 @@ public void countSampleLimit(EntitySet entitySet) {
if (estimate < estimateTreshold) {
final int count = timeCountQuery(sqlQueryBuilder.buildCount());
entitySet.setCount(count);
LOGGER.debug("Estimate: {}, Count: {}", estimate, count);
LOGGER.debug(ESTIMATE_COUNT, estimate, count);
} else {
entitySet.setCount(estimate);
}
Expand All @@ -356,7 +357,7 @@ public void countLimitSample(EntitySet entitySet) {
final var csr = sqlQueryBuilder.buildEstimateCountSample();
final int estimate = (int) (timeCountQueryRecord(csr.countQuery) * Math.pow(100, csr.sampledTables));
entitySet.setCount(Math.max(count, estimate));
LOGGER.debug("Estimate: {}, Count: {}", estimate, count);
LOGGER.debug(ESTIMATE_COUNT, estimate, count);
}
}

Expand All @@ -365,7 +366,7 @@ public void countEstimateLimit(EntitySet entitySet) {
if (estimate < estimateTreshold) {
final int count = timeCountQuery(sqlQueryBuilder.buildCount(estimateTreshold));
entitySet.setCount(count);
LOGGER.debug("Estimate: {}, Count: {}", estimate, count);
LOGGER.debug(ESTIMATE_COUNT, estimate, count);
} else {
entitySet.setCount(estimate);
}
Expand All @@ -378,7 +379,7 @@ public void countLimitEstimate(EntitySet entitySet) {
} else {
final int estimate = timeCountQuery(sqlQueryBuilder.buildEstimateCountExplain());
entitySet.setCount(Math.max(count, estimate));
LOGGER.debug("Estimate: {}, Count: {}", estimate, count);
LOGGER.debug(ESTIMATE_COUNT, estimate, count);
}
}

Expand Down
Expand Up @@ -17,10 +17,14 @@
*/
package de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.relations;

import de.fraunhofer.iosb.ilt.frostserver.model.core.Entity;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.PostgresPersistenceManager;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.tables.StaMainTable;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.utils.QueryState;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.utils.TableRef;
import de.fraunhofer.iosb.ilt.frostserver.property.NavigationPropertyMain;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.IncompleteEntityException;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.NoSuchEntityException;

/**
* The interface for table-to-table relations.
Expand All @@ -41,14 +45,22 @@ public interface Relation<S extends StaMainTable<S>> {
public TableRef join(S source, QueryState<?> queryState, TableRef sourceRef);

/**
* Create a link between the given source and target ids. This is not
* necessarily supported for all implementations, in all directions. For
* some types of relations this means creating new entries in a link table,
* for other implementations it may mean existing relations are changed.
* Create a link between the given source and target ids.This is not
* necessarily supported for all implementations, in all directions.For some
* types of relations this means creating new entries in a link table, for
* other implementations it may mean existing relations are changed.
*
* @param pm The persistence manager to use for accessing the database.
* @param sourceId The id of the entry in the source table.
* @param targetId The id of the entry in the target table.
* @param source The source entity of the link.
* @param target The target entity of the link. May or may not exist yet.
* @param navProp The navigation property of the relation.
* @param forInsert If new entities may be created. If false, the target
* must already exist.
* @throws NoSuchEntityException if the target entity does not exist yet and
* can not be created.
* @throws IncompleteEntityException if the target entity can not be created
* because it is not complete.
*/
public void link(PostgresPersistenceManager pm, Object sourceId, Object targetId);
public void link(PostgresPersistenceManager pm, Entity source, Entity target, NavigationPropertyMain navProp, boolean forInsert) throws NoSuchEntityException, IncompleteEntityException;

}
Expand Up @@ -18,13 +18,17 @@
package de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.relations;

import de.fraunhofer.iosb.ilt.frostserver.model.EntityType;
import de.fraunhofer.iosb.ilt.frostserver.model.core.Entity;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.PostgresPersistenceManager;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.QueryBuilder;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.factories.EntityFactories;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.tables.StaMainTable;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.tables.StaTable;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.utils.QueryState;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.utils.TableRef;
import de.fraunhofer.iosb.ilt.frostserver.property.NavigationPropertyMain;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.IncompleteEntityException;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.NoSuchEntityException;
import org.jooq.Field;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -144,7 +148,17 @@ public TableRef join(S source, QueryState<?> queryState, TableRef sourceRef) {
}

@Override
public void link(PostgresPersistenceManager pm, Object sourceId, Object targetId) {
public void link(PostgresPersistenceManager pm, Entity source, Entity target, NavigationPropertyMain navProp, boolean forInsert) throws NoSuchEntityException, IncompleteEntityException {
EntityFactories entityFactories = pm.getEntityFactories();
if (forInsert) {
entityFactories.entityExistsOrCreate(pm, target);
} else if (!entityFactories.entityExists(pm, target)) {
throw new NoSuchEntityException("Linked Entity with no id.");
}
link(pm, source.getId().getValue(), target.getId().getValue());
}

protected void link(PostgresPersistenceManager pm, Object sourceId, Object targetId) {
pm.getDslContext().insertInto(linkTable)
.set(sourceLinkFieldAcc.getField(linkTable), sourceId)
.set(targetLinkFieldAcc.getField(linkTable), targetId)
Expand Down
Expand Up @@ -79,7 +79,7 @@ public TableRef join(S source, QueryState<?> queryState, TableRef sourceRef) {
}

@Override
public void link(PostgresPersistenceManager pm, Object sourceId, Object targetId) {
protected void link(PostgresPersistenceManager pm, Object sourceId, Object targetId) {
final DSLContext dslContext = pm.getDslContext();
final L linkTable = getLinkTable();
final Field<Object> sourceLinkField = getSourceLinkFieldAcc().getField(linkTable);
Expand Down
Expand Up @@ -18,12 +18,16 @@
package de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.relations;

import de.fraunhofer.iosb.ilt.frostserver.model.EntityType;
import de.fraunhofer.iosb.ilt.frostserver.model.core.Entity;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.PostgresPersistenceManager;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.QueryBuilder;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.factories.EntityFactories;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.tables.StaMainTable;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.utils.QueryState;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.utils.TableRef;
import de.fraunhofer.iosb.ilt.frostserver.property.NavigationPropertyMain;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.IncompleteEntityException;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.NoSuchEntityException;
import org.jooq.Field;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -106,6 +110,28 @@ public TableRef join(S joinSource, QueryState<?> queryState, TableRef sourceRef)
return QueryBuilder.createJoinedRef(sourceRef, targetType, targetAliased);
}

@Override
public void link(PostgresPersistenceManager pm, Entity source, Entity target, NavigationPropertyMain navProp, boolean forInsert) throws IncompleteEntityException, NoSuchEntityException {
if (!distinctRequired) {
throw new IllegalStateException("Trying to update a one-to-many relation from the wrong side.");
}
EntityFactories entityFactories = pm.getEntityFactories();
if (entityFactories.entityExists(pm, target)) {
link(pm, source.getId().getValue(), target.getId().getValue());
} else if (forInsert) {
NavigationPropertyMain backLink = navProp.getInverse();
if (backLink == null) {
LOGGER.error("Back-link not found for relation {}/{}.", navProp.getEntityType(), navProp.getName());
throw new IllegalStateException("Back-link not found for relation " + navProp.getEntityType() + "/" + navProp.getName() + ".");
}
target.setProperty(backLink, source);
target.complete();
pm.insert(target);
} else {
throw new NoSuchEntityException("Linked Entity with no id.");
}
}

/**
* Re-links the one-to-many relation to a different entity. Updates the
* targetField to sourceId on TargetTable where TargetTable.getId =
Expand All @@ -115,8 +141,7 @@ public TableRef join(S joinSource, QueryState<?> queryState, TableRef sourceRef)
* @param sourceId The source id of the link.
* @param targetId The target id of the link.
*/
@Override
public void link(PostgresPersistenceManager pm, Object sourceId, Object targetId) {
protected void link(PostgresPersistenceManager pm, Object sourceId, Object targetId) {
if (!distinctRequired) {
throw new IllegalStateException("Trying to update a one-to-many relation from the wrong side.");
}
Expand Down

0 comments on commit ff47075

Please sign in to comment.