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

[CCAP-314] Moving short code generation to controller #599

Merged
merged 1 commit into from
Sep 12, 2024
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
3 changes: 3 additions & 0 deletions src/main/java/formflow/library/ScreenController.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class ScreenController extends FormFlowController {
private final ConditionManager conditionManager;
private final ActionManager actionManager;
private final FileValidationService fileValidationService;
private final SubmissionRepositoryService submissionRepositoryService;

public ScreenController(
List<FlowConfiguration> flowConfigurations,
Expand All @@ -87,6 +88,7 @@ public ScreenController(
this.conditionManager = conditionManager;
this.actionManager = actionManager;
this.fileValidationService = fileValidationService;
this.submissionRepositoryService = submissionRepositoryService;
log.info("Screen Controller Created!");
}

Expand Down Expand Up @@ -339,6 +341,7 @@ private ModelAndView handlePost(
)
);
submission.setSubmittedAt(OffsetDateTime.now());
submissionRepositoryService.generateAndSetUniqueShortCode(submission);
}

actionManager.handleBeforeSaveAction(currentScreen, submission);
Expand Down

This file was deleted.

14 changes: 0 additions & 14 deletions src/main/java/formflow/library/config/SpringConfiguration.java

This file was deleted.

77 changes: 7 additions & 70 deletions src/main/java/formflow/library/data/Submission.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,33 @@

import static formflow.library.inputs.FieldNameMarkers.UNVALIDATED_FIELD_MARKER_VALIDATED;

import formflow.library.config.SpringConfiguration;
import formflow.library.config.submission.ShortCodeConfig;
import formflow.library.inputs.AddressParts;
import io.hypersistence.utils.hibernate.type.json.JsonType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

import jakarta.persistence.Transient;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.Optional;

import java.util.UUID;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.hibernate.Hibernate;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.SourceType;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
Expand Down Expand Up @@ -82,21 +75,10 @@ public class Submission {
@Column(name = "submitted_at")
private OffsetDateTime submittedAt;

@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
@Column(name = "short_code")
private String shortCode;

@Transient
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
SubmissionRepository submissionRepository;

@Transient
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private ShortCodeConfig shortCodeConfig;

/**
* The key name for the field in an iteration's data that holds the status of completion for the iteration.
*/
Expand Down Expand Up @@ -241,6 +223,8 @@ public static Submission copySubmission(Submission origSubmission) {
newSubmission.setSubmittedAt(origSubmission.getSubmittedAt());
newSubmission.setId(origSubmission.getId());

newSubmission.setShortCode(origSubmission.getShortCode());

// deep copy the subflows and any lists
newSubmission.setInputData(copyMap(origSubmission.getInputData()));
return newSubmission;
Expand Down Expand Up @@ -272,58 +256,11 @@ private static Map<String, Object> copyMap(Map<String, Object> origMap) {
return result;
}

/**
* getShortCode returns a read-only unique code for the submission after the submission has been saved to the database initially.
* The short code generation is configurable via @ShortCodeConfig. The length, characterset (alphanumeric, numeric, alpha), and
* forced uppercasing are available (with the defaults being 6, alphanumeric, and true respectively).
*
* This method will check if the generated code exists in the database, and keep trying to create a unique code, before
* persisting and returning the newly generated code-- therefore it is very important to ensure the configuration allows for a
* suitably large set of possible codes for the application.
* @return the short code for the Submission
*/
public synchronized String getShortCode() {

if (this.createdAt == null) {
// If the submission hasn't been saved ever, there should be no concept of a short code
return null;
}

if (submissionRepository == null) {
submissionRepository = (SubmissionRepository) SpringConfiguration.contextProvider().getApplicationContext().getBean("submissionRepository");
}

shortCode = submissionRepository.findShortCodeById(this.id);
if (shortCode == null) {
if (shortCodeConfig == null) {
shortCodeConfig = (ShortCodeConfig) SpringConfiguration.contextProvider().getApplicationContext().getBean("shortCodeConfig");
}

// If there is no short code for this submission in the database, generate one
do {
int codeLength = shortCodeConfig.getCodeLength();
String newCode = switch(shortCodeConfig.getCodeType()) {
case alphanumeric -> RandomStringUtils.randomAlphanumeric(codeLength);
case alpha -> RandomStringUtils.randomAlphabetic(codeLength);
case numeric -> RandomStringUtils.randomNumeric(codeLength);
};

if (shortCodeConfig.isUppercase()) {
newCode = newCode.toUpperCase();
}

boolean exists = submissionRepository.existsByShortCode(newCode);
if (!exists) {
// If the newly generated code isn't already in the database being used by a prior submission
// set this submission's shortcode, and persist it
shortCode = newCode;
submissionRepository.save(this);
} else {
log.warn("Confirmation code {} already exists", newCode);
}
} while (shortCode == null);
public void setShortCode(String shortCode) {
if (this.shortCode != null) {
throw new UnsupportedOperationException("Cannot change shortCode for an existing submission");
}
return shortCode;
this.shortCode = shortCode;
}

@Override
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/formflow/library/data/SubmissionRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Optional;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

Expand All @@ -19,9 +18,4 @@ public interface SubmissionRepository extends JpaRepository<Submission, UUID> {

@Query("SELECT s.shortCode AS shortCode FROM Submission s WHERE s.id = ?1")
String findShortCodeById(UUID uuid);

@Modifying
@Query("UPDATE Submission s SET s.shortCode = ?1 WHERE s.id = ?2")
void saveShortCodeById(String shortCode, UUID uuid);

}
Loading