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.PROPERTY;
19  
20  import java.io.OutputStream;
21  import java.net.URI;
22  import java.util.List;
23  
24  import org.apache.commons.lang.ObjectUtils;
25  
26  import de.smartics.html5.jatl.HtmlResourceContext;
27  import de.smartics.properties.admin.domain.model.PropertiesConfiguration;
28  import de.smartics.properties.admin.resources.controller.PropertyResource;
29  import de.smartics.properties.admin.resources.representation.html.share.AbstractHtmlRepresentationRenderer;
30  import de.smartics.properties.admin.resources.representation.html.share.ApplicationHtmlRenderingHelper;
31  import de.smartics.properties.api.config.domain.DescribedProperty;
32  import de.smartics.properties.api.config.domain.ValidatedProperty;
33  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
34  import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor;
35  
36  /**
37   * Displays the list of property keys belonging to a selected configuration in
38   * an HTML page.
39   */
40  public final class PropertyKeysHtmlRepresentationRenderer extends
41      AbstractHtmlRepresentationRenderer<PropertiesConfiguration>
42  {
43    // ********************************* Fields *********************************
44  
45    // --- constants ------------------------------------------------------------
46  
47    /**
48     * The title to the page.
49     */
50    private static final String TITLE = "Property Keys";
51  
52    // --- members --------------------------------------------------------------
53  
54    /**
55     * The domain object to render.
56     */
57    private final PropertiesConfiguration 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 PropertyKeysHtmlRepresentationRenderer(
71        final HtmlResourceContext htmlContext,
72        final PropertiesConfiguration domainObject,
73        final OutputStream entityStream)
74    {
75      super(htmlContext, entityStream);
76      this.domainObject = domainObject;
77    }
78  
79    // ****************************** Inner Classes *****************************
80  
81    // ********************************* Methods ********************************
82  
83    // --- init -----------------------------------------------------------------
84  
85    // --- get&set --------------------------------------------------------------
86  
87    @Override
88    protected String getTitle()
89    {
90      return TITLE;
91    }
92  
93    // --- business -------------------------------------------------------------
94  
95    @Override
96    protected void bodyContents()
97    {
98      renderPageTitle(">>>");
99  
100     html.p()
101         .text(
102             "The following list shows the property keys available in the configuration "
103                 + domainObject.getKey()
104                 + " of the application with the attached data source.").end();
105     final ApplicationHtmlRenderingHelper helper =
106         new ApplicationHtmlRenderingHelper(html, pathHelper,
107             domainObject.getApplication());
108     helper.addApplication();
109 
110     renderRegistry();
111   }
112 
113   private void renderRegistry()
114   {
115     html.table().classAttr("table table-striped");
116 
117     html.tr();
118     html.th().text("#").end();
119     // html.th().text("Status").end();
120     html.th().text("Name").end();
121     html.th().text("Value").end();
122     html.th().text("Source").end();
123     html.end();
124 
125     int counter = 0;
126     for (final DescribedProperty property : domainObject.getProperties())
127     {
128       counter++;
129       renderRow(counter, property);
130     }
131 
132     html.end();
133   }
134 
135   private void renderRow(final int counter, final DescribedProperty property)
136   {
137     final String key = property.getName();
138     final URI url = createUrl(key);
139     html.tr();
140     html.td().text(String.valueOf(counter)).end();
141 
142     if (property instanceof ValidatedProperty)
143     {
144       // html.td().icon("icon-ok-circle").end();
145       final String value =
146           ObjectUtils.toString(((ValidatedProperty) property)
147               .getValidatedValue());
148       html.td();
149       html.a().id(key).rel(PROPERTY).href(url.toString()).text(key).end();
150       html.end();
151       html.td().text(value).end();
152     }
153     else
154     {
155       final String value = property.getValue();
156       final String label;
157       final String css;
158       // final String iconCss;
159       if (null == value)
160       {
161         css = "muted";
162         // iconCss = "icon-ban-circle";
163         label = " (unset)";
164       }
165       else
166       {
167         css = "text-error";
168         // iconCss = "icon-remove-circle";
169         label =
170             " = " + value + " (unresolvable)" + createConfigSource(property);
171       }
172 
173       // html.td().icon(iconCss).end();
174       html.td().a().id(key).rel(PROPERTY).href(url.toString()).text(key).end()
175           .end();
176       html.td().span().classAttr(css).text(label).end().end();
177     }
178     html.td().span().classAttr("configKey").text(createConfigSource(property))
179         .end().end();
180     html.end();
181   }
182 
183   private String createConfigSource(final DescribedProperty property)
184   {
185     final ConfigurationKey<?> configurationKey = property.getConfigurationKey();
186     final String label =
187         configurationKey == null ? "default" : configurationKey.toString();
188     return label;
189   }
190 
191   private URI createUrl(final String key)
192   {
193     final URI url =
194         htmlContext.getContext().getUriInfo().getBaseUriBuilder()
195             .path(PropertyResource.class, "getAsHtml")
196             .build(new Object[] { domainObject.getKey(), key }, false);
197     return url;
198   }
199 
200   @Override
201   protected List<LinkDescriptor> breadcrumbLinks()
202   {
203     return breadcrumb.configuration(domainObject.getKey());
204   }
205 
206   // --- object basics --------------------------------------------------------
207 
208 }