Skip to content

Commit

Permalink
Merge pull request #4310 from NCI-Agency/AB-813-fix-duplicate-positio…
Browse files Browse the repository at this point in the history
…n-code-error-handling

Correct error handling of duplicate position code
  • Loading branch information
gjvoosten authored Mar 27, 2023
2 parents 9792107 + b179a21 commit 7935dc2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
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

0 comments on commit 7935dc2

Please sign in to comment.