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.ifs2;
20  
21  import java.io.IOException;
22  import java.util.Date;
23  
24  import org.jdom2.JDOMException;
25  import org.mycore.common.MCRUsageException;
26  import org.mycore.common.content.MCRByteContent;
27  import org.mycore.common.content.MCRContent;
28  import org.mycore.common.content.streams.MCRByteArrayOutputStream;
29  import org.mycore.datamodel.common.MCRAbstractMetadataVersion;
30  import org.tmatesoft.svn.core.SVNException;
31  import org.tmatesoft.svn.core.io.SVNRepository;
32  
33  import jakarta.xml.bind.annotation.XmlAccessType;
34  import jakarta.xml.bind.annotation.XmlAccessorType;
35  import jakarta.xml.bind.annotation.XmlRootElement;
36  
37  /**
38   * Provides information about a stored version of metadata and allows to
39   * retrieve that version from SVN
40   * 
41   * @author Frank Lützenkirchen
42   */
43  @XmlRootElement(name = "revision")
44  @XmlAccessorType(XmlAccessType.FIELD)
45  public class MCRMetadataVersion extends MCRAbstractMetadataVersion<MCRVersionedMetadata> {
46  
47      public MCRMetadataVersion(MCRVersionedMetadata vm, String revision, String user, Date date, char type) {
48          super(vm, revision, user, date, type);
49      }
50  
51      /**
52       * Retrieves this version of the metadata
53       * 
54       * @return the metadata document as it was in this version
55       * @throws MCRUsageException
56       *             if this is a deleted version, which can not be retrieved
57       */
58      @Override
59      public MCRContent retrieve() throws IOException {
60          if (getType() == DELETED) {
61              String msg = "You can not retrieve a deleted version, retrieve a previous version instead";
62              throw new MCRUsageException(msg);
63          }
64          try {
65              SVNRepository repository = vm.getStore().getRepository();
66              MCRByteArrayOutputStream baos = new MCRByteArrayOutputStream();
67              repository.getFile(vm.getStore().getSlotPath(vm.getID()), Long.parseLong(getRevision()), null, baos);
68              baos.close();
69              return new MCRByteContent(baos.getBuffer(), 0, baos.size(), getDate().getTime());
70          } catch (SVNException e) {
71              throw new IOException(e);
72          }
73      }
74  
75      /**
76       * Replaces the current version of the metadata object with this version,
77       * which means that a new version is created that is identical to this old
78       * version. The stored metadata document is updated to this old version of
79       * the metadata.
80       */
81      @Override
82      public void restore() throws IOException, JDOMException {
83          vm.update(retrieve());
84      }
85  }