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.resources.representation.share.BreadcrumbHelper;
24  import de.smartics.properties.admin.resources.representation.xml.share.AbstractXmlRepresentationRenderer;
25  import de.smartics.properties.api.core.domain.PropertyComment;
26  import de.smartics.properties.api.core.domain.PropertyDescriptor;
27  import de.smartics.properties.api.core.domain.PropertyProjectdoc;
28  import de.smartics.properties.api.core.domain.SourceInfo;
29  import de.smartics.properties.report.data.PropertyReportItem;
30  import de.smartics.properties.reports.XmlPropertyItemReport;
31  import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor;
32  import de.smartics.resteasy.hypermedia.renderer.ResourceContext;
33  
34  /**
35   * Displays a selected property descriptor as SDoc XML.
36   */
37  public final class PropertyDescriptorXmlRepresentationRenderer extends
38      AbstractXmlRepresentationRenderer<PropertyDescriptor>
39  {
40    // ********************************* Fields *********************************
41  
42    // --- constants ------------------------------------------------------------
43  
44    // --- members --------------------------------------------------------------
45  
46    /**
47     * The domain object to render.
48     */
49    private final PropertyDescriptor domainObject;
50  
51    // ****************************** Initializer *******************************
52  
53    // ****************************** Constructors ******************************
54  
55    /**
56     * Default constructor.
57     *
58     * @param context the context information to render the representation.
59     * @param domainObject the domain object to render.
60     * @param entityStream the stream to write to.
61     */
62    public PropertyDescriptorXmlRepresentationRenderer(
63        final ResourceContext context, final PropertyDescriptor domainObject,
64        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 PropertyReportItem.Builder builder = new PropertyReportItem.Builder();
101 
102     final Locale locale = context.getL7nProvider().getLocale();
103     final PropertyProjectdoc projectdoc =
104         domainObject.getDocumentMetaData(locale);
105     final SourceInfo sourceInfo = projectdoc.getSourceInfo();
106     final PropertyComment propertyComment = projectdoc.getComment();
107     builder.with(domainObject).with(sourceInfo).with(propertyComment);
108 
109     final PropertyReportItem reportItem = builder.build();
110     return reportItem;
111   }
112 
113   @Override
114   protected List<LinkDescriptor> hyperLinks()
115   {
116     return BreadcrumbHelper.adjustSelf(breadcrumb.descriptor(domainObject
117         .getKey().toString()));
118   }
119 
120   @Override
121   protected void close()
122   {
123   }
124 
125   // --- object basics --------------------------------------------------------
126 
127 }