001package org.javasimon.console.action;
002
003import org.javasimon.Simon;
004import org.javasimon.console.Action;
005import org.javasimon.console.ActionContext;
006import org.javasimon.console.ActionException;
007import org.javasimon.console.SimonType;
008import org.javasimon.console.SimonTypeFactory;
009import org.javasimon.console.TimeFormatType;
010import org.javasimon.console.text.StringifierFactory;
011
012import java.io.IOException;
013
014import javax.servlet.ServletException;
015
016/**
017 * Export single Simon data as static HTML for printing purposes.
018 * Path: http://.../data/detail.html?name=o.j...SimonName&timeFormat=MILLISECOND
019 *
020 * @author gquintana
021 */
022public class DetailHtmlAction extends Action {
023
024        /** URI for detail action. */
025        public static final String PATH = "/data/detail.html";
026        /** Value formatter. */
027        protected StringifierFactory stringifierFactory;
028        /** Simon name. */
029        private String name;
030
031        /** Constructor. */
032        public DetailHtmlAction(ActionContext context) {
033                super(context);
034                this.stringifierFactory = new StringifierFactory();
035        }
036
037        @Override
038        public void readParameters() {
039                name = getContext().getParameterAsString("name", null);
040                stringifierFactory.init(getContext().getParameterAsEnum("timeFormat", TimeFormatType.class, TimeFormatType.MILLISECOND),
041                        StringifierFactory.READABLE_DATE_PATTERN,
042                        StringifierFactory.READABLE_NUMBER_PATTERN);
043        }
044
045        @Override
046        public void execute() throws ServletException, IOException, ActionException {
047                // Check arguments
048                if (name == null) {
049                        throw new ActionException("Null name");
050                }
051                Simon simon = getContext().getManager().getSimon(name);
052                if (simon == null) {
053                        throw new ActionException("Simon \"" + name + "\" not found");
054                }
055                getContext().setContentType("text/html");
056                SimonType simonType = SimonTypeFactory.getValueFromInstance(simon);
057                DetailHtmlBuilder htmlBuilder = new DetailHtmlBuilder(getContext().getWriter(), stringifierFactory);
058                // Page header
059                htmlBuilder.header("Detail View", DetailPlugin.getResources(getContext(), DetailPlugin.class))
060                        // Common Simon section
061                        .beginSection("simonPanel", "Simon")
062                        .beginRow()
063                        .simonProperty(simon, "Name", "name", 5)
064                        .endRow()
065                        .beginRow()
066                        .labelCell("Type")
067                        .beginValueCell().simonTypeImg(simonType, "../../").object(simonType).endValueCell()
068                        .simonProperty(simon, "State", "state")
069                        .simonProperty(simon, "Enabled", "enabled")
070                        .endRow()
071                        .beginRow()
072                        .simonProperty(simon, "Note", "note", 5)
073                        .endRow()
074                        .beginRow()
075                        .simonProperty(simon, "First Use", "firstUsage")
076                        .simonProperty(simon, "Last Use", "lastUsage")
077                        .endRow()
078                        .endSection();
079                // Specific Stopwatch/Counter section
080                switch (simonType) {
081                        case STOPWATCH:
082                                htmlBuilder.beginSection("stopwatchPanel", "Stopwatch")
083                                        .beginRow()
084                                        .simonProperty(simon, "Counter", "counter")
085                                        .simonProperty(simon, "Total", "total", 3)
086                                        .endRow()
087                                        .beginRow()
088                                        .simonProperty(simon, "Min", "min", 3)
089                                        .simonProperty(simon, "Min Timestamp", "minTimeStamp")
090                                        .endRow()
091                                        .beginRow()
092                                        .simonProperty(simon, "Mean", "mean")
093                                        .simonProperty(simon, "Standard Deviation", "standardDeviation", 3)
094                                        .endRow()
095                                        .beginRow()
096                                        .simonProperty(simon, "Max", "max", 3)
097                                        .simonProperty(simon, "Max Timestamp", "maxTimeStamp")
098                                        .endRow()
099                                        .beginRow()
100                                        .simonProperty(simon, "Last", "last", 3)
101                                        .simonProperty(simon, "Last Timestamp", "lastUsage")
102                                        .endRow()
103                                        .beginRow()
104                                        .simonProperty(simon, "Active", "active")
105                                        .simonProperty(simon, "Max Active", "maxActive")
106                                        .simonProperty(simon, "Max Active Timestamp", "maxActiveTimestamp")
107                                        .endRow()
108                                        .endSection();
109                                break;
110                        case COUNTER:
111                                htmlBuilder.beginSection("counterPanel", "Counter")
112                                        .beginRow()
113                                        .simonProperty(simon, "Counter", "counter")
114                                        .endRow()
115                                        .beginRow()
116                                        .simonProperty(simon, "Min", "min")
117                                        .simonProperty(simon, "Min Timestamp", "minTimeStamp")
118                                        .endRow()
119                                        .beginRow()
120                                        .simonProperty(simon, "Max", "max")
121                                        .simonProperty(simon, "Max Timestamp", "maxTimeStamp")
122                                        .endRow()
123                                        .beginRow()
124                                        .simonProperty(simon, "Increment Sum", "incrementSum")
125                                        .simonProperty(simon, "Decrement Sum", "decrementSum")
126                                        .endRow()
127                                        .endSection();
128                                break;
129                }
130                // Plugins
131                for (DetailPlugin plugin : getContext().getPluginManager().getPluginsByType(DetailPlugin.class)) {
132                        if (plugin.supports(simon)) {
133                                htmlBuilder.beginSection(plugin.getId() + "Panel", plugin.getLabel());
134                                plugin.executeHtml(getContext(), htmlBuilder, stringifierFactory, simon);
135                                htmlBuilder.endSection();
136                        }
137                }
138                // Page footer
139                htmlBuilder.footer();
140        }
141}