-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyncService.cs
327 lines (322 loc) · 19.9 KB
/
SyncService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
using System;
using System.Data;
using System.ServiceProcess;
using System.Timers;
using System.IO;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.Net;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
namespace TfsSyncService
{
public partial class SyncService : ServiceBase
{
System.Timers.Timer timer;
//ArrayList TeamProjectArrayList;
public SyncService()
{
try
{
InitializeComponent();
timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(TfsSync);
int time = Convert.ToInt32(ConfigurationManager.AppSettings["Time"]);
if (time < 1) time = 1;
timer.Interval = time * 60 * 1000;
timer.Enabled = true;
}
catch (ObjectDisposedException) { ServiceEventLog("Sync cannot be started - N1"); }
catch (FormatException)
{
ServiceEventLog("Invalid time format - N1");
timer.Stop();
timer.Dispose();
}
catch (OverflowException)
{
ServiceEventLog("Time exceeded the limit - N1");
timer.Stop();
timer.Dispose();
}
catch (ConfigurationErrorsException) { ServiceEventLog("No valid configuration file found - N1"); }
catch (Exception e){ ServiceEventLog("Something went wrong - N1"+e); }
}
protected override void OnStart(string[] args)
{
ServiceEventLog("Starting service");
timer.Start();
}
private void TfsSync(object sender, ElapsedEventArgs e)
{
//GetCollections();
//ServiceEventLog("###Synced collections###");
//GetProjects();
GetProjectsAndCollections();
ServiceEventLog("###Synced projects and collections###");
GetWorkitems();
ServiceEventLog("###Synced workitems###");
SyncWorkitems();
ServiceEventLog("###Synced user workitems###");
// SyncHistory();
// ServiceEventLog("###Synced history###");
ServiceEventLog("@@@Sync step completed@@@");
ServiceEventLog("------------------------------------------------------------------");
}
//private void GetCollections()
//{
// try
// {
// String CollectionQuery = "select ProjectNodeName from Tfs_Warehouse.dbo.DimTeamProject where Depth=0";
// SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["TfsDataSrc"]);
// cn.Open();
// SqlCommand cmd = new SqlCommand(CollectionQuery, cn);
// SqlDataAdapter da = new SqlDataAdapter(cmd);
// DataSet ds = new DataSet();
// da.Fill(ds, "Collections");
// CollectionAL = new ArrayList();
// foreach (DataRow row in ds.Tables["Collections"].Rows)
// {
// String dbname = row["ProjectNodeName"].ToString();
// CollectionAL.Add(dbname);
// ServiceEventLog(dbname);
// }
// }
// catch (ConfigurationErrorsException) { ServiceEventLog("No valid configuration file found - N2"); }
// catch (SqlException e) { ServiceEventLog("Database access error - N2"+e); }
// catch (Exception e){ ServiceEventLog("Something went wrong - N2"+e); }
//}
private void GetProjectsAndCollections()
{
//TeamProjectArrayList = new ArrayList();
try
{
String ProQuery = "select p.ParentNodeSK as CollectionId,p.ProjectNodeSK as ProjectId,c.ProjectNodeName as CollectionName,p.ProjectNodeName as ProjectName from dbo.DimTeamProject p,Tfs_Warehouse.dbo.DimTeamProject c where p.ParentNodeSK=c.ProjectNodeSK and p.Depth=1";
SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["TfsDataSrc"]);
cn.Open();
SqlCommand cmd = new SqlCommand(ProQuery, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Projects");
foreach (DataRow row in ds.Tables["Projects"].Rows)
{
String CollectionName = row["CollectionName"].ToString();
String ProjectName = row["ProjectName"].ToString();
int CollectionId = Convert.ToInt32(row["CollectionId"]);
int ProjectId = Convert.ToInt32(row["ProjectId"]);
//TeamProject TP = new TeamProject(CollectionId,CollectionName,ProjectName,ProjectId);
//TeamProjectArrayList.Add(TP);
//?
String AddProColQuery = "insert into dbo.TfsProjects(CollectionId,CollectionName,ProjectName,ProjectId) values(" + CollectionId + ",'" + CollectionName + "','" + ProjectName + "'," + ProjectId +")";
cn = new SqlConnection(ConfigurationManager.AppSettings["CustDataSrc"]);
cn.Open();
cmd = new SqlCommand(AddProColQuery, cn);
try
{
int x = cmd.ExecuteNonQuery();
ServiceEventLog("Inserting " + CollectionName + "-" + ProjectName);
}
catch (InvalidOperationException) { ServiceEventLog("Error inserting " + CollectionName + "-" + ProjectName); }
catch (SqlException) { ServiceEventLog(CollectionName + "-" + ProjectName + " already exists"); }
}
}
catch (ConfigurationErrorsException) { ServiceEventLog("No valid configuration file found - N3"); }
catch (SqlException e) { ServiceEventLog("Database access error - N3"+e); }
catch (Exception e){ ServiceEventLog("Something went wrong - N3"+ e); }
}
//private void SyncHistory()
//{
// for (int i = 0; i < CollectionAL.Count; i++)
// {
// String HQuery = "select Title,[Changed Date],Rev,State,Reason,ID,"+
// "(select distinct(NamePart) from Tfs_" + CollectionAL[i] + ".dbo.Constants,Tfs_" + CollectionAL[i] + ".dbo.WorkItemsAre where ConstID=a.[Changed By]) as [Changed By],"+
// "(select distinct(NamePart) from Tfs_" + CollectionAL[i] + ".dbo.Constants,Tfs_" + CollectionAL[i] + ".dbo.WorkItemsAre where ConstID=a.[Assigned To]) as [Assigned To] " +
// "FROM Tfs_" + CollectionAL[i] + ".dbo.WorkItemsWere a";
// try
// {
// SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["TfsDataSrc"]);
// cn.Open();
// SqlCommand cmd = new SqlCommand(HQuery, cn);
// SqlDataAdapter da = new SqlDataAdapter(cmd);
// DataSet ds = new DataSet();
// da.Fill(ds, "WorkitemHistory");
// for (int j = 0; j < ds.Tables["WorkitemHistory"].Rows.Count; j++)
// {
// DataRow dr = ds.Tables["WorkitemHistory"].Rows[j];
// string AddHistoryQuery = "insert into Mydb.dbo.TfsWorkitemHistory(Title,State,Reason,[Assigned To],[Changed Date],[Changed By],Version) " +
// "values('"+dr["Title"]+"','"+dr["State"]+"','"+dr["Reason"]+"','"+dr["Assigned To"]+"','"+dr["Changed Date"]+"','"+dr["Changed By"]+"',"+dr["Rev"]+")";
// cn = new SqlConnection(ConfigurationManager.AppSettings["CustDataSrc"]);
// cn.Open();
// cmd = new SqlCommand(AddHistoryQuery, cn);
// try
// {
// int x = cmd.ExecuteNonQuery();
// ServiceEventLog("inserting history - " + dr["Title"]);
// }
// catch (InvalidOperationException) { ServiceEventLog("Error inserting " + dr["Title"]); }
// catch (SqlException) { ServiceEventLog(dr["Title"] + " already exists"); }
// cn.Close();
// }
// cn.Close();
// }
// catch (ConfigurationErrorsException) { ServiceEventLog("No valid configuration file found - N6"); }
// catch (SqlException e) { ServiceEventLog("Database access error - N6" + e); }
// catch (Exception e) { ServiceEventLog("Something went wrong - N6" + e); }
// }
//}
private void SyncWorkitems()
{
String SQuery = "select ID,Title,[Type],[Desc],CollectionName,ProjectName from dbo.TfsProjects,dbo.TfsWorkitems where Project=ProjectId and Sync=0";
try
{
SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["CustDataSrc"]);
cn.Open();
SqlCommand cmd = new SqlCommand(SQuery, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Workitems");
foreach (DataRow row in ds.Tables["Workitems"].Rows)
{
String uri = ConfigurationManager.AppSettings["TfsUri"] + row["CollectionName"];
Uri collectionUri = new Uri(uri);
NetworkCredential myNetCredentials = new NetworkCredential(ConfigurationManager.AppSettings["TfsUsername"], ConfigurationManager.AppSettings["TfsPassword"]);
ICredentials myCredentials = (ICredentials)myNetCredentials;
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(collectionUri, myCredentials);
WorkItemStore workItemStore = tpc.GetService<WorkItemStore>();
Project teamProject = workItemStore.Projects[row["ProjectName"].ToString()];
WorkItemType workItemType = teamProject.WorkItemTypes[row["Type"].ToString()];
WorkItem workItem = new WorkItem(workItemType);
workItem.Title = row["Title"].ToString();
workItem.Description = row["Desc"].ToString();
workItem.Save();
cmd = new SqlCommand("update dbo.TfsWorkitems set Sync=1,TfsId=" + workItem.Id + " where ID=" + row["ID"], cn);
try
{
int x = cmd.ExecuteNonQuery();
ServiceEventLog("Status - " + row["Title"] + " synced");
}
catch (InvalidOperationException) { ServiceEventLog("Error updating sync status"); }
}
}
catch (ConfigurationErrorsException) { ServiceEventLog("No valid configuration file found - N4"); }
catch (SqlException e) { ServiceEventLog("Database access error - N4"+e); }
catch (Exception e){ ServiceEventLog("Something went wrong - N4"+e); }
}
private void GetWorkitems()
{
try
{
//String AddWIQ = "select State,Reason,[Assigned To],Title,[Work Item Type],Words " +
// "from Tfs_" + Collection + ".dbo.WorkItemsAre a,Tfs_" + Collection + ".dbo.xxTree b,Tfs_" + Collection + ".dbo.WorkItemLongTexts c " +
// "where a.AreaID=b.ID and a.ID=c.ID and b.[Node Name]='" + Project + "'";
//String AddWIQ = "select State,Reason,[Assigned To],Title,[Work Item Type],Words "+
// "from Tfs_"+Collection+".dbo.xxTree b right join Tfs_"+Collection+".dbo.WorkItemsAre a left join Tfs_"+Collection+".dbo.WorkItemLongTexts c "+
// "on a.ID=c.ID on a.AreaID=b.ID where b.[Node Name]='"+Project+"'";
//String AddWIQ = "select [Changed Date],Rev,State,Reason,NamePart as [Assigned To],[Created By],[Created Date],[Changed By],Title,[Work Item Type],Words " +
// "from "+
// "(select a.[Changed Date],a.Rev,State,Reason,[Assigned To],[Created By],[Created Date],[Changed By],Title,[Work Item Type],Words "+
// "from Tfs_"+Collection+".dbo.xxTree b right join Tfs_"+Collection+".dbo.WorkItemsAre a left join Tfs_"+Collection+".dbo.WorkItemLongTexts c "+
// "on a.ID=c.ID on a.AreaID=b.ID where b.[Node Name]='"+Project+"') d,Tfs_"+Collection+".dbo.Constants e "+
// "where d.[Assigned To]=e.ConstID";
//String AddWIQ = "select a.ID,a.[Changed Date],a.Rev,State,Reason,"+
// "(select distinct(NamePart) from Tfs_" + CollectionName + ".dbo.Constants,Tfs_" + CollectionName + ".dbo.WorkItemsAre where ConstID=a.[Assigned To]) as [Assigned To]," +
// "(select distinct(NamePart) from Tfs_" + CollectionName + ".dbo.Constants,Tfs_" + CollectionName + ".dbo.WorkItemsAre where ConstID=a.[Created By]) as [Created By],[Created Date]," +
// "(select distinct(NamePart) from Tfs_" + CollectionName + ".dbo.Constants,Tfs_" + CollectionName + ".dbo.WorkItemsAre where ConstID=a.[Changed By]) as [Changed By],Title,[Work Item Type],Words " +
// "from Tfs_" + CollectionName + ".dbo.xxTree b right join Tfs_" + CollectionName + ".dbo.WorkItemsAre a left join Tfs_" + CollectionName + ".dbo.WorkItemLongTexts c " +
// "on a.ID=c.ID on a.AreaID=b.ID where b.[Node Name]='"+ProjectName+"'";
String AddWIQ = "SELECT [System_Id] as ID"+
",[TeamProjectCollectionSK] as [Collection]"+
",[TeamProjectSK] as [Project]"+
",(select [Name] from [dbo].[DimPerson] where [PersonSK]=[System_AssignedTo__PersonSK]) as [Assigned To]"+
",(select [Name] from [dbo].[DimPerson] where [PersonSK]=[System_ChangedBy__PersonSK]) as [Changed By]"+
",(select [Name] from [dbo].[DimPerson] where [PersonSK]=[System_CreatedBy__PersonSK]) as [Created By]"+
",[System_WorkItemType] as [Work Item Type]"+
",[System_Title] as Title"+
",[System_ChangedDate] as [Changed Date]"+
",[System_State] as State"+
",[System_Rev] as [Rev]"+
",[System_Reason] as Reason"+
",[System_CreatedDate] as [Created Date]"+
"FROM [dbo].[DimWorkItem]"+
"WHERE [System_Rev]=1";
SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["TfsDataSrc"]);
cn.Open();
SqlCommand cmd = new SqlCommand(AddWIQ, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Workitems");
foreach (DataRow row in ds.Tables["Workitems"].Rows)
{
String AddWI = "insert into dbo.TfsWorkitems(Project,Type,Title,[Assigned To],[Created By],[Created Date],[Changed By],[Changed Date],State,Reason,Version,Sync,TfsId) " +
"values(" + row["Project"] + ",'" + row["Work Item Type"] + "','" + row["Title"] + "','" +
row["Assigned To"] + "','" + row["Created By"] + "','" + DateTime.Parse(row["Created Date"].ToString()).ToLongDateString() + "','" + row["Changed By"] + "','" + DateTime.Parse(row["Changed Date"].ToString()).ToLongDateString() + "','" +
row["State"] + "','" + row["Reason"] + "'," + row["Rev"] + ",1,"+row["ID"]+")";
cn = new SqlConnection(ConfigurationManager.AppSettings["CustDataSrc"]);
cn.Open();
cmd = new SqlCommand(AddWI, cn);
try
{
int x = cmd.ExecuteNonQuery();
ServiceEventLog("Inserting " + row["Title"]);
}
catch(InvalidOperationException)
{
ServiceEventLog("Error inserting " + row["Title"]);
String UpdateWI = "update dbo.TfsWorkitems " +
"set State='" + row["State"] + "',Reason='" + row["Reason"] + "',[Assigned To]='" + row["Assigned To"] + "',"+
"[Changed By]='" + row["Changed By"] + "',[Changed Date]='" + DateTime.Parse(row["Changed Date"].ToString()).ToLongDateString() + "',Version=" + row["Rev"] +
" where TdsId=" + row["ID"];
cmd = new SqlCommand(UpdateWI, cn);
try
{
int x = cmd.ExecuteNonQuery();
ServiceEventLog("Updating " + row["Title"]);
}
catch (InvalidOperationException) { ServiceEventLog("Error updating " + row["Title"]); }
catch (SqlException) { ServiceEventLog("Error updating " + row["Title"]); }
}
catch (SqlException)
{
ServiceEventLog("Error inserting " + row["Title"]);
String UpdateWI = "update dbo.TfsWorkitems " +
"set State='" + row["State"] + "',Reason='" + row["Reason"] + "',[Assigned To]='" + row["Assigned To"] + "'," +
"[Changed By]='" + row["Changed By"] + "',[Changed Date]='" + DateTime.Parse(row["Changed Date"].ToString()).ToLongDateString() + "',Version=" + row["Rev"] +
" where TdsId=" + row["ID"];
cmd = new SqlCommand(UpdateWI, cn);
try
{
int x = cmd.ExecuteNonQuery();
ServiceEventLog("Updating " + row["Title"]);
}
catch (InvalidOperationException) { ServiceEventLog("Error updating " + row["Title"]); }
catch (SqlException) { ServiceEventLog("Error updating " + row["Title"]); }
}
}
}
catch (ConfigurationErrorsException) { ServiceEventLog("No valid configuration file found - N5"); }
catch (SqlException e) { ServiceEventLog("Database access error - N5"+e); }
catch { ServiceEventLog("Something went wrong - N5"); }
}
protected override void OnStop()
{
timer.Stop();
timer.Dispose();
ServiceEventLog("Stopping service");
ServiceEventLog("------------------------------------------------------------------");
}
private void ServiceEventLog(String log)
{
try
{
FileStream fs = new FileStream("TfsSyncServiceLog.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine(DateTime.Now + " : "+ log +".");
sw.Flush();
sw.Close();
}
catch { }
}
}
}