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  
24  import org.apache.commons.lang.StringUtils;
25  
26  import de.smartics.html5.jatl.HtmlResourceContext;
27  import de.smartics.properties.admin.resources.controller.PropertyDescriptorResource;
28  import de.smartics.properties.admin.resources.representation.html.share.AbstractHtmlRepresentationRenderer;
29  import de.smartics.properties.api.core.domain.PropertyCategories;
30  import de.smartics.properties.api.core.domain.PropertyConstraint;
31  import de.smartics.properties.api.core.domain.PropertyDescriptor;
32  import de.smartics.properties.spi.core.constraints.PropertyRangeConstraint;
33  import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor;
34  import de.smartics.util.lang.StringFunction;
35  
36  /**
37   * Displays a selected property descriptor.
38   */
39  public final class PropertyDescriptorHtmlRepresentationRenderer extends
40      AbstractHtmlRepresentationRenderer<PropertyDescriptor>
41  {
42    // ********************************* Fields *********************************
43  
44    // --- constants ------------------------------------------------------------
45  
46    /**
47     * The title to the page.
48     */
49    private static final String TITLE = "Property Descriptor";
50  
51    // --- members --------------------------------------------------------------
52  
53    /**
54     * The domain object to render.
55     */
56    private final PropertyDescriptor domainObject;
57  
58    // ****************************** Initializer *******************************
59  
60    // ****************************** Constructors ******************************
61  
62    /**
63     * Default constructor.
64     *
65     * @param htmlContext the context information to render the representation.
66     * @param domainObject the domain object to render.
67     * @param entityStream the stream to write to.
68     */
69    public PropertyDescriptorHtmlRepresentationRenderer(
70        final HtmlResourceContext htmlContext,
71        final PropertyDescriptor domainObject, final OutputStream entityStream)
72    {
73      super(htmlContext, entityStream);
74      this.domainObject = domainObject;
75    }
76  
77    // ****************************** Inner Classes *****************************
78  
79    // ********************************* Methods ********************************
80  
81    // --- init -----------------------------------------------------------------
82  
83    // --- get&set --------------------------------------------------------------
84  
85    @Override
86    protected String getTitle()
87    {
88      return TITLE;
89    }
90  
91    // --- business -------------------------------------------------------------
92  
93    @Override
94    protected void bodyContents()
95    {
96      renderPageTitle(domainObject.getKey().toString());
97  
98      final String htmlFragment = domainObject.getComment().getText();
99      final String normalized = htmlUtils.cleanHtmlAndJavadoc(htmlFragment);
100     html.p().raw(normalized).end();
101 
102     descriptorInformation();
103   }
104 
105   private void descriptorInformation()
106   {
107     html.table().classAttr("table table-striped table-bordered");
108     html.tr().th().text("name").end().th().text("value").end().end();
109     renderRow("mandatory", domainObject.isMandatory());
110     renderRow("access type", domainObject.getAccessType());
111     renderRow("use type", domainObject.getUseType().toString());
112     renderRow("categories", toString(domainObject.getCategories()));
113     renderRow("configuration time", domainObject.getConfigurationTime());
114     renderRow("update interval", domainObject.getUpdateIntervalInMs());
115     renderRow("default expression", domainObject.getDefaultExpression());
116     renderRow("value type", domainObject.getType());
117     renderRow("value range", domainObject.getValueRange());
118     renderRow("constraints", toString(domainObject.getConstraints()));
119     renderRow("declaring type", domainObject.getDeclaringType().getName());
120     renderRow("home page", domainObject.getContext().getHomePageUrl());
121     final String reportUrl =
122         domainObject.getContext().getPropertiesContext()
123             .createReportUrl(domainObject);
124     renderRow("Report", reportUrl);
125     html.end();
126 
127     html.p();
128     final String key = domainObject.getKey().toString();
129     final URI url = createDownloadUrl(key);
130     html.a().id("asXml").rel(DESCRIPTOR).href(url.toString())
131         .icon("icon-download-alt").text(" Download as XML").end();
132     html.end();
133   }
134 
135   private URI createDownloadUrl(final String key)
136   {
137     final URI url =
138         htmlContext.getContext().getUriInfo().getBaseUriBuilder()
139             .path(PropertyDescriptorResource.class, "getAsDotXml")
140             .build(new Object[] { key }, false);
141     return url;
142   }
143 
144   private void renderRow(final String label, final Object value)
145   {
146     if (isNotBlank(value))
147     {
148       html.tr();
149       html.td().text(label).end().td().text(String.valueOf(value)).end();
150       html.end();
151     }
152   }
153 
154   private boolean isNotBlank(final Object value)
155   {
156     if (value == null)
157     {
158       return false;
159     }
160 
161     return StringUtils.isNotBlank(String.valueOf(value));
162   }
163 
164   private String toString(final PropertyCategories categories)
165   {
166     final List<Class<?>> types = categories.getCategories();
167     final StringBuilder buffer = new StringBuilder(64);
168     for (final Class<?> type : types)
169     {
170       buffer.append(type.getName()).append(' ');
171     }
172     return StringFunction.chop(buffer).toString();
173   }
174 
175   private Object toString(
176       final List<? extends PropertyConstraint<?>> constraints)
177   {
178     final StringBuilder buffer = new StringBuilder(64);
179 
180     for (final Object constraint : constraints)
181     {
182       if (constraint instanceof PropertyRangeConstraint)
183       {
184         continue;
185       }
186       buffer.append(' ').append(constraint);
187     }
188 
189     return buffer.toString();
190   }
191 
192   @Override
193   protected List<LinkDescriptor> breadcrumbLinks()
194   {
195     return breadcrumb.descriptor(domainObject.getKey().toString());
196   }
197 
198   // --- object basics --------------------------------------------------------
199 
200 }