001package co.codewizards.cloudstore.local.persistence;
002
003import static co.codewizards.cloudstore.core.util.HashUtil.*;
004import static java.util.Objects.*;
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(final String path, final long localRevision, final RemoteRepository remoteRepository) {
014                requireNonNull(path, "path");
015                requireNonNull(remoteRepository, "remoteRepository");
016                final String pathSha1 = sha1(path);
017                final Query query = pm().newNamedQuery(getEntityClass(), "getDeleteModificationsForPathAfter_pathSha1_localRevision_remoteRepository");
018                try {
019                        @SuppressWarnings("unchecked")
020                        final 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(final String path, final long localRevision, final RemoteRepository remoteRepository) {
028                requireNonNull(path, "path");
029                requireNonNull(remoteRepository, "remoteRepository");
030                if (!path.startsWith("/"))
031                        throw new IllegalArgumentException("path does not start with '/'!");
032
033                final ArrayList<DeleteModification> deleteModifications = new ArrayList<DeleteModification>();
034                String p = path;
035                while (true) {
036                        final Collection<DeleteModification> c = getDeleteModificationsForPathAfter(p, localRevision, remoteRepository);
037                        deleteModifications.addAll(c);
038
039                        final 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(final String sha1, final long length) {
049                requireNonNull(sha1, "sha1");
050                final Query query = pm().newNamedQuery(getEntityClass(), "getDeleteModifications_sha1_length");
051                try {
052                        @SuppressWarnings("unchecked")
053                        final
054                        Collection<DeleteModification> deleteModifications = (Collection<DeleteModification>) query.execute(sha1, length);
055                        return new ArrayList<DeleteModification>(deleteModifications);
056                } finally {
057                        query.closeAll();
058                }
059        }
060
061}