001/*
002 * Syncany, www.syncany.org
003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com>
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package org.syncany.plugins.transfer.to;
019
020import java.io.File;
021import java.util.logging.Level;
022import java.util.logging.Logger;
023
024import org.simpleframework.xml.Element;
025import org.simpleframework.xml.Root;
026import org.syncany.plugins.transfer.StorageException;
027import org.syncany.plugins.transfer.files.RemoteFile;
028import org.syncany.plugins.transfer.files.TempRemoteFile;
029
030/**
031 * A TransactionActionTO describes a single action on a file, which is to be bundled
032 * with other actions to describe a full transaction.
033 *
034 * @author Pim Otte
035 */
036@Root(name = "transactionAction", strict = false)
037public class ActionTO {
038        private static final Logger logger = Logger.getLogger(ActionTO.class.getSimpleName());
039
040        public enum ActionType {
041                UPLOAD, DELETE
042        }
043
044        public enum ActionStatus {
045                UNSTARTED, STARTED, DONE
046        }
047
048        @Element(name = "type", required = true)
049        private ActionType type;
050
051        @Element(name = "status", required = false)
052        private ActionStatus status = ActionStatus.UNSTARTED;
053
054        @Element(name = "remoteLocation", required = true)
055        private String remoteLocation;
056
057        @Element(name = "remoteTempLocation", required = true)
058        private String remoteTempLocation;
059
060        @Element(name = "localTempLocation", required = false)
061        private String localTempLocation;
062
063        public ActionType getType() {
064                return type;
065        }
066
067        public void setType(ActionType type) {
068                this.type = type;
069        }
070
071        public ActionStatus getStatus() {
072                return status;
073        }
074
075        public void setStatus(ActionStatus status) {
076                this.status = status;
077        }
078
079        public void setRemoteLocation(RemoteFile remoteFile) {
080                remoteLocation = remoteFile.getName();
081        }
082
083        public RemoteFile getRemoteFile() throws StorageException {
084                return RemoteFile.createRemoteFile(remoteLocation);
085        }
086
087        public void setRemoteTempLocation(TempRemoteFile tempRemoteFile) {
088                remoteTempLocation = tempRemoteFile.getName();
089        }
090
091        public TempRemoteFile getTempRemoteFile() {
092                try {
093                        return RemoteFile.createRemoteFile(remoteTempLocation, TempRemoteFile.class);
094                }
095                catch (StorageException e) {
096                        logger.log(Level.INFO, "Invalid remote temporary filename: " + remoteTempLocation, e);
097                        return null;
098                }
099        }
100
101        public void setLocalTempLocation(File file) {
102                localTempLocation = file.getAbsolutePath();
103        }
104
105        public File getLocalTempLocation() {
106                return new File(localTempLocation);
107        }
108
109        @Override
110        public String toString() {
111                return "ActionTO [type=" + type + ", remoteLocation=" + remoteLocation + ", remoteTempLocation=" + remoteTempLocation
112                                + ", localTempLocation=" + localTempLocation + "]";
113        }
114}