00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <fillsearchlist.h>
00018 #include <qregexp.h>
00019 #include <qsqlquery.h>
00020 #include <qsqldatabase.h>
00021 #include <qapplication.h>
00022 #include <qmessagebox.h>
00023 #include "threadsearchevent.h"
00024 #include "global.h"
00025 #include "error.h"
00026
00027
00028 QString FillSearchList::buildResultRec(QString target, QString s, QStringList keywords, QString heading){
00029 QString res;
00030 debug("buildResultRec() - begin");
00031 for(QStringList::Iterator it = keywords.begin(); it != keywords.end(); ++it ) {
00032
00033 QString r;
00034 int pos=s.find(*it, 0, false);
00035 if(pos>-1) {
00036 r=s.mid( (pos-150>0?pos-150:0), 300+QString(*it).length() );
00037 r="..."+r+"...";
00038 res+=r.replace(QRegExp("("+*it+")", false), "<a href=\""+target+"\"><font color=red>\\1</font></a>").simplifyWhiteSpace();
00039 debug(res);
00040 }
00041 }
00042 res="<b>"+heading+"</b><br>"+res+"<br>";
00043 debug("buildResultRec() - ende");
00044 return res;
00045 }
00046
00047 QMutex fillsearchlistmutex;
00048 FillSearchList::FillSearchList(QObject *parent, QStringList terms): parent(parent), terms(terms) {
00049 }
00050
00051 FillSearchList::~FillSearchList() {
00052 }
00053
00054 void FillSearchList::run() {
00055 while(fillsearchlistmutex.locked())
00056 msleep(10);
00057
00058 QMutexLocker locker(&fillsearchlistmutex);
00059
00060 try {
00061 if(!createConnection("qt_search_connection")) throw(Error("Cannot open database connection"));
00062 QSqlDatabase *conn=QSqlDatabase::database("qt_search_connection");
00063 QStringList sl=terms;
00064 QString q;
00065
00066 for(QStringList::Iterator it = sl.begin(); it != sl.end(); ++it)
00067 q+="select distinct * from search_term(?) as (a text, b text) intersect ";
00068
00069 QSqlQuery query(conn);
00070 query.prepare(q.left(q.length()-11));
00071
00072 for(QStringList::Iterator it = sl.begin(); it != sl.end(); ++it) {
00073 debug((*it).utf8());
00074 query.addBindValue((*it).utf8());
00075 }
00076
00077 query.exec();
00078 if(!query.isActive())
00079 throw(Error(query.lastError()));
00080
00081 QPtrList<QString> ids;
00082 ids.setAutoDelete(true);
00083
00084 while(query.next())
00085 ids.append(new QString("(XXTABLEXX.no="+query.value(0).toString()+" and tabid('XXTABLEXX')="+query.value(1).toString()+") or "));
00086
00087
00088 addResult("select publication.title as ordertitle, part.title, partdata.no, partdata.astext from publication, part, partdata where publication.no=part.publication_no and part.no = partdata.part_no ", "partdata", "file/partdata", ids, sl);
00089
00090 addResult("select publication.title as ordertitle, part.title, part.no, part.memo from publication, part where publication.no=part.publication_no ", "part", "part/part", ids, sl);
00091
00092 addResult("select note.title as ordertitle, '', note.no, note.memo from note where true", "note", "note/note", ids, sl);
00093
00094 addResult("select publication.title as ordertitle, '', publication.no, publication.memo from publication where true", "publication", "publication/publication", ids, sl);
00095
00096 addResult("select author.firstname, author.lastname as ordertitle, author.no, author.memo from author where true", "author", "author/author", ids, sl);
00097
00098
00099 } catch(Error &e) {
00100 qApp->lock();
00101 QMessageBox::warning(0L, "Error", e.message());
00102 qApp->unlock();
00103 }
00104 }
00105
00106
00107 void FillSearchList::addResult(QString where, QString table, QString cmd, QPtrList<QString> ids, QStringList sl) {
00108 if(ids.count()>0) {
00109 QString q=where+" and (";
00110 QString *t;
00111 for(t=ids.first(); t; t=ids.next())
00112 q+=QString(*t).replace("XXTABLEXX", table);
00113 QSqlQuery query(q.left(q.length()-4)+") order by ordertitle");
00114
00115 if(!query.isActive())
00116 throw(Error(query.lastError()));
00117
00118 while(query.next()) {
00119 QString heading=query.value(0).toString()+" - "+query.value(1).toString();
00120 QString res=buildResultRec("lit://"+cmd+"?no="+query.value(2).toString(), query.value(3).toString(), sl, heading);
00121 debug(res);
00122 QApplication::postEvent(parent, new ThreadSearchEvent(res));
00123 }
00124 }
00125 }
00126