001/*
002 * (C) Copyright 2011 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     tdelprat
016 *
017 */
018
019package org.nuxeo.wizard.nav;
020
021import java.io.IOException;
022
023import javax.servlet.ServletException;
024import javax.servlet.http.HttpServletRequest;
025import javax.servlet.http.HttpServletResponse;
026
027import org.nuxeo.wizard.context.Context;
028
029/**
030 * Represents a Page
031 *
032 * @author Tiry (tdelprat@nuxeo.com)
033 * @since 5.4.2
034 */
035public class Page {
036
037    public static String NUXEO_COM_URL = "http://www.nuxeo.com/embedded/wizard/";
038
039    protected final String action;
040
041    protected final String jsp;
042
043    protected int progress = -1;
044
045    protected Page prev;
046
047    protected Page next;
048
049    protected boolean active = true;
050
051    protected boolean hidden = false;
052
053    protected boolean navigated = false;
054
055    public Page(String pageConfigString) {
056
057        String[] parts = pageConfigString.split("\\|");
058
059        action = parts[0];
060        String jspPage = parts[1];
061        if (!jspPage.startsWith("/")) {
062            jspPage = "/" + jspPage;
063        }
064        jsp = jspPage;
065        if ("0".equals(parts[2])) {
066            active = false;
067        }
068        if ("1".equals(parts[3])) {
069            hidden = true;
070        }
071    }
072
073    public String getAction() {
074        return action;
075    }
076
077    public String getJsp() {
078        return jsp;
079    }
080
081    public Page prev() {
082        if (prev.isActive()) {
083            return prev;
084        } else {
085            return prev.prev();
086        }
087    }
088
089    public Page next() {
090        if (next.isActive()) {
091            return next;
092        } else {
093            return next.next();
094        }
095    }
096
097    public void dispatchToJSP(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
098        dispatchToJSP(req, resp, false);
099    }
100
101    public void dispatchToJSP(HttpServletRequest req, HttpServletResponse resp, boolean postBack)
102            throws ServletException, IOException {
103
104        // be sure to bind context
105        Context.instance(req);
106
107        // make currentPage object available
108        req.setAttribute("currentPage", this);
109
110        // render JSP
111        if (postBack && "POST".equals(req.getMethod())) {
112            // handle POST-Back
113            // fixes URLs and remove refresh warnings
114            String target = "/" + req.getContextPath() + "/" + action;
115            if (target.startsWith("//")) {
116                target = target.substring(1);
117            }
118            resp.sendRedirect(target);
119        } else {
120            req.getRequestDispatcher(jsp).forward(req, resp);
121        }
122
123    }
124
125    @Override
126    public String toString() {
127        return action + ":" + jsp;
128    }
129
130    public int getProgress() {
131        return progress;
132    }
133
134    public String getAssociatedIFrameUrl() {
135        return NUXEO_COM_URL + action;
136    }
137
138    public String getLabelKey() {
139        return "label.short." + jsp.replace(".jsp", "").substring(1);
140    }
141
142    public boolean isVisibleInNavigationMenu() {
143        return active && (!hidden);
144    }
145
146    public boolean isActive() {
147        return active;
148    }
149
150    public boolean hasBeenNavigatedBefore() {
151        return navigated;
152    }
153}