00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef Session_HPP
00012 #define Session_HPP
00013
00014 #include <uuid/uuid.h>
00015 #include "cgiCookie.h"
00016 #include "ocXML.h"
00017 #include "read_write_base.hpp"
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 class Session_Obj: public read_write_base
00028 {
00029 public:
00030 identifier Id;
00031 llong User_Id;
00032 string UUID;
00033 time_date Instance_Stamp;
00034 string XML_Variables;
00035
00036 protected:
00037
00038 changeMap chMap;
00039 bool isGood;
00040 bool inCharge;
00041
00042 void makeUUID(void)
00043 {
00044 uuid_t uuid;
00045 char uuid_val[37];
00046 memset(uuid,'\0',sizeof(uuid));
00047 uuid_generate(uuid);
00048 uuid_unparse(uuid, uuid_val);
00049 ocString uuid_work(uuid_val);
00050 UUID = uuid_work.replaceAll("-","");
00051 }
00052
00053 public:
00054
00055 Session_Obj(bool inCharge=true):read_write_base()
00056 ,Id(0LL)
00057 ,User_Id(0LL)
00058 ,UUID("")
00059 ,Instance_Stamp()
00060 ,XML_Variables("")
00061 ,isGood(false)
00062 ,inCharge(inCharge)
00063 {
00064
00065
00066 data_name("metasite.Session");
00067
00068 addDXMap( new llongXfer("Id", &Id ));
00069 addDXMap( new llongXfer("User_Id", &User_Id ));
00070 addDXMap( new stringXfer("UUID", &UUID ));
00071 addDXMap( new time_dateXfer("Instance_Stamp", &Instance_Stamp ));
00072 addDXMap( new stringXfer("XML_Variables", &XML_Variables ));
00073
00074 cgiCookie cookie;
00075 cookie.setPath("/");
00076 UUID = cookie.get("SessionId");
00077 if( UUID.length() )
00078 {
00079
00080 string clause = " UUID = '" + UUID + "'";
00081 get_data(clause);
00082 }
00083
00084 if(Id==0 && inCharge==true)
00085 {
00086
00087 makeUUID();
00088 cookie.set("SessionId",UUID.c_str());
00089 if( db_insert() )
00090 {
00091 Id = key();
00092 }
00093 }
00094 }
00095 ~Session_Obj(){;}
00096
00097 string operator () (string Variable_Name )
00098 {
00099 return GetData( Variable_Name );
00100 }
00101
00102 string GetData( string Variable_Name )
00103 {
00104 string ret;
00105 xmlParser parser( XML_Variables );
00106 parser.parse();
00107 node_map & nodes = parser.states.nodemap;
00108 node_map::iterator iter;
00109 iter = nodes.lower_bound(Variable_Name);
00110 if(iter != nodes.upper_bound(Variable_Name))
00111 {
00112 ret = parser.nodeList()[iter->second].data;
00113 }
00114 return ret;
00115 }
00116 bool SetData( string Variable_Name, string Data )
00117 {
00118 bool ret = true;
00119 xmlParser parser( XML_Variables );
00120 parser.parse();
00121 node_map & nodes = parser.states.nodemap;
00122 node_map::iterator pos = nodes.lower_bound(Variable_Name);
00123 if(pos != nodes.upper_bound(Variable_Name))
00124 {
00125 parser.nodeList()[pos->second].data = Data;
00126 }
00127 else
00128 {
00129 xmlNode aNode;
00130 aNode.name = Variable_Name;
00131 aNode.data = Data;
00132 parser.addNode(aNode);
00133 }
00134 XML_Variables = parser.emit();
00135 return ret;
00136 }
00137 bool Synch()
00138 {
00139 if( Id )
00140 {
00141
00142 changeMap changes;
00143
00144
00145
00146
00147 changes["User_Id"]="User_Id";
00148 changes["Instance_Stamp"]="Instance_Stamp";
00149 changes["XML_Variables"]="XML_Variables";
00150
00151 isGood = db_update( changes );
00152 }
00153 else
00154 {
00155
00156 if(UUID.length())
00157 db_insert();
00158 Id = key();
00159 }
00160 return isGood;
00161 }
00162
00163 void killCookie( void )
00164 {
00165
00166 cgiCookie cookie;
00167 cookie.setPath("/");
00168 string & date = cookie.setTimeout( 1, 1, 1970, 0, 0 );
00169 cookie.set( "SessionId", "" );
00170 UUID = "";
00171 Id = User_Id = 0;
00172 }
00173 bool IsGood( void )
00174 {
00175 return isGood;
00176 };
00177 };
00178
00179 #endif
00180