Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct error handling of duplicate position code #4310

Merged
merged 2 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions src/main/java/mil/dds/anet/database/PositionDao.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package mil.dds.anet.database;

import java.sql.SQLException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -11,8 +10,6 @@
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response.Status;
import mil.dds.anet.AnetObjectEngine;
import mil.dds.anet.beans.Organization;
import mil.dds.anet.beans.PersonPositionHistory;
Expand All @@ -24,6 +21,7 @@
import mil.dds.anet.database.mappers.PositionMapper;
import mil.dds.anet.utils.DaoUtils;
import mil.dds.anet.utils.FkDataLoaderKey;
import mil.dds.anet.utils.ResponseUtils;
import mil.dds.anet.utils.SqDataLoaderKey;
import mil.dds.anet.utils.Utils;
import mil.dds.anet.views.ForeignKeyFetcher;
Expand All @@ -39,6 +37,9 @@ public class PositionDao extends AnetSubscribableObjectDao<Position, PositionSea
"organizationUuid", "currentPersonUuid", "type", "status", "locationUuid", "customFields"};
public static final String TABLE_NAME = "positions";
public static final String POSITION_FIELDS = DaoUtils.buildFieldAliases(TABLE_NAME, fields, true);
public static final String DUPLICATE_POSITION_CODE =
"Another position is already using this code and each position must have its own code. "
+ "Please double check that you entered the right code.";

@Override
public Position insertInternal(Position p) {
Expand All @@ -60,25 +61,11 @@ public Position insertInternal(Position p) {
// Specifically don't set currentPersonUuid here because we'll handle that later in
// setPersonInPosition();
} catch (UnableToExecuteStatementException e) {
checkForUniqueCodeViolation(e);
throw e;
throw ResponseUtils.handleSqlException(e, DUPLICATE_POSITION_CODE);
}
return p;
}

public void checkForUniqueCodeViolation(UnableToExecuteStatementException e) {
if (e.getCause() != null && e.getCause() instanceof SQLException) {
SQLException cause = (SQLException) e.getCause();
if (cause.getErrorCode() == 2601) { // Unique Key Violation constant for SQL Server
if (cause.getMessage().contains("UQ_PositionCodes")) {
throw new WebApplicationException("Another position is already using this "
+ "code and each position must have its own code. "
+ "Please double check that you entered the right code. ", Status.CONFLICT);
}
}
}
}

@Override
public Position getByUuid(String uuid) {
return getByIds(Arrays.asList(uuid)).get(0);
Expand Down Expand Up @@ -176,8 +163,7 @@ public int updateInternal(Position p) {
.evictFromCacheByPositionUuid(DaoUtils.getUuid(p));
return nr;
} catch (UnableToExecuteStatementException e) {
checkForUniqueCodeViolation(e);
throw e;
throw ResponseUtils.handleSqlException(e, DUPLICATE_POSITION_CODE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.ClientErrorException;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.NotFoundException;
import mil.dds.anet.test.TestData;
Expand Down Expand Up @@ -469,10 +471,11 @@ public void createPositionTest()
adminQueryExecutor.organizationList(getListFields(ORGANIZATION_FIELDS), queryOrgs);
assertThat(orgs.getList().size()).isGreaterThan(0);

final String positionCode = UUID.randomUUID().toString();
final PositionInput newbPositionInput = PositionInput.builder()
.withName("PositionTest Position for Newb").withType(PositionType.PRINCIPAL)
.withOrganization(getOrganizationInput(orgs.getList().get(0))).withStatus(Status.ACTIVE)
.withPerson(getPersonInput(newb)).build();
.withPerson(getPersonInput(newb)).withCode(positionCode).build();

final Position newbPosition = adminMutationExecutor.createPosition(FIELDS, newbPositionInput);
assertThat(newbPosition).isNotNull();
Expand Down Expand Up @@ -546,6 +549,17 @@ public void createPositionTest()
assertThat(history.size()).isEqualTo(2);
assertThat(history.get(0).getPerson().getUuid()).isEqualTo(newb.getUuid());
assertThat(history.get(1).getPerson().getUuid()).isEqualTo(prin2.getUuid());

// Try to create another position with the same code
final PositionInput dupCodePositionInput = PositionInput.builder()
.withName("PositionTest Position for duplicate code").withType(PositionType.PRINCIPAL)
.withOrganization(getOrganizationInput(orgs.getList().get(0))).withStatus(Status.ACTIVE)
.withCode(positionCode).build();
try {
adminMutationExecutor.createPosition(FIELDS, dupCodePositionInput);
fail("Expected ClientErrorException");
} catch (ClientErrorException expectedException) {
}
}

@Test
Expand Down