DumontEXE 0.0.1
cmcQueue.cpp
00001 
00002 #include <QProcess>
00003 #include <QTimer>
00004 #include <QTimerEvent>
00005 
00006 #include "cmcApplication.h"
00007 #include "cmcCategoryDefs.h"
00008 #include "cmcCursor.h"
00009 #include "cmcRowSet.h"
00010 #include "cmcQueue.h"
00011 
00012 namespace cmcDatabaseApi {
00013 
00014 cmcQueue::cmcQueue( const QString & queueName, cmcApplication * app, QObject * parent )
00015 : cmcObject( queueName, app, parent )
00016 {
00017 //  TRACE_FUNCTION
00018 
00019   m_halt = false;
00020   m_busy = false;
00021 
00022   process = new QProcess(this);
00023 
00024   connect
00025   (
00026     process,
00027     SIGNAL(finished(int,QProcess::ExitStatus)),
00028     this,
00029     SLOT(processFinished(int,QProcess::ExitStatus))
00030   );
00031 
00032   /*
00033   ** After a delay, check the queue
00034   **
00035   */
00036   startTimer( 5000 );
00037 }
00038 
00039 cmcQueue::~cmcQueue()
00040 {
00041 //  TRACE_FUNCTION
00042 }
00043 
00044 bool cmcQueue::hasQueueCategory()
00045 {
00046   if( !app() )                                                    return( false );
00047   if( !app()-> categoryDefs() )                                   return( false );
00048   if( !app()-> categoryDefs()-> contains( queueCategoryName() ) ) return( false );
00049   return(true);
00050 }
00051 
00052 QString cmcQueue::queueCategoryName()
00053 {
00054   return( "sy.DumontQueue" );
00055 }
00056 
00057 
00058 bool cmcQueue::add
00059 (
00060   long priority,
00061   const QString & program,
00062   const QString & value
00063 )
00064 {
00065   return
00066   ( add
00067     ( QUuid::createUuid().toString(),
00068       priority,
00069       program,
00070       value
00071     )
00072   );
00073 }
00074 
00075 bool cmcQueue::add
00076 (
00077   const QString & itemName,
00078   long priority,
00079   const QString & program,
00080   const QString & arguments
00081 )
00082 {
00083   /*
00084   ** Make sure we have a queue category.  If not, then
00085   **  there's nothing to do.
00086   **
00087   */
00088   if( !hasQueueCategory() ) return( false );
00089 
00090   /*
00091   ** Get a cursor on the category
00092   **
00093   */
00094 #ifdef USE_SCOPED_POINTER
00095   QScopedPointer<cmcCursor> cs( cursor() );
00096 #else
00097   std::auto_ptr<cmcCursor> cs( cursor() );
00098 #endif
00099 
00100   /*
00101   ** Get an add rowset
00102   **
00103   */
00104 #ifdef USE_SCOPED_POINTER
00105   QScopedPointer<cmcRowSet> ars( cs-> getAddRowSet() );
00106 #else
00107   std::auto_ptr<cmcRowSet> ars( cs-> getAddRowSet() );
00108 #endif
00109 
00110   ars-> modifyRow( 0, "Item Name",     itemName                        );
00111   ars-> modifyRow( 0, "Priority",      priority                        );
00112   ars-> modifyRow( 0, "Program",       program                         );
00113   ars-> modifyRow( 0, "Queue Name",    objectName()                    );
00114   ars-> modifyRow( 0, "Arguments",     arguments                       );
00115   ars-> modifyRow( 0, "pRWD Employee", app()-> db()-> meInfo()-> key() );
00116   ars-> commit();
00117 
00118   timerEvent( NULL );
00119 
00120   return( true );
00121 
00122 }
00123 
00124 /*!
00125 ** \brief Application Timer Event
00126 **
00127 ** 
00128 **
00129 */
00130 void cmcQueue::timerEvent( QTimerEvent * event )
00131 {
00132   if( m_halt ) return;
00133   if( m_busy ) return;
00134 
00135   m_busy = true;
00136 
00137   /*
00138   ** Some simple sanity checking.
00139   **
00140   */
00141   if( hasQueueCategory() ) 
00142   {
00143     /*
00144     ** See if that category contains any rows
00145     **
00146     */
00147 #ifdef USE_SCOPED_POINTER
00148     QScopedPointer<cmcCursor> cs( cursor() );
00149 #else
00150     std::auto_ptr<cmcCursor> cs( cursor() );
00151 #endif
00152 
00153     /*
00154     ** If rows exist, we must pull the top one
00155     **
00156     */
00157     if( cs-> rowCount() > 0 )
00158     {
00159 #ifdef USE_SCOPED_POINTER
00160       QScopedPointer<cmcRowSet> rs( cs-> getDeleteRowSet(1) );
00161 #else
00162       std::auto_ptr<cmcRowSet> rs( cs-> getDeleteRowSet(1) );
00163 #endif
00164 
00165       QString program   = rs-> getRowValue( 0, "Program" );
00166       QString arguments = rs-> getRowValue( 0, "Arguments" );
00167 
00168       process-> start(program,arguments.split("\r\n"));
00169 
00170       if( rs-> deleteRow( 0 ) == -1 )
00171         qDebug( "unable to delete row" );
00172 
00173       if( rs-> commit() == -1 )
00174         qDebug( "unable to commit" );
00175 
00176       return;
00177     }
00178 
00179   }
00180 
00181   m_busy = false;
00182 
00183 }
00184 
00185 cmcCursor * cmcQueue::cursor()
00186 {
00187   cmcCursor * retVal = NULL;
00188 
00189   if( hasQueueCategory() )
00190   {
00191     retVal = app()-> db()-> getCursor( queueCategoryName() );
00192 
00193     /*
00194     ** Filter for items in this queue name
00195     **
00196     */
00197     retVal-> setFilter( "1,F,,Queue Name,Equal to," + dq(objectName()) );
00198 
00199     /*
00200     ** Filter for items attached to me
00201     **
00202     */
00203     retVal-> setFilter( "2,CTI,,pRWD,Employee," + dq(app()-> db()-> meInfo()-> key()) );
00204 
00205     /*
00206     ** Sort to get the most top priority item out
00207     **  of the queue
00208     **
00209     */
00210     retVal-> setSort( "Priority,Ascending,Seq,Ascending" );
00211 
00212   }
00213 
00214   return( retVal );
00215 }
00216 
00217 
00218 
00219 bool cmcQueue::isBusy()
00220 {
00221   return( m_busy );
00222 }
00223 
00224 bool cmcQueue::isHalted()
00225 {
00226   return( m_halt );
00227 }
00228 
00229 bool cmcQueue::setHalt( bool state )
00230 {
00231   m_halt = state;
00232   return( m_halt );
00233 }
00234 
00235 
00236 long cmcQueue::count()
00237 {
00238   long retVal = 0;
00239 
00240   if( hasQueueCategory() ) 
00241   {
00242 #ifdef USE_SCOPED_POINTER
00243     QScopedPointer<cmcCursor> cs( cursor() );
00244 #else
00245     std::auto_ptr<cmcCursor> cs( cursor() );
00246 #endif
00247 
00248     retVal = cs-> rowCount();
00249   }
00250 
00251   return( retVal );
00252 
00253 }
00254 
00255 bool cmcQueue::clear()
00256 {
00257   bool retVal = false;
00258 
00259   setHalt( true );
00260 
00261   if( hasQueueCategory() )
00262   {
00263 #ifdef USE_SCOPED_POINTER
00264     QScopedPointer<cmcCursor> cs( cursor() );
00265 #else
00266     std::auto_ptr<cmcCursor> cs( cursor() );
00267 #endif
00268 
00269     if( cs-> rowCount() )
00270     {
00271 #ifdef USE_SCOPED_POINTER
00272       QScopedPointer<cmcRowSet> rs( cs-> getDeleteRowSet() );
00273 #else
00274       std::auto_ptr<cmcRowSet> rs( cs-> getDeleteRowSet() );
00275 #endif
00276 
00277       for( int i = rs-> firstRow(); i <= rs-> lastRow(); i++ )
00278         rs-> deleteRow( i );
00279 
00280       rs-> commit();
00281     }
00282   }
00283 
00284   setHalt( false );
00285 
00286   return( retVal );
00287 }
00288 
00289 
00290 
00291 
00292 void cmcQueue::processFinished( int exitCode, QProcess::ExitStatus exitStatus )
00293 {
00294   m_busy = false;
00295   timerEvent( NULL );
00296 }
00297 
00298 
00299 
00300 } //endnamespace
00301 
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties




~ ~ ~ ~ ~ ~
Source Code without Comments is like a Cranberry Garland
without the berries. Comment your Code!
 
Commence Database User Support Group Forum
http://newsgroup.showoff-db.org/
~ ~ ~ ~ ~ ~
Author: Mark Petryk
Lorimark Solutions, LLC
mark@lorimarksolutions.com