001package co.codewizards.cloudstore.local.persistence;
002
003import static co.codewizards.cloudstore.core.util.HashUtil.*;
004import static co.codewizards.cloudstore.core.util.Util.*;
005
006import java.util.UUID;
007
008import javax.jdo.JDOHelper;
009import javax.jdo.annotations.Column;
010import javax.jdo.annotations.Index;
011import javax.jdo.annotations.Inheritance;
012import javax.jdo.annotations.InheritanceStrategy;
013import javax.jdo.annotations.NullValue;
014import javax.jdo.annotations.PersistenceCapable;
015import javax.jdo.annotations.Persistent;
016import javax.jdo.annotations.Queries;
017import javax.jdo.annotations.Query;
018import javax.jdo.annotations.Unique;
019
020/**
021 * While a file is in progress of a sync, an instance of FileInProgressMarker is stored.
022 * <p/>
023 * An db entry of this class is describing the connection (from, to) and the path of one file synchronisation, that is
024 * just in progress. If this file is transferred successfully, this instance should be removed.
025 * <p>
026 * After a successful sync run in the repository, there should be no entry for this connection (fromRepositoryId to
027 * toRepositoryId) all. If there is an entity left, it is assumed, that this connection got interrupted while syncing
028 * this file.
029 *
030 * @author Sebastian Schefczyk
031 */
032@PersistenceCapable
033@Inheritance(strategy = InheritanceStrategy.NEW_TABLE)
034@Index(
035                name="FileInProgressMark_fromRepositoryId_toRepositoryId",
036                members={"fromRepositoryId", "toRepositoryId"})
037@Unique(name = "FileInProgressMark_fromRepositoryId_toRepositoryId_pathSha1", members = { "fromRepositoryId", "toRepositoryId", "pathSha1" })
038@Queries({
039        @Query(name = "getFileInProgressMarkers_fromRepositoryId_toRepositoryId",
040                        value = "SELECT WHERE this.fromRepositoryId == :fromRepositoryId && this.toRepositoryId == :toRepositoryId"),
041        @Query(name = "getFileInProgressMarker_fromRepositoryId_toRepositoryId_pathSha1",
042                        value = "SELECT UNIQUE WHERE this.fromRepositoryId == :fromRepositoryId && this.toRepositoryId == :toRepositoryId && this.pathSha1 == :pathSha1")
043})
044public class FileInProgressMarker extends Entity {
045
046        @Persistent(nullValue = NullValue.EXCEPTION)
047        private String fromRepositoryId;
048
049        @Persistent(nullValue = NullValue.EXCEPTION)
050        private String toRepositoryId;
051
052        @Persistent(nullValue=NullValue.EXCEPTION)
053        private String pathSha1;
054
055        @Persistent(nullValue = NullValue.EXCEPTION, defaultFetchGroup = "true")
056        @Column(jdbcType="CLOB")
057        private String path;
058
059        public UUID getFromRepositoryId() {
060                return FileInProgressMarkerDao.convertToUuid(fromRepositoryId);
061        }
062
063        public void setFromRepositoryId(final UUID fromRepositoryId) {
064                if (! equal(this.getFromRepositoryId(), fromRepositoryId))
065                        this.fromRepositoryId = FileInProgressMarkerDao.convertToString(fromRepositoryId);
066        }
067
068        public UUID getToRepositoryId() {
069                return FileInProgressMarkerDao.convertToUuid(toRepositoryId);
070        }
071
072        public void setToRepositoryId(final UUID toRepositoryId) {
073                if (! equal(this.getToRepositoryId(), toRepositoryId))
074                        this.toRepositoryId = FileInProgressMarkerDao.convertToString(toRepositoryId);
075        }
076
077        public String getPath() {
078                return path;
079        }
080
081        public void setPath(final String path) {
082                this.pathSha1 = sha1(path);
083                this.path = path;
084        }
085
086        @Override
087        protected String toString_getProperties() {
088                return super.toString_getProperties() + String.format(", fromRepositoryId=\"%s\", toRepositoryId=\"%s\", path=\"%s\"",
089                                getClass().getSimpleName(), JDOHelper.getObjectId(this),
090                                fromRepositoryId, toRepositoryId, path);
091        }
092}