Wiki source code of Saving Data

Version 1.2 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 == Servlet Session ==
48
49 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.
50
51 For example, from Java in a Component:
52
53 {{code language="java"}}
54 import org.xwiki.container.*;
55
56 @Inject Container container;
57 ...
58 Request request = container.getRequest();
59 // 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)
60 if (request instanceof ServletRequest) {
61 HttpServletRequest servletRequest = (ServletRequest) request;
62 HttpSession session = servletRequest.getSession();
63 session.setAttribute("mykey", myvalue);
64 }
65 {{/code}}
66
67 == Temporary Directory ==
68
69 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.
70
71 For example, from Java in a Component:
72
73 {{code language="java"}}
74 import org.xwiki.environment.*;
75 import org.apache.commons.io.*;
76
77 @Inject Environment environment;
78 ...
79 File tmpDir = environment.getTemporaryDirectory();
80 FileUtils.write(tmpDir, "something");
81 {{/code}}
82
83 == Servlet Context ==
84
85 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.
86
87 For example, from Java in a Component:
88
89 {{code language="java"}}
90 import org.xwiki.container.*;
91 import com.xpn.xwiki.*;
92 import com.xpn.xwiki.web.*;
93
94 @Inject Execution execution;
95 ...
96 // Get the older XWiki Context
97 XWikiContext xc = (XWikiContext) execution.getContext().getProperty("xwikicontext");
98 XWikiEngineContext ec = xc.getEngineContext();
99 ec.setAttribute("mykey", myvalue);
100 {{/code}}
101
102 = Permanent Saves =
103
104 == Wiki Model ==
105
106 The best place to save Data is usually in the wiki itself in an XObject attached to a wiki page (See the [[Data Model>>DevGuide.DataModel]]).
107
108 == Permanent Directory ==
109
110 Another possibility is to use the Environment's Permanent Directory.
111
112 For example, from Java in a Component:
113
114 {{code language="java"}}
115 import org.xwiki.environment.*;
116 import org.apache.commons.io.*;
117
118 @Inject Environment environment;
119 ...
120 File permDir = environment.getPermanentDirectory();
121 FileUtils.write(permDir, "something");
122 {{/code}}

Get Connected