Wiki source code of Creating a FAQ Application

Version 48.6 by yang ningJian on 2010/06/02

Hide last authors
Silvia Macovei 48.2 1 {{box cssClass="floatinginfobox" title="**Contents**"}}{{toc/}}{{/box}}
Vincent Massol 1.1 2
Silvia Macovei 48.1 3 This tutorial will show you how to build a Frequently Asked Questions (FAQs) Application much like the one you can find on the [[FAQ page>>xwiki:FAQ.WebHome]]. This is a very simple application that makes use of XWiki's classes, properties, and objects. It also uses a technique that you may frequently use as the basis for several different kinds of applications.
Vincent Massol 1.1 4
Silvia Macovei 48.1 5 All through this tutorial you should refer to the [[XWiki Data Model>>DevGuide.DataModel]] for information on XWiki's data model. You might also use the [[XWiki Scripting Guide>>DevGuide.Scripting]] to get you started with scripting in XWiki and manipulating XWiki objects. In addition, this tutorial will introduce the concepts of Authoring Templates and Page Design Sheets, patterns that you will find particularly useful in creating XWiki applications. Completing this tutorial is a recommended prerequisite for anyone who wants to build custom applications on the XWiki engine.
Anca Luca 14.1 6
Silvia Macovei 48.2 7 = Application Overview =
Vincent Massol 1.1 8
Guillaume Lerouge 42.1 9 The FAQ application allows users to post a question by entering the question into a simple form field and then submitting the form. The question then appears in a list along with all other questions that have been previously posted. Users can click on the questions to view both the question and answer in a consistently styled view. If the question has not yet been answered, any user can post an answer to the question by editing the page. In edit mode, the page will display a web form that is the same for every FAQ page.
Vincent Massol 1.1 10
11 Let us begin by taking a look at what we are going to build. The system has the following views. Click any view link below to preview a screenshot. (Note: Firefox users can middle click or click down on the mouse scroll-wheel to open the links in a new tab.)
12
Silvia Macovei 48.1 13 * [[The FAQs Summary View>>attach:FAQsSummary.png]]
14 * [[A Question and Answer Page in Display Mode>>attach:FAQSheetView.png]]
15 * [[A Question and Answer Page in Edit Mode>>attach:FAQSheetEdit.png]]
Vincent Massol 1.1 16
Silvia Macovei 48.2 17 = Authoring Templates and Page Design Sheets =
Silvia Macovei 48.1 18
Anca Luca 14.1 19 An Authoring Template is a template for creating documents of a specific type. Unlike a regular content page in edit mode with one field for freeform editing, an Authoring Template presents a custom set of form fields for creating a document with specific type of data. These form elements are defined by the properties of a class.
Vincent Massol 1.1 20
Silvia Macovei 48.1 21 In object oriented programming, remember that a //class// is a template for an object. Using the analogy of a cookie cutter, the //class// is the //cookie cutter// and the //objects// are the actual //cookies//. An Authoring Template provides one way to represent a class visually so that users can fill out a form to set unique properties (values in form fields). When the user submits the form, they are creating a unique //object// of the //class// type.
Vincent Massol 1.1 22
Anca Luca 14.1 23 Precisely, an Authoring Template is a prototype document used to create other specific instances of documents of the same type, along with a method of exposing the creation process to the user: the properties edit form. Remember that a XWiki Document can contain objects and this is the case of an authoring template: it is a XWiki Document with an empty object of a specific class, which is duplicated to create more and more documents based on that model, using the data inserted by the user in the editing form as specific values for the particular object instance in the current copy.
Vincent Massol 1.1 24
Silvia Macovei 31.6 25 The Page Design Sheet is like a style sheet that defines what each document will look like when rendered. Even though the unique object instances will have different values for their properties, they will always have a consistent presentation display because they will be rendered through the Page Design Sheet. The XWiki API available in scripting languages provides a mechanism that will help us use the same sheet for both editing and view modes. We will see how we can achieve this once we get to the FAQ Design Sheet section.
Vincent Massol 1.1 26
Silvia Macovei 48.2 27 = Go to the Class Editor Wizard =
Silvia Macovei 31.6 28
Guillaume Lerouge 42.1 29 Five pages, which collectively make up a Class Editor Wizard application, have been developed to assist you in this process. Enter a search query for the keyword "XWikiClasses". This should return a document called XWikiClasses in the XWiki space (i.e. XWiki.XWikiClasses). This is the homepage of the class wizard creator: you are now ready to start building your FAQs application.
Vincent Massol 1.1 30
Silvia Macovei 48.2 31 = Create the FAQ Class =
Vincent Massol 1.1 32
Silvia Macovei 32.1 33 * On the Class Editor wizard entry page (XWiki.XWikiClasses), under the heading "Create a new data type", enter the following web space and class name:
Silvia Macovei 41.1 34 ** Space: FAQ
Vincent Massol 1.1 35 ** Class: FAQ
Silvia Macovei 48.4 36 (((
Silvia Macovei 48.1 37 image:CreateANewClass.PNG
Silvia Macovei 48.4 38 )))
Vincent Massol 1.1 39 * Click the "Create this Class" button. You should then see a code page with the following code:
Silvia Macovei 48.4 40 (((
Silvia Macovei 48.1 41 {{code language="none"}}
Silvia Macovei 41.1 42 {{velocity}}
43 ## Replace Main with the Space where you want your documents to be created.
44 ## Replace the default parent with the one of your choice and save the document.
45 ##
46 #set($className = $doc.name.substring(0, $doc.name.indexOf("Class")))
47 #set($defaultParent = "${doc.space}.${className}Class")
48 #set($defaultSpace = 'Main')
49 {{/velocity}}
50 {{include document="XWiki.ClassSheet"/}}
Silvia Macovei 48.1 51 {{/code}}
Silvia Macovei 48.4 52 )))
Anca Luca 14.1 53
Silvia Macovei 41.1 54 In the code, change the word "Main" with the name of the space where you want you FAQ pages to be created as the commented instructions in the page code suggest. Replace the word "Main" with the word "FAQ". The line of code should now look like this:
Vincent Massol 1.1 55
Silvia Macovei 48.1 56 {{code language="none"}}#set($defaultSpace = 'FAQ'){{/code}}
Anca Luca 14.1 57
Guillaume Lerouge 42.1 58 You can also change the default parent of the new FAQ documents that are going to be created. To do so, replace the variables with the name of your document. The line of code should now look like this:
Vincent Massol 1.1 59
Silvia Macovei 48.1 60 {{code language="none"}}#set($defaultParent = 'FAQ.WebHome'){{/code}}
Vincent Massol 1.1 61
Guillaume Lerouge 42.1 62 Click the "Save & View" button. The class is now created and you should be looking at a page titled "Class: FAQ" that looks like this:
georgosn 28.1 63
Silvia Macovei 48.1 64 image:FAQClass1.PNG
georgosn 29.1 65
Silvia Macovei 48.2 66 = Add Properties to the Class =
Vincent Massol 1.1 67
Silvia Macovei 48.3 68 Under the page title, you should see the words "The class does not have any properties yet. You can use the //__class editor__// to define them." Let's just follow those instructions!
Vincent Massol 1.1 69
Guillaume Lerouge 42.1 70 * Click on the 'class editor' link: a list of existing XClasses will be displayed
Silvia Macovei 48.1 71 * Note that the link trail in the header is something like "FAQ: XWiki Space » Data types » FAQClass". This shows you that you are indeed on the class page.
Vincent Massol 1.1 72
Silvia Macovei 48.1 73 In our document, we'll store both a //question// and an //answer//. So we need a property for each.
Vincent Massol 1.1 74
Silvia Macovei 48.1 75 * Enter the text //question// in the "NAME" field of the Class Editor panel: the "ADD PROPERTY" panel is in the right column.
Guillaume Lerouge 42.1 76 * Choose a TextArea type for the property and then click the "ADD PROPERTY" button. The TextArea will ultimately give us a multi-line text field in our authoring template.
Silvia Macovei 48.4 77 (((
Silvia Macovei 48.1 78 image:AddQuestionProperty.PNG
Silvia Macovei 48.4 79 )))
Silvia Macovei 48.2 80 * Configure this property with the following values (actually, if you are using the current XWiki version, you don't need to change anything but the Pretty name ? all the rest are the default values):
Silvia Macovei 48.4 81 (((
Silvia Macovei 48.1 82 image:QuestionProperty.PNG
Silvia Macovei 48.4 83 )))
Silvia Macovei 48.1 84 * Now add a property called //answer// in the same way that you did for the "question" property (choosing TextArea for the property type)
85 * Choose it from the property list on the left and configure this property with the same values as the "question" property, except for the //name// and //pretty name// which will, obviously, match the new property we're creating
Guillaume Lerouge 42.1 86 * When you are done adding and configuring the properties, click the "Save & View" button.
Vincent Massol 1.1 87
Silvia Macovei 48.2 88 = Create the Page Design Sheet =
Vincent Massol 1.1 89
Anca Luca 14.1 90 * After the previous step you are now on the FAQClass page which should look like this:
Silvia Macovei 48.4 91 (((
Silvia Macovei 48.1 92 image:FAQClass2.PNG
Silvia Macovei 48.4 93 )))
Guillaume Lerouge 44.1 94 * Click the first button ("CREATE THE DOCUMENT SHEET") to create the document sheet (the Page Design Sheet). This sheet determines how your page's objects will be rendered to the user. The document is automatically created.
95 * Click on "View the sheet document"
96 * Edit that page in Object edition mode
97 * Using the menu on the right, add a XWiki.SheetClass object to the page
Silvia Macovei 48.1 98 ** **Adding the XWiki.SheetClass object is important. It's because of this object that users will be sent to form edition mode when editing FAQ entries**
Guillaume Lerouge 44.1 99 * Click "Save & View"
100 * If you edit the page in wiki mode, you will see the following code:
Silvia Macovei 48.4 101 (((
Silvia Macovei 48.1 102 {{code language="none"}}
Silvia Macovei 41.1 103 {{velocity}}
104 ## You can modify this page to customize the presentation of your object.
105 ## At first you should keep the default presentation and just save the document.
106 = Document $doc.name =
Anca Luca 14.1 107
Silvia Macovei 41.1 108 #set($class = $doc.getObject('FAQ.FAQClass').xWikiClass)
Vincent Massol 1.1 109
Vincent Massol 12.1 110 #foreach($prop in $class.properties)
Silvia Macovei 41.1 111 ; $prop.prettyName
112 : $doc.display($prop.getName())
Vincent Massol 12.1 113 #end
Silvia Macovei 41.1 114 {{/velocity}}
Silvia Macovei 48.1 115 {{/code}}
Silvia Macovei 48.4 116 )))
Guillaume Lerouge 42.1 117 Let's take a moment now and analyze this code:
Anca Luca 14.1 118
Guillaume Lerouge 42.1 119 * The first line retrieves the FAQ.FAQClass from the wiki
120 * Then we iterate through all its properties and display their values for the current document in a definition list.
121
Silvia Macovei 48.1 122 As we mentioned, XWiki provides a mechanism that help us create sheets used for both view and edit mode: this is the display function used in the line:
123
124 {{code language="none"}}: $doc.display($prop.getName()){{/code}}. It detects the current mode (edit or view) and displays the property referenced by its argument as the mode dictates:
125
Guillaume Lerouge 42.1 126 * In view mode it will display the value of the property
127 * In edit mode it will display a form field that will allow the user to edit it
128
Silvia Macovei 48.1 129 This way we can use a single Design Sheet to both display and edit our FAQ entries. See the [[XWiki API reference>>DevGuide.API]] and [[XWiki Scripting>>DevGuide.Scripting]] pages for more details about this.
Guillaume Lerouge 42.1 130
131 * Click "Save & View"
132
Silvia Macovei 48.2 133 = Create the Authoring Template =
Vincent Massol 1.1 134
Guillaume Lerouge 42.1 135 * Click on "FAQClass" in the breadcrumb. The document should look like this:
Silvia Macovei 48.4 136 (((
Silvia Macovei 48.1 137 image:FAQClass3.PNG
Silvia Macovei 48.4 138 )))
Guillaume Lerouge 42.1 139 * Notice that now, there is a link for the FAQSheet in place of the button that was previously there
140 * Click on the "CREATE THE DOCUMENT TEMPLATE" button. The Authoring Template will be automatically created. If you click on "View the template document" and edit that page in wiki mode, you will see the following code:
Silvia Macovei 48.4 141 (((
Silvia Macovei 48.1 142 {{code language="none"}}{{include document="FAQ.FAQSheet"/}}{{/code}}
Silvia Macovei 48.4 143 )))
Guillaume Lerouge 42.1 144 * Note that we changed the space name preceding the page name also because we want all of our FAQ pages to reside in the same XWiki space
Silvia Macovei 48.1 145 * Remember that all our documents will be copies of the Authoring Template used as a prototype so the content will be copied in all our FAQs documents and will execute the Design Sheet code in the context of the current document. See the [[dedicated page>>DevGuide.IncludeInVelocity]] for more information regarding this technique.
Vincent Massol 1.1 146
Guillaume Lerouge 42.1 147 Now we need to associate the prototype object with this document to turn it into a true authoring template.
148
149 * If you're on the template page, click on "FAQClass" in the breadcrumb to get back to the FAQ Class page
150 * At the bottom of the page, look for the following error message: "The template does not contain an object of type FAQClass. Add a FAQ object to the template »."
151 * Click on "Add a FAQ object to the template »"
152 * Congratulations: you just created an Authoring Template! You're almost done now.
Silvia Macovei 48.4 153 (((
Silvia Macovei 48.1 154 image:FAQClass4.PNG
Silvia Macovei 48.4 155 )))
Silvia Macovei 48.2 156 = Create a home page for the FAQ application =
Silvia Macovei 38.1 157
Guillaume Lerouge 42.1 158 You want your users to be able to see a list of all existing questions and answers and to add new questions. The best way to do this is to put the FAQ application in its own space and to use that space's homepage to display existing questions.
Vincent Massol 1.1 159
Vincent Massol 46.1 160 * Use the Create top level menu (when using the Colibri skin) or the Create Panel to create the FAQ.WebHome page (Space = FAQ, Page = WebHome). Alternatively you can navigate to the wiki dashboard page (the home page) and click on the FAQ space which should have a question mark (that's because you've created documents in the FAQ space above but there's no WebHome page created yet).
161 * Edit the page in Wiki model and input a title such as "FAQs" in the title field
Vincent Massol 1.1 162
Silvia Macovei 48.2 163 == Displaying existing FAQ entries ==
Vincent Massol 1.1 164
Guillaume Lerouge 42.1 165 You will need to write the following code:
Vincent Massol 1.1 166
Guillaume Lerouge 42.1 167 * A HQL query that will find all your FAQ documents
168 ** The HQL query looks for all documents that have a FAQ.FAQClass object other than the template
169 ** If no document has been created yet, a warning message is displayed
170 * A piece of velocity tode to display all those documents
171 ** The velocity code loops in that list
172 ** For each item, the full document is loaded in memory so that values can be retrieved from it
173 ** For each document, the question is retrieved and displayed as a link towards the FAQ entry
Vincent Massol 1.1 174
Guillaume Lerouge 42.1 175 Here's the resulting code:
Vincent Massol 1.1 176
Silvia Macovei 48.1 177 {{code language="none"}}
Guillaume Lerouge 42.1 178 = Existing FAQ entries =
Vincent Massol 1.1 179
Guillaume Lerouge 42.1 180 {{velocity}}
181 #set($sql = ", BaseObject as obj where obj.name=doc.fullName and obj.className='FAQ.FAQClass' and obj.name<>'FAQ.FAQTemplate'")
182 #set($results = $xwiki.searchDocuments($sql))
183 #if($results.empty)
184 No FAQ has been created yet!
185 #else
186 #foreach ($item in $results)
187 #set($faq = $xwiki.getDocument($item))
188 * [[${faq.display("question").replace("<p>", "").replace("</p>", "")}>>${item}]]
189 #end
190 #end
191 {{/velocity}}
Silvia Macovei 48.1 192 {{/code}}
Vincent Massol 1.1 193
Guillaume Lerouge 42.1 194 * Copy that code and paste it in your page
195 * Click "Save and View"
196 * New FAQ entries will now be displayed on the page once you create them
Vincent Massol 1.1 197
Silvia Macovei 48.2 198 == Creating new FAQ entries ==
Vincent Massol 1.1 199
Guillaume Lerouge 42.1 200 Now you need to write some code to let your users create new FAQ entries. To do this, you will have to create a form in which the user can enter the name of the questions she wants to create. Once typed in, the form calls the same page to trigger the new document creation based on the parameters entered by the user:
Anca Luca 14.1 201
Guillaume Lerouge 42.1 202 * The first part of the code checks whether the page has a parameter. If so:
203 ** The name of the document that will be created is computed
204 ** The name is cleaned to remove extra characters
205 ** A check is done to verify the document doesn't exist yet
206 ** If everything's ok, the user is sent to the new document in inline edition mode
207 * The second part of the code is the actual FAQ creation form
208 ** It builds the name of the document to create it in the "FAQ" space
209 ** It sets the document parent as being the current document
210 ** It defines the template to use to create the new document
Vincent Massol 1.1 211
Silvia Macovei 48.1 212 {{code language="none"}}
Guillaume Lerouge 42.1 213 {{velocity}}
214 #if("$!request.docName" != '')
215 ## Request for creating a new instance
Guillaume Lerouge 47.1 216 #set($docName = $util.clearName(${request.docName}))
217 #set($targetDocName = "${request.spaceName}.${docName}")
Guillaume Lerouge 42.1 218 #if(!$xwiki.exists($targetDocName) && $xwiki.hasAccessLevel('edit', $context.user, $targetDocName))
219 $response.sendRedirect($xwiki.getURL($targetDocName, 'inline', "template=${request.template}&parent=${request.parent}"))
220 ## Stop processing, since we already sent a redirect.
221 #stop
222 #end
223 #end
Vincent Massol 1.1 224
Guillaume Lerouge 42.1 225 = Add a new question =
226
227 #if("$!targetDocName" != '' && $xwiki.exists($targetDocName))
228 {{warning}}The target document already exists. Please choose a different name, or [[view the existing document>>$targetDocName]]{{/warning}}
229 #elseif("$!targetDocName" != '')
230 {{warning}}You don't have permission to create that document{{/warning}}
231 #end
232
233 {{html}}
234 <form action="" id="newdoc" method="post">
235 <div>
236 <input type="hidden" name="parent" value="${doc.fullName}"/>
237 <input type="hidden" name="template" value="FAQ.FAQTemplate"/>
238 <input type="hidden" name="sheet" value="1"/>
239 <input type="hidden" name="spaceName" value="FAQ"/>
240 Document: <input type="text" name="docName" value="Enter your question here" class="withTip" size="50"/>
241 <span class="buttonwrapper"><input type="submit" value="Create this FAQ" class="button"/></span>
242 </div>
243 </form>
244 {{/html}}
245 {{/velocity}}
Silvia Macovei 48.1 246 {{/code}}
Vincent Massol 1.1 247
Guillaume Lerouge 42.1 248 * Copy that code and paste it in your page, below the code that's already there
249 * Click "Save and View"
250 * A form to create new FAQ entries is now available on the page
Anca Luca 14.1 251
Silvia Macovei 48.2 252 = Test the Application =
jingbo 27.1 253
Guillaume Lerouge 42.1 254 Now let's just create a new document in our application to test it out.
255
Silvia Macovei 48.1 256 * Under the "Add a new question" header, enter a document title in the //Document// field and click //Create this FAQ//. For example, enter //What is the meaning of life?//.
257 * You can then enter your question in longer form using the //Question// field on the template, like this:
Silvia Macovei 48.4 258 (((
Silvia Macovei 48.1 259 image:FAQSheetEdit.PNG
Silvia Macovei 48.4 260 )))
Silvia Macovei 48.1 261 * Click //Save & View// and then you will see the newly created document, like this:
Silvia Macovei 48.4 262 (((
Silvia Macovei 48.1 263 image:FAQSheetView.PNG
Silvia Macovei 48.4 264 )))
Guillaume Lerouge 43.1 265 * Go back to the "FAQ.WebHome" page to see the list of existing questions
Guillaume Lerouge 42.1 266
Silvia Macovei 48.2 267 = Conclusion =
Vincent Massol 1.1 268
Anca Luca 14.1 269 This tutorial has shown how to use the Class Wizard app and it has detailed the concepts of classes, objects, properties and introduced the authoring templates and page design sheets. You may also have learned a little bit about Velocity scripting in documents. You can use these basic concepts to build custom applications at the document or presentation layer of XWiki without having to compile or deploy code.
Vincent Massol 1.1 270
271 As always, please take the time to make this document better for other users if you find ways that it can be improved as you read it for the first time.

Get Connected