/* * Copyright (c) 2001, Xiaoping Jia. * All Rights Reserved. */ package expo2; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import org.apache.ecs.*; import org.apache.ecs.html.*; /** * Java Expo web app version 2. * The Java Expo web app selecting presentation servlet. * * @version 1.1 2001/04/20 * @since 1.0 * @author Xiaoping Jia */ public class JavaExpoPresentations extends JavaExpoBase { public boolean handleRequest(HttpServletRequest request, HttpServletResponse response, List contents) throws ServletException, IOException { contents.add(new H1("Enterprise Java Expo Presentations")); JavaExpoRegister.RegistrationForm form = null; PresentationSet presentationSet = null; HttpSession session = request.getSession(false); if (session == null) { contents.add(new H1("Your session has expired")); } else { form = (JavaExpoRegister.RegistrationForm) session.getAttribute("registration form"); presentationSet = (PresentationSet) session.getAttribute("presentation set"); } if (form != null) { if (presentationSet != null) { Enumeration names = request.getParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); String value = request.getParameter(name); if ("add".equals(value)) { presentationSet.add(name); } else if ("remove".equals(value)) { presentationSet.remove(name); } } } else { presentationSet = new PresentationSet(); } session.setAttribute("presentation set", presentationSet); List selectedSet = presentationSet.makeTableRows(true, presentations); List remainingSet = presentationSet.makeTableRows(false, presentations); Form htmlform = new Form(response.encodeURL(urlPrefix + "JavaExpoPresentations"), "post"); if (selectedSet == null) { htmlform.addElement(new H2("No presentation selected for " + form.firstName + " " + form.lastName)); } else { // Build table of selected presentations Table table = new Table(0); for (Iterator iter = selectedSet.iterator(); iter.hasNext();) { table.addElement((TR) iter.next()); } htmlform .addElement(new H2("The following presentations are selected for " + form.firstName + " " + form.lastName) .addElement(table)); } if (remainingSet != null) { // Build table of remaining presentations Table table = new Table(0); for (Iterator iter = remainingSet.iterator(); iter.hasNext();) { table.addElement((TR) iter.next()); } htmlform .addElement(new H2("Select from the following presentations:") .addElement(table)); } contents.add(htmlform); } return true; } static class Presentation implements Serializable { Presentation(String id, String title, String time) { this.id = id; this.title = title; this.time = time; } String id; String title; String time; } // the list of all presentations protected List presentations = new ArrayList(); public void init() { super.init(); try { ServletContext ctx = getServletContext(); BufferedReader mfile = new BufferedReader(new FileReader(ctx.getRealPath("WEB-INF/presentations.dat"))); String line; while ((line = mfile.readLine()) != null) { line = line.trim(); if (line.length() > 0 && line.charAt(0) != '#') { int i1 = line.indexOf(':'); if (i1 > 0) { int i2 = line.indexOf(':', i1 + 1); if (i2 > i1) { String id = line.substring(0, i1).trim(); String title = line.substring(i1 + 1, i2).trim(); String time = line.substring(i2 + 1).trim(); presentations.add(new Presentation(id, title, time)); } } } // skip empty or comment line } } catch (FileNotFoundException e) { System.out.println("Unable to open file presentations.dat"); } catch (IOException e) { System.out.println("Error in reading file presentations.dat"); } } static class PresentationSet implements Serializable { Set selected = new HashSet(); // set of presentation id's representing selected presentations void add(String id) { if (id != null) { selected.add(id); } } void remove(String id) { if (id != null) { selected.remove(id); } } // flag == true: selected list // flas == false: unselected list List makeTableRows(boolean flag, List presentations) { List list = new ArrayList(); Iterator iter = presentations.iterator(); while (iter.hasNext()) { Presentation prz = (Presentation) iter.next(); String id = prz.id; boolean in = selected.contains(id); if (flag == in) { list.add(new TR() .addElement(new TD() .addElement(new Input(Input.submit, prz.id, flag ? "remove" : "add"))) .addElement(new TD(prz.title)) .addElement(new TD(new I().addElement(prz.time)))); } } if (list.size() > 0) { return list; } else { return null; } } } }