1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
71
72 public void addLinkRelation(LinkRelation linkRelation) {
73 this.linkRelations.add(linkRelation);
74 }
75
76
77
78
79 public void resolve() {
80 linkRelations.stream().forEach(this::resolve);
81 }
82
83
84
85
86
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 }