00001
00046 #ifndef reform_base_HPP
00047 #define reform_base_HPP
00048
00049 #include <iostream>
00050 #include <iomanip>
00051 #include "ocTypes.h"
00052 #include "ocString.h"
00053 #include "read_base.hpp"
00054 #include "cgiClass.h"
00055 #include "cgiTemplates.h"
00056 #include <fstream>
00057 #include "Report.hpp"
00058 #include "Report_Params_ui.h"
00059 #include "col_handling_rep.hpp"
00060
00061 using namespace std;
00062
00063 class reform_base;
00064 void doSubReport ( reform_base & parent, llong repId );
00065
00066
00067
00068
00069
00070 class reform_base: public read_base
00071 {
00072 public:
00073 string beginDoc;
00074 ocString formTemplate;
00075 string endDoc;
00076 string pageBreak;
00077
00078 string cachedSQL;
00079 ostream & webIO;
00080 bool opened;
00081 bool more_data;
00082 long row;
00083
00084
00085
00086 reform_base(ostream&sc):read_base(),webIO(sc),
00087 opened(false),more_data(false),row(0)
00088 {
00089 }
00090
00091 virtual ~reform_base(){;}
00092
00093
00094 bool loadTemplate( string & path )
00095 {
00096 bool bRet = false;
00097 formTemplate = "";
00098 cgiTemplates templateFile;
00099 if( templateFile.load( path.c_str() ) )
00100
00101 {
00102 bRet = true;
00103
00104 paragraphMap & parags = templateFile.getParagraphs();
00105 paragraphMap::iterator pos = parags.find("begin");
00106 if( pos != parags.end() )
00107 {
00108 beginDoc = pos->second;
00109 endDoc = parags["end"];
00110 formTemplate = parags["middle"];
00111 pageBreak = parags["pagebreak"];
00112 }
00113 else
00114 {
00115 formTemplate = templateFile.getUnparsedHtml();
00116 }
00117 }
00118 return bRet;
00119 }
00120
00121 bool getData( string sql )
00122 {
00123 cachedSQL = sql;
00124 iGetData(sql);
00125 return opened;
00126 }
00127
00128 bool iGetData( string sql )
00129 {
00130 opened = rs.open(sql);
00131 return opened;
00132 }
00133
00134
00135 reform_base & emitData( void )
00136 {
00137 more_data = opened;
00138 int fieldCount;
00139 if( opened ) fieldCount = rs.getFieldCount();
00140
00141
00142 if( beginDoc.length() ) webIO << beginDoc;
00143
00144 derived_commence_event();
00145 while( more_data )
00146 {
00147 derived_prerow_event();
00148
00149
00150 parseAndReplace();
00151 derived_postrow_event();
00152
00153 if( pageBreak.length() ) webIO << pageBreak;
00154
00155 more_data = rs.next();
00156 if( more_data ) row++;
00157 }
00158 derived_complete_event();
00159 if( endDoc.length() ) webIO << endDoc;
00160
00161 return * this;
00162 }
00163 void parseAndReplace( void )
00164 {
00165 string::size_type start = 0;
00166 string::size_type found = 0;
00167 string attrDelim = "'\"";
00168 do
00169 {
00170 found = formTemplate.find("<markup",start);
00171 if( found != string::npos )
00172 {
00173 webIO << formTemplate.substr(start,found-start);
00174
00175 string::size_type typePos = formTemplate.find("type",found );
00176 if( typePos != string::npos )
00177 {
00178 typePos += 6;
00179 string::size_type typeEnd = formTemplate.find_first_of(attrDelim,typePos);
00180 if( typeEnd != string::npos )
00181 {
00182
00183 string theType = formTemplate.substr( typePos, typeEnd-typePos);
00184
00185 if( theType == "field" )
00186 {
00187
00188 string::size_type colNamePos = formTemplate.find("colname",found);
00189 if( colNamePos != string::npos )
00190 {
00191 colNamePos += 9;
00192 found = colNamePos;
00193 string::size_type colNameEnd = formTemplate.find_first_of(attrDelim,colNamePos);
00194 if( colNameEnd != string::npos )
00195 {
00196 found = colNameEnd;
00197 string colName = formTemplate.substr( colNamePos, colNameEnd-colNamePos);
00198
00199 sendField( colName );
00200 }
00201 }
00202 }
00203 else if( theType == "repeatarea" )
00204 {
00205
00206 llong repId = 0;
00207
00208
00209
00210 string::size_type ridPos = formTemplate.find("subrep",found);
00211 if( ridPos != string::npos )
00212 {
00213 ridPos += 8;
00214 found = ridPos;
00215 string::size_type ridEnd = formTemplate.find_first_of(attrDelim,ridPos);
00216 if( ridEnd != string::npos )
00217 {
00218 found = ridEnd;
00219 string strId = formTemplate.substr( ridPos, ridEnd-ridPos);
00220 if(strId.length() )
00221 {
00222 repId = atoll(strId.c_str());
00223 webIO << "<!-- rep Id " << repId << " --> ";
00224
00225 doSubReport( repId );
00226 }
00227 }
00228 }
00229 }
00230 }
00231 else
00232 {
00233 break;
00234 }
00235
00236 }
00237 if( found != string::npos )
00238 {
00239
00240 start = formTemplate.find("</markup>", found);
00241 if( start != string::npos )
00242 {
00243
00244 start += 9;
00245
00246 }
00247 else
00248 {
00249 webIO << "<h1>Bad Stuff: couldn't find end markup marker</h1>" << endl;
00250 break;
00251 }
00252 }
00253 }
00254 } while( found != string::npos );
00255
00256 webIO << formTemplate.substr(start);
00257 }
00258
00259
00260 virtual void sendField( string & fldName )
00261 {
00262 webIO << rs.getField(fldName.c_str()).format();
00263 }
00264
00265 virtual void doSubReport( llong Id );
00266
00267
00268 virtual void derived_commence_event( void )
00269 {
00270 ;
00271 }
00272 virtual void derived_prerow_event( void )
00273 {
00274 ;
00275 }
00276 virtual void derived_postrow_event( void )
00277 {
00278 ;
00279 }
00280 virtual void derived_complete_event( void )
00281 {
00282 ;
00283 }
00284 void columnTagFixup( ocString & tgt )
00285 {
00286 for( int i =0; i < rs.getFieldCount(); i++ )
00287 {
00288 ocString repTag = "{{";
00289 repTag.append(i);
00290 repTag += "}}";
00291 tgt = tgt.replaceAll(repTag, rs.getField(i).format());
00292 }
00293 }
00294 };
00295
00296 void reform_base::doSubReport ( llong repId )
00297 {
00298 Report_Obj report;
00299 report.key( repId );
00300 report.Id = repId;
00301 if( report.get_data() )
00302 {
00303 string path = "reportTemplates/" + report.getTemplatePath();
00304 ocString sql = report.getCompositeQuery();
00305 columnTagFixup(sql);
00306 col_handling_rep rpt(webIO);
00307 rpt.Formatting_Modulus = report.Formatting_Modulus;
00308 rpt. Items_Per_Page = report.Items_Per_Page;
00309 rpt.loadTemplates(path);
00310
00311 ocString top = rpt.part("top");
00312 columnTagFixup( top );
00313 rpt.reportParts.getParagraph("top") = top;
00314
00315 ocString pgbrk = rpt.part("pagebreak");
00316 if( pgbrk.length() )
00317 {
00318 columnTagFixup(pgbrk);
00319 rpt.reportParts.getParagraph("pagebreak") = pgbrk;
00320 }
00321 rpt.addColumnHandlers( report.Id );
00322
00323 if( rpt.getData(sql) )
00324 {
00325 rpt.emitTop(report.Name);
00326 rpt.emitHeader();
00327 rpt.emitData();
00328 rpt.emitEnd();
00329 }
00330 else
00331 {
00332 webIO << "<!-- " << sql << " -->" << endl;
00333 }
00334 }
00335 }
00336
00337
00338
00339 #endif