001package co.codewizards.cloudstore.local.persistence;
002
003import static co.codewizards.cloudstore.core.util.Util.*;
004
005import java.util.Collection;
006
007import javax.jdo.Query;
008
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012public class ModificationDAO extends DAO<Modification, ModificationDAO> {
013        private static final Logger logger = LoggerFactory.getLogger(ModificationDAO.class);
014
015        /**
016         * Gets those {@link Modification}s being assigned to the given {@link Modification#getRemoteRepository() remoteRepository}
017         * whose {@link Modification#getLocalRevision() localRevision} is greater than the given {@code localRevision}.
018         * @param remoteRepository the {@link Modification#getRemoteRepository() remoteRepository} the queried modifications are assigned to.
019         * @param localRevision the {@link Modification#getLocalRevision() localRevision}, after which the modifications
020         * to be queried where created.
021         * @return those {@link Modification}s matching the given criteria. Never <code>null</code>, but maybe empty.
022         */
023        public Collection<Modification> getModificationsAfter(RemoteRepository remoteRepository, long localRevision) {
024                assertNotNull("remoteRepository", remoteRepository);
025                Query query = pm().newNamedQuery(getEntityClass(), "getModificationsAfter_remoteRepository_localRevision");
026                try {
027                        long startTimestamp = System.currentTimeMillis();
028                        @SuppressWarnings("unchecked")
029                        Collection<Modification> modifications = (Collection<Modification>) query.execute(remoteRepository, localRevision);
030                        logger.debug("getModificationsAfter: query.execute(...) took {} ms.", System.currentTimeMillis() - startTimestamp);
031
032                        startTimestamp = System.currentTimeMillis();
033                        modifications = load(modifications);
034                        logger.debug("getModificationsAfter: Loading result-set with {} elements took {} ms.", modifications.size(), System.currentTimeMillis() - startTimestamp);
035
036                        return modifications;
037                } finally {
038                        query.closeAll();
039                }
040        }
041
042        public Collection<Modification> getModificationsBeforeOrEqual(RemoteRepository remoteRepository, long localRevision) {
043                assertNotNull("remoteRepository", remoteRepository);
044                Query query = pm().newNamedQuery(getEntityClass(), "getModificationsBeforeOrEqual_remoteRepository_localRevision");
045                try {
046                        long startTimestamp = System.currentTimeMillis();
047                        @SuppressWarnings("unchecked")
048                        Collection<Modification> modifications = (Collection<Modification>) query.execute(remoteRepository, localRevision);
049                        logger.debug("getModificationsBeforeOrEqual: query.execute(...) took {} ms.", System.currentTimeMillis() - startTimestamp);
050
051                        startTimestamp = System.currentTimeMillis();
052                        modifications = load(modifications);
053                        logger.debug("getModificationsBeforeOrEqual: Loading result-set with {} elements took {} ms.", modifications.size(), System.currentTimeMillis() - startTimestamp);
054
055                        return modifications;
056                } finally {
057                        query.closeAll();
058                }
059        }
060
061        /**
062         * Gets all {@link Modification}s being assigned to the given {@link Modification#getRemoteRepository() remoteRepository}.
063         * @param remoteRepository the {@link Modification#getRemoteRepository() remoteRepository} the queried modifications are assigned to.
064         * @return those {@link Modification}s matching the given criteria. Never <code>null</code>, but maybe empty.
065         */
066        public Collection<Modification> getModifications(RemoteRepository remoteRepository) {
067                return getModificationsAfter(remoteRepository, -1);
068        }
069}