Ignore:
Timestamp:
2004-12-02T16:20:11+13:00 (19 years ago)
Author:
schweer
Message:

wizard for creating subscriptions: saving state works

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/AlertingService.java

    r8717 r8724  
    1414import java.util.*;
    1515
     16import javax.mail.Session;
    1617import javax.servlet.http.*;
    1718
     
    3839        actions.add("login");
    3940        actions.add("register");
     41        actions.add("logout");
    4042    }
    4143   
     
    99101            return "login.vm";
    100102        }
    101         // TODO handle wizard-style stuff
    102         // if "finish"
    103         ProfileStore.getInstance().createSubscription(arguments);
    104         return listSubscriptions(arguments, context);
    105         // else show next page
     103        if (arguments.containsKey("next_page") && arguments.get("next_page").equals("finish")) {
     104            ProfileStore.getInstance().createSubscription(arguments);
     105            return listSubscriptions(arguments, context);
     106        } else {
     107            return showSubscriptionWizardPage(arguments, context, true);
     108        }
    106109    }
    107110
     
    117120    }
    118121   
    119     public String editSubscription(Map arguments, Context context) {
     122    public String editSubscription(Map arguments, Context context) throws Exception {
    120123        HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
    121124        if (!UserManager.getInstance().isLoggedIn(session)) {
     
    123126            return "login.vm";
    124127        }
    125         // TODO handle wizard-style stuff
    126         // if "finish"
    127         ProfileStore.getInstance().changeSubscription(arguments, session);
    128         return listSubscriptions(arguments, context);
    129         // else show next page
     128        if (arguments.containsKey("next_page") && arguments.get("next_page").equals("finish")) {
     129            ProfileStore.getInstance().changeSubscription(arguments, session);
     130            return listSubscriptions(arguments, context);
     131        } else {
     132            return showSubscriptionWizardPage(arguments, context, false);
     133        }
    130134    }
    131135   
     
    156160        UserManager.getInstance().createUser(arguments, session);
    157161        return login(arguments, context);
     162    }
     163   
     164    public String logout(Map arguments, Context context) {
     165        HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
     166        Enumeration atts = session.getAttributeNames();
     167        while (atts.hasMoreElements()) {
     168            session.removeAttribute((String) atts.nextElement());
     169        }
     170        session.invalidate();
     171        return "general.vm";
    158172    }
    159173   
     
    211225        return null;
    212226    }
     227
     228    /**
     229     * @param arguments
     230     * @param context
     231     * @param create
     232     * @return
     233     * @throws Exception
     234     */
     235    private String showSubscriptionWizardPage(Map arguments, Context context, boolean create) throws Exception {
     236        if (create) {
     237            context.put("action", "createSubscription");
     238        } else {
     239            context.put("action", "editSubscription");
     240        }
     241        if (!arguments.containsKey("current_page")) {
     242            return "sub_type-details.vm";
     243        }
     244       
     245        HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
     246        String currentPage = (String) arguments.get("current_page");
     247        String direction = (String) arguments.get("next_page");
     248       
     249        // save page arguments
     250        savePageArgsToSession(currentPage, arguments, session);
     251       
     252        String nextPage = getNextPage(currentPage, direction);
     253
     254        // fill prefill
     255        context.put("prefill", getPageArgsFromSession(nextPage, session));
     256       
     257        // fill preview
     258        context.put("preview", getPagePreview(nextPage, session));
     259       
     260        // get page-specific stuff
     261        if (nextPage.equals("host")) {
     262           
     263        } else if (nextPage.equals("collection")) {
     264           
     265        }
     266       
     267        return "sub_" + nextPage + ".vm";
     268    }
     269
     270    /**
     271     * @param nextPage
     272     * @param session
     273     * @return
     274     */
     275    private Map getPagePreview(String nextPage, HttpSession session) {
     276        Map preview = new TreeMap();
     277        if (nextPage.equals("type-details")) {
     278            return preview;
     279        }
     280        preview.putAll(getPageArgsFromSession("type-details", session));
     281        if (nextPage.equals("host")) {
     282            return preview;
     283        }
     284        preview.putAll(getPageArgsFromSession("host", session));
     285        if (nextPage.equals("collection")) {
     286            return preview;
     287        }
     288        preview.putAll(getPageArgsFromSession("collection", session));
     289        return preview;
     290    }
     291
     292    /**
     293     * @param currentPage
     294     * @param direction
     295     * @return
     296     * @throws Exception
     297     */
     298    private String getNextPage(String currentPage, String direction) throws Exception {
     299        String nextPage;
     300        if (currentPage.equals("host") && direction.equals("back")) {
     301            nextPage = "type-details";
     302        } else if (currentPage.equals("type-details") || (currentPage.equals("collection") && direction.equals("back"))) {
     303            nextPage = "host";
     304        } else if (currentPage.equals("host") || (currentPage.equals("notification") && direction.equals("back"))) {
     305            nextPage = "collection";
     306        } else if (currentPage.equals("collection")) {
     307            nextPage = "notification";
     308        } else {
     309            throw new Exception("unknown combination of currentPage=" + currentPage + " and nextPage=" + direction);
     310        }
     311        return nextPage;
     312    }
     313
     314    /**
     315     * @param page
     316     * @param session
     317     * @return TODO
     318     */
     319    private Map getPageArgsFromSession(String page, HttpSession session) {
     320        Map pageArgs = (Map) session.getAttribute("page_args");
     321        if (pageArgs == null || !pageArgs.containsKey(page))
     322            return new TreeMap();
     323        return (Map) pageArgs.get(page);
     324    }
     325
     326    /**
     327     * @param page
     328     * @param arguments
     329     * @param session
     330     */
     331    private void savePageArgsToSession(String page, Map arguments, HttpSession session) {
     332        Map pageArgs = (Map) session.getAttribute("page_args");
     333        if (pageArgs == null) {
     334            pageArgs = new TreeMap();
     335        }
     336        pageArgs.put(page, arguments);
     337        session.setAttribute("page_args", pageArgs);
     338    }
    213339}
Note: See TracChangeset for help on using the changeset viewer.