/**
   * Method for telling whether a file entry exists.
   *
   * @param filename The name of the file, the existence of whose entry is to be determined.
   * @return Whether the entry exists.
   * @throws ArgumentNotValid If the filename is either null or empty.
   */
  @Override
  public boolean hasEntry(String filename) throws ArgumentNotValid {
    ArgumentNotValid.checkNotNullOrEmpty(filename, "String filename");

    // See if the file can be found in the database.
    return database.existsFileInDB(filename);
  }
 /**
  * Retrieves a set of the names for all the known files.
  *
  * @return A set of the names for all the known file.
  */
 @Override
 public Set<String> getAllFileNames() {
   // initialise the set
   Set<String> res = new HashSet<String>();
   // put the collection of filenames into the set.
   res.addAll(database.retrieveAllFilenames());
   // return the set.
   return res;
 }
  /**
   * Method for adding an entry for administration.
   *
   * @param filename The name of the file to be stored.
   * @param msg The StoreMessage of the entry.
   * @param checksum The checksum of the entry.
   * @throws ArgumentNotValid If either the filename or checksum is either null or the empty string.
   */
  @Override
  public void addEntry(String filename, StoreMessage msg, String checksum) throws ArgumentNotValid {
    ArgumentNotValid.checkNotNullOrEmpty(filename, "String filename");
    ArgumentNotValid.checkNotNullOrEmpty(checksum, "String checksum");

    // insert this into the entries map.
    storeEntries.put(filename, msg);

    // insert into database.
    database.insertNewFileForUpload(filename, checksum);
  }
  /**
   * Returns the ReplicaStoreState of a given file in a specific replica.
   *
   * @param filename The name of the file for the ReplicaStoreState.
   * @param replicaChannelName The name of the identification channel for uniquely identifying the
   *     replica of for the ReplicaStoreState.
   * @return The ReplicaStoreState of a given file in a specific replica.
   * @throws ArgumentNotValid If the filename or the replica id is null or the empty string.
   */
  @Override
  public ReplicaStoreState getState(String filename, String replicaChannelName)
      throws ArgumentNotValid {
    ArgumentNotValid.checkNotNullOrEmpty(filename, "String filename");
    ArgumentNotValid.checkNotNullOrEmpty(replicaChannelName, "String replicaChannelName");

    Replica rep = Channels.retrieveReplicaFromIdentifierChannel(replicaChannelName);

    // retrieve the ReplicaStoreState from the database.
    return database.getReplicaStoreState(filename, rep.getId());
  }
  /**
   * Retrieves a set with the name of the files with a specific ReplicaStoreState in a specific
   * replica.
   *
   * @param rep The replica where the files belong.
   * @param state The ReplicaStoreState for the files.
   * @return A set with the names of the files with a specific ReplicaStoreState in a specific
   *     replica.
   * @throws ArgumentNotValid If the Replica or the ReplicaStoreState is null.
   */
  @Override
  public Set<String> getAllFileNames(Replica rep, ReplicaStoreState state) throws ArgumentNotValid {
    ArgumentNotValid.checkNotNull(rep, "Replica rep");
    ArgumentNotValid.checkNotNull(state, "ReplicaStoreState state");

    // initialise the set
    Set<String> res = new HashSet<String>();
    // put the collection of filenames into the set.
    res.addAll(database.retrieveFilenamesForReplicaEntries(rep.getId(), state));
    // return the set.
    return res;
  }
  /**
   * Retrieves the checksum of a given file.
   *
   * @param filename The name of the file, whose checksum should be retrieved.
   * @return The checksum of the file.
   * @throws ArgumentNotValid If the filename is either null or the empty string.
   */
  @Override
  public String getCheckSum(String filename) throws ArgumentNotValid {
    ArgumentNotValid.checkNotNullOrEmpty(filename, "String filename");

    // Ensure that we have the file requested.
    if (!hasEntry(filename)) {
      throw new UnknownID("Don't know anything about file '" + filename + "'");
    }

    // Retrieve the checksum for a specific entry.
    return database.getChecksum(filename);
  }
  /**
   * Sets the store state of an entry to a specific value.
   *
   * @param filename The name of the file for the entry.
   * @param repChannelId The identification channel of the replica for the entry.
   * @param state The new state for the entry.
   * @throws ArgumentNotValid If the ReplicaStoreState is null, or if either the filename or the
   *     replica identification channel is either null or the empty string.
   */
  @Override
  public void setState(String filename, String repChannelId, ReplicaStoreState state)
      throws ArgumentNotValid {
    ArgumentNotValid.checkNotNull(state, "ReplicaStoreState state");
    ArgumentNotValid.checkNotNullOrEmpty(filename, "String filename");
    ArgumentNotValid.checkNotNullOrEmpty(repChannelId, "String repChannelId");

    // retrieve the replica
    Replica rep = Channels.retrieveReplicaFromIdentifierChannel(repChannelId);

    // update the database.
    database.setReplicaStoreState(filename, rep.getId(), state);
  }
  /**
   * Determines whether a given file in a specific replica has a valid replica store state. By valid
   * means a replica store state other that UNKNOWN_UPLOAD_STATE.
   *
   * <p>TODO Find out if the assumption that all upload states besides UNKNOWN_UPLOAD_STATE are
   * acceptable!
   *
   * @param filename The name of the file for the ReplicaStoreState.
   * @param repChannelId The identification channel of the replica for the ReplicaStoreState.
   * @return Whether a given file in a specific replica has a valid store state.
   * @throws ArgumentNotValid If either the filenames or the replica identification channel is null
   *     or the empty string.
   */
  @Override
  public boolean hasState(String filename, String repChannelId) throws ArgumentNotValid {
    ArgumentNotValid.checkNotNullOrEmpty(filename, "String filename");
    ArgumentNotValid.checkNotNullOrEmpty(repChannelId, "String repChannelId");

    // retrieve the replica
    Replica rep = Channels.retrieveReplicaFromIdentifierChannel(repChannelId);

    // retrieve the state for the entry for the replica and filename
    ReplicaStoreState state = database.getReplicaStoreState(filename, rep.getId());

    // return whether the entry has a known upload state
    return state != ReplicaStoreState.UNKNOWN_UPLOAD_STATE;
  }
 /** Constructor. Initialises the access to the database. */
 private DatabaseAdmin() {
   database = ReplicaCacheDatabase.getInstance();
 }
 /** Close and cleanup of this class. */
 @Override
 public void close() {
   storeEntries.clear();
   database.cleanup();
   instance = null;
 }