1 /*
2  * Copyright 2008-2019 by Emeric Vernat
3  *
4  *     This file is part of Java Melody.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.bull.javamelody.internal.web.html;
19
20 import java.io.IOException;
21 import java.io.Writer;
22 import java.text.DateFormat;
23 import java.text.SimpleDateFormat;
24 import java.util.Collection;
25 import java.util.Date;
26 import java.util.Locale;
27 import java.util.Map;
28
29 import net.bull.javamelody.internal.common.I18N;
30 import net.bull.javamelody.internal.model.Range;
31
32 /**
33  * Formulaires html.
34  * @author Emeric Vernat
35  */

36 class HtmlForms extends HtmlAbstractReport {
37     HtmlForms(Writer writer) {
38         super(writer);
39     }
40
41     void writeCustomPeriodLinks(Map<String, Date> datesByWebappVersions, Range currentRange,
42             String graphName, String part) throws IOException {
43         writeln("<a class='showHide' data-show-hide-id='customPeriod' ");
44         writeln("title='" + getFormattedString("Choisir_periode", getString("personnalisee"))
45                 + "'>");
46         writeln("<img src='?resource=calendar.png' alt='#personnalisee#' /> #personnalisee#</a>");
47
48         if (!datesByWebappVersions.isEmpty()) {
49             writeln("&nbsp;<a class='showHide' data-show-hide-id='deploymentPeriod' ");
50             writeln("title='" + getFormattedString("Choisir_periode", getString("par_deploiement"))
51                     + "'>");
52             writeln("<img src='?resource=calendar.png' alt='#par_deploiement#' /> #par_deploiement#</a>");
53         }
54
55         writeCustomPeriodDiv(currentRange, graphName, part);
56         if (!datesByWebappVersions.isEmpty()) {
57             writeDeploymentPeriodDiv(datesByWebappVersions, currentRange, graphName, part);
58         }
59     }
60
61     private void writeCustomPeriodDiv(Range currentRange, String graphName, String part)
62             throws IOException {
63         writeln("<div id='customPeriod' class='displayNone'>");
64         writeln("<br/>");
65         // yyyy-MM-dd is always the pattern of the input type=date
66         final String pattern = "yyyy-MM-dd";
67         final DateFormat dateFormat = new SimpleDateFormat(pattern, Locale.US);
68         final String max = dateFormat.format(new Date());
69         writeln("<form name='customPeriodForm' method='get' action=''>");
70         writeln("<br/><b><label for='customPeriodStartDate'>#startDate#</label></b>&nbsp;&nbsp;");
71         writeln("<input type='date' id='customPeriodStartDate' name='startDate' size='10' required max='"
72                 + max + "' ");
73         if (currentRange.getStartDate() != null) {
74             writeln("value='" + dateFormat.format(currentRange.getStartDate()) + '\'');
75         }
76         writeln("/>&nbsp;&nbsp;<b><label for='customPeriodEndDate'>#endDate#</label></b>&nbsp;&nbsp;");
77         writeln("<input type='date' id='customPeriodEndDate' name='endDate' size='10' required max='"
78                 + max + "' ");
79         if (currentRange.getEndDate() != null) {
80             writeln("value='" + dateFormat.format(currentRange.getEndDate()) + '\'');
81         }
82         writeln("/>&nbsp;&nbsp;");
83         final DateFormat localeDateFormat = I18N.createDateFormat();
84         final String localeDateFormatPattern;
85         if (getString("dateFormatPattern").isEmpty()) {
86             localeDateFormatPattern = ((SimpleDateFormat) localeDateFormat).toPattern()
87                     .toLowerCase(I18N.getCurrentLocale());
88         } else {
89             localeDateFormatPattern = getString("dateFormatPattern");
90         }
91         // ce customPeriodPattern ne sera pas affiché si html5
92         writeDirectly("<span id='customPeriodPattern' class='displayNone'>("
93                 + localeDateFormatPattern + ")</span>");
94         writeln("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='submit' value='#ok#'/><br/><br/>");
95         writeln("<input type='hidden' name='period' value=''/>");
96         writeln("<input type='hidden' name='pattern' value='" + pattern + "'/>");
97         if (graphName != null) {
98             writeln("<input type='hidden' name='part' value='" + part + "'/>");
99             writeln("<input type='hidden' name='graph' value='" + urlEncode(graphName) + "'/>");
100         }
101         writeln("</form><br/>");
102         writeln("</div>");
103     }
104
105     private void writeDeploymentPeriodDiv(Map<String, Date> datesByWebappVersions,
106             Range currentRange, String graphName, String part) throws IOException {
107         writeln("<div id='deploymentPeriod' class='displayNone'>");
108         writeln("<br/>");
109         final DateFormat dateFormat = I18N.createDateFormat();
110         final String currentRangeValue = currentRange.getValue();
111         final String startDateLabel = I18N.getString("startDate")
112                 .toLowerCase(I18N.getCurrentLocale());
113         final String endDateLabel = I18N.getString("endDate");
114         writeln("<form name='deploymentPeriodForm' method='get' action=''>");
115         writeln("<br/><b>#Version#</b>&nbsp;&nbsp;");
116         writeln("<select name='period' class='selectDeploymentPeriod'>");
117         writeDirectly("<option>&nbsp;</option>");
118         // on doit retrier les versions ici, notamment s'il y en a une ajoutée à la fin
119         String previousDate = null;
120         for (final Map.Entry<String, Date> entry : datesByWebappVersions.entrySet()) {
121             final String version = entry.getKey();
122             final String date = dateFormat.format(entry.getValue());
123             final String label;
124             if (previousDate == null) {
125                 previousDate = dateFormat.format(new Date());
126                 label = version + ' ' + startDateLabel + ' ' + date;
127             } else {
128                 label = version + ' ' + startDateLabel + ' ' + date + ' ' + endDateLabel + ' '
129                         + previousDate;
130             }
131             final String rangeValue = date + Range.CUSTOM_PERIOD_SEPARATOR + previousDate;
132             writeDirectly("<option value='" + rangeValue + "'");
133             if (rangeValue.equals(currentRangeValue)) {
134                 writeDirectly(" selected='selected'");
135             }
136             writeDirectly(">");
137             writeDirectly(htmlEncodeButNotSpace(label));
138             writeDirectly("</option>");
139             previousDate = date;
140         }
141         writeln("</select><br/><br/>");
142         if (graphName != null) {
143             writeln("<input type='hidden' name='part' value='" + part + "'/>");
144             writeln("<input type='hidden' name='graph' value='" + urlEncode(graphName) + "'/>");
145         }
146         writeln("</form><br/>");
147         writeln("</div>");
148     }
149
150     void writeAddAndRemoveApplicationLinks(String currentApplication,
151             Collection<String> applications) throws IOException {
152         if (currentApplication == null) {
153             writeln("<div align='center'><h3>#add_application#</h3>");
154             writeln("#collect_server_intro#");
155         } else {
156             final String separator = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
157             writeln(separator);
158             writeln("<a href='' class='showHide noPrint' data-show-hide-id='addApplication'>");
159             writeln("<img src='?resource=action_add.png' alt='#add_application#'/> #add_application#</a>");
160             writeln(separator);
161             if (applications.size() > 1) {
162                 writeln("<a href='' class='showHide noPrint' data-show-hide-id='addAggregation'>");
163                 writeln("<img src='?resource=action_add.png' alt='#add_aggregation#'/> #add_aggregation#</a>");
164                 writeln(separator);
165             }
166             writeln("<a href='?action=remove_application&amp;application=" + currentApplication
167                     + getCsrfTokenUrlPart() + "' class='confirm noPrint' ");
168             final String messageConfirmation = getFormattedString("confirm_remove_application",
169                     currentApplication);
170             writeln("data-confirm='" + htmlEncodeButNotSpaceAndNewLine(messageConfirmation) + "'>");
171             final String removeApplicationLabel = getFormattedString("remove_application",
172                     currentApplication);
173             writeln("<img src='?resource=action_delete.png' alt=\"" + removeApplicationLabel
174                     + "\"/> " + removeApplicationLabel + "</a>");
175             writeln("<div id='addApplication' class='displayNone'>");
176         }
177         writeln("<br/> <br/>");
178         writeln("<form name='appForm' method='post' action=''>");
179         writeln("<br/><b><label for='appName'>#app_name_to_monitor#</label> :</b>&nbsp;&nbsp;");
180         writeln("<input type='text' size='15' id='appName' name='appName' required />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
181         writeln("<b><label for='appUrls'>#app_urls#</label> :</b>&nbsp;&nbsp;");
182         writeln("<input type='text' size='50' id='appUrls' name='appUrls' required />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
183         writeln("<input type='submit' value='#add#'/><br/>");
184         writeln("#urls_sample# : <i>http://myhost/myapp/</i> #or# <i>http://host1/myapp/,http://host2/myapp/</i>");
185         writeln("<br/> <br/>");
186         writeln("</form>");
187         writeln("</div>\n");
188
189         if (applications.size() > 1) {
190             writeln("<div id='addAggregation' class='displayNone'>");
191             writeln("<br/> <br/>");
192             writeln("<form name='aggregationForm' method='post' action=''>");
193             writeln("<br/><b><label for='appName'>#aggregation_name_to_monitor#</label> :</b>&nbsp;&nbsp;");
194             writeln("<input type='text' size='15' id='appName' name='appName' required />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
195             writeln("<br/><b>#aggregated_apps# :</b>");
196             writeln("<table summary=''>");
197             for (final String application : applications) {
198                 writeln("<tr><td>");
199                 writeln("<input type='checkbox' name='aggregatedApps' value='" + application
200                         + "' /> <label for='aggregatedApps'>" + application + "</label>");
201                 writeln("</td></tr>");
202             }
203             writeln("</table>");
204             writeln("<br/>");
205             writeln("<input type='submit' value='#add#'/><br/>");
206             writeln("<br/>");
207             writeln("</form>\n");
208             writeln("</div>\n");
209         }
210     }
211
212     @Override
213     void toHtml() {
214         throw new UnsupportedOperationException();
215     }
216 }
217