001/*
002 * #%L
003 * Netarchivesuite - heritrix 3 monitor
004 * %%
005 * Copyright (C) 2005 - 2018 The Royal Danish Library, 
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023
024package dk.netarkivet.heritrix3.monitor.resources;
025
026import java.io.IOException;
027import java.util.Locale;
028
029import javax.servlet.http.HttpServletRequest;
030
031import com.antiaction.common.templateengine.TemplateBuilderBase;
032import com.antiaction.common.templateengine.TemplateBuilderPlaceHolder;
033import com.antiaction.common.templateengine.TemplatePlaceHolder;
034
035import dk.netarkivet.common.CommonSettings;
036import dk.netarkivet.common.Constants;
037import dk.netarkivet.common.utils.Settings;
038import dk.netarkivet.common.webinterface.HTMLUtils;
039import dk.netarkivet.heritrix3.monitor.Heritrix3JobMonitor;
040import dk.netarkivet.heritrix3.monitor.NASEnvironment;
041
042/**
043 * Class to handle the generation of HTML using a template engine.
044 * Extends a template engine class which uses some reflection magic.
045 */
046public class MasterTemplateBuilder extends TemplateBuilderBase {
047
048        /** Get and cache the versions string from the jar manifest file. */
049    protected final String version = Constants.getVersionString(true);
050
051    /** Get and cache the environment name used for this installation. */
052    protected final String environment = Settings.get(CommonSettings.ENVIRONMENT_NAME);
053
054    /** Hook into the template to insert title content. */
055        @TemplateBuilderPlaceHolder("title")
056    public TemplatePlaceHolder titlePlace;
057
058    /** Hook into the template to insert menu content. */
059    @TemplateBuilderPlaceHolder("menu")
060    public TemplatePlaceHolder menuPlace;
061
062    /** Hook into the template to insert language selection content. */
063    @TemplateBuilderPlaceHolder("languages")
064    public TemplatePlaceHolder languagesPlace;
065
066    /** Hook into the template to insert heading content. */
067    @TemplateBuilderPlaceHolder("heading")
068    public TemplatePlaceHolder headingPlace;
069
070    /** Hook into the template to insert main content. */
071    @TemplateBuilderPlaceHolder("content")
072    public TemplatePlaceHolder contentPlace;
073
074    /** Hook into the template to insert version content. */
075    @TemplateBuilderPlaceHolder("version")
076    public TemplatePlaceHolder versionPlace;
077
078    /** Hook into the template to insert environment content. */
079    @TemplateBuilderPlaceHolder("environment")
080    public TemplatePlaceHolder environmentPlace;
081
082    /** Hook into the template to insert refresh content. */
083    @TemplateBuilderPlaceHolder("refresh")
084    public TemplatePlaceHolder refreshPlace;
085
086    /**
087     * Construct menu HTML based on the HTTP request, requested locale and optional h3 job monitor.
088     * 
089     * @param menuSb <code>StringBuilder</code> used to construct the menu HTML
090     * @param req HTTP request object
091     * @param locale <code>Locale</code> of the requested response language to use
092     * @param h3Job H3 job monitor to use if the menu need to show a job sub sub menu item
093     * @return constructed menu HTML
094     * @throws IOException if an I/O exception occurs during construction
095     */
096    public StringBuilder buildMenu(StringBuilder menuSb, HttpServletRequest req, Locale locale, Heritrix3JobMonitor h3Job) throws IOException {
097        String subMenu = null;
098        if (h3Job != null) {
099                StringBuilder subMenuSb = new StringBuilder();
100                subMenuSb.append("<tr><td>&nbsp; &nbsp; &nbsp; <a href=\"");
101                subMenuSb.append(NASEnvironment.servicePath);
102                subMenuSb.append("job/");
103            subMenuSb.append(h3Job.jobId);
104            subMenuSb.append("/");
105            subMenuSb.append("\"><b> Job ");
106            subMenuSb.append(h3Job.jobId);
107            subMenuSb.append("</b></a></td></tr>");
108            subMenu = subMenuSb.toString();
109        }
110        HTMLUtils.generateNavigationTree(menuSb, req, req.getRequestURL().toString(), subMenu, locale);
111        return menuSb;
112    }
113
114    /**
115     * Insert content into the different placeholders defined as template engine attributes above.
116     * 
117     * @param title title text
118     * @param menu menu HTML
119     * @param languages language selection HTML
120     * @param heading heading HTML
121     * @param content main content text
122     * @param refresh refresh meta header text
123     * @return a reference to this object so more methods can be called on it immediately
124     */
125    public MasterTemplateBuilder insertContent(String title, String menu, String languages, String heading, String content, String refresh) {
126        if (titlePlace != null) {
127            titlePlace.setText(title);
128        }
129        if (menuPlace != null) {
130            menuPlace.setText(menu);
131        }
132        if (languagesPlace != null) {
133            languagesPlace.setText(languages);
134        }
135        if (headingPlace != null) {
136            headingPlace.setText(heading);
137        }
138        if (contentPlace != null) {
139            contentPlace.setText(content);
140        }
141        if (versionPlace != null) {
142            versionPlace.setText(version);
143        }
144        if (environmentPlace != null) {
145            environmentPlace.setText(environment);
146        }
147        if (refreshPlace != null) {
148                refreshPlace.setText(refresh);
149        }
150        return this;
151    }
152
153}