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