001package co.codewizards.cloudstore.local.persistence; 002 003import static co.codewizards.cloudstore.core.util.HashUtil.*; 004 005import java.util.ArrayList; 006import java.util.Collection; 007 008import javax.jdo.Query; 009 010import co.codewizards.cloudstore.core.util.AssertUtil; 011 012public class DeleteModificationDao extends Dao<DeleteModification, DeleteModificationDao> { 013 014 public Collection<DeleteModification> getDeleteModificationsForPathAfter(final String path, final long localRevision, final RemoteRepository remoteRepository) { 015 AssertUtil.assertNotNull(path, "path"); 016 AssertUtil.assertNotNull(remoteRepository, "remoteRepository"); 017 final String pathSha1 = sha1(path); 018 final Query query = pm().newNamedQuery(getEntityClass(), "getDeleteModificationsForPathAfter_pathSha1_localRevision_remoteRepository"); 019 try { 020 @SuppressWarnings("unchecked") 021 final Collection<DeleteModification> deleteModifications = (Collection<DeleteModification>) query.execute(pathSha1, localRevision, remoteRepository); 022 return new ArrayList<DeleteModification>(deleteModifications); 023 } finally { 024 query.closeAll(); 025 } 026 } 027 028 public Collection<DeleteModification> getDeleteModificationsForPathOrParentOfPathAfter(final String path, final long localRevision, final RemoteRepository remoteRepository) { 029 AssertUtil.assertNotNull(path, "path"); 030 AssertUtil.assertNotNull(remoteRepository, "remoteRepository"); 031 if (!path.startsWith("/")) 032 throw new IllegalArgumentException("path does not start with '/'!"); 033 034 final ArrayList<DeleteModification> deleteModifications = new ArrayList<DeleteModification>(); 035 String p = path; 036 while (true) { 037 final Collection<DeleteModification> c = getDeleteModificationsForPathAfter(p, localRevision, remoteRepository); 038 deleteModifications.addAll(c); 039 040 final int lastSlash = p.lastIndexOf('/'); 041 if (lastSlash <= 0) // The root itself cannot be deleted, hence we can quit as soon as we reached '/'. 042 break; 043 044 p = p.substring(0, lastSlash); 045 } 046 return deleteModifications; 047 } 048 049 public Collection<DeleteModification> getDeleteModificationsForSha1(final String sha1, final long length) { 050 AssertUtil.assertNotNull(sha1, "sha1"); 051 final Query query = pm().newNamedQuery(getEntityClass(), "getDeleteModifications_sha1_length"); 052 try { 053 @SuppressWarnings("unchecked") 054 final 055 Collection<DeleteModification> deleteModifications = (Collection<DeleteModification>) query.execute(sha1, length); 056 return new ArrayList<DeleteModification>(deleteModifications); 057 } finally { 058 query.closeAll(); 059 } 060 } 061 062}