<% Option Explicit %>
<!--#include file="inc/Cls_DB.asp" -->
<!--#include file="../../Inc/Const.asp" -->
<!--#include file="inc/PublicFun.asp" -->
<%
'==============================================================================
'软件名称：风讯新闻采集系统V1.0
'最新更新：2004.12
'==============================================================================
'商业注册联系：028-85098980-601,602 技术支持：028-85098980-606、607,客户支持：608
'产品咨询QQ：159410,655071,66252421
'技术支持:所有程序使用问题，请提问到bbs.foosun.net我们将及时回答您
'程序开发：风讯开发组 & 风讯插件开发组
'论坛支持：风讯在线论坛(http://bbs.foosun.net)
'官方网站：www.Foosun.net  演示站点：test.cooin.com    
'网站建设专区：www.cooin.com
'==============================================================================
'免费版本请在新闻首页保留版权信息，并做上本站LOGO友情连接
'==============================================================================
Dim DBC,UpdateConn,Conn
Set DBC = New DataBaseClass
DBC.ConnStr = "DBQ=" + server.mappath("FooSunData/Update.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
Set UpdateConn = DBC.OpenConnection()
DBC.ConnStr = "DBQ=" + Server.MapPath(DataBaseConnectStr) + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
Set Conn = DBC.OpenConnection()
Set DBC = Nothing
'判断权限
%>
<!--#include file="../../Inc/Session.asp" -->
<!--#include file="../../Inc/CheckPopedom.asp" -->
<%
if Not JudgePopedomTF(Session("Name"),"P1405") then Call ReturnError1()
'判断权限结束
Dim Submit,MainID,TableFieldCorrInfo,i
Submit = Request("Submit")
MainID = Request("MainID")
TableFieldCorrInfo = Request("TableFieldCorrInfo")
if MainID = "" then
	Set UpdateConn = Nothing
	Response.Write("<script>alert('参数传递出错');window.location.href='UpDateManage.asp'</script>")  
	Response.End
end if


if Submit = "Add" then
	Dim TableFieldCorrInfoArray,FieldCorrInfoArray,FieldInfoArray,FieldArray,LoopVar,TempLoopVar,TableSOID,SField,OField
	Dim FieldSOSql,RSFieldSOObj,ExistIDStr
	Dim DelExistIDArray,DelArray,DelLoopVar,TempDelExistIDStr,DelExistIDStr
	TableFieldCorrInfoArray = Split(TableFieldCorrInfo,"***")
	for LoopVar = LBound(TableFieldCorrInfoArray) to UBound(TableFieldCorrInfoArray)
		ExistIDStr = ""
		FieldCorrInfoArray = Split(TableFieldCorrInfoArray(LoopVar),"###")
		TableSOID = FieldCorrInfoArray(0)
		FieldSOSql = "Select * from FieldSO where TableSOID=" & TableSOID & ""
		Set RSFieldSOObj = Server.CreateObject("ADODB.RecordSet")
		RSFieldSOObj.Open FieldSOSql,UpdateConn,3,3
		do while Not RSFieldSOObj.Eof
			ExistIDStr = ExistIDStr & "$$$" & RSFieldSOObj("ID") & "---" & RSFieldSOObj("SField") & "---" & RSFieldSOObj("OField")
			RSFieldSOObj.MoveNext
		Loop
		RSFieldSOObj.Close
		if FieldCorrInfoArray(1) <> "" then
			FieldInfoArray = Split(FieldCorrInfoArray(1),"$$$")
			for TempLoopVar = LBound(FieldInfoArray) to UBound(FieldInfoArray)
				FieldArray = Split(FieldInfoArray(TempLoopVar),"---")
				SField = FieldArray(1)
				OField = FieldArray(2)
				if FieldArray(0) = "" then '添加
					RSFieldSOObj.Open "Select * from FieldSO where 1=0"
					RSFieldSOObj.AddNew
					RSFieldSOObj("TableSOID") = TableSOID
					RSFieldSOObj("SField") = SField
					RSFieldSOObj("OField") = OField
					RSFieldSOObj.UpDate
					RSFieldSOObj.Close
				else
					if InStr(ExistIDStr,FieldInfoArray(TempLoopVar)) <> 0 then '修改 
						RSFieldSOObj.Open "Select * from FieldSO where ID=" & FieldArray(0) & ""
						RSFieldSOObj("TableSOID") = TableSOID
						RSFieldSOObj("SField") = SField
						RSFieldSOObj("OField") = OField
						RSFieldSOObj.UpDate
						RSFieldSOObj.Close
						ExistIDStr = Replace(ExistIDStr,"$$$" & FieldInfoArray(TempLoopVar),"")
					else
						RSFieldSOObj.Open "Select * from FieldSO where 1=0"
						RSFieldSOObj.AddNew
						RSFieldSOObj("TableSOID") = TableSOID
						RSFieldSOObj("SField") = SField
						RSFieldSOObj("OField") = OField
						RSFieldSOObj.UpDate
						RSFieldSOObj.Close
					end if
				end if
			Next
		end if
		if ExistIDStr <> "" then '添加要删除垃圾ID
			TempDelExistIDStr = ""
			if Left(ExistIDStr,3) = "$$$" then '取掉头部
				ExistIDStr = Right(ExistIDStr,Len(ExistIDStr)-3)
			end if
			DelExistIDArray = Split(ExistIDStr,"$$$")
			for DelLoopVar = LBound(DelExistIDArray) to UBound(DelExistIDArray)
				DelArray = Split(DelExistIDArray(DelLoopVar),"---")
				if TempDelExistIDStr = "" then
					TempDelExistIDStr = DelArray(0)
				else
					TempDelExistIDStr = TempDelExistIDStr & "," & DelArray(0)
				end if
			Next
		end if
		if TempDelExistIDStr <> "" then
			if DelExistIDStr = "" then
				DelExistIDStr = TempDelExistIDStr
			else
				DelExistIDStr = DelExistIDStr & "," & TempDelExistIDStr
			end if
		end if
		Set RSFieldSOObj = Nothing
	Next
	if DelExistIDStr <> "" then '删除FieldSO表中的垃圾
		UpdateConn.Execute("Delete from FieldSO where ID in (" & DelExistIDStr & ")") 
	end if
	if Err.Number <> 0 then
		Response.Write("<script>alert('数据出错');window.location.href='UpdateAddFourStep.asp?MainID=" & MainID & "';</script>")  
		Response.End
	end if
end if

Dim RSUpdateObj
Dim ObjectDBType,OServer,ObjectDB,OUser,OPassWord,ObjectDBTableArray
Set RSUpdateObj = UpdateConn.Execute("Select * from DBSO where MainID='" & MainID & "'")
if RSUpdateObj.Eof then
	Response.Write("<script>alert('数据出错');window.location.href='UpdateAddFourStep.asp?MainID=" & MainID & "';</script>")  
	Response.End
else
	ObjectDBType = RSUpdateObj("ObjectDBType")
	OServer = RSUpdateObj("OServer")
	ObjectDB = RSUpdateObj("ObjectDB")
	OUser = RSUpdateObj("OUser")
	OPassWord = RSUpdateObj("OPassWord")
	ObjectDBTableArray = ListDBAllTable(ObjectDBType,OServer,ObjectDB,OUser,OPassWord)
end if
Set RSUpdateObj = Nothing
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>添加数据库转换规则</title>
</head>
<link href="inc/Collect.css" rel="stylesheet">
<body topmargin="0" leftmargin="0">
<table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
  <form name="UpdateForm" method="get" action="UpdateAddSixStep.asp">
    <tr bgcolor="#E4E4E4"> 
    <td height="30" background="images/Collect_bg.gif"><font color="#FF0000"><strong>数据转移规则---第六步---定义关联</strong></font></td>
    </tr>
    <tr> 
    <td bgcolor="#F3F3F3"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr> 
            <td width="50%" height="20"> <div align="center">关联源表</div></td>
            <td height="20"> <div align="center">关联目标表</div></td>
          </tr>
          <tr> 
            <td><select name="RelateSourceTableList" onChange="GetTableFieldStr(this,'Source');" style="width:100%;" size="6">
                <%
			  for i = LBound(ObjectDBTableArray) to UBound(ObjectDBTableArray)
			  %>
                <option value="<% = ObjectDBTableArray(i) %>"> 
                <% = ObjectDBTableArray(i) %>
                </option>
                <%
			  Next
			  %>
              </select></td>
            <td><select name="RelateObjectTableList" onChange="GetTableFieldStr(this,'Object');" style="width:100%;" size="6">
                <%
			  for i = LBound(ObjectDBTableArray) to UBound(ObjectDBTableArray)
			  %>
                <option value="<% = ObjectDBTableArray(i) %>"> 
                <% = ObjectDBTableArray(i) %>
                </option>
                <%
			  Next
			  %>
              </select></td>
          </tr>
          <tr> 
            <td height="20"> <div align="center">关联表源字段</div></td>
            <td height="20"> <div align="center">关联表目标字段</div></td>
          </tr>
          <tr> 
            <td><select style="width:100%;" name="RelateSourceFieldList" size="6">
              </select></td>
            <td><select name="RelateObjectFieldList" style="width:100%;" size="6">
              </select></td>
          </tr>
          <tr> 
            <td height="30"> <div align="center"> 
                <input type="button" onClick="AddRelateField();" name="BtnAdd" value=" 添 加 ">
              </div></td>
            <td height="30"> <div align="center"> 
                <input type="button" onClick="DelRelateField();" name="BtnDel" value=" 删 除 ">
              </div></td>
          </tr>
          <tr> 
            <td colspan="2"><select name="RelateFieldCorrList" size="6" multiple style="width:100%;">
				<%
				Dim RSRelateInfoObj
				Set RSRelateInfoObj = UpdateConn.Execute("Select * from RelateInfo where DBSOID='" & MainID & "'")
				do while Not RSRelateInfoObj.Eof
				%>
                <option value="<% = RSRelateInfoObj("ID") & "---" & RSRelateInfoObj("SRelate") & "---" & RSRelateInfoObj("ORelate") %>"><% = RSRelateInfoObj("SRelate") & "---" & RSRelateInfoObj("ORelate") %></option>
				<%
					RSRelateInfoObj.MoveNext
				Loop
				Set RSRelateInfoObj = Nothing
				%>
              </select></td>
          </tr>
        </table></td>
    </tr>
    <tr> 
      <td bgcolor="#F3F3F3"><div align="center">
          <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr> 
              <td><div align="center"> 
                  <input name="BtnUp" onClick="window.location.href='UpDateAddFourStep.asp?MainID=<% = MainID %>';" type="button" id="Submitttt" value="上 一 步">
                <input type="hidden" name="MainID" value="<% = MainID %>">
                <input type="hidden" name="Submit" value="Add">
                  <input name="RelateFieldInfo" type="hidden" id="RelateFieldInfo">
                </div></td>
              <td><div align="center"> 
                <input name="BtnNext" onClick="SubmitData();" type="button" id="Submit2dfsd" value="下 一 步">
              </div></td>
            </tr>
          </table>
        </div></td>
    </tr>
	</form>
</table>
<iframe id="ListTableFieldFrame" src="ListOneTableAllField.asp" width="0" height="0"></iframe>
</body>
</html>
<script language="JavaScript">
var OperateType='',ObjectDBType='<% = ObjectDBType %>',OServer='<% = OServer %>',ObjectDB='<% = ObjectDB %>',OUser='<% = OUser %>',OPassWord='<% = OPassWord %>';
function GetTableFieldStr(Obj,Flag)
{
	SetBtnStatus(true);
	OperateType=Flag
	if (Obj.value!='')
	{
		frames["ListTableFieldFrame"].location='ListOneTableAllField.asp?Table='+Obj.value+'&ObjectDBType='+ObjectDBType+'&OServer='+OServer+'&ObjectDB='+ObjectDB+'&OUser='+OUser+'&OPassWord='+OPassWord+'&OperateType='+OperateType;
	}
}
function SetBtnStatus(Flag)
{
	document.UpdateForm.BtnAdd.disabled=Flag;
	document.UpdateForm.BtnDel.disabled=Flag;
	document.UpdateForm.BtnUp.disabled=Flag;
	document.UpdateForm.BtnNext.disabled=Flag;
}
function SetTableField(FieldStr,Flag)
{
	var FieldArray=FieldStr.split('$$$'),FieldAttrubute='';
	var OperateObj=null,i=0,AddOption=null;
	switch (Flag)
	{
		case 'Source':
			OperateObj=document.UpdateForm.RelateSourceFieldList;
			break;
		case 'Object':
			OperateObj=document.UpdateForm.RelateObjectFieldList;
			break;
		default :
			return;
	}
	DeleteAllOption(OperateObj);
	for (i=0;i<FieldArray.length;i++)
	{
		FieldAttrubute=FieldArray[i].substr(FieldArray[i].lastIndexOf('<'));
		FieldAttrubute=FieldAttrubute.replace('<','');
		FieldAttrubute=FieldAttrubute.replace('>','');
		AddOption=document.createElement("OPTION");
		AddOption.text=FieldArray[i];
		AddOption.value=FieldArray[i].slice(0,FieldArray[i].lastIndexOf('<'));
		AddOption.IsRequested=FieldAttrubute.split('|')[0];
		AddOption.Type=FieldAttrubute.split('|')[1];
		OperateObj.add(AddOption);
	}
}
function DeleteAllOption(SelectObj)
{
	var OptionLength=SelectObj.length;
	for(var i=0;i<OptionLength;i++)
	{
		SelectObj.options.remove(SelectObj.length-1);
	}
}
function AddRelateField()
{
	var ErrorStr='',i=0,AddOption=null,AddTF=false;
	var RelateFieldCorrListObj=document.UpdateForm.RelateFieldCorrList;
	var RelateSourceFieldListObj=document.UpdateForm.RelateSourceFieldList;
	var RelateObjectFieldListObj=document.UpdateForm.RelateObjectFieldList;
	var SelectedSourceTable=RelateSourceFieldListObj.value;
	var SelectedObjectTable=RelateObjectFieldListObj.value;
	if (SelectedSourceTable==''){ErrorStr='请选择源数据库表字段';alert(ErrorStr);RelateSourceFieldListObj.focus();return;}
	if (SelectedObjectTable=='') {ErrorStr='请选择目标数据库表字段';alert(ErrorStr);RelateObjectFieldListObj.focus();return;}
	var OptionTextStr='',OptionValueStr='';
	SelectedSourceTable=document.UpdateForm.RelateSourceTableList.value+'.'+SelectedSourceTable;
	SelectedObjectTable=document.UpdateForm.RelateObjectTableList.value+'.'+SelectedObjectTable;
	OptionTextStr=SelectedSourceTable+'---'+SelectedObjectTable;
	OptionValueStr='---'+SelectedSourceTable+'---'+SelectedObjectTable;
	if (!SearchUserPosition(RelateFieldCorrListObj,OptionTextStr))
	{
		AddOption=document.createElement("OPTION");
		AddOption.text=OptionTextStr;
		AddOption.value=OptionValueStr;
		RelateFieldCorrListObj.add(AddOption);
	}
	else alert(OptionTextStr+'关联已经添加了');
}
function SearchUserPosition(ObjectObj,Str)
{
	var i=0;
	for(i=0;i<ObjectObj.length;i++)
	{
		if (ObjectObj.options(i).text==Str) return true;
	}
	return false;
}
function DelRelateField()
{
	var TableFieldCorrListObj=document.UpdateForm.RelateFieldCorrList,i=0;
	while (i<TableFieldCorrListObj.options.length)
	{
		if (TableFieldCorrListObj.options(i).selected) {TableFieldCorrListObj.options.remove(i);}
		else i=i+1;
	}
}
function SubmitData()
{
	var i=0,ReturnStr='';
	var OperateObj=document.UpdateForm.RelateFieldCorrList;
	for (i=0;i<OperateObj.length;i++)
	{
		if (ReturnStr=='') ReturnStr=OperateObj.options(i).value;
		else ReturnStr=ReturnStr+'$$$'+OperateObj.options(i).value;
	}
	document.UpdateForm.RelateFieldInfo.value=ReturnStr;
	document.UpdateForm.submit();
}
</script>