diff options
Diffstat (limited to 'OpenSim/Data/NHibernate/NHibernateUserData.cs')
-rw-r--r-- | OpenSim/Data/NHibernate/NHibernateUserData.cs | 155 |
1 files changed, 99 insertions, 56 deletions
diff --git a/OpenSim/Data/NHibernate/NHibernateUserData.cs b/OpenSim/Data/NHibernate/NHibernateUserData.cs index ee0d2b8..3a22dc3 100644 --- a/OpenSim/Data/NHibernate/NHibernateUserData.cs +++ b/OpenSim/Data/NHibernate/NHibernateUserData.cs | |||
@@ -33,7 +33,10 @@ using System.Text.RegularExpressions; | |||
33 | using OpenMetaverse; | 33 | using OpenMetaverse; |
34 | using log4net; | 34 | using log4net; |
35 | using NHibernate; | 35 | using NHibernate; |
36 | using NHibernate.Criterion; | 36 | using NHibernate.Cfg; |
37 | using NHibernate.Expression; | ||
38 | using NHibernate.Mapping.Attributes; | ||
39 | using NHibernate.Tool.hbm2ddl; | ||
37 | using OpenSim.Framework; | 40 | using OpenSim.Framework; |
38 | using Environment=NHibernate.Cfg.Environment; | 41 | using Environment=NHibernate.Cfg.Environment; |
39 | 42 | ||
@@ -46,7 +49,9 @@ namespace OpenSim.Data.NHibernate | |||
46 | { | 49 | { |
47 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 50 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
48 | 51 | ||
49 | private NHibernateManager manager; | 52 | private Configuration cfg; |
53 | private ISessionFactory factory; | ||
54 | private ISession session; | ||
50 | 55 | ||
51 | public override void Initialise() | 56 | public override void Initialise() |
52 | { | 57 | { |
@@ -56,40 +61,62 @@ namespace OpenSim.Data.NHibernate | |||
56 | 61 | ||
57 | public override void Initialise(string connect) | 62 | public override void Initialise(string connect) |
58 | { | 63 | { |
59 | m_log.InfoFormat("[NHIBERNATE] Initializing NHibernateUserData"); | 64 | char[] split = {';'}; |
60 | manager = new NHibernateManager(connect, "UserStore"); | 65 | string[] parts = connect.Split(split, 3); |
66 | if (parts.Length != 3) | ||
67 | { | ||
68 | // TODO: make this a real exception type | ||
69 | throw new Exception("Malformed Inventory connection string '" + connect + "'"); | ||
70 | } | ||
71 | string dialect = parts[0]; | ||
72 | |||
73 | // This is stubbing for now, it will become dynamic later and support different db backends | ||
74 | cfg = new Configuration(); | ||
75 | cfg.SetProperty(Environment.ConnectionProvider, | ||
76 | "NHibernate.Connection.DriverConnectionProvider"); | ||
77 | cfg.SetProperty(Environment.Dialect, | ||
78 | "NHibernate.Dialect." + parts[0]); | ||
79 | cfg.SetProperty(Environment.ConnectionDriver, | ||
80 | "NHibernate.Driver." + parts[1]); | ||
81 | cfg.SetProperty(Environment.ConnectionString, parts[2]); | ||
82 | cfg.AddAssembly("OpenSim.Data.NHibernate"); | ||
83 | |||
84 | factory = cfg.BuildSessionFactory(); | ||
85 | session = factory.OpenSession(); | ||
86 | |||
87 | // This actually does the roll forward assembly stuff | ||
88 | Assembly assem = GetType().Assembly; | ||
89 | Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, "UserStore"); | ||
90 | m.Update(); | ||
61 | } | 91 | } |
62 | 92 | ||
63 | private bool ExistsUser(UUID uuid) | 93 | private bool ExistsUser(UUID uuid) |
64 | { | 94 | { |
65 | UserProfileData user = null; | 95 | UserProfileData user = null; |
66 | 96 | try | |
67 | m_log.InfoFormat("[NHIBERNATE] ExistsUser; {0}", uuid); | ||
68 | user = (UserProfileData)manager.Load(typeof(UserProfileData), uuid); | ||
69 | |||
70 | if (user == null) | ||
71 | { | 97 | { |
72 | m_log.InfoFormat("[NHIBERNATE] User with given UUID does not exist {0} ", uuid); | 98 | user = session.Load(typeof(UserProfileData), uuid) as UserProfileData; |
73 | return false; | 99 | } |
100 | catch (ObjectNotFoundException) | ||
101 | { | ||
102 | user = null; | ||
74 | } | 103 | } |
75 | 104 | ||
76 | return true; | 105 | return (user != null); |
77 | |||
78 | } | 106 | } |
79 | 107 | ||
80 | override public UserProfileData GetUserByUUID(UUID uuid) | 108 | override public UserProfileData GetUserByUUID(UUID uuid) |
81 | { | 109 | { |
82 | UserProfileData user; | 110 | UserProfileData user; |
83 | m_log.InfoFormat("[NHIBERNATE] GetUserByUUID: {0} ", uuid); | 111 | // TODO: I'm sure I'll have to do something silly here |
84 | 112 | try | |
85 | user = (UserProfileData)manager.Load(typeof(UserProfileData), uuid); | ||
86 | if (user != null) | ||
87 | { | 113 | { |
88 | UserAgentData agent = GetAgentByUUID(uuid); | 114 | user = session.Load(typeof(UserProfileData), uuid) as UserProfileData; |
89 | if (agent != null) | 115 | user.CurrentAgent = GetAgentByUUID(uuid); |
90 | { | 116 | } |
91 | user.CurrentAgent = agent; | 117 | catch (ObjectNotFoundException) |
92 | } | 118 | { |
119 | user = null; | ||
93 | } | 120 | } |
94 | 121 | ||
95 | return user; | 122 | return user; |
@@ -99,30 +126,33 @@ namespace OpenSim.Data.NHibernate | |||
99 | { | 126 | { |
100 | if (!ExistsUser(profile.ID)) | 127 | if (!ExistsUser(profile.ID)) |
101 | { | 128 | { |
102 | m_log.InfoFormat("[NHIBERNATE] AddNewUserProfile {0}", profile.ID); | 129 | session.Save(profile); |
103 | manager.Save(profile); | ||
104 | SetAgentData(profile.ID, profile.CurrentAgent); | 130 | SetAgentData(profile.ID, profile.CurrentAgent); |
105 | |||
106 | } | 131 | } |
107 | else | 132 | else |
108 | { | 133 | { |
109 | m_log.ErrorFormat("[NHIBERNATE] Attempted to add User {0} {1} that already exists, updating instead", profile.FirstName, profile.SurName); | 134 | m_log.ErrorFormat("Attempted to add User {0} {1} that already exists, updating instead", profile.FirstName, profile.SurName); |
110 | UpdateUserProfile(profile); | 135 | UpdateUserProfile(profile); |
111 | } | 136 | } |
112 | } | 137 | } |
113 | 138 | ||
114 | private void SetAgentData(UUID uuid, UserAgentData agent) | 139 | private void SetAgentData(UUID uuid, UserAgentData agent) |
115 | { | 140 | { |
116 | UserAgentData old = (UserAgentData)manager.Load(typeof(UserAgentData), uuid); | 141 | if (agent == null) |
117 | if (old != null) | ||
118 | { | 142 | { |
119 | m_log.InfoFormat("[NHIBERNATE] SetAgentData deleting old: {0} ",uuid); | 143 | // TODO: got to figure out how to do a delete right |
120 | manager.Delete(old); | ||
121 | } | 144 | } |
122 | if (agent != null) | 145 | else |
123 | { | 146 | { |
124 | m_log.InfoFormat("[NHIBERNATE] SetAgentData: {0} ", agent.ProfileID); | 147 | try |
125 | manager.Save(agent); | 148 | { |
149 | UserAgentData old = session.Load(typeof(UserAgentData), uuid) as UserAgentData; | ||
150 | session.Delete(old); | ||
151 | } | ||
152 | catch (ObjectNotFoundException) | ||
153 | { | ||
154 | } | ||
155 | session.Save(agent); | ||
126 | } | 156 | } |
127 | 157 | ||
128 | } | 158 | } |
@@ -130,13 +160,13 @@ namespace OpenSim.Data.NHibernate | |||
130 | { | 160 | { |
131 | if (ExistsUser(profile.ID)) | 161 | if (ExistsUser(profile.ID)) |
132 | { | 162 | { |
133 | manager.Update(profile); | 163 | session.Update(profile); |
134 | SetAgentData(profile.ID, profile.CurrentAgent); | 164 | SetAgentData(profile.ID, profile.CurrentAgent); |
135 | return true; | 165 | return true; |
136 | } | 166 | } |
137 | else | 167 | else |
138 | { | 168 | { |
139 | m_log.ErrorFormat("[NHIBERNATE] Attempted to update User {0} {1} that doesn't exist, updating instead", profile.FirstName, profile.SurName); | 169 | m_log.ErrorFormat("Attempted to update User {0} {1} that doesn't exist, updating instead", profile.FirstName, profile.SurName); |
140 | AddNewUserProfile(profile); | 170 | AddNewUserProfile(profile); |
141 | return true; | 171 | return true; |
142 | } | 172 | } |
@@ -144,32 +174,37 @@ namespace OpenSim.Data.NHibernate | |||
144 | 174 | ||
145 | override public void AddNewUserAgent(UserAgentData agent) | 175 | override public void AddNewUserAgent(UserAgentData agent) |
146 | { | 176 | { |
147 | UserAgentData old = (UserAgentData)manager.Load(typeof(UserAgentData), agent.ProfileID); | 177 | try |
148 | if (old != null) | ||
149 | { | 178 | { |
150 | manager.Delete(old); | 179 | UserAgentData old = session.Load(typeof(UserAgentData), agent.ProfileID) as UserAgentData; |
180 | session.Delete(old); | ||
151 | } | 181 | } |
152 | 182 | catch (ObjectNotFoundException) | |
153 | manager.Save(agent); | 183 | { |
154 | 184 | } | |
185 | session.Save(agent); | ||
155 | } | 186 | } |
156 | 187 | ||
157 | public void UpdateUserAgent(UserAgentData agent) | 188 | public void UpdateUserAgent(UserAgentData agent) |
158 | { | 189 | { |
159 | m_log.InfoFormat("[NHIBERNATE] UpdateUserAgent: {0} ", agent.ProfileID); | 190 | session.Update(agent); |
160 | manager.Update(agent); | ||
161 | } | 191 | } |
162 | 192 | ||
163 | override public UserAgentData GetAgentByUUID(UUID uuid) | 193 | override public UserAgentData GetAgentByUUID(UUID uuid) |
164 | { | 194 | { |
165 | m_log.InfoFormat("[NHIBERNATE] GetAgentByUUID: {0} ", uuid); | 195 | try |
166 | return (UserAgentData)manager.Load(typeof(UserAgentData), uuid); | 196 | { |
197 | return session.Load(typeof(UserAgentData), uuid) as UserAgentData; | ||
198 | } | ||
199 | catch | ||
200 | { | ||
201 | return null; | ||
202 | } | ||
167 | } | 203 | } |
168 | 204 | ||
169 | override public UserProfileData GetUserByName(string fname, string lname) | 205 | override public UserProfileData GetUserByName(string fname, string lname) |
170 | { | 206 | { |
171 | m_log.InfoFormat("[NHIBERNATE] GetUserByName: {0} {1} ", fname, lname); | 207 | ICriteria criteria = session.CreateCriteria(typeof(UserProfileData)); |
172 | ICriteria criteria = manager.GetSession().CreateCriteria(typeof(UserProfileData)); | ||
173 | criteria.Add(Expression.Eq("FirstName", fname)); | 208 | criteria.Add(Expression.Eq("FirstName", fname)); |
174 | criteria.Add(Expression.Eq("SurName", lname)); | 209 | criteria.Add(Expression.Eq("SurName", lname)); |
175 | foreach (UserProfileData profile in criteria.List()) | 210 | foreach (UserProfileData profile in criteria.List()) |
@@ -198,7 +233,7 @@ namespace OpenSim.Data.NHibernate | |||
198 | 233 | ||
199 | if (querysplit.Length == 2) | 234 | if (querysplit.Length == 2) |
200 | { | 235 | { |
201 | ICriteria criteria = manager.GetSession().CreateCriteria(typeof(UserProfileData)); | 236 | ICriteria criteria = session.CreateCriteria(typeof(UserProfileData)); |
202 | criteria.Add(Expression.Like("FirstName", querysplit[0])); | 237 | criteria.Add(Expression.Like("FirstName", querysplit[0])); |
203 | criteria.Add(Expression.Like("SurName", querysplit[1])); | 238 | criteria.Add(Expression.Like("SurName", querysplit[1])); |
204 | foreach (UserProfileData profile in criteria.List()) | 239 | foreach (UserProfileData profile in criteria.List()) |
@@ -227,18 +262,26 @@ namespace OpenSim.Data.NHibernate | |||
227 | /// TODO: stubs for now to get us to a compiling state gently | 262 | /// TODO: stubs for now to get us to a compiling state gently |
228 | public override AvatarAppearance GetUserAppearance(UUID user) | 263 | public override AvatarAppearance GetUserAppearance(UUID user) |
229 | { | 264 | { |
230 | return (AvatarAppearance)manager.Load(typeof(AvatarAppearance), user); | 265 | AvatarAppearance appearance; |
266 | // TODO: I'm sure I'll have to do something silly here | ||
267 | try { | ||
268 | appearance = session.Load(typeof(AvatarAppearance), user) as AvatarAppearance; | ||
269 | } catch (ObjectNotFoundException) { | ||
270 | appearance = null; | ||
271 | } | ||
272 | return appearance; | ||
231 | } | 273 | } |
232 | 274 | ||
233 | private bool ExistsAppearance(UUID uuid) | 275 | private bool ExistsAppearance(UUID uuid) |
234 | { | 276 | { |
235 | AvatarAppearance appearance = (AvatarAppearance)manager.Load(typeof(AvatarAppearance), uuid); | 277 | AvatarAppearance appearance; |
236 | if (appearance == null) | 278 | try { |
237 | { | 279 | appearance = session.Load(typeof(AvatarAppearance), uuid) as AvatarAppearance; |
238 | return false; | 280 | } catch (ObjectNotFoundException) { |
281 | appearance = null; | ||
239 | } | 282 | } |
240 | 283 | ||
241 | return true; | 284 | return (appearance == null) ? false : true; |
242 | } | 285 | } |
243 | 286 | ||
244 | 287 | ||
@@ -252,11 +295,11 @@ namespace OpenSim.Data.NHibernate | |||
252 | bool exists = ExistsAppearance(user); | 295 | bool exists = ExistsAppearance(user); |
253 | if (exists) | 296 | if (exists) |
254 | { | 297 | { |
255 | manager.Update(appearance); | 298 | session.Update(appearance); |
256 | } | 299 | } |
257 | else | 300 | else |
258 | { | 301 | { |
259 | manager.Save(appearance); | 302 | session.Save(appearance); |
260 | } | 303 | } |
261 | } | 304 | } |
262 | 305 | ||