Thanks a great bunch John, will go through it. Stephen --- On Tue, 14/7/09, John Armstrong <[email protected]> wrote:
From: John Armstrong <[email protected]> Subject: Re: Wizard help To: [email protected] Date: Tuesday, 14 July, 2009, 5:43 PM I haven't had time to nice it up but here is a class that does this. Its a wizard that interviews the user based on pre-configured questions in the database. There are 3 'types' of questions in this app I wrote earlier this year (Text Field, Text Area and File) and there can be any number of each type in each defined interview process. The app was a website configuration engine essentially. The user would select a template and the template would have 'Configuration Definitions' associated with it. The app would ask the user for each of these defs what their answer was. This example has all of the ugly data access stuff so you should get a good idea how I pull the data in and add it to the model. This is not shrinkwrap but just an example. This is ugly but it should give you the idea: ----MetadataWizard--- /* * */ package mypackage.wicket; import org.apache.cayenne.DataObjectUtils; import org.apache.cayenne.ObjectContext; import org.apache.cayenne.exp.Expression; import org.apache.cayenne.exp.ExpressionFactory; import org.apache.cayenne.query.SelectQuery; import org.apache.wicket.extensions.wizard.Wizard; import org.apache.wicket.extensions.wizard.WizardModel; import mypackage.cayenne.Template; import org.apache.wicket.model.CompoundPropertyModel; import util.SymLink; // TODO: Auto-generated Javadoc /** * The Class MetadataWizard is a wizard responsible for interviewing the user during site configuration */ public class MetadataWizard extends Wizard { /** The template. */ Template theTemplate; /** * Instantiates a new metadata wizard. * * @param id * the id * @param theTemplateIn * the the template in */ public MetadataWizard(String id, Template theTemplateIn){ super(id, false); theTemplate = theTemplateIn; setModel(new CompoundPropertyModel(theTemplate)); WizardModel model = new WizardModel(); // We need to put a model on this that is the config DEFs value for this user mypackage.cayenne.Account theAccount = MySession.get().getUser(); ObjectContext context = theAccount.getObjectContext(); for (mypackage.cayenne.ConfigurationDef configDefinition : theTemplate.getConfigurationDefArray()) { // Get this data element from the users data list Expression qual = ExpressionFactory.likeIgnoreCaseExp( mypackage.cayenne.ConfigurationData.TO_ACCOUNT_PROPERTY, theAccount) .andExp( ExpressionFactory.likeIgnoreCaseExp( mypackage.cayenne.ConfigurationData.TO_CONFIGURATION_DEF_PROPERTY, configDefinition)); SelectQuery query = new SelectQuery(mypackage.cayenne.ConfigurationData.class, qual); java.util.List defList = context.performQuery(query); mypackage.cayenne.ConfigurationData data = null; // If an element exists assign it so we can pass it to our PropertyModel for population // If it does not exist then create a new one, map it into the account/template if(defList.size()>0){ data = (mypackage.cayenne.ConfigurationData)defList.get(0); } else { data = (mypackage.cayenne.ConfigurationData) context.newObject(mypackage.cayenne.ConfigurationData.class); data.setToAccount(theAccount); data.setToConfigurationDef(configDefinition); } if(configDefinition.getElementtype().equals("string")){ model.add(new TextStep(configDefinition, data)); } if(configDefinition.getElementtype().equals("textarea")){ model.add(new TextAreaStep(configDefinition, data)); } else if(configDefinition.getElementtype().equals("file")){ model.add(new FileStep(configDefinition, data)); } } init(model); } /** * On cancel. * * @see org.apache.wicket.extensions.wizard.Wizard#onCancel() */ @Override public void onCancel() { setResponsePage(mypackage.wicket.MyPage.class); } /** * On finish. * * @see org.apache.wicket.extensions.wizard.Wizard#onFinish() */ @Override public void onFinish() { // Commit meta data util.Settings settings = new util.Settings(); MySession.get().getUser().setToTemplate(theTemplate); MySession.get().getUser().getObjectContext().commitChanges(); // Send them on their way setResponsePage(mypackage.wicket.MyPage.class); } } ----TextStep---- /* * */ package edu.ndnu.wicket; import org.apache.wicket.markup.html.panel.Panel; import org.apache.wicket.extensions.wizard.WizardStep; import org.apache.wicket.markup.html.basic.*; import org.apache.wicket.markup.html.form.*; import org.apache.wicket.model.CompoundPropertyModel; import org.apache.wicket.model.Model; import org.apache.wicket.model.PropertyModel; // TODO: Auto-generated Javadoc //import org.apache.wicket.markup.html. /** * The Class TextStep is a step in the {...@link MetadataWizard}, responsible for interviewing the user and gathering metadata for template setup (not creation!) * * This step collects a simple unformatted string. * * @author jarmstrong */ public class TextStep extends WizardStep { /** The configuration data. */ edu.ndnu.cayenne.ConfigurationData configurationData; /** * Instantiates a new text step. * * @param configurationDefinition * the configuration definition * @param configurationDataIn * the configuration data in */ public TextStep(edu.ndnu.cayenne.ConfigurationDef configurationDefinition, edu.ndnu.cayenne.ConfigurationData configurationDataIn) { this.configurationData = configurationDataIn; setModel(new CompoundPropertyModel(configurationDefinition)); add(new Label("configelement")); //add(new Label("elementtype")); add(new Label("elementdescription").setEscapeModelStrings(false)); TextField theField = new TextField("elementvalue",new PropertyModel(configurationData, "elementvalue")); theField.setRequired(true); add(theField); } /* (non-Javadoc) * @see org.apache.wicket.extensions.wizard.WizardStep#applyState() */ public void applyState(){ System.err.println("Applying state!"); if(isComplete()){ configurationData.getObjectContext().commitChanges(); } } } On Tue, Jul 14, 2009 at 5:47 AM, Steve Olara<[email protected]> wrote: > Hello All, > > Am new to wickets and trying to learn a few things. To do that am trying to > build a web application that a user can post a exam and a responder can > answer that exam. So far I have managed to Post a set of questions for a exam > into the database. my problem is how to answer those questions. > > A single exam can have 3 different question types Objectives, short answers > and phrase match. Each of this have different forms of interface for > responding to it. If there were only 3 questions, one of each type I would > just have 3 Wizard Steps, but the number of questions can vary depending on > the exam. I would like to reuse the steps for each question type when needed > (Have only 3 predefined steps). > > Can someone guide me on this or point me to a thread that can do the same. > > A suggestion of an alternate solution is also welcome. > > Thanks > > Stephen > > > --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
