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.augmenter;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.commons.lang3.StringUtils;
26  
27  import com.google.common.collect.ImmutableMap;
28  
29  import io.wcm.caravan.hal.docs.impl.model.LinkRelation;
30  import io.wcm.caravan.hal.docs.impl.model.Service;
31  
32  /**
33   * Curie and link relation title metadata extracted from service info metadata.
34   */
35  class DocsMetadata {
36  
37    private final Map<String, String> curieLinks;
38    private final Map<String, String> linkRelationTitles;
39  
40    /**
41     * @param serviceModel Service model
42     * @param docsPath Documenation URI base path
43     */
44    DocsMetadata(Service serviceModel, String docsPath) {
45      curieLinks = buildCurieLinkMap(serviceModel, docsPath);
46      linkRelationTitles = buildLinkRelationTitlesMap(serviceModel);
47    }
48  
49    private static Map<String, String> buildCurieLinkMap(Service serviceModel, String docsPath) {
50      Map<String, String> map = new HashMap<>();
51  
52      serviceModel.getLinkRelations().stream()
53          .map(rel -> CurieUtil.getCurieName(rel.getRel()))
54          .filter(StringUtils::isNotEmpty)
55          .forEach(curie -> map.put(curie, CurieUtil.toDocTemplate(docsPath, curie)));
56  
57      return ImmutableMap.copyOf(map);
58    }
59  
60    private static Map<String, String> buildLinkRelationTitlesMap(Service serviceModel) {
61      Map<String, String> map = new HashMap<>();
62  
63      for (LinkRelation rel : serviceModel.getLinkRelations()) {
64        String title = rel.getShortDescription();
65        if (title != null) {
66          map.put(rel.getRel(), title);
67        }
68      }
69  
70      return ImmutableMap.copyOf(map);
71    }
72  
73    public String getCurieLink(String curie) {
74      return curieLinks.get(curie);
75    }
76  
77    public String getLinkRelationTitle(String rel) {
78      return linkRelationTitles.get(rel);
79    }
80  
81  }