001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     tdelprat
018 *
019 */
020
021package org.nuxeo.wizard.nav;
022
023import java.io.IOException;
024
025import javax.servlet.ServletException;
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028
029import org.nuxeo.wizard.context.Context;
030
031/**
032 * Represents a Page
033 *
034 * @author Tiry (tdelprat@nuxeo.com)
035 * @since 5.4.2
036 */
037public class Page {
038
039    public static final String NUXEO_COM_URL = "http://www.nuxeo.com/embedded/wizard/";
040
041    protected final String action;
042
043    protected final String jsp;
044
045    protected int progress = -1;
046
047    protected Page prev;
048
049    protected Page next;
050
051    protected boolean active = true;
052
053    protected boolean hidden = false;
054
055    protected boolean navigated = false;
056
057    public Page(String pageConfigString) {
058
059        String[] parts = pageConfigString.split("\\|");
060
061        action = parts[0];
062        String jspPage = parts[1];
063        if (!jspPage.startsWith("/")) {
064            jspPage = "/" + jspPage;
065        }
066        jsp = jspPage;
067        if ("0".equals(parts[2])) {
068            active = false;
069        }
070        if ("1".equals(parts[3])) {
071            hidden = true;
072        }
073    }
074
075    public String getAction() {
076        return action;
077    }
078
079    public String getJsp() {
080        return jsp;
081    }
082
083    public Page prev() {
084        if (prev.isActive()) {
085            return prev;
086        } else {
087            return prev.prev();
088        }
089    }
090
091    public Page next() {
092        if (next.isActive()) {
093            return next;
094        } else {
095            return next.next();
096        }
097    }
098
099    public void dispatchToJSP(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
100        dispatchToJSP(req, resp, false);
101    }
102
103    public void dispatchToJSP(HttpServletRequest req, HttpServletResponse resp, boolean postBack)
104            throws ServletException, IOException {
105
106        // be sure to bind context
107        Context.instance(req);
108
109        // make currentPage object available
110        req.setAttribute("currentPage", this);
111
112        // render JSP
113        if (postBack && "POST".equals(req.getMethod())) {
114            // handle POST-Back
115            // fixes URLs and remove refresh warnings
116            String target = "/" + req.getContextPath() + "/" + action;
117            if (target.startsWith("//")) {
118                target = target.substring(1);
119            }
120            resp.sendRedirect(target);
121        } else {
122            req.getRequestDispatcher(jsp).forward(req, resp);
123        }
124
125    }
126
127    @Override
128    public String toString() {
129        return action + ":" + jsp;
130    }
131
132    public int getProgress() {
133        return progress;
134    }
135
136    public String getAssociatedIFrameUrl() {
137        return NUXEO_COM_URL + action;
138    }
139
140    public String getLabelKey() {
141        return "label.short." + jsp.replace(".jsp", "").substring(1);
142    }
143
144    public boolean isVisibleInNavigationMenu() {
145        return active && (!hidden);
146    }
147
148    public boolean isActive() {
149        return active;
150    }
151
152    public boolean hasBeenNavigatedBefore() {
153        return navigated;
154    }
155}