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