New test.
[mono.git] / mcs / class / System.Web / System.Web.Profile / ProfileBase.cs
1 //
2 // System.Web.UI.WebControls.ProfileBase.cs
3 //
4 // Authors:
5 //      Chris Toshok (toshok@ximian.com)
6 //      Vladimir Krasnov (vladimirk@mainsoft.com)
7 //
8 // (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if NET_2_0
31 using System;
32 using System.Configuration;
33 using System.Configuration.Provider;
34 using System.Web.Configuration;
35
36 namespace System.Web.Profile
37 {
38         public class ProfileBase : SettingsBase
39         {
40                 bool _propertiyValuesLoaded = false;
41                 bool _dirty = false;
42                 DateTime _lastActivityDate = DateTime.MinValue;
43                 DateTime _lastUpdatedDate = DateTime.MinValue;
44                 SettingsContext _settingsContext = null;
45                 SettingsPropertyValueCollection _propertiyValues = null;
46
47 #if TARGET_J2EE
48                 const string Profiles_SettingsPropertyCollection = "Profiles.SettingsPropertyCollection";
49                 static SettingsPropertyCollection _properties
50                 {
51                         get
52                         {
53                                 object o = AppDomain.CurrentDomain.GetData (Profiles_SettingsPropertyCollection);
54                                 return (SettingsPropertyCollection) o;
55                         }
56                         set
57                         {
58                                 AppDomain.CurrentDomain.SetData (Profiles_SettingsPropertyCollection, value);
59                         }
60                 }
61 #else
62                 static SettingsPropertyCollection _properties = null;
63 #endif
64
65                 static void InitProperties ()
66                 {
67                         _properties = new SettingsPropertyCollection ();
68
69                         ProfileSection config = (ProfileSection) WebConfigurationManager.GetSection ("system.web/profile");
70                         RootProfilePropertySettingsCollection ps = config.PropertySettings;
71
72                         for (int i = 0; i < ps.GroupSettings.Count; i++) {
73                                 ProfileGroupSettings pgs = ps.GroupSettings [i];
74                                 ProfilePropertySettingsCollection ppsc = pgs.PropertySettings;
75
76                                 for (int s = 0; s < ppsc.Count; s++) {
77                                         _properties.Add (CreateSettingsPropery (pgs, ppsc [s]));
78                                 }
79                         }
80
81                         for (int s = 0; s < ps.Count; s++)
82                                 _properties.Add (CreateSettingsPropery (null, ps [s]));
83                         
84                         _properties.SetReadOnly ();
85                 }
86                 
87                 public ProfileBase ()
88                 {
89                 }
90
91                 public static ProfileBase Create (string username)
92                 {
93                         return Create (username, true);
94                 }
95
96                 public static ProfileBase Create (string username, bool isAuthenticated)
97                 {
98                         ProfileBase profile = null;
99                         Type profileType = ProfileParser.GetProfileCommonType (HttpContext.Current);
100                         if (profileType != null)
101                                 profile = (ProfileBase) Activator.CreateInstance (profileType);
102                         else
103                                 profile = (ProfileBase) new DefaultProfile ();
104
105                         profile.Initialize (username, isAuthenticated);
106                         return profile;
107                 }
108
109                 public ProfileGroupBase GetProfileGroup (string groupName)
110                 {
111                         ProfileGroupBase group = null;
112                         Type groupType = ProfileParser.GetProfileGroupType (HttpContext.Current, "ProfileGroup" + groupName);
113                         if (groupType != null)
114                                 group = (ProfileGroupBase) Activator.CreateInstance (groupType);
115                         else
116                                 throw new ProviderException ("Group " + groupName + " not found");
117
118                         group.Init (this, groupName);
119                         return group;
120                 }
121
122                 public object GetPropertyValue (string propertyName)
123                 {
124                         if (!_propertiyValuesLoaded)
125                                 InitPropertiesValues ();
126
127                         _lastActivityDate = DateTime.UtcNow;
128
129                         return ((SettingsPropertyValue) _propertiyValues [propertyName]).PropertyValue;
130                 }
131
132                 public void SetPropertyValue (string propertyName, object propertyValue)
133                 {
134                         if (!_propertiyValuesLoaded)
135                                 InitPropertiesValues ();
136
137                         if (_propertiyValues [propertyName] == null)
138                                 throw new SettingsPropertyNotFoundException ("The settings property '" + propertyName + "' was not found.");
139
140                         ((SettingsPropertyValue) _propertiyValues [propertyName]).PropertyValue = propertyValue;
141                         _dirty = true;
142                         _lastActivityDate = DateTime.UtcNow;
143                         _lastUpdatedDate = _lastActivityDate;
144                 }
145
146                 public override object this [string propertyName]
147                 {
148                         get
149                         {
150                                 return GetPropertyValue (propertyName);
151                         }
152                         set
153                         {
154                                 SetPropertyValue (propertyName, value);
155                         }
156                 }
157
158                 void InitPropertiesValues ()
159                 {
160                         if (!_propertiyValuesLoaded) {
161                                 _propertiyValues = ProfileManager.Provider.GetPropertyValues (_settingsContext, Properties);
162                                 _propertiyValuesLoaded = true;
163                         }
164                 }
165
166                 static SettingsProperty CreateSettingsPropery (ProfileGroupSettings pgs, ProfilePropertySettings pps)
167                 {
168                         string name = ((pgs == null) ? "" : pgs.Name + ".") + pps.Name;
169                         SettingsProperty sp = new SettingsProperty (name);
170
171                         sp.Attributes.Add ("AllowAnonymous", pps.AllowAnonymous);
172                         sp.DefaultValue = pps.DefaultValue;
173                         sp.IsReadOnly = pps.ReadOnly;
174                         sp.Provider = ProfileManager.Provider;
175                         sp.ThrowOnErrorDeserializing = false;
176                         sp.ThrowOnErrorSerializing = true;
177
178                         if (pps.Type.Length == 0 || pps.Type == "string")
179                                 sp.PropertyType = typeof (string);
180                         else
181                                 sp.PropertyType = Type.GetType (pps.Type);
182
183                         switch (pps.SerializeAs) {
184                                 case SerializationMode.Binary:
185                                         sp.SerializeAs = SettingsSerializeAs.Binary;
186                                         break;
187                                 case SerializationMode.ProviderSpecific:
188                                         sp.SerializeAs = SettingsSerializeAs.ProviderSpecific;
189                                         break;
190                                 case SerializationMode.String:
191                                         sp.SerializeAs = SettingsSerializeAs.String;
192                                         break;
193                                 case SerializationMode.Xml:
194                                         sp.SerializeAs = SettingsSerializeAs.Xml;
195                                         break;
196                         }
197
198                         return sp;
199                 }
200
201
202                 public void Initialize (string username, bool isAuthenticated)
203                 {
204                         _settingsContext = new SettingsContext ();
205                         _settingsContext.Add ("UserName", username);
206                         _settingsContext.Add ("IsAuthenticated", isAuthenticated);
207                 }
208
209                 public override void Save ()
210                 {
211                         if (IsDirty) {
212                                 ProfileManager.Provider.SetPropertyValues (_settingsContext, _propertiyValues);
213                         }
214                 }
215
216                 public bool IsAnonymous {
217                         get {
218                                 return !(bool) _settingsContext ["IsAuthenticated"];
219                         }
220                 }
221
222                 public bool IsDirty {
223                         get {
224                                 return _dirty;
225                         }
226                 }
227
228                 public DateTime LastActivityDate {
229                         get {
230                                 return _lastActivityDate;
231                         }
232                 }
233
234                 public DateTime LastUpdatedDate {
235                         get {
236                                 return _lastUpdatedDate;
237                         }
238                 }
239
240                 public static SettingsPropertyCollection Properties {
241                         get {
242                                 if (_properties == null)
243                                         InitProperties ();
244
245                                 return _properties;
246                         }
247                 }
248
249                 public string UserName {
250                         get {
251                                 return (string) _settingsContext ["UserName"];
252                         }
253                 }
254         }
255
256 }
257
258 #endif