This project has retired. For details please refer to its Attic page.
RelationshipService 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.server.impl.atompub;
20  
21  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.RESOURCE_RELATIONSHIPS;
22  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileBaseUrl;
23  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileUrl;
24  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileUrlBuilder;
25  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.writeObjectEntry;
26  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getBigIntegerParameter;
27  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getBooleanParameter;
28  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getEnumParameter;
29  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getStringParameter;
30  
31  import java.math.BigInteger;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  import org.apache.chemistry.opencmis.commons.data.ObjectData;
37  import org.apache.chemistry.opencmis.commons.data.ObjectList;
38  import org.apache.chemistry.opencmis.commons.enums.RelationshipDirection;
39  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
40  import org.apache.chemistry.opencmis.commons.impl.Constants;
41  import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
42  import org.apache.chemistry.opencmis.commons.server.CallContext;
43  import org.apache.chemistry.opencmis.commons.server.CmisService;
44  import org.apache.chemistry.opencmis.commons.server.ObjectInfo;
45  
46  /**
47   * Relationship Service operations.
48   */
49  public class RelationshipService {
50  
51      private RelationshipService() {
52      }
53  
54      /**
55       * Get object relationships.
56       */
57      public static void getObjectRelationships(CallContext context, CmisService service, String repositoryId,
58              HttpServletRequest request, HttpServletResponse response) throws Exception {
59          // get parameters
60          String objectId = getStringParameter(request, Constants.PARAM_ID);
61          Boolean includeSubRelationshipTypes = getBooleanParameter(request, Constants.PARAM_SUB_RELATIONSHIP_TYPES);
62          RelationshipDirection relationshipDirection = getEnumParameter(request, Constants.PARAM_RELATIONSHIP_DIRECTION,
63                  RelationshipDirection.class);
64          String typeId = getStringParameter(request, Constants.PARAM_TYPE_ID);
65          String filter = getStringParameter(request, Constants.PARAM_FILTER);
66          Boolean includeAllowableActions = getBooleanParameter(request, Constants.PARAM_ALLOWABLE_ACTIONS);
67          BigInteger maxItems = getBigIntegerParameter(request, Constants.PARAM_MAX_ITEMS);
68          BigInteger skipCount = getBigIntegerParameter(request, Constants.PARAM_SKIP_COUNT);
69  
70          // execute
71          ObjectList relationships = service.getObjectRelationships(repositoryId, objectId, includeSubRelationshipTypes,
72                  relationshipDirection, typeId, filter, includeAllowableActions, maxItems, skipCount, null);
73  
74          if (relationships == null) {
75              throw new CmisRuntimeException("Relationships are null!");
76          }
77  
78          ObjectInfo objectInfo = service.getObjectInfo(repositoryId, objectId);
79          if (objectInfo == null) {
80              throw new CmisRuntimeException("Object Info is missing!");
81          }
82  
83          // set headers
84          response.setStatus(HttpServletResponse.SC_OK);
85          response.setContentType(Constants.MEDIATYPE_FEED);
86  
87          // write XML
88          AtomFeed feed = new AtomFeed();
89          feed.startDocument(response.getOutputStream());
90          feed.startFeed(true);
91  
92          // write basic Atom feed elements
93          feed.writeFeedElements(objectInfo.getId(), objectInfo.getCreatedBy(), objectInfo.getName(),
94                  objectInfo.getLastModificationDate(), null, relationships.getNumItems());
95  
96          // write links
97          UrlBuilder baseUrl = compileBaseUrl(request, repositoryId);
98  
99          feed.writeServiceLink(baseUrl.toString(), repositoryId);
100 
101         feed.writeSelfLink(compileUrl(baseUrl, RESOURCE_RELATIONSHIPS, objectInfo.getId()), null);
102 
103         UrlBuilder pagingUrl = new UrlBuilder(compileUrlBuilder(baseUrl, RESOURCE_RELATIONSHIPS, objectInfo.getId()));
104         pagingUrl.addParameter(Constants.PARAM_SUB_RELATIONSHIP_TYPES, includeSubRelationshipTypes);
105         pagingUrl.addParameter(Constants.PARAM_RELATIONSHIP_DIRECTION, relationshipDirection);
106         pagingUrl.addParameter(Constants.PARAM_TYPE_ID, typeId);
107         pagingUrl.addParameter(Constants.PARAM_FILTER, filter);
108         pagingUrl.addParameter(Constants.PARAM_ALLOWABLE_ACTIONS, includeAllowableActions);
109         feed.writePagingLinks(pagingUrl, maxItems, skipCount, relationships.getNumItems(),
110                 relationships.hasMoreItems(), AtomPubUtils.PAGE_SIZE);
111 
112         // write entries
113         AtomEntry entry = new AtomEntry(feed.getWriter());
114         for (ObjectData object : relationships.getObjects()) {
115             if (object == null) {
116                 continue;
117             }
118             writeObjectEntry(service, entry, object, null, repositoryId, null, null, baseUrl, false);
119         }
120 
121         // we are done
122         feed.endFeed();
123         feed.endDocument();
124     }
125 }