using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Net.Mail;
public partial class ReviseComponent : System.Web.UI.Page
{
/// <summary>
/// Copyright (C) 2007 Jiing Deng @ CathayBK
///
/// </summary>
private string strFileSavePath =
System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
#region lɩҰʧ@
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
fvReviseComponent.ChangeMode(FormViewMode.Edit); // NFormView ܴEditҦ
// btnGo2Main.Visible = false;
// btnUpdate.Visible = true;
// qƮwd߬ơA⥦̨qbEdit ModeWAӵϥΪ̽s
// showMsg("qƮwd߬ơA⥦̨qbEdit ModeWAӵϥΪ̽s");
try
{
// // qƮwoƤAAsثeEdit Mode
// // string componentName; ݯणo~Ѽ -- componentNamehd
string componentID = Request.QueryString["componentID"]; //γoӴե\i
string[] myDSetOfComponent = queryFromDBbyComponentID(componentID);
// Label lblTemp = (Label)fvReviseComponent.FindControl("lblContributor");
// lblTemp.Text = Context.User.Identity.Name; //put user name on pages
FormView myFormView = (FormView)Page.FindControl("fvReviseComponent");
DropDownList myDdlVersion1 = (DropDownList)myFormView.FindControl("ddlVersion1");
DropDownList myDdlVersion2 = (DropDownList)myFormView.FindControl("ddlVersion2");
DropDownList myDdlVersion3 = (DropDownList)myFormView.FindControl("ddlVersion3");
string[] strVersionTmp = myDSetOfComponent[8].Split('.');
myDdlVersion1.SelectedValue = strVersionTmp[0];
myDdlVersion2.SelectedValue = strVersionTmp[1];
myDdlVersion3.SelectedValue = strVersionTmp[2];
Image myImageDemoScrShot = (Image)myFormView.FindControl("imgDemoScrShot");
//myDSetOfComponent[8].Trim(); is the old version
if (File.Exists(strFileSavePath + "UploadImages/" + componentID + "_" + myDSetOfComponent[8].Trim()))
{
myImageDemoScrShot.ImageUrl = strFileSavePath + "UploadImages/" + componentID + "_" + myDSetOfComponent[8].Trim();
}else
{
myImageDemoScrShot.ImageUrl=strFileSavePath + "UploadImages/default.png";
}
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
}
#endregion
#region qƮwŪƥX
/// <summary>
/// @SQLROӱqDBd߷QnӸ`C
/// </summary>
/// <param name="componentName">W١AdߦӸ`</param>
/// <returns>^Ǥ@DataSet</returns>
protected string[] queryFromDBbyComponentID(string componentID)
{
//}lӭnqƮwŪXJ, sqlOtW٩ΤID
ConnectionStringSettings connSettings = ConfigurationManager.ConnectionStrings["CCSConnectionString"];
string CCSConnectionString = connSettings.ConnectionString;
string cmdQuery = "SELECT * FROM COMPONENT WHERE COMPONENT_ID = '" + componentID + "' ";
SqlConnection Conn = new SqlConnection(CCSConnectionString);
//DataSet dsComponetFields = new DataSet();
string[] retString = new string[28];
try
{
Conn.Open();
}
catch (SqlException ex)
{
// Connection failed
showMsg(ex.Message);
}
SqlCommand command = new SqlCommand(cmdQuery, Conn);
command.CommandType = System.Data.CommandType.Text;
SqlDataReader rdr = command.ExecuteReader();
try
{
do
{
while (rdr.Read())
{
for(int ix = 0; ix< rdr.FieldCount - 1; ix++){
retString[ix] = rdr.GetValue(ix).ToString();
}
//retString[ix] = rdr.GetValue(0).ToString();
//ix++;
}
} while (rdr.NextResult());
}
catch (SqlException ex)
{
showMsg(ex.Message);
}
finally
{
Conn.Close(); //remember to close the db connection
rdr.Close();
}
return retString;
}
#endregion
#region bק蠟ApG榡XzAUssʧ@
protected void UpdateButton_Click(object sender, EventArgs e)
{
if (!IsPostBack)
{
showMsg("AUFssAUӷ|NƧsܸƮw");
}
StringBuilder strBd = new StringBuilder("");
strBd.AppendFormat("{0:yyyy/MM/dd-HH:mm:ss}", System.DateTime.Now);
//A@ǭnJ쵹X
FormView myFormView = (FormView)Page.FindControl("fvReviseComponent");
Label myComponentID = (Label)myFormView.FindControl("lblComponentID");
TextBox myTbxName = (TextBox)myFormView.FindControl("tbxName");
DropDownList myDdlVersion1 = (DropDownList)myFormView.FindControl("ddlVersion1");
DropDownList myDdlVersion2 = (DropDownList)myFormView.FindControl("ddlVersion2");
DropDownList myDdlVersion3 = (DropDownList)myFormView.FindControl("ddlVersion3");
TextBox myTbxPath = (TextBox)myFormView.FindControl("tbxPath");
DropDownList myDdlFramework = (DropDownList)myFormView.FindControl("ddlFramework");
TextBox myTbxFrame = (TextBox)myFormView.FindControl("tbxFramework");
Label myLblContributor = (Label)myFormView.FindControl("lblContributor");
DropDownList myDdlType = (DropDownList)myFormView.FindControl("ddlType");
TextBox myTbxType = (TextBox)myFormView.FindControl("tbxType");
TextBox myTbxNote = (TextBox)myFormView.FindControl("tbxNote");
TextBox myTbxToDo = (TextBox)myFormView.FindControl("tbxToDo");
DropDownList myDdlIDE = (DropDownList)myFormView.FindControl("ddlIDE");
TextBox myTbxDescription = (TextBox)myFormView.FindControl("tbxDescription");
TextBox myTbxSynposis = (TextBox)myFormView.FindControl("tbxSynposis");
DropDownList myDdlTestEnv = (DropDownList)myFormView.FindControl("ddlTestEnv");
DropDownList myDdlCopyright = (DropDownList)myFormView.FindControl("ddlCopyright");
TextBox myTbxAcknowledgement = (TextBox)myFormView.FindControl("tbxAcknowledgement");
TextBox myTbxSeeAlso = (TextBox)myFormView.FindControl("tbxSeeAlso");
TextBox myTbxAP_ID = (TextBox)myFormView.FindControl("tbxAP_ID");
TextBox myTbxAP_Name = (TextBox)myFormView.FindControl("tbxAP_Name");
TextBox myTbxEmail = (TextBox)myFormView.FindControl("tbxEmail");
TextBox myTbxPhone = (TextBox)myFormView.FindControl("tbxPhone");
//next ,we all insert the all available data into database
ConnectionStringSettings connSettings = ConfigurationManager.ConnectionStrings["CCSConnectionString"];
string CCSConnectionString = connSettings.ConnectionString;
//UPDATE Table1
//SET Column1 = Value1, Column2 = Value2
//WHERE Some_Column = Some_Value
string componentID = Request.QueryString["componentID"]; //γoӴե\i
string UpdateCommand =
"UPDATE COMPONENT SET NAME = " + "'" + myTbxName.Text.Trim() + "'" + ", "
+ "TYPE = " + "'" + myDdlType.SelectedValue.Trim() + "'" + ", "
+ "CONTRIBUTOR = " + "'" + myLblContributor.Text.Trim() + "'" + ", "
+ "FRAMEWORK = " + "'" + myDdlFramework.SelectedValue.Trim() + "'" + ", "
+ "PATH = " + "'" + myTbxPath.Text.Trim() + "'" + ", "
+ "VERSION = " + "'" + myDdlVersion1.SelectedValue.Trim() + "." + myDdlVersion2.SelectedValue.Trim() + "." + myDdlVersion3.SelectedValue.Trim() + "'" + ", "
+ "SYNPOSIS = " + "'" + myTbxSynposis.Text.Trim() + "'" + ", "
+ "DESCRIPTION = " + "'" + myTbxDescription.Text.Trim() + "'" + ", "
+ "TEST_ENVIRONMENT = " + "'" + myDdlTestEnv.SelectedValue.Trim() + "'" + ", "
+ "IDE = " + "'" + myDdlIDE.SelectedValue.Trim() + "'" + ", "
+ "NOTE = " + "'" + myTbxNote.Text.Trim() + "'" + ", "
+ "TODO = " + "'" + myTbxToDo.Text.Trim() + "'" + ", "
+ "SEE_ALSO = " + "'" + myTbxSeeAlso.Text.Trim() + "'" + ", "
+ "ACKNOWLEDGEMENT = " + "'" + myTbxAcknowledgement.Text.Trim() + "'" + ", "
+ "COPYRIGHT = " + "'" + myDdlCopyright.SelectedValue.Trim() + "'" + ", "
+ "AP_ID = " + "'" + myTbxAP_ID.Text.Trim() + "'" + ", "
+ "AP_NAME = " + "'" + myTbxAP_Name.Text.Trim() + "'" + ", "
+ "EMAIL = " + "'" + myTbxEmail.Text.Trim() + "'" + ", "
+ "PHONE = " + "'" + myTbxPhone.Text.Trim() + "'" + ", "
+ "IS_VALID1 = " + "'' , "
+ "IS_VALID2 = " + "'' , "
+ "MYDATETIME = " + "'" + strBd + "'" + ", "
+ "STATUS = " + "'R' "
+ "WHERE COMPONENT_ID = '" + componentID +"' " ;
// showMsg(UpdateCommand);
SqlConnection Conn = new SqlConnection(CCSConnectionString);
try
{
Conn.Open();
}
catch (SqlException ex)
{
// Connection failed
showMsg(ex.Message);
}
try
{
SqlCommand command = new SqlCommand(UpdateCommand, Conn);
command.CommandType = System.Data.CommandType.Text;
command.ExecuteNonQuery();
}
catch (SqlException ex)
{
showMsg(ex.Message);
}
catch (Exception ex)
{
showMsg(ex.Message);
}
finally
{
Conn.Close(); //remember to close the db connection
// generateRSSFeed("ק", componentID);
}
string strMyVersionNow = myDdlVersion1.SelectedValue.Trim() + "." + myDdlVersion2.SelectedValue.Trim() + "." + myDdlVersion3.SelectedValue.Trim();
ImageFileUpload(componentID, strMyVersionNow);
SrcCodeFileUpload(componentID, strMyVersionNow); //WǭlX{
showMsg("ק粒");
SendMail2ContributorAboutWaitForAudit(componentID);
//btnUpdate.Visible = false;
fvReviseComponent.ChangeMode(FormViewMode.ReadOnly);
//Server.Transfer("~/Default.aspx");
//ק露\ɥ[WGrowlĪGH
// FormView myFormView2 = (FormView)Page.FindControl("fvReviseComponent");
// fvReviseComponent.ChangeMode(FormViewMode.ReadOnly); // NFormView ܴRead onlyҦ
// fvReviseComponent.DataBind();
// Server.Transfer("./ReviseComponent.aspx");
// myFormView2.DataBind();
}
#endregion
#region vɮפWǨ /UploadImages/
protected void ImageFileUpload(string strComponentID, string strMyVersionNow)
{
FormView myFormView = (FormView)Page.FindControl("fvReviseComponent");
FileUpload myFileuploadImage = (FileUpload)myFormView.FindControl("fileUploadImage");
Label myLblUploadImage = (Label)myFormView.FindControl("lblUploadImage");
Label myLblVersionOld = (Label)myFormView.FindControl("lblVersionOld");
string fullname = strComponentID + "_" + strMyVersionNow; //`Nsɪ覡
string url = myFileuploadImage.PostedFile.FileName.ToString(); //oӬOHe2003ΪAoWǤ|ӦWrAMAQSubStringkӱoΤWA{bݨӬOSnF
string typ = myFileuploadImage.PostedFile.ContentType.ToString(); //MIMEe
string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1); //Wr . ᭱rŧ@
string size = myFileuploadImage.PostedFile.ContentLength.ToString();
bool fileIsOK = false;
// string path = Server.MapPath("~/");
if (myFileuploadImage.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(myFileuploadImage.FileName).ToLower();
string[] allowedExtensions ={ ".gif", ".png", ".jpeg", ".jpg" }; //ΪN\F
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileIsOK = true;
}
}
if (fileIsOK)
{
try
{
myFileuploadImage.SaveAs(Server.MapPath("~/UploadImages/") + "\\" + fullname);//NOsbؿUPU
myLblUploadImage.Text = ("AWǤF@ӦW" + fullname + "AMIMEe[" + typ + "]AW[" + typ2 + "]A@[" + size + "] bytejp<br>wgOsbFAWǥؿFI");
}
catch (Exception ex)
{
myLblUploadImage.Text = ex.Message.ToString();
}
}
else
{
myLblUploadImage.Text = "WǮ榡~H";
}
}
else {
if (File.Exists(Server.MapPath("~/UploadImages/") + "\\" + strComponentID + "_" + myLblVersionOld.Text))
File.Move(Server.MapPath("~/UploadImages/") + "\\" + strComponentID + "_" + myLblVersionOld.Text, Server.MapPath("~/UploadImages/") + "\\" + fullname);
}
}
#endregion
protected void showMsg(string AlertMessage)
{
Literal txtMsg = new Literal();
txtMsg.Text = "<script>alert('" + AlertMessage + "')</script>" + "<br/>";
Page.Controls.Add(txtMsg);
}
protected void fvReviseComponent_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
{
fvReviseComponent.ChangeMode(FormViewMode.ReadOnly); // NFormView ܴReadOnlyҦ
}
#region ͦRSS Feed z
private void generateRSSFeed(string strMsgLevel, string strComponentID)
{
SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataReader reader = null;
RSSChannel channel = null;
StreamWriter sw = null;
StreamReader sr = null;
int a = 0;
try
{
channel = new RSSChannel();
channel.title = "@ظTBG@Τzt Feed";
channel.webMaster = "jiing.deng@cathaybk.com.tw";
channel.managingEditor = "jiing.deng@cathaybk.com.tw";
// channel.link = "http://www.cathaybk.com.tw";
channel.description = "@ظTBUx@Τi";
ConnectionStringSettings connSettings = ConfigurationManager.ConnectionStrings["CCSConnectionString"];
string CCSConnectionString = connSettings.ConnectionString;
conn = new SqlConnection(CCSConnectionString);
cmd = new SqlCommand("SELECT NAME,FRAMEWORK,VERSION,DL_LINK,DESCRIPTION FROM COMPONENT WHERE COMPONENT_ID ='" + strComponentID + "'" + "WITH (NOLOCK) ORDER BY NAME,VERSION DESC",
conn);
conn.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
channel.AppendRSSItem("[" + strMsgLevel + "]" + reader["NAME"].ToString().Trim() + " for " +
reader["FRAMEWORK"].ToString().Trim() + " Ver" +
reader["VERSION"].ToString().Trim(),
// reader["DL_LINK"].ToString().Trim(),
reader["DESCRIPTION"].ToString().Trim(), string.Empty);
}
reader.Close();
conn.Close();
string filename = strFileSavePath + "CathayBK.rss";
bool isAppend = File.Exists(filename);
if (!isAppend)
{
sw = new StreamWriter(filename, false, System.Text.Encoding.GetEncoding("utf-8"));
sw.WriteLine(channel.GenerateChannel());
}
else
{
sr = new StreamReader(filename, System.Text.Encoding.GetEncoding("utf-8"));
string original = sr.ReadToEnd();
sr.DiscardBufferedData();
sr.Dispose();
string newTxt = string.Empty;
for (a = 0; a < channel.itemLst.Count; a++)
{
newTxt += ((RSSItem)channel.itemLst[a]).GenerateItem();
}
//夤Ĥ@Item
int thePos = original.IndexOf("<item>");
if (thePos > 0)
{
//NHUeUguidXﻼW
int target1 = original.IndexOf("<guid>", thePos);
int target2 = 0;
while (target1 > thePos)
{
target2 = original.IndexOf("</guid>", target1 + "<guid>".Length);
string match = original.Substring(target1 + "<guid>".Length, target2 - (target1 + "<guid>".Length));
int lastSharp = match.LastIndexOf('#');
int newNum = int.Parse(match.Substring(lastSharp + "#".Length)) + a;
string newMatch = match.Substring(0, lastSharp) + "#" + newNum.ToString().Trim();
original = original.Replace("<guid>" + match + "</guid>", "<guid>" + newMatch + "</guid>");
target1 = original.IndexOf("<guid>", target2 + "</guid>".Length);
}
original = original.Insert(thePos, newTxt);
}
sw = new StreamWriter(filename, false, System.Text.Encoding.GetEncoding("utf-8"));
sw.WriteLine(original);
}
sw.Flush();
sw.Close();
sw.Dispose();
reader.Dispose();
cmd.Dispose();
conn.Dispose();
}
catch (Exception exp)
{
showMsg(exp.Message);
}
finally
{
reader = null;
cmd = null;
conn = null;
channel = null;
sw = null;
sr = null;
}
}
#endregion
protected void tbxType_TextChanged(object sender, EventArgs e)
{
FormView myFormView = (FormView)Page.FindControl("fvReviseComponent");
DropDownList myDdlType = (DropDownList)myFormView.FindControl("ddlType");
TextBox myTbxType = (TextBox)myFormView.FindControl("tbxType");
myDdlType.Enabled = true;
}
protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
{
// FormView myFormView = (FormView)Page.FindControl("fvReviseComponent");
// DropDownList myDdlType = (DropDownList)myFormView.FindControl("ddlType");
//// myDdlType.Enabled = true;
// TextBox myTbxType = (TextBox)myFormView.FindControl("tbxType");
// myTbxType.Text = myDdlType.Text;
}
protected void ddlType_TextChanged(object sender, EventArgs e)
{
}
protected void btnGo2Main_Click(object sender, EventArgs e)
{
Server.Transfer("~/Default.aspx");
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
}
#region {lXWǨ UploadSrcCode
protected void SrcCodeFileUpload(string strComponentID, string strMyVersionNow)
{
FormView myFormView = (FormView)Page.FindControl("fvReviseComponent");
FileUpload myFileuploadSrcCode = (FileUpload)myFormView.FindControl("fileUploadSrcCode");
Label myLblUploadSrcCode = (Label)myFormView.FindControl("lblUploadSrcCode");
string fullname = strComponentID + "_" + strMyVersionNow + "_" + myFileuploadSrcCode.FileName.ToString(); //oW
//configSection.MaxRequestLength = 1024*10; //̤jW10MBAw]ȬO4MB
string url = myFileuploadSrcCode.PostedFile.FileName.ToString(); //oӬOHe2003ΪAoWǤ|ӦWrAMAQSubStringkӱoΤWA{bݨӬOSnF
string mimeType = myFileuploadSrcCode.PostedFile.ContentType.ToString(); //MIMEe
string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1); //Wr . ᭱rŧ@
string size = myFileuploadSrcCode.PostedFile.ContentLength.ToString();
bool fileIsOK = false;
// string path = Server.MapPath("~/");
if (myFileuploadSrcCode.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(myFileuploadSrcCode.FileName).ToLower();
string[] allowedExtensions ={ ".zip", ".rar", ".c", ".cpp", ".txt", ".pl", ".plx", ".cs", ".aspx", ".m", ".asp", ".vb" }; //VCiWǪɮ榡
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileIsOK = true;
}
}
}
if (fileIsOK)
{
try
{
myFileuploadSrcCode.SaveAs(Server.MapPath("~/UploadSrcCode/") + "\\" + fullname);//NOsbؿUPU
myLblUploadSrcCode.Text = ("AWǤF@ӦW" + fullname + "AMIMEe[" + mimeType + "]AW[" + typ2 + "]A@[" + size + "] bytejp<br>wgOsbFAWǥؿFI");
}
catch (Exception ex)
{
myLblUploadSrcCode.Text = ex.Message;
}
}
else
{
myLblUploadSrcCode.Text = "WǮ榡~H";
}
}
#endregion
#region Heemail}o̡Aiݺz̼f
private void SendMail2ContributorAboutWaitForAudit(string strComponentID)
{
//Step 1. dEmail
string strEmailAddress = "";
SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataReader reader = null;
try
{
ConnectionStringSettings connSettings = ConfigurationManager.ConnectionStrings["CCSConnectionString"];
string CCSConnectionString = connSettings.ConnectionString;
conn = new SqlConnection(CCSConnectionString);
cmd = new SqlCommand("SELECT EMAIL FROM COMPONENT WHERE COMPONENT_ID ='" + strComponentID + "'", conn);
conn.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
strEmailAddress = reader["EMAIL"].ToString().Trim();
}
}
catch (Exception ex)
{
showMsg(ex.Message);
}
finally
{
reader.Close();
conn.Close();
}
//Step 2.HoEmail }o
sendEMail("", strEmailAddress);
}
#endregion
#region HeEmailiT
private void sendEMail(string strMailBody, string strMailToAddress)
{
//PɱHez̻PϥΪ
String strTo = "<jiing.deng@cathaybk.com.tw>;" + strMailToAddress;
//ӷO@ΤztΩMz
String strFrom = "<CCOM@cathaybk.com.tw>";
String strMailSubject = "[@Τ]@ΤztΪAqHI";
if (String.IsNullOrEmpty(strMailBody))
{
strMailBody = "oOѦ@ΤztγqT</b>A<b>zҭק諸Aثeݺz̼f<br>";
}
System.Net.Mail.SmtpClient client = new SmtpClient();
client.Host = "PIEX2K05.cathaybk.intra.uwccb";
client.Port = 25;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("uid", "pwd"); //uid, pwd please replace as yours
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new MailMessage(strFrom, strTo, strMailSubject, strMailBody);
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
try
{
client.Send(message);
Response.Write("<font color=\"blue\">Email successfully sent.</font>");
}
catch (Exception ex)
{
Response.Write("Send Email Failed." + ex.ToString()); ;
}
}
#endregion
}