View Javadoc

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  package org.apache.chemistry.opencmis.client.runtime.objecttype;
20  
21  import java.io.Serializable;
22  import java.util.List;
23  
24  import org.apache.chemistry.opencmis.client.api.ItemIterable;
25  import org.apache.chemistry.opencmis.client.api.ObjectType;
26  import org.apache.chemistry.opencmis.client.api.Session;
27  import org.apache.chemistry.opencmis.client.api.Tree;
28  
29  /**
30   * Helper for object types, containing session-related info.
31   * <p>
32   * This is needed because Java doesn't support multiple inheritance.
33   */
34  public class ObjectTypeHelper implements Serializable {
35  
36      private static final long serialVersionUID = 1L;
37  
38      private final Session session;
39      private final ObjectType objectType;
40      private ObjectType baseType;
41      private ObjectType parentType;
42  
43      public ObjectTypeHelper(Session session, ObjectType objectType) {
44          assert session != null;
45          assert objectType != null;
46  
47          this.session = session;
48          this.objectType = objectType;
49      }
50  
51      public Session getSession() {
52          return session;
53      }
54  
55      public boolean isBaseType() {
56          return objectType.getParentTypeId() == null || objectType.getParentTypeId().length() == 0;
57      }
58  
59      public ObjectType getBaseType() {
60          if (isBaseType()) {
61              return null;
62          }
63          if (baseType != null) {
64              return baseType;
65          }
66          if (objectType.getBaseTypeId() == null) {
67              return null;
68          }
69          baseType = session.getTypeDefinition(objectType.getBaseTypeId().value());
70          return baseType;
71      }
72  
73      public ObjectType getParentType() {
74          if (parentType != null) {
75              return parentType;
76          }
77          if (objectType.getParentTypeId() == null || objectType.getParentTypeId().length() == 0) {
78              return null;
79          }
80          parentType = session.getTypeDefinition(objectType.getParentTypeId());
81          return parentType;
82      }
83  
84      public ItemIterable<ObjectType> getChildren() {
85          return session.getTypeChildren(objectType.getId(), true);
86      }
87  
88      public List<Tree<ObjectType>> getDescendants(int depth) {
89          return session.getTypeDescendants(objectType.getId(), depth, true);
90      }
91  }