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;
20  
21  import org.apache.chemistry.opencmis.client.api.CmisObject;
22  import org.apache.chemistry.opencmis.client.api.ObjectId;
23  import org.apache.chemistry.opencmis.client.api.ObjectType;
24  import org.apache.chemistry.opencmis.client.api.OperationContext;
25  import org.apache.chemistry.opencmis.client.api.Relationship;
26  import org.apache.chemistry.opencmis.client.api.RelationshipType;
27  import org.apache.chemistry.opencmis.commons.PropertyIds;
28  import org.apache.chemistry.opencmis.commons.data.ObjectData;
29  
30  public class RelationshipImpl extends AbstractCmisObject implements Relationship {
31  
32      private static final long serialVersionUID = 1L;
33  
34      /**
35       * Constructor.
36       */
37      public RelationshipImpl(SessionImpl session, ObjectType objectType, ObjectData objectData, OperationContext context) {
38          initialize(session, objectType, objectData, context);
39      }
40  
41      @Override
42      public RelationshipType getRelationshipType() {
43          ObjectType objectType = super.getType();
44          if (objectType instanceof RelationshipType) {
45              return (RelationshipType) objectType;
46          } else {
47              throw new ClassCastException("Object type is not a relationship type.");
48          }
49      }
50  
51      @Override
52      public CmisObject getSource() {
53          return getSource(getSession().getDefaultContext());
54      }
55  
56      @Override
57      public CmisObject getSource(OperationContext context) {
58          readLock();
59          try {
60              ObjectId sourceId = getSourceId();
61              if (sourceId == null) {
62                  return null;
63              }
64  
65              return getSession().getObject(sourceId, context);
66          } finally {
67              readUnlock();
68          }
69      }
70  
71      @Override
72      public ObjectId getSourceId() {
73          String sourceId = getPropertyValue(PropertyIds.SOURCE_ID);
74          if (sourceId == null || sourceId.length() == 0) {
75              return null;
76          }
77  
78          return getSession().createObjectId(sourceId);
79      }
80  
81      @Override
82      public CmisObject getTarget() {
83          return getTarget(getSession().getDefaultContext());
84      }
85  
86      @Override
87      public CmisObject getTarget(OperationContext context) {
88          readLock();
89          try {
90              ObjectId targetId = getTargetId();
91              if (targetId == null) {
92                  return null;
93              }
94  
95              return getSession().getObject(targetId, context);
96          } finally {
97              readUnlock();
98          }
99      }
100 
101     @Override
102     public ObjectId getTargetId() {
103         String targetId = getPropertyValue(PropertyIds.TARGET_ID);
104         if (targetId == null || targetId.length() == 0) {
105             return null;
106         }
107 
108         return getSession().createObjectId(targetId);
109     }
110 }