New test.
[mono.git] / mcs / class / System.DirectoryServices / System.DirectoryServices / SearchResult.cs
1 /******************************************************************************
2 * The MIT License
3 * Copyright (c) 2003 Novell Inc.,  www.novell.com
4
5 * Permission is hereby granted, free of charge, to any person obtaining  a copy
6 * of this software and associated documentation files (the Software), to deal
7 * in the Software without restriction, including  without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
9 * copies of the Software, and to  permit persons to whom the Software is 
10 * furnished to do so, subject to the following conditions:
11
12 * The above copyright notice and this permission notice shall be included in 
13 * all copies or substantial portions of the Software.
14
15 * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *******************************************************************************/
23
24 //
25 // System.DirectoryServices.SearchResult.cs
26 //
27 // Author:
28 //   Sunil Kumar (sunilk@novell.com)
29 //
30 // (C)  Novell Inc.
31 //
32
33 using System.ComponentModel;
34 using Novell.Directory.Ldap;
35 using System.Collections.Specialized;
36
37 namespace System.DirectoryServices
38 {
39         
40         /// <summary>
41         ///Encapsulates a node or object in the Ldap Directory hierarchy.
42         /// </summary>
43     public class SearchResult
44         {
45
46                 private string _Path=null;
47                 private ResultPropertyCollection _Properties=null;
48                 private DirectoryEntry _Entry=null;
49                 private StringCollection _PropsToLoad=null;
50                 private bool ispropnull=true;
51                 private PropertyCollection _Rproperties = null;
52
53                 internal PropertyCollection Rproperties
54                 {
55                         get
56                         {
57                                 return _Rproperties;
58                         }
59                 }
60
61                 private void InitBlock()
62                 {
63                         _Properties=null;
64                         _Entry=null;
65                         _PropsToLoad=null;
66                         ispropnull=true;
67                         _Rproperties=null;
68                 }
69
70                 internal StringCollection PropsToLoad
71                 {
72                         get
73                         {
74                                 if( _PropsToLoad != null )
75                                 {
76                                         return _PropsToLoad;
77                                 }
78                                 else
79                                         return null;
80                         }
81                 }
82                 /// <summary>
83                 /// Gets a ResultPropertyCollection of properties set on this object.
84                 /// </summary>
85                 /// <value>
86                 /// A ResultPropertyCollection of properties set on this object.
87                 /// </value>
88                 /// <remarks>
89                 /// This collection only contains properties that were explicitly 
90                 /// requested through DirectorySearcher.PropertiesToLoad.
91                 /// </remarks>
92                 public ResultPropertyCollection Properties
93                 {
94                         get
95                         {
96                                 if ( ispropnull )
97                                 {
98                                         _Properties= new ResultPropertyCollection();
99                                         System.Collections.IDictionaryEnumerator id = 
100                                                 Rproperties.GetEnumerator();
101 //                                              _Entry.Properties.GetEnumerator();
102                                         while(id.MoveNext())
103                                         {
104                                                 string attribute=(string)id.Key;
105                                                         ResultPropertyValueCollection rpVal=
106                                                                 new ResultPropertyValueCollection();
107                                                         if(Rproperties[attribute].Count==1)
108                                                         {
109                                                                 String val = (String)Rproperties[attribute].Value;
110                                                                 rpVal.Add(val);
111                                                         }
112                                                         else if (Rproperties[attribute].Count > 1)
113                                                         {
114                                                                 Object[] vals=(Object [])Rproperties[attribute].Value;
115 //                                                              String[] aStrVals= new String[_Entry.Properties[attribute].Count];
116                                                                 rpVal.AddRange(vals);
117                                                         }
118                                                         _Properties.Add(attribute,rpVal);
119                                         }
120                                         ispropnull=false;
121                                 }
122                                 return _Properties;
123                         }
124                 }
125
126                 internal SearchResult(DirectoryEntry entry)
127                 {
128                         InitBlock();
129                         _Entry = entry;
130                         _Path = entry.Path;
131                 }
132
133                 internal SearchResult(DirectoryEntry entry, PropertyCollection props)
134                 {
135                         InitBlock();
136                         _Entry = entry;
137                         _Path = entry.Path;
138                         _Rproperties = props;
139                 }
140                 /// <summary>
141                 /// Gets the path for this SearchResult.
142                 /// </summary>
143                 /// <value>
144                 /// The path of this SearchResult.
145                 /// </value>
146                 /// <remarks>
147                 /// The Path property uniquely identifies this entry in the Active 
148                 /// Directory hierarchy. The entry can always be retrieved using this 
149                 /// path
150                 /// </remarks>
151                 public string Path 
152                 {
153                         get
154                         {
155                                 return _Path;
156                         }
157                 }
158
159                 /// <summary>
160                 /// Retrieves the DirectoryEntry that corresponds to the SearchResult, 
161                 /// from the Active Directory hierarchy.
162                 /// </summary>
163                 /// <returns>
164                 /// The DirectoryEntry that corresponds to the SearchResult
165                 /// </returns>
166                 /// <remarks>
167                 /// Use GetDirectoryEntry when you want to look at the live entry 
168                 /// instead of the entry returned through DirectorySearcher, or when 
169                 /// you want to invoke a method on the object that was returned.
170                 /// </remarks>
171                 public DirectoryEntry GetDirectoryEntry()
172                 {
173                         return _Entry;
174                 }
175
176         }
177 }
178