00001
00021 #ifndef Audit_Base_Hpp
00022 #define Audit_Base_Hpp
00023
00024
00025 #include "read_write_base.hpp"
00026 #include "Deletion.hpp"
00027 #include "FieldChange.hpp"
00028 #include "AuditTable.hpp"
00029 #include <set>
00030
00031 using namespace std;
00032
00033
00034 class ref_base;
00035
00037 typedef set<string> auditSet;
00038
00040 class audit_base : public read_write_base
00041 {
00042 protected:
00043
00044 auditSet audit_fields;
00045 llong user_id;
00046 string db_name;
00047
00048 enum {
00049 audit_nil=0,
00050 audit_insert=1,
00051 audit_update=2,
00052 audit_delete=3
00053 } audit_type;
00054
00055 bool getOldValues( changeMap & changes )
00056 {
00057 bool btrue = true;
00058 ocString select = "select ";
00059 auditSet::iterator pos;
00060 for( pos=audit_fields.begin(); pos != audit_fields.end(); )
00061 {
00062 select += *pos;
00063 ++pos;
00064 if( pos != audit_fields.end() )
00065 select += ", ";
00066 }
00067 select += " from ";
00068 select += data_name();
00069 select += " where Id = ";
00070 select.append(key());
00071 if( rs.open(select) )
00072 {
00073 for( pos=audit_fields.begin(); pos != audit_fields.end(); ++pos )
00074 {
00075 string name = *pos;
00076 string value = rs.getField(name.c_str()).format();
00077
00078 if( audit_type == audit_update )
00079 {
00080
00081 if( changes.find(name)!=changes.end() || audit_type == audit_delete )
00082 {
00083 changes[name] = value;
00084 }
00085 }
00086 else if( audit_type == audit_delete )
00087 {
00088
00089 changes[name] = value;
00090 }
00091 }
00092 rs.close();
00093 }
00094 return btrue;
00095 }
00096 bool changeAudits( changeMap & changes, AuditTable_Obj & audt)
00097 {
00098 changeMap::iterator pos;
00099 for( pos=changes.begin(); pos != changes.end(); ++pos )
00100 {
00101 auditSet::iterator spos = audit_fields.find( pos->first );
00102 if( spos != audit_fields.end() )
00103 {
00104 FieldChange_Obj fcng;
00105 fcng.auditTable = audt.key();
00106 fcng.Name = pos->first;
00107 fcng.Value = pos->second;
00108 fcng.db_insert();
00109 }
00110 }
00111 return true;
00112 }
00113 bool deleteAudits( changeMap & changes, AuditTable_Obj & audt )
00114 {
00115 changeMap::iterator pos;
00116 for( pos=changes.begin(); pos != changes.end(); ++pos )
00117 {
00118 Deletion_Obj fdel;
00119 fdel.auditTable = audt.key();
00120 fdel.Name = pos->first;
00121 fdel.Value = pos->second;
00122 }
00123 return true;
00124 }
00125 bool performAudit( changeMap & changes )
00126 {
00127 if( !(audit_type==audit_update && changes.size()==0) )
00128 {
00129 AuditTable_Obj audt;
00130 audt.Actor = user_id;
00131 audt.aDatabase = db_name;
00132 audt.aTable = data_name();
00133 audt.auditType = static_cast<int>( audit_type );
00134 audt.aRow = key();
00135 audt.db_insert();
00136 switch( audit_type)
00137 {
00138 case audit_update:
00139 getOldValues( changes );
00140 changeAudits( changes, audt );
00141 break;
00142 case audit_delete:
00143 getOldValues( changes );
00144 deleteAudits( changes, audt );
00145 break;
00146 default:
00147 break;
00148 }
00149 }
00150 return true;
00151 }
00152
00153 public:
00154 audit_base():read_write_base(),user_id(0),audit_type(audit_nil){;}
00155 virtual ~audit_base(){;}
00156
00157 bool db_insert( void )
00158 {
00159 changeMap changes;
00160 audit_type = audit_insert;
00161 performAudit( changes );
00162 return read_write_base::db_insert();
00163 }
00164 bool db_update( changeMap & changes )
00165 {
00166 audit_type = audit_update;
00167 performAudit( changes );
00168 return read_write_base::db_update(changes);
00169 }
00170 bool db_delete( void )
00171 {
00172 changeMap changes;
00173 audit_type = audit_delete;
00174 performAudit( changes );
00175 return read_write_base::db_delete();
00176 }
00177
00178
00179 audit_base & setUser( llong id )
00180 {
00181 user_id = id;
00182 return *this;
00183 }
00184
00185 audit_base & setDbName( string name )
00186 {
00187 db_name = name;
00188 return *this;
00189 }
00190
00191 audit_base & auditField( string name )
00192 {
00193 audit_fields.insert(name);
00194 return *this;
00195 }
00196
00197 audit_base & auditAll( void )
00198 {
00199 int count = rs.getFieldCount();
00200 for( int ifc = 0; ifc<count; ++ifc )
00201 {
00202 audit_fields.insert(rs.getField(ifc).getName());
00203 }
00204 return *this;
00205 }
00206 };
00207
00208 #endif