📄 serviceutility.cs
字号:
namespace ASPNET.StarterKit.Communities.Services {
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Caching;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
//*********************************************************************
//
// ServiceUtility Class
//
// Contains static methods for working with both community and RSS
// services.
//
//*********************************************************************
public class ServiceUtility {
//*********************************************************************
//
// GetAllServiceSubscriptions Method
//
// This method is used in the EditServiceSubscriptions section of
// the admin pages.
//
//*********************************************************************
public static ArrayList GetAllServiceSubscriptions() {
ArrayList colServiceSubscriptions = new ArrayList();
SqlConnection con = new SqlConnection(CommunityGlobals.ConnectionString);
SqlCommand cmd = new SqlCommand("Community_ServicesGetAllServiceSubscriptions", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
colServiceSubscriptions.Add(new ServiceSubscriptionInfo(dr));
con.Close();
return colServiceSubscriptions;
}
//*********************************************************************
//
// GetServiceSubscription Method
//
// This method is used in the EditServiceSubscriptions section of
// the admin pages.
//
//*********************************************************************
public static ServiceSubscriptionInfo GetServiceSubscriptionInfo(string subscriptionName) {
ServiceSubscriptionInfo serviceInfo = null;
SqlConnection con = new SqlConnection(CommunityGlobals.ConnectionString);
SqlCommand cmd = new SqlCommand("Community_ServicesGetServiceSubscription", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
cmd.Parameters.Add("@subscriptionName", subscriptionName);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
serviceInfo = new ServiceSubscriptionInfo(dr);
con.Close();
return serviceInfo;
}
//*********************************************************************
//
// GetRemoteContent Method
//
// This method is kicked off by the timer in the Global.asax file.
// It grabs the list of subscriptions for all communities and retrieves
// the remote community content updating the local database.
//
//*********************************************************************
public static void GetRemoteContent() {
// Get all the subscriptions (for all communities)
SqlDataAdapter dad = new SqlDataAdapter("Community_ServicesGetServiceSubscriptions", CommunityGlobals.ConnectionString);
dad.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet dstServices = new DataSet();
dad.Fill(dstServices);
// Now loop, calling each remote service
foreach (DataRow serviceRow in dstServices.Tables[0].Rows)
GetRemoteServiceContent(serviceRow);
}
private static void GetRemoteServiceContent(DataRow serviceRow) {
// Retrieve remote content through CommunityService
Client.CommunityService objService = new Client.CommunityService();
objService.Url = (string)serviceRow["ss_url"];
// set the password
string password = (string)serviceRow["ss_password"];
if (password.Trim() != String.Empty) {
Client.SecurityHeader objHeader = new Client.SecurityHeader();
objHeader.Password = password;
objService.SecurityHeaderValue = objHeader;
}
// call the service
DataSet dstContent;
try {
dstContent = objService.GetFullCommunityContent
(
(string)serviceRow["communityName"],
(string)serviceRow["sectionPageType"],
(Byte[])serviceRow["lastTimeStamp"]
);
} catch (Exception ex) {
ActivityUtility.RecordError
(
(int)serviceRow["ss_communityID"],
String.Format("Error retrieving remote content"),
ex
);
return;
}
// Multiple DataTables may have been returned (e.g., both Articles and Comments)
// So we need to iterate through the DataTables to get a total number of records
int recordCount = 0;
foreach (DataTable table in dstContent.Tables)
recordCount += table.Rows.Count;
// update skip count
UpdateSkipCount((int)serviceRow["ss_ID"], recordCount);
// Record Get Content Results
ActivityUtility.RecordMessage
(
(int)serviceRow["ss_communityID"],
String.Format("Retrieving content from {0}", (string)serviceRow["ss_url"]),
String.Format("Retrieved {0} records", recordCount)
);
// okay, now we iterate through each datatable to add the content
foreach (DataTable table in dstContent.Tables)
AddRemoteContent
(
(int)serviceRow["ss_communityID"], // The community to add the content to
(int)serviceRow["sss_sectionID"], // The section to add the content to
(string)serviceRow["section_Name"], // the name of the section to add content to
table // the data to add
);
}
private static void AddRemoteContent(int communityID, int sectionID, string sectionName, DataTable contentTable) {
// Update skip counter and return when no records
if (contentTable.Rows.Count == 0)
return;
// We can determine the type of page content from the pageType field
string pageType = (string)contentTable.Rows[0]["pageType"];
// Using the pageType, lookup the name of the stored proc for adding content
string commandText = GetServiceUpdateCommand(pageType);
// Now, we can autogenerate the SqlCommand used for updating
SqlCommand cmdUpdate = BuildServiceUpdateCommand(communityID, sectionID, commandText, contentTable);
// Perform the update with a DataAdapter
SqlDataAdapter dad = new SqlDataAdapter();
dad.InsertCommand = cmdUpdate;
int results = dad.Update(contentTable);
// record the results
ActivityUtility.RecordMessage
(
communityID,
String.Format("Added {0} pages to the {1} section", results, sectionName),
String.Format("The pages were of type {0}", pageType)
);
}
private static void UpdateSkipCount(int serviceID, int recordCount) {
// initialize the command object
SqlConnection con = new SqlConnection(CommunityGlobals.ConnectionString);
SqlCommand cmd = new SqlCommand("Community_ServicesUpdateSkipCount", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@serviceID", serviceID);
cmd.Parameters.Add("@recordCount", recordCount);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
private static string GetServiceUpdateCommand(string pageType) {
// initialize the command object
SqlConnection con = new SqlConnection(CommunityGlobals.ConnectionString);
SqlCommand cmd = new SqlCommand("Community_ServicesGetServiceUpdateCommand", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@pageType", pageType);
con.Open();
string result = (string)cmd.ExecuteScalar();
con.Close();
return result;
}
private static SqlCommand BuildServiceUpdateCommand(int communityID, int sectionID, string commandText, DataTable contentTable) {
// initialize the command object
SqlConnection con = new SqlConnection(CommunityGlobals.ConnectionString);
SqlCommand cmd = new SqlCommand(commandText, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
// add (local) communityID and sectionID parameter
cmd.Parameters.Add("@communityID", communityID);
cmd.Parameters.Add("@sectionID", sectionID);
// create the parameters
SqlParameter param;
foreach (DataColumn col in contentTable.Columns) {
param = new SqlParameter();
param.SourceColumn = col.ColumnName;
//param.SourceVersion = DataRowVersion.Current;
param.ParameterName = "@" + col.ColumnName;
//param.SqlDbType = ConvertToSqlDbType(col.DataType);
if (col.MaxLength > 0)
param.Size = col.MaxLength;
cmd.Parameters.Add(param);
}
return cmd;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -