initial commit
[CPE_learningsite] / CPE / CPE.App / CPE.App.Web / Connect / PrincipalInfo.cs
1 using System;
2 using System.Xml;
3
4 namespace CPE.App.Web.Connect {
5
6         /// <summary>
7         /// Represents a principal-info object returned from Connect.
8         /// </summary>
9         public class PrincipalInfo {
10                 
11                 /// <summary>
12                 /// The ID of the account the principal belongs to.
13                 /// </summary>
14                 public int AccountId { get; private set; }
15
16                 /// <summary>
17                 /// The date the account was disabled. If the account is active, then the value will be null.
18                 /// </summary>
19                 public DateTime? Disabled { get; private set; }
20
21                 /// <summary>
22                 /// The ID of the principal.
23                 /// </summary>
24                 public int PrincipalId { get; private set; }
25
26                 /// <summary>
27                 /// The principal’s login ID on Connect Enterprise. Can be the same as an e-mail address.
28                 /// </summary>
29                 public string Login { get; private set; }
30
31                 /// <summary>
32                 /// For a user, the e-mail address.
33                 /// </summary>
34                 public string Email { get; private set; }
35
36                 /// <summary>
37                 /// For a user, the full name, concatenated from their first and last name fields.
38                 /// </summary>
39                 public string Name { get; private set; }
40
41                 /// <summary>
42                 /// For a user, the first name.
43                 /// </summary>
44                 public string FirstName { get; private set; }
45
46                 /// <summary>
47                 /// For a user, the last name.
48                 /// </summary>
49                 public string LastName { get; private set; }
50
51
52             public string DisplayName
53             {
54                 get
55                 {
56                     return string.Format("{0} {1}", FirstName, LastName).Trim();
57                 }
58             }
59
60                 /// <summary>
61                 /// Initializes the object by parsing the values from the XmlDocument.
62                 /// </summary>
63                 /// <param name="document">The XmlDocument returned from Connect for this object.</param>
64                 public PrincipalInfo(XmlNode document) {
65
66                         // Find main node
67                         XmlNode principal = document.SelectSingleNode("/results/principal");
68
69                         // Parse values 
70                         AccountId = int.Parse(principal.Attributes["account-id"].Value);
71                         PrincipalId = int.Parse(principal.Attributes["principal-id"].Value);
72                         Login = principal.SelectSingleNode("login").InnerText;
73                         Name = principal.SelectSingleNode("name").InnerText;
74             try
75             {
76                 FirstName = principal.SelectSingleNode("first-name").InnerText;
77             }
78             catch
79             {
80                 FirstName = "";
81             }
82             try
83             {
84                 LastName = principal.SelectSingleNode("last-name").InnerText;
85             }
86             catch
87             {
88                 LastName = "";
89             }
90                     Email = principal.SelectSingleNode("email").InnerText;
91
92                         if(principal.Attributes["disabled"].Value != string.Empty)
93                                 Disabled = DateTime.Parse(principal.Attributes["disabled"].Value);
94                 }
95         }
96 }