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.bindings.spi.atompub;
20  
21  import java.math.BigInteger;
22  import java.util.ArrayList;
23  
24  import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
25  import org.apache.chemistry.opencmis.client.bindings.spi.atompub.objects.AtomElement;
26  import org.apache.chemistry.opencmis.client.bindings.spi.atompub.objects.AtomEntry;
27  import org.apache.chemistry.opencmis.client.bindings.spi.atompub.objects.AtomFeed;
28  import org.apache.chemistry.opencmis.client.bindings.spi.atompub.objects.AtomLink;
29  import org.apache.chemistry.opencmis.client.bindings.spi.http.Response;
30  import org.apache.chemistry.opencmis.commons.data.ExtensionsData;
31  import org.apache.chemistry.opencmis.commons.data.ObjectData;
32  import org.apache.chemistry.opencmis.commons.data.ObjectList;
33  import org.apache.chemistry.opencmis.commons.enums.RelationshipDirection;
34  import org.apache.chemistry.opencmis.commons.impl.Constants;
35  import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
36  import org.apache.chemistry.opencmis.commons.impl.dataobjects.ObjectListImpl;
37  import org.apache.chemistry.opencmis.commons.spi.RelationshipService;
38  
39  /**
40   * Relationship Service AtomPub client.
41   */
42  public class RelationshipServiceImpl extends AbstractAtomPubService implements RelationshipService {
43  
44      /**
45       * Constructor.
46       */
47      public RelationshipServiceImpl(BindingSession session) {
48          setSession(session);
49      }
50  
51      @Override
52      public ObjectList getObjectRelationships(String repositoryId, String objectId, Boolean includeSubRelationshipTypes,
53              RelationshipDirection relationshipDirection, String typeId, String filter, Boolean includeAllowableActions,
54              BigInteger maxItems, BigInteger skipCount, ExtensionsData extension) {
55          ObjectListImpl result = new ObjectListImpl();
56  
57          // find the link
58          String link = loadLink(repositoryId, objectId, Constants.REL_RELATIONSHIPS, Constants.MEDIATYPE_FEED);
59  
60          if (link == null) {
61              throwLinkException(repositoryId, objectId, Constants.REL_RELATIONSHIPS, Constants.MEDIATYPE_FEED);
62          }
63  
64          UrlBuilder url = new UrlBuilder(link);
65          url.addParameter(Constants.PARAM_SUB_RELATIONSHIP_TYPES, includeSubRelationshipTypes);
66          url.addParameter(Constants.PARAM_RELATIONSHIP_DIRECTION, relationshipDirection);
67          url.addParameter(Constants.PARAM_TYPE_ID, typeId);
68          url.addParameter(Constants.PARAM_FILTER, filter);
69          url.addParameter(Constants.PARAM_ALLOWABLE_ACTIONS, includeAllowableActions);
70          url.addParameter(Constants.PARAM_MAX_ITEMS, maxItems);
71          url.addParameter(Constants.PARAM_SKIP_COUNT, skipCount);
72  
73          // read and parse
74          Response resp = read(url);
75          AtomFeed feed = parse(resp.getStream(), AtomFeed.class);
76  
77          // handle top level
78          for (AtomElement element : feed.getElements()) {
79              if (element.getObject() instanceof AtomLink) {
80                  if (isNextLink(element)) {
81                      result.setHasMoreItems(Boolean.TRUE);
82                  }
83              } else if (isInt(NAME_NUM_ITEMS, element)) {
84                  result.setNumItems((BigInteger) element.getObject());
85              }
86          }
87  
88          // get the children
89          if (!feed.getEntries().isEmpty()) {
90              result.setObjects(new ArrayList<ObjectData>(feed.getEntries().size()));
91  
92              for (AtomEntry entry : feed.getEntries()) {
93                  ObjectData relationship = null;
94  
95                  lockLinks();
96                  try {
97                      // clean up cache
98                      removeLinks(repositoryId, entry.getId());
99  
100                     // walk through the entry
101                     for (AtomElement element : entry.getElements()) {
102                         if (element.getObject() instanceof AtomLink) {
103                             addLink(repositoryId, entry.getId(), (AtomLink) element.getObject());
104                         } else if (element.getObject() instanceof ObjectData) {
105                             relationship = (ObjectData) element.getObject();
106                         }
107                     }
108                 } finally {
109                     unlockLinks();
110                 }
111 
112                 if (relationship != null) {
113                     result.getObjects().add(relationship);
114                 }
115             }
116 
117         }
118 
119         return result;
120     }
121 }