001/* 002 * (C) Copyright 2006-2008 Nuxeo SAS (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.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 * bstefanescu 016 */ 017package org.nuxeo.ecm.webengine.ui.wizard; 018 019import java.util.HashMap; 020import java.util.Map; 021 022import org.nuxeo.ecm.webengine.WebException; 023import org.nuxeo.ecm.webengine.forms.validation.Form; 024import org.nuxeo.ecm.webengine.forms.validation.ValidationException; 025import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException; 026 027/** 028 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 029 */ 030public class WizardSession extends HashMap<String, Object> { 031 032 private static final long serialVersionUID = 1L; 033 034 protected String id; 035 036 protected Object data; 037 038 protected ValidationException error; 039 040 protected WizardPage lastPage; 041 042 protected Map<String, WizardPage> pages; 043 044 protected WizardPage[] orderedPages; 045 046 public WizardSession(String wizardId, WizardPage[] pages) { 047 if (pages == null || pages.length == 0) { 048 throw new WebException("Wizard has no pages!"); 049 } 050 this.id = wizardId; 051 this.lastPage = pages[0]; 052 this.pages = new HashMap<String, WizardPage>(); 053 for (int i = 0; i < pages.length; i++) { 054 WizardPage p = pages[i]; 055 p.setIndex(i); 056 this.pages.put(p.getId(), p); 057 } 058 this.orderedPages = pages; 059 } 060 061 public WizardPage pushPage(String pageId) { 062 WizardPage page = pages.get(pageId); 063 if (page == null) { 064 throw new WebResourceNotFoundException("No such wizard page: " + pageId); 065 } 066 if (lastPage == null) { 067 lastPage = page; 068 } else { 069 page.prev = lastPage; 070 lastPage = page; 071 } 072 return page; 073 } 074 075 public WizardPage popPage() { 076 if (lastPage == null) { 077 return null; 078 } 079 WizardPage page = lastPage; 080 lastPage = page.prev; 081 page.prev = null; 082 return page; 083 } 084 085 public int getPageCount() { 086 return pages.size(); 087 } 088 089 public WizardPage getPage() { 090 return lastPage; 091 } 092 093 public WizardPage getPage(String id) { 094 return pages.get(id); 095 } 096 097 public String getPageAt(int index) { 098 return index < orderedPages.length ? orderedPages[index].getId() : null; 099 } 100 101 public String getId() { 102 return id; 103 } 104 105 public void setError(ValidationException e) { 106 this.error = e; 107 } 108 109 public ValidationException removeError() { 110 ValidationException e = error; 111 error = null; 112 return e; 113 } 114 115 @SuppressWarnings("unchecked") 116 public <T extends Form> T getForm(Class<T> formType) { 117 WizardPage p = lastPage; 118 while (p != null) { 119 if (formType == p.getFormType()) { 120 return (T) p.getForm(); 121 } 122 p = p.prev; 123 } 124 return null; 125 } 126 127}