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.net.URL;
007import java.util.UUID;
008
009import javax.jdo.annotations.Column;
010import javax.jdo.annotations.Discriminator;
011import javax.jdo.annotations.DiscriminatorStrategy;
012import javax.jdo.annotations.Index;
013import javax.jdo.annotations.Inheritance;
014import javax.jdo.annotations.InheritanceStrategy;
015import javax.jdo.annotations.NullValue;
016import javax.jdo.annotations.PersistenceCapable;
017import javax.jdo.annotations.Persistent;
018import javax.jdo.annotations.Queries;
019import javax.jdo.annotations.Query;
020
021import org.slf4j.Logger;
022import org.slf4j.LoggerFactory;
023
024import co.codewizards.cloudstore.core.util.UrlUtil;
025
026@PersistenceCapable
027@Inheritance(strategy=InheritanceStrategy.SUPERCLASS_TABLE)
028@Discriminator(strategy=DiscriminatorStrategy.VALUE_MAP, value="RemoteRepository")
029//@Index(name="RemoteRepository_remoteRoot", members="remoteRoot") // Indexing a CLOB with Derby throws an exception :-( [should be a warning, IMHO for portability reasons]
030@Index(name="RemoteRepository_remoteRootSha1", members="remoteRootSha1")
031@Queries({
032        @Query(name="getRemoteRepository_repositoryId", value="SELECT UNIQUE WHERE this.repositoryId == :repositoryId"),
033        @Query(name="getRemoteRepository_remoteRootSha1", value="SELECT UNIQUE WHERE this.remoteRootSha1 == :remoteRootSha1")
034})
035public class RemoteRepository extends Repository implements AutoTrackLocalRevision {
036        private static final Logger logger = LoggerFactory.getLogger(RemoteRepository.class);
037
038        @Column(jdbcType="CLOB")
039        private URL remoteRoot;
040
041        private String remoteRootSha1;
042
043        private long localRevision;
044
045        @Persistent(nullValue=NullValue.EXCEPTION)
046        private String localPathPrefix;
047
048        public RemoteRepository() { }
049
050        public RemoteRepository(UUID repositoryId) {
051                super(repositoryId);
052        }
053
054        public URL getRemoteRoot() {
055                return remoteRoot;
056        }
057
058        public void setRemoteRoot(URL remoteRoot) {
059                remoteRoot = UrlUtil.canonicalizeURL(remoteRoot);
060                this.remoteRoot = remoteRoot;
061                this.remoteRootSha1 = remoteRoot == null ? null : sha1(remoteRoot.toExternalForm());
062        }
063
064        public String getRemoteRootSha1() {
065                return remoteRootSha1;
066        }
067
068        @Override
069        public long getLocalRevision() {
070                return localRevision;
071        }
072        @Override
073        public void setLocalRevision(long localRevision) {
074                if (this.localRevision != localRevision) {
075                        if (logger.isDebugEnabled())
076                                logger.debug("setLocalRevision: repositoryId={} old={} new={}", getRepositoryId(), this.localRevision, localRevision);
077
078                        this.localRevision = localRevision;
079                }
080        }
081
082        public String getLocalPathPrefix() {
083                return localPathPrefix;
084        }
085        public void setLocalPathPrefix(String localPathPrefix) {
086                assertNotNull("localPathPrefix", localPathPrefix);
087                if (!localPathPrefix.isEmpty() && !localPathPrefix.startsWith("/"))
088                        throw new IllegalArgumentException("localPathPrefix must start with '/' but does not: " + localPathPrefix);
089
090                this.localPathPrefix = localPathPrefix;
091        }
092}