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.xml;
17  
18  import java.io.IOException;
19  import java.io.OutputStream;
20  import java.util.List;
21  import java.util.Locale;
22  
23  import de.smartics.properties.admin.domain.model.ConfigurationProperty;
24  import de.smartics.properties.admin.resources.representation.share.BreadcrumbHelper;
25  import de.smartics.properties.admin.resources.representation.xml.share.AbstractXmlRepresentationRenderer;
26  import de.smartics.properties.api.core.domain.PropertyComment;
27  import de.smartics.properties.api.core.domain.PropertyDescriptor;
28  import de.smartics.properties.api.core.domain.PropertyProjectdoc;
29  import de.smartics.properties.api.core.domain.SourceInfo;
30  import de.smartics.properties.report.data.PropertyReportItem;
31  import de.smartics.properties.reports.XmlPropertyItemReport;
32  import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor;
33  import de.smartics.resteasy.hypermedia.renderer.ResourceContext;
34  
35  /**
36   * Displays a single property in an SDoc XML page.
37   */
38  public final class PropertyXmlRepresentationRenderer extends
39      AbstractXmlRepresentationRenderer<ConfigurationProperty>
40  {
41    // ********************************* Fields *********************************
42  
43    // --- constants ------------------------------------------------------------
44  
45    // --- members --------------------------------------------------------------
46  
47    /**
48     * The domain object to render.
49     */
50    private final ConfigurationProperty domainObject;
51  
52    // ****************************** Initializer *******************************
53  
54    // ****************************** Constructors ******************************
55  
56    /**
57     * Default constructor.
58     *
59     * @param context the context information to render the representation.
60     * @param domainObject the domain object to render.
61     * @param entityStream the stream to write to.
62     */
63    public PropertyXmlRepresentationRenderer(final ResourceContext context,
64        final ConfigurationProperty domainObject, final OutputStream entityStream)
65    {
66      super(context, entityStream);
67      this.domainObject = domainObject;
68    }
69  
70    // ****************************** Inner Classes *****************************
71  
72    // ********************************* Methods ********************************
73  
74    // --- init -----------------------------------------------------------------
75  
76    // --- get&set --------------------------------------------------------------
77  
78    // --- business -------------------------------------------------------------
79  
80    @Override
81    protected void httpBody()
82    {
83      final XmlPropertyItemReport report =
84          new XmlPropertyItemReport(characterEncoding);
85  
86      final PropertyReportItem reportItem = createReportItem();
87      try
88      {
89        report.write(reportItem, entityStream);
90      }
91      catch (final IOException e)
92      {
93        // TODO: How should we signal this? Separate exception?
94        throw new IllegalStateException("Cannot render to the stream.", e);
95      }
96    }
97  
98    private PropertyReportItem createReportItem()
99    {
100     final Object value = domainObject.getPropertyValue();
101 
102     final PropertyDescriptor descriptor = domainObject.getDescriptor();
103 
104     final PropertyReportItem.Builder builder = new PropertyReportItem.Builder();
105     final Locale locale = context.getL7nProvider().getLocale();
106     final PropertyProjectdoc projectdoc =
107         descriptor.getDocumentMetaData(locale);
108     final SourceInfo sourceInfo = projectdoc.getSourceInfo();
109     final PropertyComment propertyComment = projectdoc.getComment();
110     builder.with(descriptor).with(sourceInfo).with(propertyComment);
111 
112     final PropertyReportItem reportItem = builder.build();
113     reportItem.setValue(value);
114     return reportItem;
115   }
116 
117   @Override
118   protected List<LinkDescriptor> hyperLinks()
119   {
120     return BreadcrumbHelper.adjustSelf(breadcrumb.property(
121         domainObject.getConfigurationKey(), domainObject.getPropertyKey()));
122   }
123 
124   @Override
125   protected void close()
126   {
127   }
128 
129   // --- object basics --------------------------------------------------------
130 
131 }