private void cleanUpSnapshots(final Requisition requisition) {
    final String foreignSource = requisition.getForeignSource();
    final Date pendingDate = m_pendingForeignSourceRepository.getRequisitionDate(foreignSource);

    final List<File> pendingSnapshots =
        RequisitionFileUtils.findSnapshots(m_pendingForeignSourceRepository, foreignSource);

    if (pendingDate != null) {
      /* determine whether to delete the pending requisition */
      boolean deletePendingRequisition = true;
      if (pendingSnapshots.size() > 0) {
        for (final File pendingSnapshotFile : pendingSnapshots) {
          if (isNewer(pendingSnapshotFile, pendingDate)) {
            // the pending file is newer than an in-process snapshot, don't delete it
            deletePendingRequisition = false;
            break;
          }
        }
      }
      if (deletePendingRequisition) {
        m_pendingForeignSourceRepository.delete(requisition);
      }
    }

    /* determine whether this requisition was imported from a snapshot, and if so, delete its snapshot file */
    RequisitionFileUtils.deleteResourceIfSnapshot(requisition);

    final Date deployedDate = m_deployedForeignSourceRepository.getRequisitionDate(foreignSource);
    if (deployedDate != null) {
      RequisitionFileUtils.deleteSnapshotsOlderThan(
          getPendingForeignSourceRepository(), foreignSource, deployedDate);
    }
  }
  @Test
  public void simpleSnapshotTest() throws URISyntaxException {
    Requisition pendingReq = new Requisition("test");
    pendingReq.putNode(createNode("1"));
    m_pending.save(pendingReq);
    m_pending.flush();
    pendingReq = m_pending.getRequisition(pendingReq.getForeignSource());
    final File pendingSnapshot =
        RequisitionFileUtils.createSnapshot(
            m_pending, pendingReq.getForeignSource(), pendingReq.getDate());

    m_repository.importResourceRequisition(new FileSystemResource(pendingSnapshot));

    assertFalse(pendingSnapshot.exists());
    final URL pendingUrl = m_pending.getRequisitionURL(pendingReq.getForeignSource());
    final File pendingFile = new File(pendingUrl.toURI());
    assertFalse(pendingFile.exists());
  }
 /**
  * getProvisioningGroupNames
  *
  * @return a {@link java.util.Collection} object.
  */
 @Override
 public Collection<String> getProvisioningGroupNames() {
   m_readLock.lock();
   try {
     m_deployedForeignSourceRepository.flush();
     final Set<String> names = new TreeSet<String>();
     for (final Requisition r : m_deployedForeignSourceRepository.getRequisitions()) {
       names.add(r.getForeignSource());
     }
     m_pendingForeignSourceRepository.flush();
     for (final Requisition r : m_pendingForeignSourceRepository.getRequisitions()) {
       names.add(r.getForeignSource());
     }
     return names;
   } finally {
     m_readLock.unlock();
   }
 }
  /** {@inheritDoc} */
  @Override
  public synchronized Requisition importResourceRequisition(final Resource resource)
      throws ForeignSourceRepositoryException {
    final Requisition requisition =
        m_deployedForeignSourceRepository.importResourceRequisition(resource);
    final String foreignSource = requisition.getForeignSource();

    cleanUpDeployedForeignSources(foreignSource);
    cleanUpSnapshots(requisition);

    return requisition;
  }
  @Test
  public void multipleSnapshotTest() throws URISyntaxException, InterruptedException {
    Requisition pendingReq = new Requisition("test");
    pendingReq.putNode(createNode("1"));
    m_pending.save(pendingReq);
    m_pending.flush();
    final String foreignSource = pendingReq.getForeignSource();
    pendingReq = m_pending.getRequisition(foreignSource);
    final File pendingSnapshotA =
        RequisitionFileUtils.createSnapshot(m_pending, foreignSource, pendingReq.getDate());

    // Now, start a new pending update after the original snapshot is "in progress"
    pendingReq.updateDateStamp();
    m_pending.save(pendingReq);
    m_pending.flush();

    final File pendingSnapshotB =
        RequisitionFileUtils.createSnapshot(m_pending, foreignSource, pendingReq.getDate());

    // "import" the A snapshot
    m_repository.importResourceRequisition(new FileSystemResource(pendingSnapshotA));

    assertFalse(pendingSnapshotA.exists());
    assertTrue(pendingSnapshotB.exists());

    // since there's still a newer snapshot in-progress, it is safe to delete the pending test.xml
    URL pendingUrl = m_pending.getRequisitionURL(foreignSource);
    assertNotNull(pendingUrl);
    assertFalse(new File(pendingUrl.toURI()).exists());

    // then, "import" the B snapshot
    final Requisition bReq =
        m_repository.importResourceRequisition(new FileSystemResource(pendingSnapshotB));

    assertFalse(pendingSnapshotA.exists());
    assertFalse(pendingSnapshotB.exists());

    // now the pending test.xml should be gone
    pendingUrl = m_pending.getRequisitionURL(foreignSource);
    assertNotNull(pendingUrl);
    assertFalse(new File(pendingUrl.toURI()).exists());

    // the last (B) pending import should match the deployed
    final Requisition deployedRequisition = m_active.getRequisition(foreignSource);
    assertEquals(deployedRequisition.getDate().getTime(), bReq.getDate().getTime());
  }