Wiki source code of Saving Data

Version 5.1 by Vincent Massol on 2012/08/20

Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
4
5 There are times when you wish to save data. The XWiki platform offers various places that you can use depending on your needs. Here's a rundown of all the options you have.
6
7 = Transient Saves =
8
9 == Execution Context ==
10
11 If the data to be saved should only be saved for the duration of a Request (i.e. a call to a XWiki URL), then you should use the **Execution Context**.
12
13 For example, from Java in a Component:
14
15 {{code language="java"}}
16 import org.xwiki.execution.*;
17
18 @Inject Execution execution;
19 ...
20 // Saving
21 ExecutionContext ec = execution.getContext();
22 ec.setProperty("mykey", myvalue);
23 ...
24 // Retrieving
25 Object myvalue = e.getProperty("mykey");
26 {{/code}}
27
28 From a wiki page (Note that [[we currently cannot access easily the Execution Context from a wiki page but this should be improved>>http://jira.xwiki.org/browse/XCOMMONS-242]] and currently it's not available only from Velocity but it is from Groovy for example):
29
30 {{code}}
31 {{groovy}}
32 import org.xwiki.context.*
33
34 def ec = services.component.getInstance(Execution.class).getContext()
35 ec.setProperty("mykey", myvalue)
36 {{/groovy}}
37 {{/code}}
38
39 Note that from a wiki page it's much easier to use the older **XWiki Context** (which is supposed to be fully replaced by the Execution Context at some point in the future). For example:
40
41 {{code}}
42 {{velocity}}
43 $xcontext.put("mykey", value)
44 {{/velocity}}
45 {{/code}}
46
47 Note that it's also possible to save data in the HTTP Request even though it's recommended to use the Execution Context instead (since the Execution Context is independent of the execution environment and will work everywhere: Servlets, Portlets, JavaSE, etc). For example from Velocity in a wiki page:
48
49 {{code}}
50 {{velocity}}
51 $request.setAttribute("mykey", value)
52 {{/velocity}}
53 {{/code}}
54
55 == Servlet Session ==
56
57 If the data should last a little longer (i.e. for example span more than 1 request), you could save it in the Servlet Session (note that in this case your code will only work in a Servlet Environment).
58
59 For example, from Java in a Component:
60
61 {{code language="java"}}
62 import org.xwiki.container.*;
63
64 @Inject Container container;
65 ...
66 Request request = container.getRequest();
67 // Note that this is a bit of a hack and the notion of Session exists in the Container class (getSession()) but the Session interface is empty at the moment, making it useless)
68 if (request instanceof ServletRequest) {
69 HttpServletRequest servletRequest = (ServletRequest) request;
70 HttpSession session = servletRequest.getSession();
71 session.setAttribute("mykey", myvalue);
72 }
73 {{/code}}
74
75 Example from a wiki page:
76
77 {{code}}
78 {{velocity}}
79 $request.getSession().setAttribute("mykey", myvalue)
80 {{/velocity}}
81 {{/code}}
82
83 == Temporary Directory ==
84
85 If the data should last even a little longer than a session, you could save it in a File in the Environment's Temporary Directory.
86
87 For example, from Java in a Component:
88
89 {{code language="java"}}
90 import org.xwiki.environment.*;
91 import org.apache.commons.io.*;
92
93 @Inject Environment environment;
94 ...
95 File tmpDir = environment.getTemporaryDirectory();
96 FileUtils.write(tmpDir, "something");
97 {{/code}}
98
99 == Servlet Context ==
100
101 If the Data should last as long as the web application is up, you could use the Servlet Context. It's not very easy to access it but it's possible (note that in this case your code will only work in a Servlet Environment).
102
103 For example, from Java in a Component:
104
105 {{code language="java"}}
106 import org.xwiki.container.*;
107 import com.xpn.xwiki.*;
108 import com.xpn.xwiki.web.*;
109
110 @Inject Execution execution;
111 ...
112 // Get the older XWiki Context
113 XWikiContext xc = (XWikiContext) execution.getContext().getProperty("xwikicontext");
114 XWikiEngineContext ec = xc.getEngineContext();
115 ec.setAttribute("mykey", myvalue);
116 {{/code}}
117
118 = Permanent Saves =
119
120 == Wiki Model ==
121
122 The best place to save Data is usually in the wiki itself (See the [[Data Model>>DevGuide.DataModel]] and the [[API Guide for some examples>>DevGuide.APIGuide]]). There are several places where you can store data in the model:
123 * In the Document content itself,
124 * In an XObject attached to a Document,
125 * In an Attachment
126
127 == Permanent Directory ==
128
129 Another possibility is to use the Environment's Permanent Directory.
130
131 For example, from Java in a Component:
132
133 {{code language="java"}}
134 import org.xwiki.environment.*;
135 import org.apache.commons.io.*;
136
137 @Inject Environment environment;
138 ...
139 File permDir = environment.getPermanentDirectory();
140 FileUtils.write(permDir, "something");
141 {{/code}}

Get Connected