diff options
author | Sean Dague | 2008-04-07 18:02:58 +0000 |
---|---|---|
committer | Sean Dague | 2008-04-07 18:02:58 +0000 |
commit | 98b5276068ed02bc6d2bdeeb9928c035efc326b4 (patch) | |
tree | 9de3eb474bda2483cf2dca371d33de09e07e29fa /OpenSim/Data/NHibernate | |
parent | fix bad indentation (diff) | |
download | opensim-SC_OLD-98b5276068ed02bc6d2bdeeb9928c035efc326b4.zip opensim-SC_OLD-98b5276068ed02bc6d2bdeeb9928c035efc326b4.tar.gz opensim-SC_OLD-98b5276068ed02bc6d2bdeeb9928c035efc326b4.tar.bz2 opensim-SC_OLD-98b5276068ed02bc6d2bdeeb9928c035efc326b4.tar.xz |
added swag #1 on Inventory NHibernate implementation. There
is no hbm.xml mapping yet, so this isn't going to do anything,
plus I'm sure I didn't get the list interfaces right. However
it now compiles, so worth getting into the tree.
Diffstat (limited to 'OpenSim/Data/NHibernate')
-rw-r--r-- | OpenSim/Data/NHibernate/NHibernateInventoryData.cs | 383 |
1 files changed, 383 insertions, 0 deletions
diff --git a/OpenSim/Data/NHibernate/NHibernateInventoryData.cs b/OpenSim/Data/NHibernate/NHibernateInventoryData.cs new file mode 100644 index 0000000..d6cd664 --- /dev/null +++ b/OpenSim/Data/NHibernate/NHibernateInventoryData.cs | |||
@@ -0,0 +1,383 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Data; | ||
30 | using System.Reflection; | ||
31 | using System.Collections.Generic; | ||
32 | using libsecondlife; | ||
33 | using NHibernate; | ||
34 | using NHibernate.Cfg; | ||
35 | using NHibernate.Tool.hbm2ddl; | ||
36 | using NHibernate.Mapping.Attributes; | ||
37 | using OpenSim.Data; | ||
38 | using OpenSim.Framework; | ||
39 | using OpenSim.Framework.Console; | ||
40 | using Environment = NHibernate.Cfg.Environment; | ||
41 | |||
42 | namespace OpenSim.Data.NHibernate | ||
43 | { | ||
44 | public class NHibernateInventoryData: IInventoryData | ||
45 | { | ||
46 | private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | ||
47 | |||
48 | private Configuration cfg; | ||
49 | private ISessionFactory factory; | ||
50 | |||
51 | /// <summary> | ||
52 | /// Initialises the interface | ||
53 | /// </summary> | ||
54 | public void Initialise() | ||
55 | { | ||
56 | Initialise("Inventory.db", "Inventory"); | ||
57 | } | ||
58 | |||
59 | public void Initialise(string dbfile, string dbname) | ||
60 | { | ||
61 | // TODO: hard coding for sqlite based stuff to begin with, just making it easier to test | ||
62 | |||
63 | // This is stubbing for now, it will become dynamic later and support different db backends | ||
64 | cfg = new Configuration(); | ||
65 | cfg.SetProperty(Environment.ConnectionProvider, | ||
66 | "NHibernate.Connection.DriverConnectionProvider"); | ||
67 | cfg.SetProperty(Environment.Dialect, | ||
68 | "NHibernate.Dialect.SQLiteDialect"); | ||
69 | cfg.SetProperty(Environment.ConnectionDriver, | ||
70 | "NHibernate.Driver.SqliteClientDriver"); | ||
71 | cfg.SetProperty(Environment.ConnectionString, | ||
72 | "URI=file:" + dbfile + ",version=3"); | ||
73 | cfg.AddAssembly("OpenSim.Data.NHibernate"); | ||
74 | |||
75 | HbmSerializer.Default.Validate = true; | ||
76 | // using ( System.IO.MemoryStream stream = | ||
77 | // HbmSerializer.Default.Serialize(System.Reflection.Assembly.GetExecutingAssembly())) | ||
78 | // cfg.AddInputStream(stream); | ||
79 | |||
80 | // new SchemaExport(cfg).Create(true, true); | ||
81 | |||
82 | factory = cfg.BuildSessionFactory(); | ||
83 | } | ||
84 | |||
85 | /***************************************************************** | ||
86 | * | ||
87 | * Basic CRUD operations on Data | ||
88 | * | ||
89 | ****************************************************************/ | ||
90 | |||
91 | // READ | ||
92 | |||
93 | /// <summary> | ||
94 | /// Returns an inventory item by its UUID | ||
95 | /// </summary> | ||
96 | /// <param name="item">The UUID of the item to be returned</param> | ||
97 | /// <returns>A class containing item information</returns> | ||
98 | public InventoryItemBase getInventoryItem(LLUUID item) | ||
99 | { | ||
100 | using(ISession session = factory.OpenSession()) { | ||
101 | try { | ||
102 | return session.Load(typeof(InventoryItemBase), item.ToString()) as InventoryItemBase; | ||
103 | } catch { | ||
104 | return null; | ||
105 | } | ||
106 | } | ||
107 | } | ||
108 | |||
109 | /// <summary> | ||
110 | /// Creates a new inventory item based on item | ||
111 | /// </summary> | ||
112 | /// <param name="item">The item to be created</param> | ||
113 | public void addInventoryItem(InventoryItemBase item) | ||
114 | { | ||
115 | if (!ExistsItem(item.inventoryID)) { | ||
116 | using(ISession session = factory.OpenSession()) { | ||
117 | using(ITransaction transaction = session.BeginTransaction()) { | ||
118 | session.Save(item); | ||
119 | transaction.Commit(); | ||
120 | } | ||
121 | } | ||
122 | } else { | ||
123 | m_log.ErrorFormat("Attempted to add Inventory Item {0} that already exists, updating instead", item.inventoryID); | ||
124 | updateInventoryItem(item); | ||
125 | } | ||
126 | } | ||
127 | |||
128 | /// <summary> | ||
129 | /// Updates an inventory item with item (updates based on ID) | ||
130 | /// </summary> | ||
131 | /// <param name="item">The updated item</param> | ||
132 | public void updateInventoryItem(InventoryItemBase item) | ||
133 | { | ||
134 | if (ExistsItem(item.inventoryID)) { | ||
135 | using(ISession session = factory.OpenSession()) { | ||
136 | using(ITransaction transaction = session.BeginTransaction()) { | ||
137 | session.Update(item); | ||
138 | transaction.Commit(); | ||
139 | } | ||
140 | } | ||
141 | } else { | ||
142 | m_log.ErrorFormat("Attempted to add Inventory Item {0} that already exists", item.inventoryID); | ||
143 | } | ||
144 | } | ||
145 | |||
146 | /// <summary> | ||
147 | /// | ||
148 | /// </summary> | ||
149 | /// <param name="item"></param> | ||
150 | public void deleteInventoryItem(LLUUID itemID) | ||
151 | { | ||
152 | using(ISession session = factory.OpenSession()) { | ||
153 | using(ITransaction transaction = session.BeginTransaction()) { | ||
154 | session.Delete(itemID.ToString()); | ||
155 | transaction.Commit(); | ||
156 | } | ||
157 | } | ||
158 | } | ||
159 | |||
160 | |||
161 | /// <summary> | ||
162 | /// Returns an inventory folder by its UUID | ||
163 | /// </summary> | ||
164 | /// <param name="folder">The UUID of the folder to be returned</param> | ||
165 | /// <returns>A class containing folder information</returns> | ||
166 | public InventoryFolderBase getInventoryFolder(LLUUID folder) | ||
167 | { | ||
168 | using(ISession session = factory.OpenSession()) { | ||
169 | try { | ||
170 | return session.Load(typeof(InventoryFolderBase), folder.ToString()) as InventoryFolderBase; | ||
171 | } catch { | ||
172 | return null; | ||
173 | } | ||
174 | } | ||
175 | } | ||
176 | |||
177 | /// <summary> | ||
178 | /// Creates a new inventory folder based on folder | ||
179 | /// </summary> | ||
180 | /// <param name="folder">The folder to be created</param> | ||
181 | public void addInventoryFolder(InventoryFolderBase folder) | ||
182 | { | ||
183 | if (!ExistsFolder(folder.folderID)) { | ||
184 | using(ISession session = factory.OpenSession()) { | ||
185 | using(ITransaction transaction = session.BeginTransaction()) { | ||
186 | session.Save(folder); | ||
187 | transaction.Commit(); | ||
188 | } | ||
189 | } | ||
190 | } else { | ||
191 | m_log.ErrorFormat("Attempted to add Inventory Folder {0} that already exists, updating instead", folder.folderID); | ||
192 | updateInventoryFolder(folder); | ||
193 | } | ||
194 | } | ||
195 | |||
196 | /// <summary> | ||
197 | /// Updates an inventory folder with folder (updates based on ID) | ||
198 | /// </summary> | ||
199 | /// <param name="folder">The updated folder</param> | ||
200 | public void updateInventoryFolder(InventoryFolderBase folder) | ||
201 | { | ||
202 | if (ExistsFolder(folder.folderID)) { | ||
203 | using(ISession session = factory.OpenSession()) { | ||
204 | using(ITransaction transaction = session.BeginTransaction()) { | ||
205 | session.Update(folder); | ||
206 | transaction.Commit(); | ||
207 | } | ||
208 | } | ||
209 | } else { | ||
210 | m_log.ErrorFormat("Attempted to add Inventory Folder {0} that already exists", folder.folderID); | ||
211 | } | ||
212 | } | ||
213 | |||
214 | /// <summary> | ||
215 | /// | ||
216 | /// </summary> | ||
217 | /// <param name="folder"></param> | ||
218 | public void deleteInventoryFolder(LLUUID folderID) | ||
219 | { | ||
220 | using(ISession session = factory.OpenSession()) { | ||
221 | using(ITransaction transaction = session.BeginTransaction()) { | ||
222 | session.Delete(folderID.ToString()); | ||
223 | transaction.Commit(); | ||
224 | } | ||
225 | } | ||
226 | } | ||
227 | |||
228 | // useful private methods | ||
229 | private bool ExistsItem(LLUUID uuid) | ||
230 | { | ||
231 | return (getInventoryItem(uuid) != null) ? true : false; | ||
232 | } | ||
233 | |||
234 | private bool ExistsFolder(LLUUID uuid) | ||
235 | { | ||
236 | return (getInventoryFolder(uuid) != null) ? true : false; | ||
237 | } | ||
238 | |||
239 | public void Shutdown() | ||
240 | { | ||
241 | // TODO: DataSet commit | ||
242 | } | ||
243 | |||
244 | /// <summary> | ||
245 | /// Closes the interface | ||
246 | /// </summary> | ||
247 | public void Close() | ||
248 | { | ||
249 | } | ||
250 | |||
251 | /// <summary> | ||
252 | /// The plugin being loaded | ||
253 | /// </summary> | ||
254 | /// <returns>A string containing the plugin name</returns> | ||
255 | public string getName() | ||
256 | { | ||
257 | return "NHibernate Inventory Data Interface"; | ||
258 | } | ||
259 | |||
260 | /// <summary> | ||
261 | /// The plugins version | ||
262 | /// </summary> | ||
263 | /// <returns>A string containing the plugin version</returns> | ||
264 | public string getVersion() | ||
265 | { | ||
266 | Module module = GetType().Module; | ||
267 | string dllName = module.Assembly.ManifestModule.Name; | ||
268 | Version dllVersion = module.Assembly.GetName().Version; | ||
269 | |||
270 | |||
271 | return | ||
272 | string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build, | ||
273 | dllVersion.Revision); | ||
274 | } | ||
275 | |||
276 | // Move seems to be just update | ||
277 | |||
278 | public void moveInventoryFolder(InventoryFolderBase folder) | ||
279 | { | ||
280 | updateInventoryFolder(folder); | ||
281 | } | ||
282 | |||
283 | public void moveInventoryItem(InventoryItemBase item) | ||
284 | { | ||
285 | updateInventoryItem(item); | ||
286 | } | ||
287 | |||
288 | |||
289 | |||
290 | /// <summary> | ||
291 | /// Returns a list of inventory items contained within the specified folder | ||
292 | /// </summary> | ||
293 | /// <param name="folderID">The UUID of the target folder</param> | ||
294 | /// <returns>A List of InventoryItemBase items</returns> | ||
295 | public List<InventoryItemBase> getInventoryInFolder(LLUUID folderID) | ||
296 | { | ||
297 | using(ISession session = factory.OpenSession()) { | ||
298 | try { | ||
299 | IQuery query = session.CreateQuery("from InventoryItems i where i.parentFolderID = :parent"); | ||
300 | query.SetString("parent", folderID.ToString()); | ||
301 | List<InventoryItemBase> list = new List<InventoryItemBase>(); | ||
302 | foreach (InventoryItemBase item in query.List()) | ||
303 | { | ||
304 | list.Add(item); | ||
305 | } | ||
306 | return list; | ||
307 | } catch { | ||
308 | return new List<InventoryItemBase>(); | ||
309 | } | ||
310 | } | ||
311 | } | ||
312 | |||
313 | public List<InventoryFolderBase> getUserRootFolders(LLUUID user) | ||
314 | { | ||
315 | return new List<InventoryFolderBase>(); | ||
316 | } | ||
317 | |||
318 | // see InventoryItemBase.getUserRootFolder | ||
319 | public InventoryFolderBase getUserRootFolder(LLUUID user) | ||
320 | { | ||
321 | // using(ISession session = factory.OpenSession()) { | ||
322 | // try { | ||
323 | // IQuery query = session.CreateQuery("from InventoryItems i where i.parentFolderID = :parent"); | ||
324 | // query.SetString("parent", folderID.ToString()); | ||
325 | // List<InventoryItemBase> list = new List<InventoryItemBase>(); | ||
326 | // foreach (InventoryItemBase item in query.List()) | ||
327 | // { | ||
328 | // list.Add(item); | ||
329 | // } | ||
330 | // return list; | ||
331 | // } catch { | ||
332 | // return new List<InventoryItemBase>(); | ||
333 | // } | ||
334 | // } | ||
335 | return new InventoryFolderBase(); | ||
336 | } | ||
337 | |||
338 | /// <summary> | ||
339 | /// Append a list of all the child folders of a parent folder | ||
340 | /// </summary> | ||
341 | /// <param name="folders">list where folders will be appended</param> | ||
342 | /// <param name="parentID">ID of parent</param> | ||
343 | protected void getInventoryFolders(ref List<InventoryFolderBase> folders, LLUUID parentID) | ||
344 | { | ||
345 | using(ISession session = factory.OpenSession()) { | ||
346 | try { | ||
347 | IQuery query = session.CreateQuery("from InventoryFolders i where i.parentFolderID = :parent"); | ||
348 | query.SetString("parent", parentID.ToString()); | ||
349 | foreach (InventoryFolderBase item in query.List()) | ||
350 | { | ||
351 | folders.Add(item); | ||
352 | } | ||
353 | } catch { | ||
354 | |||
355 | } | ||
356 | } | ||
357 | } | ||
358 | |||
359 | /// <summary> | ||
360 | /// Returns a list of inventory folders contained in the folder 'parentID' | ||
361 | /// </summary> | ||
362 | /// <param name="parentID">The folder to get subfolders for</param> | ||
363 | /// <returns>A list of inventory folders</returns> | ||
364 | public List<InventoryFolderBase> getInventoryFolders(LLUUID parentID) | ||
365 | { | ||
366 | List<InventoryFolderBase> folders = new List<InventoryFolderBase>(); | ||
367 | getInventoryFolders(ref folders, Util.ToRawUuidString(parentID)); | ||
368 | return folders; | ||
369 | } | ||
370 | |||
371 | // See IInventoryData | ||
372 | public List<InventoryFolderBase> getFolderHierarchy(LLUUID parentID) | ||
373 | { | ||
374 | List<InventoryFolderBase> folders = new List<InventoryFolderBase>(); | ||
375 | getInventoryFolders(ref folders, Util.ToRawUuidString(parentID)); | ||
376 | |||
377 | for (int i = 0; i < folders.Count; i++) | ||
378 | getInventoryFolders(ref folders, Util.ToRawUuidString(folders[i].folderID)); | ||
379 | |||
380 | return folders; | ||
381 | } | ||
382 | } | ||
383 | } | ||