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.io.FileInputStream;
022import java.io.FileOutputStream;
023import java.io.InputStream;
024import java.io.OutputStreamWriter;
025import java.io.PrintWriter;
026import java.util.ArrayList;
027import java.util.List;
028
029import org.simpleframework.xml.Element;
030import org.simpleframework.xml.ElementList;
031import org.simpleframework.xml.Root;
032import org.simpleframework.xml.Serializer;
033import org.simpleframework.xml.core.Persister;
034import org.syncany.chunk.Transformer;
035
036/**
037 * The Transaction transfer object exists to serialize a transaction,
038 * which is saved on the remote location and deleted once the transaction is
039 * completed.
040 * 
041 * <p>It uses the Simple framework for XML serialization, and its corresponding
042 * annotation-based configuration.  
043 *  
044 * @see <a href="http://simple.sourceforge.net/">Simple framework</a> at simple.sourceforge.net
045 * @author Pim Otte
046 */
047@Root(name = "transaction", strict = false)
048public class TransactionTO {
049        @Element(name = "machineName")
050        private String machineName;
051
052        @ElementList(name = "actions", entry = "action")
053        private ArrayList<ActionTO> actionTOs;
054
055        public TransactionTO() {
056                // Nothing
057        }
058
059        public TransactionTO(String machineName) {
060                this.machineName = machineName;
061                this.actionTOs = new ArrayList<ActionTO>();
062        }
063
064        public String getMachineName() {
065                return machineName;
066        }
067
068        public List<ActionTO> getActions() {
069                return actionTOs;
070        }
071
072        public void addAction(ActionTO transactionAction) {
073                actionTOs.add(transactionAction);
074        }
075
076        public static TransactionTO load(Transformer transformer, File transactionFile) throws Exception {
077                InputStream is;
078
079                if (transformer == null) {
080                        is = new FileInputStream(transactionFile);
081                }
082                else {
083                        is = transformer.createInputStream(new FileInputStream(transactionFile));
084                }
085
086                return new Persister().read(TransactionTO.class, is);
087        }
088
089        public void save(Transformer transformer, File transactionFile) throws Exception {
090                PrintWriter out;
091
092                if (transformer == null) {
093                        out = new PrintWriter(new OutputStreamWriter(
094                                        new FileOutputStream(transactionFile), "UTF-8"));
095                }
096                else {
097                        out = new PrintWriter(new OutputStreamWriter(
098                                        transformer.createOutputStream(new FileOutputStream(transactionFile)), "UTF-8"));
099                }
100
101                Serializer serializer = new Persister();
102                serializer.write(this, out);
103                out.flush();
104                out.close();
105        }
106}