#include <afx.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstring>
#include <direct.h>
#include <lncppapi.h>
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","rsEOF")
using namespace std;
#define ERR_BUF_SIZE 512
#define QUIT_MSG " shutdown complete."
int PushDataToSQL(LNViewFolder vf,_RecordsetPtr pRst);
vector<string> split_str(LNString splitstr);
VARIANT PushAttach(LNString attachPath,SAFEARRAY *psa);
int main(int argc, char *argv[])
{
char * ServerName;
char * DBName="mail\\Fax.nsf";
LNNotesSession s;
LNDatabase db;
LNViewFolder vf;
LNServerAddin addin;
LNString TaskName;
LNString buffer;
LNMessageQueue msg;
LNINT msg_len;
LNString tmp;
const LNINT msg_MAX = MQ_MAX_MSGSIZE;
char console_msg[msg_MAX+1];
int msg_list_index;
LNINT loc;
int PushDataToSQL_Num;
LNSetThrowAllErrors(true);
s.Init(argc, argv);
s.GetServerAddin("Fax","Fax add-in task",&addin);
TaskName = addin.GetDefaultStatusLineTaskName();
try
{
if(s.GetDatabase(DBName,&db))
{
cout << "Can't found mail\\Fax.nsf." << endl;
return 1;
}
db.Open();
db.GetViewFolder("($Inbox)",&vf);
vf.Open();
cout << TaskName << " is starting....." << endl;
//create msgQueue
buffer = TASK_QUEUE_PREFIX;
buffer.Append(TaskName);
s.CreateMessageQueue(buffer,&msg);
const LNString msg_list[3]={"HELP","SHOW","DETAIL"};
//建立数据库连接
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
_CommandPtr pCmd(__uuidof(Command));
pConn->ConnectionString="Provider=SQLOLEDB.1;Password=sms;Persist Security Info=True;User ID=sms;Initial Catalog=SMS;Data Source=192.168.0.118";
pConn->Open("","","",adConnectUnspecified);
pRst->Open("select * from SendFax",_variant_t((IDispatch*)pConn),
adOpenDynamic,adLockOptimistic,adCmdText);
while (!addin.Idle())
{
if (msg.GetMessageCount()>0)
{
msg.GetNextMessage(msg_MAX,console_msg,&msg_len);
tmp = LNString((const char *)console_msg, msg_len);
tmp.ToUpper();
if ( msg_list[0].Find(tmp,0,&loc) != LNWARN_NOT_FOUND)
{
msg_list_index = 0;
}else if ( msg_list[1].Find(tmp,0,&loc) != LNWARN_NOT_FOUND)
{
msg_list_index = 1;
}
else if ( msg_list[2].Find(tmp,0,&loc) != LNWARN_NOT_FOUND)
{
msg_list_index = 2;
}else
{
msg_list_index = 3;
}
switch(msg_list_index)
{
case 0 :
{
addin.AppendLogMessage("command is help.");
break;
}
case 1 :
{
addin.AppendLogMessage("command is show.");
break;
}
case 2 :
{
addin.AppendLogMessage("command is detail.");
break;
}
default:
{
addin.AppendLogMessage("command is unknow.");
}
}
}
if (addin.HaveSecondsElapsed(5))
{
//cout << "\n" << addin.GetDefaultStatusLineTaskName() << " is process....." << endl;
PushDataToSQL_Num = PushDataToSQL(vf,pRst);
//cout << "\n" << addin.GetDefaultStatusLineTaskName() << " is finished." << endl;
if ( PushDataToSQL_Num!=0)
{
LNString tmp;
tmp << "Task " << TaskName << " pushed " << PushDataToSQL_Num
<< " record(s) to SQL. ";
addin.AppendLogMessage(tmp);
PushDataToSQL_Num=0;
}
}
}
}
catch (LNSTATUS Lnerror)
{
char ErrorBuf[ERR_BUF_SIZE];
LNGetErrorMessage(Lnerror, ErrorBuf, ERR_BUF_SIZE);
cout << "Error: " << ErrorBuf << endl;
}
vf.Close();
db.Close();
buffer = TaskName;
buffer.Append(QUIT_MSG);
addin.AppendLogMessage(buffer);
s.Term();
return(0);
}
vector<string> split_str(LNString splitstr)
{
vector<string> parts;
char* str = new char[splitstr.GetLength()];
for (int i=0;i<splitstr.GetLength();i++)
{
str[i]=splitstr[i];
}
str[splitstr.GetLength()]='\0';
char* tok = strtok ( str, ",");
while ( tok != NULL ) {
parts.push_back( tok );
tok = strtok (NULL, ",");
}
return parts;
}
VARIANT PushAttach(LNString attachPath,SAFEARRAY *psa)
{
VARIANT varChunk;
SAFEARRAYBOUND rgsabound[1];
LPCTSTR AttachName = attachPath;
long nLen;
//VT_ARRAY | VT_UI1
CFile f;
if (f.Open(AttachName,CFile::modeRead)==0) return varChunk;
nLen = f.GetLength();
byte* bVal = new byte[nLen];
long uIsRead=0;
//Create a safe array to store the array of BYTES
uIsRead=f.Read(bVal,nLen);
if(uIsRead==0) return varChunk;
rgsabound[0].cElements=uIsRead;
rgsabound[0].lLbound=0;
psa = SafeArrayCreate(VT_UI1,1,rgsabound);
for(long index=0;index<uIsRead;index++)
{
if(FAILED(SafeArrayPutElement(psa,&index,&bVal[index])));
}
varChunk.vt=VT_ARRAY|VT_UI1;
varChunk.parray=psa;
f.Close();
return varChunk;
}
int PushDataToSQL(LNViewFolder vf,_RecordsetPtr pRst)
{
CString tmp;
LNString name;
LNVFNavigator nav;
LNVFEntry entry;
LNDocument doc;
LNText item;
LNText Sender;
LNString itemtmp;
LNString attachPath;
attachPath = "tmp";
_mkdir(attachPath);
attachPath.Append("//");
int PushDataToSQL = 0;
try
{
vf.GetEntries(&nav);
nav.GotoFirst(&entry);
do
{
if(entry.IsNull())
break;
entry.GetDocument(&doc);
doc.Open();
if (doc.HasAttachments())
{
doc.GetItem("subject",&item);
item.GetText(&itemtmp);
doc.GetItem("Principal",&Sender);
LNAttachmentArray AttachmentArray;
LNAttachment Attachment;
doc.GetAttachments(&AttachmentArray);
for (int i=0;i<AttachmentArray.GetCount();i++)
{
LNString tmp_attachPath;
tmp_attachPath = attachPath;
Attachment=AttachmentArray[i];
tmp_attachPath.Append(Attachment.GetAttachmentName());
Attachment.Detach(tmp_attachPath);
SAFEARRAY *lpsa;
VARIANT vChunk = PushAttach(tmp_attachPath,lpsa);
vector<string> splitstr;
splitstr = split_str(itemtmp);
for (int j=0;j<splitstr.size();j++)
{
pRst->AddNew();
pRst->PutCollect("FaxTo",_variant_t(splitstr[j].data()));
pRst->PutCollect("username",_variant_t((LNString)Sender[0]));
pRst->Fields->GetItem("Attach")->AppendChunk(vChunk);
pRst->PutCollect("AttachName",_variant_t(Attachment.GetAttachmentName()));
pRst->Update();
}
::VariantClear(&vChunk);
::SafeArrayDestroyData(lpsa);
}
}
vf.FolderRemoveDocument(doc);
doc.Close();
PushDataToSQL++;
}while (nav.GotoNext(&entry)!=LNWARN_NOT_FOUND);
return PushDataToSQL;
}
catch (_com_error &e)
{
cout << e.Description();
return PushDataToSQL;
}
}