View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.datamodel.common;
20  
21  import java.io.IOException;
22  import java.util.Date;
23  import java.util.stream.Stream;
24  
25  import org.jdom2.JDOMException;
26  import org.mycore.common.MCRUsageException;
27  import org.mycore.common.content.MCRContent;
28  
29  import jakarta.xml.bind.annotation.XmlAccessType;
30  import jakarta.xml.bind.annotation.XmlAccessorType;
31  import jakarta.xml.bind.annotation.XmlAttribute;
32  import jakarta.xml.bind.annotation.XmlRootElement;
33  import jakarta.xml.bind.annotation.XmlTransient;
34  
35  /**
36   * Provides information about a stored version of metadata and allows to
37   * retrieve that version from SVN
38   * 
39   * @author Frank Lützenkirchen
40   */
41  @XmlRootElement(name = "revision")
42  @XmlAccessorType(XmlAccessType.FIELD)
43  public abstract class MCRAbstractMetadataVersion<T> {
44  
45      protected enum Type {
46          created(MCRAbstractMetadataVersion.CREATED),
47          modified(MCRAbstractMetadataVersion.UPDATED),
48          deleted(MCRAbstractMetadataVersion.DELETED);
49  
50          private final char charValue;
51  
52          Type(char a) {
53              this.charValue = a;
54          }
55  
56          public static Type fromValue(char a) {
57              return Stream.of(values()).filter(t -> t.charValue == a)
58                  .findAny()
59                  .orElseThrow(IllegalArgumentException::new);
60          }
61      }
62  
63      /**
64       * The metadata document this version belongs to
65       */
66      @XmlTransient
67      protected T vm;
68  
69      /**
70       * The revision number of this version
71       */
72      @XmlAttribute(name = "r")
73      protected String revision;
74  
75      /**
76       * The user that created this version
77       */
78      @XmlAttribute
79      protected String user;
80  
81      /**
82       * The date this version was created
83       */
84      @XmlAttribute
85      protected Date date;
86  
87      /**
88       * Was this version result of a create, update or delete?
89       */
90      @XmlAttribute()
91      protected Type type;
92  
93      /**
94       * A version that was created in store
95       */
96      public static final char CREATED = 'A';
97  
98      /**
99       * A version that was updated in store
100      */
101     public static final char UPDATED = 'M';
102 
103     /**
104      * A version that was deleted in store
105      */
106     public static final char DELETED = 'D';
107 
108     //required for JAXB serialization
109     @SuppressWarnings("unused")
110     private MCRAbstractMetadataVersion() {
111     }
112 
113     /**
114      * Creates a new metadata version info object
115      * 
116      * @param vm
117      *            the metadata document this version belongs to
118      * @param revision
119      *            the revision of this object, serialised as a string
120      * @param user
121      *            the user that created this revision
122      * @param date
123      *            the date on which this revision was created
124      * @param type
125      *            the type of commit
126      */
127     public MCRAbstractMetadataVersion(T vm, String revision, String user, Date date, char type) {
128         this.vm = vm;
129         this.revision = revision;
130         this.user = user;
131         this.date = date;
132         this.type = Type.fromValue(type);
133     }
134 
135     /**
136      * Returns the metadata object this version belongs to
137      * 
138      * @return the metadata object this version belongs to
139      */
140     public T getMetadataObject() {
141         return vm;
142     }
143 
144     /**
145      * Returns the type of operation this version comes from
146      * 
147      * @see #CREATED
148      * @see #UPDATED
149      * @see #DELETED
150      */
151     public char getType() {
152         return type.charValue;
153     }
154 
155     /**
156      * Returns the SVN revision number of this version
157      * 
158      * @return the SVN revision number of this version
159      */
160     public String getRevision() {
161         return revision;
162     }
163 
164     /**
165      * Returns the user that created this version
166      * 
167      * @return the user that created this version
168      */
169     public String getUser() {
170         return user;
171     }
172 
173     /**
174      * Returns the date and time this version was created
175      * 
176      * @return the date and time this version was created
177      */
178     public Date getDate() {
179         return date;
180     }
181 
182     /**
183      * Retrieves this version of the metadata
184      * 
185      * @return the metadata document as it was in this version
186      * @throws MCRUsageException
187      *             if this is a deleted version, which can not be retrieved
188      */
189     abstract public MCRContent retrieve() throws IOException;
190 
191     /**
192      * Replaces the current version of the metadata object with this version,
193      * which means that a new version is created that is identical to this old
194      * version. The stored metadata document is updated to this old version of
195      * the metadata.
196      */
197     abstract public void restore() throws IOException, JDOMException;
198 }