diff options
Diffstat (limited to 'OpenSim/Data/NHibernate/NHibernateEstateData.cs')
-rw-r--r-- | OpenSim/Data/NHibernate/NHibernateEstateData.cs | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/OpenSim/Data/NHibernate/NHibernateEstateData.cs b/OpenSim/Data/NHibernate/NHibernateEstateData.cs new file mode 100644 index 0000000..59896dd --- /dev/null +++ b/OpenSim/Data/NHibernate/NHibernateEstateData.cs | |||
@@ -0,0 +1,161 @@ | |||
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.Reflection; | ||
29 | using log4net; | ||
30 | using OpenMetaverse; | ||
31 | using OpenSim.Framework; | ||
32 | using OpenSim.Region.Framework.Interfaces; | ||
33 | using NHibernate; | ||
34 | using NHibernate.Criterion; | ||
35 | using System.Collections; | ||
36 | using System; | ||
37 | |||
38 | namespace OpenSim.Data.NHibernate | ||
39 | { | ||
40 | /// <summary> | ||
41 | /// A User storage interface for the DB4o database system | ||
42 | /// </summary> | ||
43 | public class NHibernateEstateData : IEstateDataStore | ||
44 | { | ||
45 | |||
46 | #region Fields | ||
47 | |||
48 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
49 | |||
50 | public NHibernateManager manager; | ||
51 | |||
52 | public string Name | ||
53 | { | ||
54 | get { return "NHibernateEstateData"; } | ||
55 | } | ||
56 | |||
57 | public string Version | ||
58 | { | ||
59 | get { return "0.1"; } | ||
60 | } | ||
61 | |||
62 | #endregion | ||
63 | |||
64 | #region Startup and shutdown. | ||
65 | |||
66 | public void Initialise() | ||
67 | { | ||
68 | m_log.Info("[NHIBERNATE]: " + Name + " cannot be default-initialized!"); | ||
69 | throw new PluginNotInitialisedException(Name); | ||
70 | } | ||
71 | |||
72 | public void Initialise(string connect) | ||
73 | { | ||
74 | |||
75 | m_log.InfoFormat("[NHIBERNATE] Initializing " + Name + "."); | ||
76 | manager = new NHibernateManager(connect, "EstateStore"); | ||
77 | } | ||
78 | |||
79 | public void Dispose() { } | ||
80 | |||
81 | #endregion | ||
82 | |||
83 | #region IEstateDataStore Members | ||
84 | |||
85 | public EstateSettings LoadEstateSettings(UUID regionID) | ||
86 | { | ||
87 | EstateRegionLink link = LoadEstateRegionLink(regionID); | ||
88 | |||
89 | // Ensure that estate settings exist for the link | ||
90 | if (link != null) | ||
91 | { | ||
92 | if (manager.Load(typeof(EstateSettings), link.EstateID) == null) | ||
93 | { | ||
94 | // Delete broken link | ||
95 | manager.Delete(link); | ||
96 | link = null; | ||
97 | } | ||
98 | } | ||
99 | |||
100 | // If estate link does not exist create estate settings and link it to region. | ||
101 | if (link == null) | ||
102 | { | ||
103 | EstateSettings estateSettings = new EstateSettings(); | ||
104 | //estateSettings.EstateOwner = UUID.Random(); | ||
105 | //estateSettings.BlockDwell = false; | ||
106 | object identifier = manager.Insert(estateSettings); | ||
107 | |||
108 | if (identifier == null) | ||
109 | { | ||
110 | // Saving failed. Error is logged in the manager. | ||
111 | return null; | ||
112 | } | ||
113 | |||
114 | uint estateID = (uint)identifier; | ||
115 | link = new EstateRegionLink(); | ||
116 | link.EstateRegionLinkID = UUID.Random(); | ||
117 | link.RegionID = regionID; | ||
118 | link.EstateID = estateID; | ||
119 | manager.Insert(link); | ||
120 | } | ||
121 | |||
122 | // Load estate settings according to the existing or created link. | ||
123 | return (EstateSettings)manager.Load(typeof(EstateSettings), link.EstateID); | ||
124 | } | ||
125 | |||
126 | public void StoreEstateSettings(EstateSettings estateSettings) | ||
127 | { | ||
128 | // Estates are always updated when stored. | ||
129 | // Insert is always done via. load method as with the current API | ||
130 | // this is explicitly the only way to create region link. | ||
131 | manager.Update(estateSettings); | ||
132 | } | ||
133 | |||
134 | #endregion | ||
135 | |||
136 | #region Private Utility Methods | ||
137 | private EstateRegionLink LoadEstateRegionLink(UUID regionID) | ||
138 | { | ||
139 | ICriteria criteria = manager.GetSession().CreateCriteria(typeof(EstateRegionLink)); | ||
140 | criteria.Add(Expression.Eq("RegionID", regionID)); | ||
141 | IList links = criteria.List(); | ||
142 | |||
143 | // Fail fast if more than one estate links exist | ||
144 | if (links.Count > 1) | ||
145 | { | ||
146 | m_log.Error("[NHIBERNATE]: Region had more than one estate linked: " + regionID); | ||
147 | throw new Exception("[NHIBERNATE]: Region had more than one estate linked: " + regionID); | ||
148 | } | ||
149 | |||
150 | if (links.Count == 1) | ||
151 | { | ||
152 | return (EstateRegionLink)links[0]; | ||
153 | } | ||
154 | else | ||
155 | { | ||
156 | return null; | ||
157 | } | ||
158 | } | ||
159 | #endregion | ||
160 | } | ||
161 | } | ||