2 // Microsoft.TeamFoundation.VersionControl.Client.Item
5 // Joel Reed (joelwreed@gmail.com)
7 // Copyright (C) 2007 Joel Reed
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System
.Collections
;
31 using System
.Collections
.Generic
;
36 using System
.Web
.Services
;
38 namespace Microsoft
.TeamFoundation
.VersionControl
.Client
40 public sealed class Item
42 private string downloadUrl
;
43 private int contentLength
;
44 private int changesetId
;
45 private int deletionId
;
47 private byte[] hashValue
;
49 private DateTime checkinDate
;
50 private ItemType itemType
= ItemType
.File
;
51 private string serverItem
;
52 private VersionControlServer versionControlServer
;
54 internal Item (VersionControlServer versionControlServer
, string serverItem
)
56 this.versionControlServer
= versionControlServer
;
57 this.serverItem
= serverItem
;
60 public void DownloadFile(string localFileName
)
62 if (itemType
== ItemType
.Folder
) return;
64 Client
.DownloadFile
.WriteTo(localFileName
, VersionControlServer
.Repository
,
68 // <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><QueryItemsResponse xmlns="http://schemas.microsoft.com/TeamFoundation/2005/06/VersionControl/ClientServices/03"><QueryItemsResult><ItemSet><QueryPath>$/LSG-1.0</QueryPath><Pattern>*</Pattern><Item cs="7550" date="2006-07-31T18:28:33.933Z" enc="1252" type="File" itemid="41475" item="$/LSG-1.0/configure" hash="KW/KR55BW9qI4Fcg0cEVJQ==" len="146815" durl="sfid=62793,109833,108681,62795,72164,108744,72943,87351,72163,21458&ts=633006048546770184&s=ZNoNAgB8PlCgbs2Lqc4
69 //zH21A4SRFW3edqRcbnRKOeC%2F1IZ02f%2FHe8EdYvcU8PNwnBPhBGSGgaKcQWNjPussH3LgjhxQLzxZsfXQWDAllOnbf%2BrOMQYY30SF9e4R4eUjg1wccIkUpkEMOv1edrteyDu5H5ZjISxHQWhLTJ4OyJQ%3D&fid=72164" /><Item cs="7043" date="2006-06-14T20:33:16.16Z" enc="-3" type="Folder" itemid="41609" item="$/LSG-1.0/db" hash="" />
70 internal static Item
FromXml(Repository repository
, XmlReader reader
)
72 string serverItem
= reader
.GetAttribute("item");
74 Item item
= new Item(repository
.VersionControlServer
, serverItem
);
76 string itype
= reader
.GetAttribute("type");
77 if (!String
.IsNullOrEmpty(itype
))
78 item
.itemType
= (ItemType
) Enum
.Parse(typeof(ItemType
), itype
, true);
80 string sdate
= reader
.GetAttribute("date");
81 if (!String
.IsNullOrEmpty(sdate
))
82 item
.checkinDate
= DateTime
.Parse(sdate
);
84 item
.changesetId
= Convert
.ToInt32(reader
.GetAttribute("cs"));
85 item
.itemId
= Convert
.ToInt32(reader
.GetAttribute("itemid"));
86 item
.encoding
= Convert
.ToInt32(reader
.GetAttribute("enc"));
88 if (item
.ItemType
== ItemType
.File
)
90 item
.contentLength
= Convert
.ToInt32(reader
.GetAttribute("len"));
91 item
.downloadUrl
= reader
.GetAttribute("durl");
93 string hash
= reader
.GetAttribute("hash");
94 if (!String
.IsNullOrEmpty(hash
))
95 item
.hashValue
= Convert
.FromBase64String(hash
);
101 public override string ToString()
103 StringBuilder sb
= new StringBuilder();
105 sb
.Append("Item instance ");
106 sb
.Append(GetHashCode());
108 sb
.Append("\n ItemId: ");
111 sb
.Append("\n CheckinDate: ");
112 sb
.Append(CheckinDate
.ToString("s"));
114 sb
.Append("\n ChangesetId: ");
115 sb
.Append(ChangesetId
);
117 sb
.Append("\n DeletionId: ");
118 sb
.Append(DeletionId
);
120 sb
.Append("\n ItemType: ");
123 sb
.Append("\n ServerItem: ");
124 sb
.Append(ServerItem
);
126 sb
.Append("\n ContentLength: ");
127 sb
.Append(ContentLength
);
129 sb
.Append("\n Download URL: ");
130 sb
.Append(downloadUrl
);
132 sb
.Append("\n Hash: ");
133 string hash
= String
.Empty
;
134 if (HashValue
!= null) hash
= Convert
.ToBase64String(HashValue
);
137 return sb
.ToString();
140 public int ContentLength
142 get { return contentLength; }
145 public ItemType ItemType
147 get { return itemType; }
150 public DateTime CheckinDate
152 get { return checkinDate; }
155 public int ChangesetId
157 get { return changesetId; }
160 public int DeletionId
162 get { return deletionId; }
167 get { return encoding; }
172 get { return itemId; }
175 public byte[] HashValue
177 get { return hashValue; }
180 public Uri ArtifactUri
184 if (String
.IsNullOrEmpty(downloadUrl
))
186 Item item
= VersionControlServer
.GetItem(ItemId
, ChangesetId
, true);
187 downloadUrl
= item
.downloadUrl
;
190 return new Uri(String
.Format("{0}?{1}", VersionControlServer
.Repository
.ItemUrl
, downloadUrl
));
194 public string ServerItem
196 get { return serverItem; }
199 public VersionControlServer VersionControlServer
201 get { return versionControlServer; }
204 internal class ItemGenericComparer
: IComparer
<Item
>
206 public static ItemGenericComparer Instance
= new ItemGenericComparer();
208 public int Compare(Item x
, Item y
)
210 return x
.ServerItem
.CompareTo(y
.ServerItem
);
214 internal static IComparer
<Item
> GenericComparer
{
216 return ItemGenericComparer
.Instance
;
220 internal class ItemComparer
: IComparer
222 public static ItemComparer Instance
= new ItemComparer();
224 public int Compare(object x
, object y
)
226 Item itemX
= (Item
)x
;
227 Item itemY
= (Item
)y
;
228 return itemX
.ServerItem
.CompareTo(itemY
.ServerItem
);
232 public static IComparer Comparer
{
234 return ItemComparer
.Instance
;