This project has retired. For details please refer to its Attic page.
JcrVersion xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.chemistry.opencmis.jcr;
21  
22  import org.apache.chemistry.opencmis.jcr.type.JcrTypeHandlerManager;
23  
24  import javax.jcr.Node;
25  import javax.jcr.RepositoryException;
26  import javax.jcr.version.Version;
27  import java.util.regex.Matcher;
28  import java.util.regex.Pattern;
29  
30  /**
31   * Instances of this class represent a specific version of a cmis:document backed by an underlying
32   * JCR <code>Node</code>.
33   */
34  public class JcrVersion extends JcrVersionBase {
35      private static final Pattern VERSION_LABEL_PATTERN = Pattern.compile("(\\d+)(\\.(\\d+))?.*");
36      private static final int GROUP_MAJOR = 1;
37      private static final int GROUP_MINOR = 3;
38  
39      private final Version version;
40  
41      public JcrVersion(Node node, Version version, JcrTypeManager typeManager, PathManager pathManager,
42              JcrTypeHandlerManager typeHandlerManager) {
43  
44          super(node, typeManager, pathManager, typeHandlerManager);
45          this.version = version;
46      }
47  
48      //------------------------------------------< protected >---
49  
50      @Override
51      protected Node getContextNode() throws RepositoryException {
52          Node frozen = version.getFrozenNode();
53          if (frozen.hasNode(Node.JCR_CONTENT)) {
54              return frozen.getNode(Node.JCR_CONTENT);
55          }
56          else {
57              return getNode().getNode(Node.JCR_CONTENT);  // root version
58          }
59      }
60      
61      @Override
62      protected String getObjectId() throws RepositoryException {
63          return getVersionSeriesId() + '/' + version.getName();
64      }
65  
66      @Override
67      protected boolean isLatestVersion() throws RepositoryException {
68          Version baseVersion = getBaseVersion(getNode());
69          return baseVersion.isSame(version);
70      }
71  
72      @Override
73      protected boolean isMajorVersion() {
74          return true;
75      }
76  
77      @Override
78      protected boolean isLatestMajorVersion() throws RepositoryException {
79          return isLatestVersion();
80      }
81  
82      @Override
83      protected String getVersionLabel() throws RepositoryException {
84          String name = version.getName();
85          String major = parseVersion(name, GROUP_MINOR);
86  
87          return major == null
88                  ? name
89                  : (Integer.parseInt(major) + 1) + ".0";
90      }
91  
92      @Override
93      protected String getCheckInComment() throws RepositoryException {
94          // todo set checkinComment
95          return "";
96      }
97  
98      //------------------------------------------< private >---
99  
100     private static String parseVersion(String name, int group) {
101         Matcher matcher = VERSION_LABEL_PATTERN.matcher(name);
102         return matcher.matches()
103                 ? matcher.group(group)
104                 : null;
105     }
106 
107 
108 }