View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2015 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.caravan.hal.docs.impl.model;
21  
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.SortedSet;
25  import java.util.TreeSet;
26  
27  import org.apache.commons.lang3.StringUtils;
28  
29  import com.google.common.collect.ImmutableList;
30  
31  /**
32   * Describes a HAL RESTful service.
33   */
34  public class Service {
35  
36    private String serviceId;
37    private String name;
38    private String descriptionMarkup;
39    private SortedSet<LinkRelation> linkRelations = new TreeSet<>();
40  
41    public String getServiceId() {
42      return this.serviceId;
43    }
44  
45    public void setServiceId(String serviceId) {
46      this.serviceId = serviceId;
47    }
48  
49    public String getName() {
50      return this.name;
51    }
52  
53    public void setName(String name) {
54      this.name = name;
55    }
56  
57    public String getDescriptionMarkup() {
58      return this.descriptionMarkup;
59    }
60  
61    public void setDescriptionMarkup(String descriptionMarkup) {
62      this.descriptionMarkup = descriptionMarkup;
63    }
64  
65    public List<LinkRelation> getLinkRelations() {
66      return ImmutableList.copyOf(this.linkRelations);
67    }
68  
69    /**
70     * @param linkRelation Link relation to add
71     */
72    public void addLinkRelation(LinkRelation linkRelation) {
73      this.linkRelations.add(linkRelation);
74    }
75  
76    /**
77     * Resolves all nested link relations.
78     */
79    public void resolve() {
80      linkRelations.stream().forEach(this::resolve);
81    }
82  
83    /**
84     * Iterates over nested link relations. If they are valid filename is set and decription inherited (if empty).
85     * Invalid ones are removed.
86     * @param rel Link relations
87     */
88    private void resolve(LinkRelation rel) {
89      Iterator<LinkRelationRef> refs = rel.getLinkRelationRefs().iterator();
90      while (refs.hasNext()) {
91        LinkRelationRef ref = refs.next();
92        LinkRelation referencedRel = linkRelations.stream()
93            .filter(item -> StringUtils.equals(item.getRel(), ref.getRel()))
94            .findFirst().orElse(null);
95        if (referencedRel == null) {
96          refs.remove();
97        }
98        else {
99          if (StringUtils.isBlank(ref.getShortDescription())) {
100           ref.setShortDescription(referencedRel.getShortDescription());
101         }
102       }
103     }
104   }
105 
106 }