View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.admin.resources.representation.share;
17  
18  import java.net.URI;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.ws.rs.core.UriBuilder;
23  import javax.ws.rs.core.UriInfo;
24  
25  import de.smartics.properties.admin.domain.model.ApiServices;
26  import de.smartics.properties.admin.domain.model.AppRelations;
27  import de.smartics.properties.admin.resources.controller.ApiResource;
28  import de.smartics.properties.admin.resources.controller.PropertyDescriptorResource;
29  import de.smartics.properties.admin.resources.controller.PropertyKeysResource;
30  import de.smartics.properties.admin.resources.controller.PropertyResource;
31  import de.smartics.resteasy.hypermedia.relations.Relations;
32  import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor;
33  
34  /**
35   * A simple helper to create breadcrumbs.
36   */
37  public final class BreadcrumbHelper
38  {
39    // ********************************* Fields *********************************
40  
41    // --- constants ------------------------------------------------------------
42  
43    // --- members --------------------------------------------------------------
44  
45    /**
46     * Creates link descriptors for the breadcrumbs.
47     */
48    private final LinkDescriptorFactory linkDescriptorFactory;
49  
50    /**
51     * The method to select the service method of the resource.
52     */
53    private final String method;
54  
55    // ****************************** Initializer *******************************
56  
57    // ****************************** Constructors ******************************
58  
59    /**
60     * Default constructor.
61     *
62     * @param linkDescriptorFactory the value for linkDescriptorFactory.
63     * @param method the method to select the service method of the resource.
64     */
65    public BreadcrumbHelper(final LinkDescriptorFactory linkDescriptorFactory,
66        final String method)
67    {
68      this.linkDescriptorFactory = linkDescriptorFactory;
69      this.method = method;
70    }
71  
72    // ****************************** Inner Classes *****************************
73  
74    // ********************************* Methods ********************************
75  
76    // --- init -----------------------------------------------------------------
77  
78    // --- get&set --------------------------------------------------------------
79  
80    // --- business -------------------------------------------------------------
81  
82    /**
83     * Creates the link descriptor to the configurations services that is part of
84     * the API.
85     *
86     * @param descriptor the descriptor of the service.
87     * @return the link descriptor to the configurations services.
88     */
89    public LinkDescriptor service(final ApiServices.ServiceDescriptor descriptor)
90    {
91      final LinkDescriptor desc =
92          linkDescriptorFactory.createDescriptor(descriptor, method);
93      return desc;
94    }
95  
96    /**
97     * Returns the breadcrumbs for the view that shows the home page.
98     *
99     * @return the link of breadcrumb elements.
100    */
101   public List<LinkDescriptor> home()
102   {
103     final UriInfo uriInfo = linkDescriptorFactory.getUriInfo();
104     final String full = uriInfo.getBaseUri().toString();
105     final int index = full.lastIndexOf("/api");
106     final UriBuilder builder = UriBuilder.fromPath(full.substring(0, index));
107     final URI uri = builder.build();
108     final LinkDescriptor desc =
109         linkDescriptorFactory.createDescriptor("home", uri);
110     desc.addRels(Relations.HOME);
111 
112     final List<LinkDescriptor> links = new ArrayList<LinkDescriptor>();
113     links.add(desc);
114     return links;
115   }
116 
117   /**
118    * Returns the breadcrumbs for the view that shows the list of services
119    * provided by the API.
120    *
121    * @return the link of breadcrumb elements.
122    */
123   public List<LinkDescriptor> api()
124   {
125     final UriInfo uriInfo = linkDescriptorFactory.getUriInfo();
126     final UriBuilder builder = uriInfo.getBaseUriBuilder();
127     final URI uri = builder.build();
128     final LinkDescriptor desc =
129         linkDescriptorFactory.createDescriptor("api", uri);
130     desc.addRels(AppRelations.API_HOME);
131 
132     final List<LinkDescriptor> links = home();
133     links.add(desc);
134     return links;
135   }
136 
137   /**
138    * Returns the breadcrumbs for the view that shows the list of property
139    * descriptors.
140    *
141    * @return the link of breadcrumb elements.
142    */
143   public List<LinkDescriptor> descriptors()
144   {
145     final LinkDescriptor desc =
146         linkDescriptorFactory.createDescriptor(ApiResource.DESCRIPTORS, method);
147     desc.addRels(AppRelations.DESCRIPTORS);
148 
149     final List<LinkDescriptor> links = api();
150     adjust(links);
151     links.add(desc);
152 
153     return links;
154   }
155 
156   /**
157    * Returns the breadcrumbs for the view that shows a selected property
158    * descriptor.
159    *
160    * @param propertyKey the selected property key.
161    * @return the link of breadcrumb elements.
162    */
163   public List<LinkDescriptor> descriptor(final String propertyKey)
164   {
165     final LinkDescriptor desc =
166         linkDescriptorFactory.createDescriptor("descriptor",
167             PropertyDescriptorResource.class, method, propertyKey);
168     desc.setShortLabel(propertyKey);
169     desc.addRels(AppRelations.DESCRIPTOR);
170 
171     final List<LinkDescriptor> links = descriptors();
172     adjust(links);
173     links.add(desc);
174 
175     return links;
176   }
177 
178   private static void adjust(final List<LinkDescriptor> links)
179   {
180     for (final LinkDescriptor link : links)
181     {
182       link.addRels(Relations.UP);
183     }
184   }
185 
186   /**
187    * Adds the self relation to the last element in the list of links.
188    *
189    * @param links the links to adjust.
190    * @return the adjusted links.
191    */
192   public static List<LinkDescriptor> adjustSelf(final List<LinkDescriptor> links)
193   {
194     final int lastIndex = links.size() - 1;
195     if (lastIndex >= 0)
196     {
197       final LinkDescriptor link = links.get(lastIndex);
198       link.addRels(Relations.SELF);
199     }
200 
201     return links;
202   }
203 
204   /**
205    * Returns the breadcrumbs for the view that shows the list of configurations
206    * by their keys.
207    *
208    * @return the link of breadcrumb elements.
209    */
210   public List<LinkDescriptor> configurations()
211   {
212     final LinkDescriptor desc =
213         linkDescriptorFactory.createDescriptor(ApiResource.CONFIGURATIONS,
214             method);
215     desc.addRels(AppRelations.CONFIGURATIONS);
216 
217     final List<LinkDescriptor> links = api();
218     adjust(links);
219     links.add(desc);
220 
221     return links;
222   }
223 
224   /**
225    * Returns the breadcrumbs for the view that shows the list of properties by
226    * their keys.
227    *
228    * @param configurationKey the key of the currently selected configuration.
229    * @return the link of breadcrumb elements.
230    */
231   public List<LinkDescriptor> configuration(final String configurationKey)
232   {
233     final LinkDescriptor desc =
234         linkDescriptorFactory.createDescriptor("configuration",
235             PropertyKeysResource.class, method, configurationKey);
236     desc.addRels(AppRelations.CONFIGURATION);
237     desc.setShortLabel(configurationKey);
238 
239     final List<LinkDescriptor> links = configurations();
240     adjust(links);
241     links.add(desc);
242 
243     return links;
244   }
245 
246   /**
247    * Returns the breadcrumbs for the view that shows a single property.
248    *
249    * @param configurationKey the key of the currently selected configuration.
250    * @param propertyKey the key of the currently selected property.
251    * @return the link of breadcrumb elements.
252    */
253   public List<LinkDescriptor> property(final String configurationKey,
254       final String propertyKey)
255   {
256     final LinkDescriptor desc =
257         linkDescriptorFactory.createDescriptor("property",
258             PropertyResource.class, method, configurationKey, propertyKey);
259     desc.addRels(AppRelations.PROPERTY);
260     desc.setShortLabel(propertyKey);
261 
262     final List<LinkDescriptor> links = configuration(configurationKey);
263     adjust(links);
264     links.add(desc);
265 
266     return links;
267   }
268 
269   /**
270    * Returns the breadcrumbs for the view that allows to en- and decrypt
271    * property values.
272    *
273    * @return the link of breadcrumb elements.
274    */
275   public List<LinkDescriptor> security()
276   {
277     final LinkDescriptor desc =
278         linkDescriptorFactory.createDescriptor(ApiResource.SECURITY, method);
279     desc.addRels(AppRelations.SECURITY);
280 
281     final List<LinkDescriptor> links = api();
282     adjust(links);
283     links.add(desc);
284 
285     return links;
286   }
287 
288   /**
289    * Returns the breadcrumbs for the view that allows to configure JNDI
290    * properties.
291    *
292    * @return the link of breadcrumb elements.
293    */
294   public List<LinkDescriptor> jndi()
295   {
296     final LinkDescriptor desc =
297         linkDescriptorFactory.createDescriptor(ApiResource.JNDI, method);
298     desc.addRels(AppRelations.JNDI);
299 
300     final List<LinkDescriptor> links = api();
301     adjust(links);
302     links.add(desc);
303 
304     return links;
305   }
306 
307   // --- object basics --------------------------------------------------------
308 
309 }