diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Data/NHibernate/NHibernateUserData.cs | 269 |
1 files changed, 269 insertions, 0 deletions
diff --git a/OpenSim/Data/NHibernate/NHibernateUserData.cs b/OpenSim/Data/NHibernate/NHibernateUserData.cs new file mode 100644 index 0000000..0bbebaa --- /dev/null +++ b/OpenSim/Data/NHibernate/NHibernateUserData.cs | |||
@@ -0,0 +1,269 @@ | |||
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.Expression; | ||
36 | using NHibernate.Tool.hbm2ddl; | ||
37 | using NHibernate.Mapping.Attributes; | ||
38 | using OpenSim.Data; | ||
39 | using OpenSim.Framework; | ||
40 | using OpenSim.Framework.Console; | ||
41 | using Environment = NHibernate.Cfg.Environment; | ||
42 | |||
43 | namespace OpenSim.Data.NHibernate | ||
44 | { | ||
45 | /// <summary> | ||
46 | /// A User storage interface for the DB4o database system | ||
47 | /// </summary> | ||
48 | public class NHibernateUserData : UserDataBase | ||
49 | { | ||
50 | private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | ||
51 | |||
52 | private Configuration cfg; | ||
53 | private ISessionFactory factory; | ||
54 | |||
55 | public override void Initialise() | ||
56 | { | ||
57 | // TODO: hard coding for sqlite based stuff to begin with, just making it easier to test | ||
58 | |||
59 | // This is stubbing for now, it will become dynamic later and support different db backends | ||
60 | cfg = new Configuration(); | ||
61 | cfg.SetProperty(Environment.ConnectionProvider, | ||
62 | "NHibernate.Connection.DriverConnectionProvider"); | ||
63 | cfg.SetProperty(Environment.Dialect, | ||
64 | "NHibernate.Dialect.SQLiteDialect"); | ||
65 | cfg.SetProperty(Environment.ConnectionDriver, | ||
66 | "NHibernate.Driver.SqliteClientDriver"); | ||
67 | cfg.SetProperty(Environment.ConnectionString, | ||
68 | "URI=file:User.db,version=3"); | ||
69 | cfg.AddAssembly("OpenSim.Data.NHibernate"); | ||
70 | |||
71 | HbmSerializer.Default.Validate = true; | ||
72 | using ( System.IO.MemoryStream stream = | ||
73 | HbmSerializer.Default.Serialize(System.Reflection.Assembly.GetExecutingAssembly())) | ||
74 | cfg.AddInputStream(stream); | ||
75 | |||
76 | // new SchemaExport(cfg).Create(true, true); | ||
77 | |||
78 | factory = cfg.BuildSessionFactory(); | ||
79 | } | ||
80 | |||
81 | private bool ExistsUser(LLUUID uuid) | ||
82 | { | ||
83 | UserProfileData user; | ||
84 | using(ISession session = factory.OpenSession()) { | ||
85 | user = session.Load(typeof(UserProfileData), uuid) as UserProfileData; | ||
86 | } | ||
87 | return (user == null) ? false : true; | ||
88 | } | ||
89 | |||
90 | override public UserProfileData GetUserByUUID(LLUUID uuid) | ||
91 | { | ||
92 | UserProfileData user; | ||
93 | // TODO: I'm sure I'll have to do something silly here | ||
94 | using(ISession session = factory.OpenSession()) { | ||
95 | user = session.Load(typeof(UserProfileData), uuid) as UserProfileData; | ||
96 | user.CurrentAgent = GetAgentByUUID(uuid); | ||
97 | } | ||
98 | return user; | ||
99 | } | ||
100 | |||
101 | override public void AddNewUserProfile(UserProfileData profile) | ||
102 | { | ||
103 | if (!ExistsUser(profile.ID)) { | ||
104 | using(ISession session = factory.OpenSession()) { | ||
105 | using(ITransaction transaction = session.BeginTransaction()) { | ||
106 | session.Save(profile); | ||
107 | SetAgentData(profile.ID, profile.CurrentAgent, session); | ||
108 | // TODO: save agent | ||
109 | transaction.Commit(); | ||
110 | } | ||
111 | } | ||
112 | } else { | ||
113 | m_log.ErrorFormat("Attempted to add User {0} {1} that already exists, updating instead", profile.FirstName, profile.SurName); | ||
114 | UpdateUserProfile(profile); | ||
115 | } | ||
116 | } | ||
117 | |||
118 | private void SetAgentData(LLUUID uuid, UserAgentData agent, ISession session) | ||
119 | { | ||
120 | if (agent == null) | ||
121 | { | ||
122 | // TODO: got to figure out how to do a delete right | ||
123 | } | ||
124 | else | ||
125 | { | ||
126 | UserAgentData old = session.Load(typeof(UserAgentData), uuid) as UserAgentData; | ||
127 | if (old == null) | ||
128 | { | ||
129 | session.Save(agent); | ||
130 | } | ||
131 | else | ||
132 | { | ||
133 | session.Update(agent); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | } | ||
138 | override public bool UpdateUserProfile(UserProfileData profile) | ||
139 | { | ||
140 | if (ExistsUser(profile.ID)) { | ||
141 | using(ISession session = factory.OpenSession()) { | ||
142 | using(ITransaction transaction = session.BeginTransaction()) { | ||
143 | session.Update(profile); | ||
144 | SetAgentData(profile.ID, profile.CurrentAgent, session); | ||
145 | transaction.Commit(); | ||
146 | return true; | ||
147 | } | ||
148 | } | ||
149 | } else { | ||
150 | m_log.ErrorFormat("Attempted to update User {0} {1} that doesn't exist, updating instead", profile.FirstName, profile.SurName); | ||
151 | AddNewUserProfile(profile); | ||
152 | return true; | ||
153 | } | ||
154 | } | ||
155 | |||
156 | override public void AddNewUserAgent(UserAgentData agent) | ||
157 | { | ||
158 | using(ISession session = factory.OpenSession()) { | ||
159 | using(ITransaction transaction = session.BeginTransaction()) { | ||
160 | session.Save(agent); | ||
161 | transaction.Commit(); | ||
162 | } | ||
163 | } | ||
164 | } | ||
165 | |||
166 | public void UpdateUserAgent(UserAgentData agent) | ||
167 | { | ||
168 | using(ISession session = factory.OpenSession()) { | ||
169 | using(ITransaction transaction = session.BeginTransaction()) { | ||
170 | session.Update(agent); | ||
171 | transaction.Commit(); | ||
172 | } | ||
173 | } | ||
174 | } | ||
175 | |||
176 | |||
177 | |||
178 | override public UserAgentData GetAgentByUUID(LLUUID uuid) | ||
179 | { | ||
180 | using(ISession session = factory.OpenSession()) { | ||
181 | return session.Load(typeof(UserAgentData), uuid) as UserAgentData; | ||
182 | } | ||
183 | } | ||
184 | |||
185 | override public UserProfileData GetUserByName(string fname, string lname) | ||
186 | { | ||
187 | using(ISession session = factory.OpenSession()) { | ||
188 | ICriteria criteria = session.CreateCriteria(typeof(UserProfileData)); | ||
189 | criteria.Add(Expression.Eq("FirstName", fname)); | ||
190 | criteria.Add(Expression.Eq("SurName", lname)); | ||
191 | foreach (UserProfileData profile in criteria.List()) | ||
192 | { | ||
193 | profile.CurrentAgent = GetAgentByUUID(profile.ID); | ||
194 | return profile; | ||
195 | } | ||
196 | return null; | ||
197 | } | ||
198 | } | ||
199 | |||
200 | override public UserAgentData GetAgentByName(string fname, string lname) | ||
201 | { | ||
202 | return GetUserByName(fname, lname).CurrentAgent; | ||
203 | } | ||
204 | |||
205 | override public UserAgentData GetAgentByName(string name) | ||
206 | { | ||
207 | return GetAgentByName(name.Split(' ')[0], name.Split(' ')[1]); | ||
208 | } | ||
209 | |||
210 | override public List<AvatarPickerAvatar> GeneratePickerResults(LLUUID queryID, string query) | ||
211 | { | ||
212 | List<AvatarPickerAvatar> results = new List<AvatarPickerAvatar>(); | ||
213 | string[] querysplit; | ||
214 | querysplit = query.Split(' '); | ||
215 | |||
216 | if (querysplit.Length == 2) | ||
217 | { | ||
218 | using(ISession session = factory.OpenSession()) { | ||
219 | ICriteria criteria = session.CreateCriteria(typeof(UserProfileData)); | ||
220 | criteria.Add(Expression.Like("FirstName", querysplit[0])); | ||
221 | criteria.Add(Expression.Like("SurName", querysplit[1])); | ||
222 | foreach(UserProfileData profile in criteria.List()) | ||
223 | { | ||
224 | AvatarPickerAvatar user = new AvatarPickerAvatar(); | ||
225 | user.AvatarID = profile.ID; | ||
226 | user.firstName = profile.FirstName; | ||
227 | user.lastName = profile.SurName; | ||
228 | results.Add(user); | ||
229 | } | ||
230 | } | ||
231 | } | ||
232 | return results; | ||
233 | } | ||
234 | |||
235 | // TODO: actually implement these | ||
236 | public override void UpdateUserCurrentRegion(LLUUID avatarid, LLUUID regionuuid) {return;} | ||
237 | public override void StoreWebLoginKey(LLUUID agentID, LLUUID webLoginKey) {return;} | ||
238 | public override void AddNewUserFriend(LLUUID friendlistowner, LLUUID friend, uint perms) {return;} | ||
239 | public override void RemoveUserFriend(LLUUID friendlistowner, LLUUID friend) {return;} | ||
240 | public override void UpdateUserFriendPerms(LLUUID friendlistowner, LLUUID friend, uint perms) {return;} | ||
241 | public override List<FriendListItem> GetUserFriendList(LLUUID friendlistowner) {return new List<FriendListItem>();} | ||
242 | public override bool MoneyTransferRequest(LLUUID from, LLUUID to, uint amount) {return true;} | ||
243 | public override bool InventoryTransferRequest(LLUUID from, LLUUID to, LLUUID inventory) {return true;} | ||
244 | |||
245 | |||
246 | public override string getName() | ||
247 | { | ||
248 | return Name; | ||
249 | } | ||
250 | |||
251 | public string Name { | ||
252 | get { return "NHibernate"; } | ||
253 | } | ||
254 | |||
255 | public override string GetVersion() | ||
256 | { | ||
257 | return Version; | ||
258 | } | ||
259 | |||
260 | public string Version { | ||
261 | get { return "0.1"; } | ||
262 | } | ||
263 | |||
264 | public void Dispose() | ||
265 | { | ||
266 | |||
267 | } | ||
268 | } | ||
269 | } | ||