00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef oPenTreeS_hPp
00013 #define oPenTreeS_hPp
00014
00015 #include <iostream>
00016 #include <iomanip>
00017 #include <uuid/uuid.h>
00018
00019 #include "ocTypes.h"
00020 #include "ocString.h"
00021 #include "read_base.hpp"
00022 #include "cgiTemplates.h"
00023
00024
00025 class baseTreeNode: public read_base
00026 {
00027 protected:
00028 ostream & webIO;
00029 cgiTemplates & treeTemplate;
00030
00031
00032 ocString treeStem;
00033 ocString treeSpacer;
00034 ocString nodeBranch;
00035 ocString nodeExpand;
00036 ocString nodeLeaf;
00037 ocString nodeLastBranch;
00038 ocString nodeElement;
00039 ocString nodeLeafEnd;
00040 ocString vizDiv;
00041 ocString vizDivEnd;
00042 int count;
00043 bool isLastNode;
00044
00045 public:
00046 baseTreeNode( cgiTemplates & templateIn, ostream & webIo )
00047 : webIO( webIo ),treeTemplate( templateIn ),count(0),level(0),isLastNode(false)
00048 {
00049 treeStem = treeTemplate.getParagraph("treeStem");
00050 treeSpacer = treeTemplate.getParagraph("treeSpacer");
00051
00052 nodeBranch = treeTemplate.getParagraph("nodeBranch");
00053 nodeExpand = treeTemplate.getParagraph("nodeExpand");
00054 nodeLeaf = treeTemplate.getParagraph("nodeLeaf");
00055 nodeLastBranch = treeTemplate.getParagraph("nodeLastBranch");
00056 nodeElement = treeTemplate.getParagraph("nodeElement");
00057 nodeLeafEnd = treeTemplate.getParagraph("nodeLeafEnd");
00058 vizDiv = treeTemplate.getParagraph("vizDiv");
00059 vizDivEnd = treeTemplate.getParagraph("vizDivEnd");
00060 }
00061 virtual ~baseTreeNode() {;}
00062 virtual bool emit( string filter = "" ){;}
00063 bool Count( void )
00064 {
00065 return count;
00066 }
00067
00068 ocString fields;
00069 ocString from;
00070 ocString location;
00071
00072
00073 ocString childFilter;
00074 int level;
00075
00076
00077 ocString label;
00078
00079 ocString addLink;
00080 string ParentId;
00081 };
00082
00083 typedef map<int,baseTreeNode*> nodeHandlers;
00084
00085 class ocTreeNode: public baseTreeNode
00086 {
00087 nodeHandlers & handlers;
00088 public:
00089 ocTreeNode( cgiTemplates & templateIn, ostream & webIo, nodeHandlers & inHandlers )
00090 :baseTreeNode( templateIn, webIo ),handlers(inHandlers)
00091 {
00092
00093 }
00094 virtual ~ocTreeNode(){;}
00095 virtual void emitStem( int stemLevel )
00096 {
00097 ocTreeNode * thatNode = 0;
00098 if( handlers.find(stemLevel) != handlers.end() )
00099 {
00100 thatNode = dynamic_cast<ocTreeNode*>(handlers[stemLevel]);
00101 if( thatNode->isLastNode )
00102 {
00103 webIO << treeSpacer;
00104 }
00105 else
00106 {
00107 webIO << treeStem;
00108 }
00109 }
00110 }
00111
00112 virtual string NodeLeaf( int level, string id )
00113 {
00114 ocString ID;
00115 ID.append(level);
00116 ID += "_";
00117 ID += id;
00118 return nodeLeaf.replace("$ID",ID.c_str());
00119 }
00120 virtual bool emit( string filter = "" )
00121 {
00122 bool bRet = false;
00123 isLastNode = false;
00124 int cursor = 0;
00125 string qry = "select " + fields;
00126 qry += " from " + from;
00127 qry += filter;
00128
00129 if( label.length() )
00130 {
00131 webIO << NodeLeaf(level,"LBL");
00132 for( int iLev = 0; iLev < level; iLev++ )
00133 {
00134 emitStem(iLev);
00135 }
00136 webIO << label << nodeLeafEnd << endl;
00137 }
00138 if( addLink.length() )
00139 {
00140 webIO << NodeLeaf(level,"0");
00141 for( int iLev = 0; iLev < level; iLev++ )
00142 {
00143 emitStem(iLev);
00144 }
00145 if( count == 1 )
00146 {
00147 webIO << nodeLastBranch;
00148 }
00149 else
00150 {
00151 webIO << nodeBranch;
00152 }
00153 webIO << addLink.replace("$ID",ParentId.c_str());
00154 webIO << nodeLeafEnd << endl;
00155 if( count ) count--;
00156 }
00157 if( rs.open( qry ) )
00158 {
00159 bRet = true;
00160
00161 do {
00162 cursor++;
00163 isLastNode = (cursor == count);
00164 string tempLoc = location.replace("$ID", rs.getField(0).format().c_str());
00165 webIO << NodeLeaf(level,rs.getField(0).format());
00166 for( int iLev = 0; iLev < level; iLev++ )
00167 {
00168 emitStem(iLev);
00169 }
00170 if( isLastNode )
00171 {
00172 webIO << nodeLastBranch;
00173 }
00174 else
00175 {
00176 webIO << nodeBranch;
00177 }
00178
00179 int childCount = 0;
00180 ocTreeNode * theNext = 0;
00181 string filter = childFilter.replace("$ID", rs.getField(0).format().c_str() );
00182 if( handlers.find(level+1) != handlers.end() )
00183 {
00184 theNext = dynamic_cast<ocTreeNode*>(handlers[level+1]);
00185 childCount = theNext->Count(filter);
00186 if( childCount > 0 )
00187 {
00188
00189 webIO << nodeExpand;
00190 }
00191 }
00192
00193 string label = "";
00194 for( int lx=1; lx<rs.getFieldCount(); lx++ )
00195 {
00196 if( label.length() && rs.getField(lx).format().length() ) label += " - ";
00197 label += rs.getField(lx).format();
00198 }
00199 webIO << nodeElement.replace("$location", tempLoc)
00200 .replace("$label", label );
00201 if( theNext && childCount > 0 )
00202 {
00203 webIO << vizDiv;
00204 theNext->ParentId = rs.getField(0).format();
00205 theNext->emit(filter);
00206 webIO << vizDivEnd;
00207 }
00208 webIO << nodeLeafEnd << endl;
00209
00210 } while( rs.next() );
00211 }
00212 return bRet;
00213 }
00214 bool Count( string filter = "" )
00215 {
00216 count = 0;
00217 string qry = "select count(*) from " + from;
00218 qry += filter;
00219 if( rs.open( qry ) )
00220 {
00221 count = atoi( rs.getField(0).format().c_str() );
00222 }
00223 if( addLink.length() ) count++;
00224
00225 return count;
00226 }
00227 };
00228
00229
00230
00231
00232
00233 class ocTreeControl
00234 {
00235 cgiTemplates treeTemplate;
00236 ostream & webIO;
00237 nodeHandlers handlers;
00238
00239
00240 ocString treeTop;
00241 ocString treeEnd;
00242 string UUID;
00243
00244
00245 public:
00246
00247 ocTreeControl( string templatePath, ostream & webIo )
00248 :webIO(webIo)
00249 {
00250 treeTemplate.load(templatePath.c_str());
00251 treeTop = treeTemplate.getParagraph("treetop");
00252 treeEnd = treeTemplate.getParagraph("treeEnd");
00253
00254 }
00255 virtual ~ ocTreeControl()
00256 {
00257 nodeHandlers::iterator it = handlers.begin();
00258 while( it != handlers.end() )
00259 {
00260 if( it->second ) delete it->second;
00261 ++it;
00262 }
00263 handlers.clear();
00264 }
00265 void persitantStates(void)
00266 {
00267 uuid_t uuid;
00268 char uuid_val[37];
00269 memset(uuid,'\0',sizeof(uuid));
00270 uuid_generate(uuid);
00271 uuid_unparse(uuid, uuid_val);
00272 ocString uuid_work(uuid_val);
00273 UUID = uuid_work.replaceAll("-","");
00274 }
00275 ocTreeNode * addHandler( int nodeLevel )
00276 {
00277 ocTreeNode * newNode = new ocTreeNode(treeTemplate, webIO, handlers);
00278 if( newNode )
00279 {
00280 newNode->level = nodeLevel;
00281 handlers.insert( make_pair( nodeLevel, newNode ) );
00282 }
00283 return newNode;
00284 }
00285 bool emit( string filter = "" )
00286 {
00287 webIO << treeTop;
00288
00289
00290 if( handlers.find(0) != handlers.end() )
00291 {
00292 ocTreeNode * theBase = dynamic_cast<ocTreeNode*>(handlers[0]);
00293 theBase->Count();
00294 theBase->emit(filter);
00295 }
00296 webIO << treeEnd;
00297 }
00298 };
00299
00300 #endif
00301
00302 #ifdef IN_T2_TESTHARNESS
00303 {
00304 cgiHead head;
00305 {
00306 cgiCan style("style", "type=\"text/css\"");
00307 style <<
00308 "div.viz, div.branch, div.leaf { font: normal 8pt/10pt san-serif; clear: left; }" << endl;
00309 style << "div.viz { margin: 0px; display:none; } "
00310 "img{ float: left; } "
00311 "a { display: block; float: left; margin-left: 4px; "
00312 "color: #059; text-decoration: none; }"<< endl;
00313
00314 }
00315 {
00316 cgiCan script("script"," type=\"text/javascript\" src=\"openTree.js\" ");
00317 }
00318 }
00319 {
00320 cgiBody body;
00321 body << "<h1>tree testing</h1>" << endl;
00322
00323 ocTreeControl tc( "tree.htmp", script );
00324 ocTreeNode * theBase = tc.addHandler( 0 );
00325 if( theBase )
00326 {
00327 theBase->fields = "id, name";
00328 theBase->from = "sites";
00329 theBase->location = "http://devlinux/sys/site_ui.cgi?id==$ID";
00330 theBase->childFilter = " where site_id = $ID ";
00331 theBase->label = "<span style=\"font: bold 12pt arial;text-decoration: underline;\">SITES</span>";
00332 }
00333 ocTreeNode * theNext = tc.addHandler( 1 );
00334 if( theNext )
00335 {
00336 theNext->fields = "id, name";
00337 theNext->from = "pages";
00338 theNext->location = "http://devlinux/sys/page_ui.cgi?id=$ID";
00339 theNext->childFilter = " where page_id = $ID ";
00340 theNext->label = "<span style=\"font: bold 9pt/10pt arial\">PAGES</span>";
00341 }
00342 ocTreeNode * theLast = tc.addHandler( 2 );
00343 if( theNext )
00344 {
00345 theLast->fields = "id, name";
00346 theLast->from = "paragraphs";
00347 theLast->location = "http://devlinux/sys/paragraph_ui.cgi?id==$ID";
00348 theLast->label = "<span style=\"font: bold 8pt/10p arial\">PARAGRAPHS</span>";
00349 }
00350 tc.emit();
00351 }
00352 #endif
00353
00354