Class AlgorithmDBService

java.lang.Object
org.trustdeck.service.AlgorithmDBService

@Service public class AlgorithmDBService extends Object
This class encapsulates the database access for algorithm objects.
  • Field Details

    • DEFAULT_ALGORITHM_NAME

      public static final String DEFAULT_ALGORITHM_NAME
      The name of the default algorithm.
      See Also:
    • DEFAULT_RANDOM_ALGORITHM_DESIRED_SIZE

      public static final long DEFAULT_RANDOM_ALGORITHM_DESIRED_SIZE
      The default number of pseudonyms that a randomness-based algorithm should be able to produce.
      See Also:
    • DEFAULT_RANDOM_ALGORITHM_DESIRED_SUCCESS_PROBABILITY

      public static final double DEFAULT_RANDOM_ALGORITHM_DESIRED_SUCCESS_PROBABILITY
      The default success probability for creating a new pseudonym when using a randomness-based algorithm.
      See Also:
    • DEFAULT_RANDOM_ALGORITHM_ALPHABET

      public static final String DEFAULT_RANDOM_ALGORITHM_ALPHABET
      The default success probability for creating a new pseudonym when using a randomness-based algorithm.
      See Also:
    • DEFAULT_CONSECUTIVE_VALUE_COUNTER

      public static final long DEFAULT_CONSECUTIVE_VALUE_COUNTER
      The default starting point for consecutive numbers as pseudonyms.
      See Also:
    • DEFAULT_PSEUDONYM_LENGTH

      public static final int DEFAULT_PSEUDONYM_LENGTH
      The default length for the pseudonyms.
      See Also:
    • DEFAULT_PSEUDONYM_LENGTH_RND

      public static final int DEFAULT_PSEUDONYM_LENGTH_RND
      The default length for the pseudonyms when using a random algorithm.
      See Also:
    • DEFAULT_PADDING_CHARACTER

      public static final String DEFAULT_PADDING_CHARACTER
      The default character used for padding the pseudonyms to the desired length.
      See Also:
    • DEFAULT_ADD_CHECK_DIGIT

      public static final boolean DEFAULT_ADD_CHECK_DIGIT
      The default value for whether or not to add a check digit to the pseudonym.
      See Also:
    • DEFAULT_LENGTH_INCLUDES_CHECK_DIGIT

      public static final boolean DEFAULT_LENGTH_INCLUDES_CHECK_DIGIT
      The default value for whether or not the check digit should be included in the pseudonym length.
      See Also:
    • DEFAULT_SALT_LENGTH

      public static final int DEFAULT_SALT_LENGTH
      The default length of a newly generated salt value.
      See Also:
  • Constructor Details

    • AlgorithmDBService

      public AlgorithmDBService()
  • Method Details

    • createAlgorithm

      @Transactional public Integer createAlgorithm(Algorithm algorithm)
      Creates an algorithm object in the database.
      Parameters:
      algorithm - the algorithm POJO to store
      Returns:
      the ID of the newly created algorithm, or null if an error occurred.
    • createOrGetAlgorithm

      @Transactional public Integer createOrGetAlgorithm(Algorithm algorithm)
      Creates an algorithm object in the database if it does not already exist.
      Parameters:
      algorithm - the algorithm POJO to store
      Returns:
      the ID of the (already) created algorithm, or null if an error occurred.
    • deleteAlgorithm

      @Transactional public boolean deleteAlgorithm(int ID) throws UnexpectedResultSizeException
      Deletes the algorithm object based on its unique ID.
      Parameters:
      ID - the ID corresponding to the algorithm object of interest
      Returns:
      true if the deletion was successful, false otherwise
      Throws:
      UnexpectedResultSizeException - whenever the deletion would not exactly affect one entry
    • getAlgorithmByID

      @Transactional public Algorithm getAlgorithmByID(int ID)
      Retrieves the algorithm object based on its unique ID.
      Parameters:
      ID - the ID corresponding to the algorithm object of interest
      Returns:
      the algorithm POJO, or null if nothing could be found
    • getAlgorithmByValues

      @Transactional public Algorithm getAlgorithmByValues(String name, String alphabet, Long randomAlgoDesiredSize, Double randomAlgoDesiredSuccessProbability, Integer pseudonymLength, String paddingChar, Boolean addCheckDigit, Boolean lengthIncludesCheckDigit, String salt, Integer saltLength)
      Method to retrieve an algorithm-object from the database. If there is more than one result, this method will return an arbitrary one that fits the search criteria.
      Parameters:
      name - the name of the algorithm
      alphabet - the alphabet used in the algorithm
      randomAlgoDesiredSize - the desired number of possible pseudonyms in the output space of a randomness-based algorithm
      randomAlgoDesiredSuccessProbability - the desired success probability for a randomness-based algorithm
      pseudonymLength - the length of the pseudonyms
      paddingChar - the character used to pad short pseudonyms to the desired length
      addCheckDigit - whether or not to add a check digit to the pseudonym
      lengthIncludesCheckDigit - whether or not the desired length includes the check digit
      salt - the salt used for pseudonymization
      saltLength - the length of the salt value
      Returns:
      the algorithm object that was found when searching for the given attributes, or null when nothing was found
    • isAlgorithmInUse

      @Transactional public boolean isAlgorithmInUse(int algorithmID)
      Helper method that checks if an algorithm is still used anywhere by searching for references to its ID.
      Parameters:
      algorithmID - the algorithm's ID
      Returns:
      true when the algorithm ID is referenced anywhere, false otherwise
    • updateAlgorithm

      @Transactional public Integer updateAlgorithm(Algorithm oldAlgorithm, Algorithm updatedAlgorithm)
      Updates an algorithm object in the database. Null values in the updateAlgorithm object indicate that this specific attribute is to be kept as before.
      Parameters:
      oldAlgorithm - the old algorithm POJO that should be updated
      updatedAlgorithm - the algorithm POJO that contains all the updated values
      Returns:
      the ID of the updated algorithm, or null if an error occurred
    • updateCounter

      @Transactional public boolean updateCounter(Long counter, int algorithmID)
      Method to update only the counter value of an algorithm.
      Parameters:
      counter - the new counter value
      algorithmID - the ID of the algorithm-object for which the counter should be updated
      Returns:
      true, when the update was successful, false otherwise.