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.CONFIGURATION;
19  
20  import java.io.OutputStream;
21  import java.net.URI;
22  import java.util.List;
23  
24  import de.smartics.html5.jatl.HtmlResourceContext;
25  import de.smartics.properties.admin.domain.model.ConfigurationKeys;
26  import de.smartics.properties.admin.domain.model.Paths;
27  import de.smartics.properties.admin.domain.model.Roles;
28  import de.smartics.properties.admin.resources.controller.PropertyKeysResource;
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.key.ConfigurationKey;
32  import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor;
33  
34  /**
35   * Displays the list of configuration keys in an HTML page.
36   */
37  public final class ConfigurationKeysHtmlRepresentationRenderer extends
38      AbstractHtmlRepresentationRenderer<ConfigurationKeys>
39  {
40    // ********************************* Fields *********************************
41  
42    // --- constants ------------------------------------------------------------
43  
44    /**
45     * The title to the page.
46     */
47    private static final String TITLE = "Configuration Keys";
48  
49    // --- members --------------------------------------------------------------
50  
51    /**
52     * The domain object to render.
53     */
54    private final ConfigurationKeys domainObject;
55  
56    // ****************************** Initializer *******************************
57  
58    // ****************************** Constructors ******************************
59  
60    /**
61     * Default constructor.
62     *
63     * @param htmlContext the context information to render the representation.
64     * @param domainObject the domain object to render.
65     * @param entityStream the stream to write to.
66     */
67    public ConfigurationKeysHtmlRepresentationRenderer(
68        final HtmlResourceContext htmlContext,
69        final ConfigurationKeys domainObject, final OutputStream entityStream)
70    {
71      super(htmlContext, entityStream);
72      this.domainObject = domainObject;
73    }
74  
75    // ****************************** Inner Classes *****************************
76  
77    // ********************************* Methods ********************************
78  
79    // --- init -----------------------------------------------------------------
80  
81    // --- get&set --------------------------------------------------------------
82  
83    // --- business -------------------------------------------------------------
84  
85    @Override
86    protected void bodyContents()
87    {
88      renderPageTitle(">>>");
89      html.p()
90          .text(
91              "The following list shows the keys to configurations available"
92                  + " to the application with the attached data source.").end();
93      final ApplicationHtmlRenderingHelper helper =
94          new ApplicationHtmlRenderingHelper(html, pathHelper,
95              domainObject.getApplication());
96      helper.addApplication();
97  
98      if(htmlContext.getContext().isUserInRole(Roles.CREATE_CONFIG))
99      {
100       renderNewConfigurationForm();
101       html.hr().end();
102     }
103 
104     html.div().classAttr("alert alert-info");
105     html.button().type("button").classAttr("close").attr("data-dismiss", "alert").raw("&times;").end();
106     html.h4().text("Secured Values").end();
107     html.text("Please note that secured values are displayed in plain text.");
108     html.end();
109 
110     renderKeys();
111   }
112 
113   private void renderNewConfigurationForm()
114   {
115     html.form().classAttr("form-inline").method("POST");
116 
117     html.fieldset().legend().text("Create new configuration").end();
118     createInputField();
119     createSubmitButton();
120     createHelp();
121     html.end();
122 
123     html.end();
124   }
125 
126   private void createInputField()
127   {
128     html.input().type("text").id(Paths.PARAM_CONFIGURATION_KEY)
129         .name(Paths.PARAM_CONFIGURATION_KEY).classAttr("input-xlarge");
130     // TODO: derive format from configuration ...
131     html.attr("placeholder", "//:/::");
132     html.end();
133   }
134 
135   private void createSubmitButton()
136   {
137     html.button().type("submit").id("submit")
138         .classAttr("btn btn-mini btn-default");
139     html.text("create").end();
140   }
141 
142   private void createHelp()
143   {
144     // TODO: derive format from configuration ...
145     html.div()
146         .small()
147         .text(
148             "Pattern: /{user}/{tenant}/{env}:{node}/{app-group}:{app-id}:{version}")
149         .end().end();
150     html.p()
151         .small()
152         .text(
153             "Please consult the documentation of the selected key format to be"
154                 + " informed, which values are allowed.").end().end();
155   }
156 
157   private void renderKeys()
158   {
159     html.ol();
160     for (final ConfigurationKey<?> key : domainObject.getKeys())
161     {
162       final URI url =
163           htmlContext.getContext().getUriInfo().getBaseUriBuilder()
164               .path(PropertyKeysResource.class, "getAsHtml")
165               .build(new Object[] { key.toString() }, false);
166 
167       final String keyString = key.toString();
168       html.li().a().id(keyString).rel(CONFIGURATION).href(url.toString())
169           .text(keyString).end().end();
170     }
171 
172     html.end();
173   }
174 
175   @Override
176   protected String getTitle()
177   {
178     return TITLE;
179   }
180 
181   @Override
182   protected List<LinkDescriptor> breadcrumbLinks()
183   {
184     return breadcrumb.configurations();
185   }
186 
187   // --- object basics --------------------------------------------------------
188 
189 }