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