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