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.html;
17  
18  import static de.smartics.properties.admin.domain.model.AppRelations.DESCRIPTOR;
19  
20  import java.io.OutputStream;
21  import java.net.URI;
22  import java.util.List;
23  import java.util.Locale;
24  
25  import org.apache.commons.lang3.StringUtils;
26  
27  import de.smartics.html5.jatl.HtmlResourceContext;
28  import de.smartics.properties.admin.domain.model.PropertyDescriptors;
29  import de.smartics.properties.admin.resources.controller.PropertyDescriptorResource;
30  import de.smartics.properties.admin.resources.representation.html.share.AbstractHtmlRepresentationRenderer;
31  import de.smartics.properties.api.core.domain.PropertyDescriptor;
32  import de.smartics.properties.api.core.domain.PropertyKey;
33  import de.smartics.properties.api.core.domain.PropertySetProjectdoc;
34  import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor;
35  
36  /**
37   * Displays the list of property descriptors belonging to a selected
38   * configuration in an HTML page.
39   */
40  public final class PropertyDescriptorsHtmlRepresentationRenderer extends
41      AbstractHtmlRepresentationRenderer<PropertyDescriptors>
42  {
43    // ********************************* Fields *********************************
44  
45    // --- constants ------------------------------------------------------------
46  
47    /**
48     * The title to the page.
49     */
50    private static final String TITLE = "Property Descriptors";
51  
52    // --- members --------------------------------------------------------------
53  
54    /**
55     * The domain object to render.
56     */
57    private final PropertyDescriptors domainObject;
58  
59    // ****************************** Initializer *******************************
60  
61    // ****************************** Constructors ******************************
62  
63    /**
64     * Default constructor.
65     *
66     * @param htmlContext the context information to render the representation.
67     * @param domainObject the domain object to render.
68     * @param entityStream the stream to write to.
69     */
70    public PropertyDescriptorsHtmlRepresentationRenderer(
71        final HtmlResourceContext htmlContext,
72        final PropertyDescriptors domainObject, final OutputStream entityStream)
73    {
74      super(htmlContext, entityStream);
75      this.domainObject = domainObject;
76    }
77  
78    // ****************************** Inner Classes *****************************
79  
80    // ********************************* Methods ********************************
81  
82    // --- init -----------------------------------------------------------------
83  
84    // --- get&set --------------------------------------------------------------
85  
86    @Override
87    protected String getTitle()
88    {
89      return TITLE;
90    }
91  
92    // --- business -------------------------------------------------------------
93  
94    @Override
95    protected void bodyContents()
96    {
97      renderPageTitle(">>>");
98  
99      html.p()
100         .text(
101             "The following list shows the property descriptors registered in the application.")
102         .end();
103 
104     renderRegistry();
105   }
106 
107   private void renderRegistry()
108   {
109     for (final String propertySet : domainObject.getPropertyKeys())
110     {
111       final List<PropertyDescriptor> descriptors =
112           domainObject.get(propertySet);
113 
114       html.h2().text(propertySet).end();
115       html.table().classAttr("table table-striped descriptors");
116 
117       if (!descriptors.isEmpty())
118       {
119         final Locale locale =
120             this.htmlContext.getContext().getResponse().getLocale();
121         final PropertyDescriptor descriptor = descriptors.get(0);
122         final PropertySetProjectdoc projectdoc =
123             descriptor.getDocumentMetaDataProjectSet(locale);
124         final String comment = projectdoc.getComment();
125         if (StringUtils.isNotBlank(comment))
126         {
127           html.raw(comment);
128         }
129       }
130 
131       html.tr();
132       html.th().classAttr("no").text("#").end();
133       html.th().classAttr("descriptorName").text("Name").end();
134       html.th().classAttr("download").text("Download").end();
135       html.end();
136       int counter = 0;
137       for (final PropertyDescriptor descriptor : descriptors)
138       {
139         counter++;
140         renderRow(counter, descriptor);
141       }
142       html.end();
143     }
144   }
145 
146   private void renderRow(final int counter, final PropertyDescriptor descriptor)
147   {
148     final PropertyKey key = descriptor.getKey();
149     final String keyString = key.toString();
150     html.tr();
151     html.td().text(String.valueOf(counter)).end();
152 
153     final URI url = createUrl(keyString);
154     html.td();
155     html.a().id(keyString).rel(DESCRIPTOR).href(url.toString())
156         .text(key.getName()).end();
157     html.end();
158     html.td();
159     final URI downloadUrl = createDownloadUrl(keyString);
160     html.a().id(keyString + "asXml").rel(DESCRIPTOR)
161         .href(downloadUrl.toString()).icon("icon-download-alt").text("XML")
162         .end();
163     html.end();
164     html.end();
165   }
166 
167   private URI createUrl(final String key)
168   {
169     final URI url =
170         htmlContext.getContext().getUriInfo().getBaseUriBuilder()
171             .path(PropertyDescriptorResource.class, "getAsHtml")
172             .build(new Object[] { key }, false);
173     return url;
174   }
175 
176   private URI createDownloadUrl(final String key)
177   {
178     final URI url =
179         htmlContext.getContext().getUriInfo().getBaseUriBuilder()
180             .path(PropertyDescriptorResource.class, "getAsDotXml")
181             .build(new Object[] { key }, false);
182     return url;
183   }
184 
185   @Override
186   protected List<LinkDescriptor> breadcrumbLinks()
187   {
188     return breadcrumb.descriptors();
189   }
190 
191   // --- object basics --------------------------------------------------------
192 
193 }