001 /*
002 * $Revision: 15006 $
003 * $Date: 2009-03-25 10:28:39 +0100 (Wed, 25 Mar 2009) $
004 *
005 * This file is part of *** M y C o R e ***
006 * See http://www.mycore.de/ for details.
007 *
008 * This program is free software; you can use it, redistribute it
009 * and / or modify it under the terms of the GNU General Public License
010 * (GPL) as published by the Free Software Foundation; either version 2
011 * of the License or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful, but
014 * WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program, in a file called gpl.txt or license.txt.
020 * If not, write to the Free Software Foundation Inc.,
021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA
022 */
023
024 package org.mycore.datamodel.ifs2;
025
026 import java.io.ByteArrayOutputStream;
027 import java.util.Date;
028
029 import org.mycore.common.MCRUsageException;
030 import org.tmatesoft.svn.core.SVNLogEntry;
031 import org.tmatesoft.svn.core.io.SVNRepository;
032
033 /**
034 * Provides information about a stored version of metadata and allows to
035 * retrieve that version from SVN
036 *
037 * @author Frank Lützenkirchen
038 */
039 public class MCRMetadataVersion {
040 /**
041 * The metadata document this version belongs to
042 */
043 private MCRVersionedMetadata vm;
044
045 /**
046 * The revision number of this version
047 */
048 private long revision;
049
050 /**
051 * The user that created this version
052 */
053 private String user;
054
055 /**
056 * The date this version was created
057 */
058 private Date date;
059
060 /**
061 * Was this version result of a create, update or delete?
062 */
063 private char type;
064
065 /**
066 * A version that was created in store
067 */
068 public final static char CREATED = 'A';
069
070 /**
071 * A version that was updated in store
072 */
073 public final static char UPDATED = 'M';
074
075 /**
076 * A version that was deleted in store
077 */
078 public final static char DELETED = 'D';
079
080 /**
081 * Creates a new metadata version info object
082 *
083 * @param vm
084 * the metadata document this version belongs to
085 * @param logEntry
086 * the log entry from SVN holding data on this version
087 * @param type
088 * the type of commit
089 */
090 MCRMetadataVersion(MCRVersionedMetadata vm, SVNLogEntry logEntry, char type) {
091 this.vm = vm;
092 this.revision = logEntry.getRevision();
093 this.user = logEntry.getAuthor();
094 this.date = logEntry.getDate();
095 this.type = type;
096 }
097
098 /**
099 * Returns the metadata object this version belongs to
100 *
101 * @return the metadata object this version belongs to
102 */
103 public MCRVersionedMetadata getMetadataObject() {
104 return vm;
105 }
106
107 /**
108 * Returns the type of operation this version comes from
109 *
110 * @see #CREATED
111 * @see #UPDATED
112 * @see #DELETED
113 */
114 public char getType() {
115 return type;
116 }
117
118 /**
119 * Returns the SVN revision number of this version
120 *
121 * @return the SVN revision number of this version
122 */
123 public long getRevision() {
124 return revision;
125 }
126
127 /**
128 * Returns the user that created this version
129 *
130 * @return the user that created this version
131 */
132 public String getUser() {
133 return user;
134 }
135
136 /**
137 * Returns the date and time this version was created
138 *
139 * @return the date and time this version was created
140 */
141 public Date getDate() {
142 return date;
143 }
144
145 /**
146 * Retrieves this version of the metadata
147 *
148 * @return the metadata document as it was in this version
149 * @throws MCRUsageException
150 * if this is a deleted version, which can not be retrieved
151 */
152 public MCRContent retrieve() throws Exception {
153 if (type == DELETED) {
154 String msg = "You can not retrieve a deleted version, retrieve a previous version instead";
155 throw new MCRUsageException(msg);
156 }
157 SVNRepository repository = vm.getStore().getRepository();
158 ByteArrayOutputStream baos = new ByteArrayOutputStream();
159 repository.getFile(vm.getStore().getSlotPath(vm.getID()), revision, null, baos);
160 baos.close();
161 return MCRContent.readFrom(baos.toByteArray());
162 }
163
164 /**
165 * Replaces the current version of the metadata object with this version,
166 * which means that a new version is created that is identical to this old
167 * version. The stored metadata document is updated to this old version of
168 * the metadata.
169 */
170 public void restore() throws Exception {
171 vm.update(retrieve());
172 }
173 }