001/*
002 * (C) Copyright 2011-2015 Nuxeo SA (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-2.1.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, jcarsique
016 *
017 */
018
019package org.nuxeo.wizard.nav;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.launcher.config.ConfigurationGenerator;
027
028/**
029 * Very basic Navigation handler
030 *
031 * @author Tiry (tdelprat@nuxeo.com)
032 * @since 5.4.2
033 */
034public class SimpleNavigationHandler {
035
036    public static final String SKIP_PAGES_KEY = "nuxeo.wizard.skippedpages";
037
038    // I am too lazy to load a file
039    // navCode / jsp Page / active flag / hidden flag
040    protected static final String[] nav = { "Home|welcome.jsp|1|0", "NetworkBlocked|networkBlocked.jsp|0|0",
041            "General|generalSettings.jsp|1|0", "Proxy|proxySettings.jsp|1|0", "DB|dbSettings.jsp|1|0",
042            "User|userSettings.jsp|1|0", "Smtp|smtpSettings.jsp|1|0", "Connect|connectForm.jsp|1|0",
043            "ConnectCallback|connectCallback.jsp|0|1", "ConnectFinish|connectFinish.jsp|0|0",
044            "PackagesSelection|packagesSelection.jsp|1|0", "PackagesDownload|packagesDownload.jsp|1|0",
045            "PackagesSelectionDone|packagesSelectionDone.jsp|0|0", "Recap|recapScreen.jsp|1|0",
046            "Restart|reStarting.jsp|1|1", "Reset|Welcome.jsp|1|1", "PackageOptionsResource||1|1" };
047
048    protected List<Page> pages = new ArrayList<>();
049
050    protected static SimpleNavigationHandler instance;
051
052    protected static Log log = LogFactory.getLog(SimpleNavigationHandler.class);
053
054    public static SimpleNavigationHandler instance() {
055        if (instance == null) {
056            instance = new SimpleNavigationHandler();
057        }
058        return instance;
059    }
060
061    public static void reset() {
062        instance = null;
063    }
064
065    protected SimpleNavigationHandler() {
066
067        Page previousPage = null;
068        for (int idx = 0; idx < nav.length; idx++) {
069            String token = nav[idx];
070
071            Page page = new Page(token);
072            pages.add(page);
073
074            if (previousPage != null) {
075                previousPage.next = page;
076                page.prev = previousPage;
077            }
078
079            // XXX false !
080            page.progress = new Double((idx + 1) * (100.0 / nav.length)).intValue();
081            previousPage = page;
082
083        }
084
085        ConfigurationGenerator configurationGenerator = new ConfigurationGenerator();
086        configurationGenerator.init();
087        String skipPages = configurationGenerator.getUserConfig().getProperty(SKIP_PAGES_KEY, null);
088        if (skipPages != null) {
089            String[] pages2Skip = skipPages.split(",");
090            for (String pageKey : pages2Skip) {
091                deactivatePage(pageKey);
092            }
093        }
094    }
095
096    public Page getDefaultPage() {
097        return getCurrentPage(pages.get(0).action);
098    }
099
100    public int getProgress(String action) {
101
102        int activePageIdx = 0;
103        int totalActivePages = 0;
104
105        for (int idx = 0; idx < pages.size(); idx++) {
106
107            if (pages.get(idx).isVisibleInNavigationMenu()) {
108                totalActivePages += 1;
109            }
110            if (pages.get(idx).getAction().equals(action)) {
111                activePageIdx = totalActivePages;
112            }
113        }
114        if (totalActivePages == 0) {
115            return 0;
116        }
117        return new Double((activePageIdx) * (100.0 / totalActivePages)).intValue();
118    }
119
120    public Page getCurrentPage(String action) {
121
122        Page currentPage = null;
123
124        if (action == null || action.isEmpty()) {
125            currentPage = pages.get(0);
126        } else {
127            currentPage = findPageByAction(action);
128        }
129
130        if (currentPage == null) {
131            log.warn("No Page found for action " + action);
132            return null;
133        }
134
135        // mark as navigated
136        currentPage.navigated = true;
137
138        return currentPage;
139    }
140
141    public Page findPageByAction(String action) {
142        for (int idx = 0; idx < pages.size(); idx++) {
143            if (pages.get(idx).getAction().equals(action)) {
144                return pages.get(idx);
145            }
146        }
147        return null;
148    }
149
150    public void activatePage(String action) {
151        Page page = findPageByAction(action);
152        if (page != null) {
153            page.active = true;
154        }
155    }
156
157    public void deactivatePage(String action) {
158        Page page = findPageByAction(action);
159        if (page != null) {
160            page.active = false;
161        }
162    }
163
164    public List<Page> getPages() {
165        return pages;
166    }
167
168}