ffead.server.doc
AfcUtil.cpp
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * AfcUtil.cpp
18  *
19  * Created on: Aug 27, 2009
20  * Author: sumeet
21  */
22 
23 #include "AfcUtil.h"
24 #include <dlfcn.h>
25 typedef map<string,strVec> mapofvec;
26 
27 Logger AfcUtil::logger;
28 
29 AfcUtil::AfcUtil() {
30  logger = Logger::getLogger("AfcUtil");
31 }
32 
33 AfcUtil::~AfcUtil() {
34  // TODO Auto-generated destructor stub
35 }
36 static map<string,string> doneMap;
37 string AfcUtil::generateJsObjectsAll(vector<string> obj,strVec files,vector<bool> stat,string &headers,string &objs,string &infjs,vector<string> pv,
38  string& ajaxret, string& typrefs,map<string, string> ajintpthMap)
39 {
40  Reflection ref;
41  string ttem = "Date readDate(JSONElement& obj){DateFormat formt(\"yyyy-mm-dd hh:mi:ss\");\nreturn *formt.parse(obj.getValue());}";
42  ttem += "Date* readDateP(JSONElement& obj){DateFormat formt(\"yyyy-mm-dd hh:mi:ss\");\nreturn formt.parse(obj.getValue());}";
43  ttem += "\nstring fromDateToJSON(Date d){DateFormat formt(\"yyyy-mm-dd hh:mi:ss\");\nreturn formt.format(d);}";
44  ttem += "\nstring fromDateVPToJSON(Date *d){DateFormat formt(\"yyyy-mm-dd hh:mi:ss\");\nreturn formt.format(*d);}";
45  ttem += "BinaryData readBinaryData(JSONElement& obj){BinaryData bd;return bd;}";
46  ttem += "BinaryData* readBinaryDataP(JSONElement& obj){return new BinaryData;}";
47  ttem += "\nstring fromBinaryDataToJSON(BinaryData d){return \"\";}";
48  ttem += "\nstring fromBinaryDataVPToJSON(BinaryData *d){return \"\";}";
49  ajaxret = ttem + typrefs + ajaxret;
50  string ret ="#include \"AfcInclude.h\"\n\nextern \"C\"\n{\n" + ajaxret;
51  headers += "#include \"CastUtil.h\"\n#include \"JSONUtil.h\"\n#include \"sstream\"\n#include <algorithm>\n#include \"JSONSerialize.h\"\n";
52  headers += "#include \"set\"\n#include \"list\"\n#include \"queue\"\n#include \"deque\"\n#include \"DateFormat.h\"\n";
53  if(obj.size()==0)return ret + "}";
54  //ret += "\nconst mValue& find_value(const mObject& obj, const string& name)\n{\nmObject::const_iterator i = obj.find(name);\nreturn i->second;\n}\n";
55  for (unsigned int var = 0; var < obj.size(); ++var)
56  {
57  /*if(stat.at(var))
58  {
59  strVec pinfo;
60  bool isOpForSet = false;
61  strVec info = ref.getAfcObjectData(obj.at(var)+files.at(var)+".h", true,pinfo,isOpForSet);
62  ret += generateJsObjects(info,files.at(var),headers,obj.at(var),objs,pinfo,isOpForSet);
63  }
64  else*/
65  if(!stat.at(var))
66  {
67  //logger << "=============" << obj.at(var)+files.at(var)+".h" << flush;
68  strVec pinfo;
69  bool isOpForSet = false;
70  strVec info = ref.getAfcObjectData(obj.at(var)+files.at(var)+".h", false,pinfo,isOpForSet);
71  ret += generateJsInterfaces(info,files.at(var),headers,obj.at(var),infjs,pv.at(var),ajintpthMap);
72  }
73  }
74  //headers += "\nusing namespace json_spirit;\n";
75  ret = (ret + "\n}\n");
76  //writeTofile("/home/sumeet/workspace/inter/AfcInclude.h",headers,true);
77  return ret;
78 }
79 
80 string AfcUtil::generateJsObjects(strVec obj,string claz,string &headers,string path,string &objs,strVec pobj, bool isOpForSet, string& typrefs,strVec minfo)
81 {
82  if(doneMap.find(claz)==doneMap.end())
83  doneMap[claz] = "done";
84  else
85  return "";
86  string tes,tes1,fres,fres1;
87  Reflection ref;
88  string retu;
89  string test = "function _"+claz+"()\n{\n";
90  tes += "JSONElement* _node;\n";
91  tes1 += "JSONElement* _node;\n";
92  fres = "string json=\"{\";\n";
93  fres1 = "string json=\"{\";\n";
94  bool priv = false;
95  size_t tess;
96  string meth;
97  strVec fldnames;
98  map<string, int> fldstat;
99  for (unsigned int i = 0; i < pobj.size(); i++)
100  {
101  if(((tess=pobj.at(i).find("("))==string::npos && (tess=pobj.at(i).find(")"))==string::npos && pobj.at(i).find("~")==string::npos))
102  {
103  string fld = pobj.at(i);
104  StringUtil::replaceFirst(fld,";","");
105  RegexUtil::replace(fld, "[\t]+", " ");
106  RegexUtil::replace(fld, "[ ]+", " ");
107  strVec fldp;
108  StringUtil::split(fldp, fld, (" "));
109  if(fldp.size()==2)
110  {
111  string nam = fldp.at(1);
112  fldnames.push_back(fldp.at(0));
113  fldnames.push_back(nam);
114  fldstat[nam] = 0;
115  }
116  }
117  }
118  for(unsigned int i=0;i<minfo.size();i++)
119  {
120  if((tess=minfo.at(i).find("("))!=string::npos && (tess=minfo.at(i).find(")"))!=string::npos && minfo.at(i).find("~")==string::npos
121  && fldnames.size()>0)
122  {
123  meth = minfo.at(i);
124  StringUtil::replaceFirst(meth,";","");
125  RegexUtil::replace(meth, "[\t]+", " ");
126  RegexUtil::replace(meth, "[ ]+", " ");
127  RegexUtil::replace(meth, "[ ?, ?]+", ",");
128  //bool ptr = false;
129  if(meth.find("*")!=string::npos)
130  {
131  //ptr = true;
132  StringUtil::replaceFirst(meth,"*","");
133  }
134 
135  string argts = meth.substr(meth.find("("),meth.find(")")-meth.find("("));
136  StringUtil::replaceFirst(argts,"(","");
137  StringUtil::replaceAll(argts,")","");
138  meth = meth.substr(0,meth.find("("));
139 
140  if(meth.find("operator")!=string::npos)
141  {
142  }
143  else
144  {
145  strVec methp,methpm,argp,argpm;
146  StringUtil::split(argp, argts, (","));
147  StringUtil::split(methp, meth, (" "));
148  for(unsigned int j = 0; j < methp.size(); j++)
149  {
150  if(methp.at(j)!="")
151  methpm.push_back(methp.at(j));
152  }
153  for(unsigned int j = 0; j < argp.size(); j++)
154  {
155  if(argp.at(j)!="" && argp.at(j)!="(")
156  {
157  string tty = argp.at(j);
158  StringUtil::trim(tty);
159  if(tty.find(" ")!=string::npos)
160  {
161  vector<string> temargt = StringUtil::split(tty, " ");
162  argpm.push_back(temargt.at(0));
163  }
164  else
165  {
166  argpm.push_back(tty);
167  }
168  }
169  }
170 
171  if(methpm.at(0)!=claz)
172  {
173  for(unsigned int k = 0; k < fldnames.size(); k=k+2)
174  {
175  string cam = AfcUtil::camelCased(fldnames.at(k+1));
176  if("set"+cam==methpm.at(1) && argpm.size()==1 && argpm.at(0)==fldnames.at(k) && methpm.at(0)=="void")
177  {
178  fldstat[fldnames.at(k+1)]++;
179  }
180  else if("get"+cam==methpm.at(1) && argpm.size()==0 && methpm.at(0)==fldnames.at(k))
181  {
182  fldstat[fldnames.at(k+1)]++;
183  }
184  }
185  }
186  }
187  }
188  }
189  pobj.clear();
190  for(unsigned int k = 0; k < fldnames.size(); k=k+2)
191  {
192  if(fldstat[fldnames.at(k+1)]==2)
193  {
194  string data = fldnames.at(k) + " " + fldnames.at(k+1) + ";";
195  //logger << "===========> " << data << endl;
196  pobj.push_back(data);
197  }
198  }
199  for(unsigned int i=0;i<pobj.size();i++)
200  {
201  obj.push_back(pobj.at(i));
202  }
203  strVec tobj;
204  for(unsigned int i=0;i<obj.size();i++)
205  {
206  strVec vemp;
207  string data = obj.at(i);
208  //bool ptr = false;
209  if(data.find("*")!=string::npos)
210  {
211  //ptr = true;
212  StringUtil::replaceFirst(data,"*","");
213  }
214  StringUtil::replaceFirst(data,";","");
215  StringUtil::split(vemp, data, (" "));
216  if(vemp.size()<2)
217  {
218  //logger << data << " error" << endl;
219  }
220  else
221  {
222  tobj.push_back(obj.at(i));
223  }
224  }
225  obj = tobj;
226  for(unsigned int i=0;i<obj.size();i++)
227  {
228  strVec vemp;
229  string data = obj.at(i);
230  bool ptr = false;
231  if(data.find("*")!=string::npos)
232  {
233  ptr = true;
234  StringUtil::replaceFirst(data,"*","");
235  }
236  StringUtil::replaceFirst(data,";","");
237  StringUtil::split(vemp, data, (" "));
238  if(vemp.size()<2)
239  {
240  //logger << data << " error" << endl;
241  continue;
242  }
243 
244  priv = (fldstat[vemp.at(1)]==2?true:false);
245 
246  test += "this." + vemp.at(1) + "= null;\n";
247  tes += generateReadObjects(vemp.at(0), vemp.at(1), priv, ptr, ".");
248  tes1 += generateReadObjects(vemp.at(0), vemp.at(1), priv, ptr, "->");
249  fres += generateToJSONObjects(vemp.at(0), vemp.at(1), priv, obj, i, retu, headers, path, objs, ".", ptr);
250  fres1 += generateToJSONObjects(vemp.at(0), vemp.at(1), priv, obj, i, retu, headers, path, objs, "->", ptr);
251  }
252 
253  /*for(unsigned int i=0;i<pobj.size();i++)
254  {
255  priv = true;
256  strVec vemp;
257  string data = pobj.at(i);
258  bool ptr = false;
259  if(data.find("*")!=string::npos)
260  {
261  ptr = true;
262  StringUtil::replaceFirst(data,"*","");
263  }
264  StringUtil::replaceFirst(data,";","");
265  StringUtil::split(vemp, data, (" "));
266  if(vemp.size()<2)
267  {
268  logger << data << " error" << endl;
269  continue;
270  }
271 
272  test += "this." + vemp.at(1) + "= null;\n";
273  tes += generateReadObjects(vemp.at(0), vemp.at(1), priv, ptr, ".");
274  tes1 += generateReadObjects(vemp.at(0), vemp.at(1), priv, ptr, "->");
275  fres += generateToJSONObjects(vemp.at(0), vemp.at(1), priv, pobj, i, retu, headers, path, objs, ".", ptr);
276  fres1 += generateToJSONObjects(vemp.at(0), vemp.at(1), priv, pobj, i, retu, headers, path, objs, "->", ptr);
277  }*/
278  fres += "\njson += \"}\";";
279  fres1 += "\njson += \"}\";";
280  //logger << fres << flush;
281  test += "}";
282  //logger << test << flush;
283  objs += test;
284  headers += "#include \""+claz+".h\"\n";
285 
286  typrefs += claz + " read"+claz+"(JSONElement& obj);\n" + claz + "* read"+claz+"P(JSONElement& obj);\n";
287  typrefs += "\nstring from"+claz+"ToJSON("+claz+" _obj);\nstring from"+claz+"VPToJSON("+claz+"* _obj);\n";
288  typrefs += claz + " to"+claz+"(string s);\n" + claz + "* to"+claz+"P(string s);\n";
289  typrefs += "vector<"+claz + "> to"+claz+"Vec(string s);\nvector<" + claz + ">* to"+claz+"VecVP(string s);\n";
290  typrefs += "list<"+claz + "> to"+claz+"Lis(string s);\nlist<" + claz + ">* to"+claz+"LisVP(string s);\n";
291  typrefs += "deque<"+claz + "> to"+claz+"Dq(string s);\ndeque<" + claz + ">* to"+claz+"DqVP(string s);\n";
292  typrefs += "std::queue<"+claz + "> to"+claz+"Q(string s);\nstd::queue<" + claz + ">* to"+claz+"QVP(string s);\n";
293  if(isOpForSet)
294  {
295  typrefs += "set<"+claz + "> to"+claz+"Set(string s);\nset<" + claz + ">* to"+claz+"SetVP(string s);\n";
296  typrefs += "multiset<"+claz + "> to"+claz+"MulSet(string s);\nmultiset<" + claz + ">* to"+claz+"MulSetVP(string s);\n";
297  }
298  typrefs += "\nstring from"+claz+"VecToJSON(vector<"+claz+"> _obj);\nstring from"+claz+"VecVPToJSON(vector<"+claz+">* _obj);\n";
299  typrefs += "\nstring from"+claz+"LisToJSON(list<"+claz+"> _obj);\nstring from"+claz+"LisVPToJSON(list<"+claz+">* _obj);\n";
300  typrefs += "\nstring from"+claz+"DqToJSON(deque<"+claz+"> _obj);\nstring from"+claz+"DqVPToJSON(deque<"+claz+">* _obj);\n";
301  typrefs += "\nstring from"+claz+"QToJSON(std::queue<"+claz+"> _obj);\nstring from"+claz+"QVPToJSON(std::queue<"+claz+">* _obj);\n";
302  if(isOpForSet)
303  {
304  typrefs += "\nstring from"+claz+"SetToJSON(set<"+claz+"> _obj);\nstring from"+claz+"SetVPToJSON(set<"+claz+">* _obj);\n";
305  typrefs += "\nstring from"+claz+"MulSetToJSON(multiset<"+claz+"> _obj);\nstring from"+claz+"MulSetVPToJSON(multiset<"+claz+">* _obj);\n";
306  }
307 
308  test = retu+ "\n\n" + claz + " read"+claz+"(JSONElement& obj)\n{\n"+claz+" _obj;\n";
309  test += tes + "\nreturn _obj;\n}\n";
310  test += "\n\n" + claz + "* read"+claz+"P(JSONElement& obj)\n{\n"+claz+"* _obj = new "+claz+";\n";
311  test += tes1 + "\nreturn _obj;\n}\n";
312  test += claz + " to"+claz+"(string s)\n{\nJSONElement element = JSONUtil::getDocument(s);\n"+ claz +" _obj = read"+claz+"(element);\nreturn _obj;\n}\n";
313  test += "vector<"+claz+"> to"+claz+"Vec(string s)\n{\nJSONElement element = JSONUtil::getDocument(s);\n" +
314  "vector<"+claz+"> vec;\nfor(int i=0;i<(int)element.getChildren().size();i++){\n" +
315  claz +" _obj = read"+claz+"(*element.getChildren().at(i));\nvec.push_back(_obj);\n" +
316  "}\nreturn vec;\n}\n";
317  test += "list<"+claz+"> to"+claz+"Lis(string s)\n{\nvector<"+claz+"> vec = to"+claz+"Vec(s);\n" +
318  "list<"+claz+"> tt;\nstd::copy(vec.begin(), vec.end(), std::back_inserter(tt));\n" +
319  "return tt;\n}\n";
320  if(isOpForSet)
321  {
322  test += "set<"+claz+"> to"+claz+"Set(string s)\n{\nvector<"+claz+"> vec = to"+claz+"Vec(s);\n" +
323  "set<"+claz+"> tt;\nstd::copy(vec.begin(), vec.end(), std::inserter(tt, tt.begin()));\n" +
324  "return tt;\n}\n";
325  test += "multiset<"+claz+"> to"+claz+"MulSet(string s)\n{\nvector<"+claz+"> vec = to"+claz+"Vec(s);\n" +
326  "multiset<"+claz+"> tt;\nstd::copy(vec.begin(), vec.end(), std::inserter(tt, tt.begin()));\n" +
327  "return tt;\n}\n";
328  }
329  test += "deque<"+claz+"> to"+claz+"Dq(string s)\n{\nvector<"+claz+"> vec = to"+claz+"Vec(s);\n" +
330  "deque<"+claz+"> tt;\nstd::copy(vec.begin(), vec.end(), std::inserter(tt, tt.begin()));\n" +
331  "return tt;\n}\n";
332  test += "std::queue<"+claz+"> to"+claz+"Q(string s)\n{\nvector<"+claz+"> vec = to"+claz+"Vec(s);\n" +
333  "std::queue<"+claz+"> tt;for (int var = 0; var < (int)vec.size(); ++var) {\ntt.push(vec.at(var));\n}\n" +
334  "return tt;\n}\n";
335  test += "vector<"+claz+">* to"+claz+"VecVP(string s)\n{\nJSONElement element = JSONUtil::getDocument(s);\n" +
336  "vector<"+claz+">* vec = new vector<"+claz+">;\nfor(int i=0;i<element.getChildren().size();i++){\n" +
337  claz +" _obj = read"+claz+"(*element.getChildren().at(i));\nvec->push_back(_obj);\n" +
338  "}\nreturn vec;\n}\n";
339  test += "list<"+claz+">* to"+claz+"LisVP(string s)\n{\nvector<"+claz+"> vec = to"+claz+"Vec(s);\n" +
340  "list<"+claz+">* tt = new list<"+claz+">;\nstd::copy(vec.begin(), vec.end(), std::back_inserter(*tt));\n" +
341  "return tt;\n}\n";
342  if(isOpForSet)
343  {
344  test += "set<"+claz+">* to"+claz+"SetVP(string s)\n{\nvector<"+claz+"> vec = to"+claz+"Vec(s);\n" +
345  "set<"+claz+">* tt = new set<"+claz+">;\nstd::copy(vec.begin(), vec.end(), std::inserter(*tt, tt->begin()));\n" +
346  "return tt;\n}\n";
347  test += "multiset<"+claz+">* to"+claz+"MulSetVP(string s)\n{\nvector<"+claz+"> vec = to"+claz+"Vec(s);\n" +
348  "multiset<"+claz+">* tt = new multiset<"+claz+">;\nstd::copy(vec.begin(), vec.end(), std::inserter(*tt, tt->begin()));\n" +
349  "return tt;\n}\n";
350  }
351  test += "deque<"+claz+">* to"+claz+"DqVP(string s)\n{\nvector<"+claz+"> vec = to"+claz+"Vec(s);\n" +
352  "deque<"+claz+">* tt = new deque<"+claz+">;\nstd::copy(vec.begin(), vec.end(), std::inserter(*tt, tt->begin()));\n" +
353  "return tt;\n}\n";
354  test += "std::queue<"+claz+">* to"+claz+"QVP(string s)\n{\nvector<"+claz+"> vec = to"+claz+"Vec(s);\n" +
355  "std::queue<"+claz+">* tt = new std::queue<"+claz+">;for (int var = 0; var < (int)vec.size(); ++var) {\ntt->push(vec.at(var));\n}\n" +
356  "return tt;\n}\n";
357  test += "void* toVoidP"+claz+"(string s)\n{\nJSONElement element = JSONUtil::getDocument(s);\n"+ claz +" *_obj = new "+claz+";\n*_obj = read"+claz+"(element);\nreturn _obj;\n}\n";
358  test += "\nstring from"+claz+"ToJSON("+claz+" _obj)\n{\n"+fres+"\nreturn json;\n}\n";
359  test += "\nstring from"+claz+"VPToJSON("+claz+"* _obj)\n{\n"+fres1+"\nreturn json;\n}\n";
360  test += "\nstring from"+claz+"VecToJSON(vector<"+claz+"> _vecobj)\n{\nstring json = \"[\";\nfor(int i=0;i<(int)_vecobj.size();i++){\n" +
361  "json += from"+claz+"ToJSON(_vecobj.at(i));\n" +
362  "if(i!=(int)_vecobj.size()-1)json += \",\";\n" +
363  "\n}\njson += \"]\";\nreturn json;\n}\n";
364  test += "\nstring from"+claz+"LisToJSON(list<"+claz+"> _lisobj)\n{\nvector<"+claz+"> _vecobj;\nstd::copy(_lisobj.begin(), _lisobj.end(), std::back_inserter(_vecobj));" +
365  "\nstring json = \"[\";\nfor(int i=0;i<(int)_vecobj.size();i++){\n" +
366  "json += from"+claz+"ToJSON(_vecobj.at(i));\n" +
367  "if(i!=(int)_vecobj.size()-1)json += \",\";\n" +
368  "\n}\njson += \"]\";\nreturn json;\n}\n";
369  if(isOpForSet)
370  {
371  test += "\nstring from"+claz+"SetToJSON(set<"+claz+"> _lisobj)\n{\nvector<"+claz+"> _vecobj;\nstd::copy(_lisobj.begin(), _lisobj.end(), std::back_inserter(_vecobj));" +
372  "\nstring json = \"[\";\nfor(int i=0;i<(int)_vecobj.size();i++){\n" +
373  "json += from"+claz+"ToJSON(_vecobj.at(i));\n" +
374  "if(i!=(int)_vecobj.size()-1)json += \",\";\n" +
375  "\n}\njson += \"]\";\nreturn json;\n}\n";
376  test += "\nstring from"+claz+"MulSetToJSON(multiset<"+claz+"> _lisobj)\n{\nvector<"+claz+"> _vecobj;\nstd::copy(_lisobj.begin(), _lisobj.end(), std::back_inserter(_vecobj));" +
377  "\nstring json = \"[\";\nfor(int i=0;i<(int)_vecobj.size();i++){\n" +
378  "json += from"+claz+"ToJSON(_vecobj.at(i));\n" +
379  "if(i!=(int)_vecobj.size()-1)json += \",\";\n" +
380  "\n}\njson += \"]\";\nreturn json;\n}\n";
381  }
382  test += "\nstring from"+claz+"DqToJSON(deque<"+claz+"> _lisobj)\n{\nvector<"+claz+"> _vecobj;\nstd::copy(_lisobj.begin(), _lisobj.end(), std::back_inserter(_vecobj));" +
383  "\nstring json = \"[\";\nfor(int i=0;i<(int)_vecobj.size();i++){\n" +
384  "json += from"+claz+"ToJSON(_vecobj.at(i));\n" +
385  "if(i!=(int)_vecobj.size()-1)json += \",\";\n" +
386  "\n}\njson += \"]\";\nreturn json;\n}\n";
387  test += "\nstring from"+claz+"QToJSON(std::queue<"+claz+"> _lisobj)\n{\nvector<"+claz+"> _vecobj;" +
388  "std::queue<"+claz+"> qq = _lisobj;\nfor (int var = 0; var < (int)qq.size(); ++var)\n{" +
389  "_vecobj.push_back(qq.front());\nqq.pop();\n}\n" +
390  "\nstring json = \"[\";\nfor(int i=0;i<(int)_vecobj.size();i++){\n" +
391  "json += from"+claz+"ToJSON(_vecobj.at(i));\n" +
392  "if(i!=(int)_vecobj.size()-1)json += \",\";\n" +
393  "\n}\njson += \"]\";\nreturn json;\n}\n";
394  test += "\nstring from"+claz+"VecVPToJSON(vector<"+claz+">* _vecobj)\n{\nstring json = \"[\";\nfor(int i=0;i<_vecobj->size();i++){\n" +
395  "json += from"+claz+"ToJSON(_vecobj->at(i));\n" +
396  "if(i!=_vecobj->size()-1)json += \",\";\n" +
397  "\n}\njson += \"]\";\nreturn json;\n}\n";
398  test += "\nstring from"+claz+"LisVPToJSON(list<"+claz+">* _lisobj)\n{\nvector<"+claz+"> _vecobj;\nstd::copy(_lisobj->begin(), _lisobj->end(), std::back_inserter(_vecobj));" +
399  "\nstring json = \"[\";\nfor(int i=0;i<(int)_vecobj.size();i++){\n" +
400  "json += from"+claz+"ToJSON(_vecobj.at(i));\n" +
401  "if(i!=(int)_vecobj.size()-1)json += \",\";\n" +
402  "\n}\njson += \"]\";\nreturn json;\n}\n";
403  if(isOpForSet)
404  {
405  test += "\nstring from"+claz+"SetVPToJSON(set<"+claz+">* _lisobj)\n{\nvector<"+claz+"> _vecobj;\nstd::copy(_lisobj->begin(), _lisobj->end(), std::back_inserter(_vecobj));" +
406  "\nstring json = \"[\";\nfor(int i=0;i<(int)_vecobj.size();i++){\n" +
407  "json += from"+claz+"ToJSON(_vecobj.at(i));\n" +
408  "if(i!=(int)_vecobj.size()-1)json += \",\";\n" +
409  "\n}\njson += \"]\";\nreturn json;\n}\n";
410  test += "\nstring from"+claz+"MulSetVPToJSON(multiset<"+claz+">* _lisobj)\n{\nvector<"+claz+"> _vecobj;\nstd::copy(_lisobj->begin(), _lisobj->end(), std::back_inserter(_vecobj));" +
411  "\nstring json = \"[\";\nfor(int i=0;i<(int)_vecobj.size();i++){\n" +
412  "json += from"+claz+"ToJSON(_vecobj.at(i));\n" +
413  "if(i!=(int)_vecobj.size()-1)json += \",\";\n" +
414  "\n}\njson += \"]\";\nreturn json;\n}\n";
415  }
416  test += "\nstring from"+claz+"DqVPToJSON(deque<"+claz+">* _lisobj)\n{\nvector<"+claz+"> _vecobj;\nstd::copy(_lisobj->begin(), _lisobj->end(), std::back_inserter(_vecobj));" +
417  "\nstring json = \"[\";\nfor(int i=0;i<(int)_vecobj.size();i++){\n" +
418  "json += from"+claz+"ToJSON(_vecobj.at(i));\n" +
419  "if(i!=(int)_vecobj.size()-1)json += \",\";\n" +
420  "\n}\njson += \"]\";\nreturn json;\n}\n";
421  test += "\nstring from"+claz+"QVPToJSON(std::queue<"+claz+">* _lisobj)\n{\nvector<"+claz+"> _vecobj;\n" +
422  "std::queue<"+claz+"> qq = *_lisobj;\nfor (int var = 0; var < (int)qq.size(); ++var)\n{" +
423  "_vecobj.push_back(qq.front());\nqq.pop();\n}\n" +
424  "\nstring json = \"[\";\nfor(int i=0;i<(int)_vecobj.size();i++){\n" +
425  "json += from"+claz+"ToJSON(_vecobj.at(i));\n" +
426  "if(i!=(int)_vecobj.size()-1)json += \",\";\n" +
427  "\n}\njson += \"]\";\nreturn json;\n}\n";
428  return test;
429 }
430 
431 
432 string AfcUtil::generateReadObjects(string type, string name, bool priv, bool ptr, string typ)
433 {
434  string tes;
435  if(type=="int" || type=="short" || type=="long" || type=="double" || type=="float" || type=="string" || type=="bool")
436  {
437  tes += "_node = obj.getNode(\""+name+"\");\n";
438  if(!ptr)
439  {
440  if(!priv)tes += "if(_node!=NULL)_obj"+typ + name + "= CastUtil::lexical_cast<"+type+">(_node->getValue());\n";
441  else tes += "if(_node!=NULL)_obj"+typ+"set"+camelCased(name)+"(CastUtil::lexical_cast<"+type+">(_node->getValue()));\n";
442  }
443  else
444  {
445  if(!priv)tes += "if(_node!=NULL)_obj"+typ + name + "= new "+type+"(CastUtil::lexical_cast<"+type+">(_node->getValue()));\n";
446  else tes += "if(_node!=NULL)_obj"+typ+"set"+camelCased(name)+"(new "+type+"(CastUtil::lexical_cast<"+type+">(_node->getValue())));\n";
447  }
448  }
449  else
450  {
451  tes += "_node = obj.getNode(\""+name+"\");\n";
452  string tempp = StringUtil::replaceAllCopy(type, " ", "");
453  if(tempp.find("vector<")!=string::npos || tempp.find("list<")!=string::npos || tempp.find("set<")!=string::npos
454  || tempp.find("multiset<")!=string::npos || tempp.find("queue<")!=string::npos || tempp.find("deque<")!=string::npos)
455  {
456  string stlcnttyp;
457  if(tempp.find("vector")!=string::npos)
458  stlcnttyp = "vector";
459  else if(tempp.find("queue")!=string::npos)
460  stlcnttyp = "std::queue";
461  else if(tempp.find("deque")!=string::npos)
462  stlcnttyp = "deque";
463  else if(tempp.find("list")!=string::npos)
464  stlcnttyp = "list";
465  else if(tempp.find("multiset")!=string::npos)
466  stlcnttyp = "multiset";
467  else
468  stlcnttyp = "set";
469  tempp = StringUtil::replaceAllCopy(tempp, "vector<", "");
470  tempp = StringUtil::replaceAllCopy(tempp, "multiset<", "");
471  tempp = StringUtil::replaceAllCopy(tempp, "set<", "");
472  tempp = StringUtil::replaceAllCopy(tempp, "queue<", "");
473  tempp = StringUtil::replaceAllCopy(tempp, "deque<", "");
474  tempp = StringUtil::replaceAllCopy(tempp, "list<", "");
475  tempp = StringUtil::replaceAllCopy(tempp, "std::", "");
476  tempp = StringUtil::replaceAllCopy(tempp, ">", "");
477  tes += "if(_node!=NULL){\n";
478  tes += "for(int i=0;i<_node->getChildren().size();i++)\n{\n";
479  tes += "JSONElement* __node = _node->getChildren().at(i);\n";
480  tes += generateReadVectorObjects(tempp, name, priv, ptr, typ, stlcnttyp);
481  tes += "}\n}\n";
482 
483  }
484  else if(Reflection::isValidClass(tempp))
485  {
486  if(!ptr)
487  {
488  if(!priv)tes += "if(_node!=NULL)_obj"+typ + name + "= read"+tempp+"(*_node);\n";
489  else tes += "if(_node!=NULL)_obj"+typ+"set"+camelCased(name)+"(read"+tempp+"(*_node));\n";
490  }
491  else
492  {
493  if(!priv)tes += "if(_node!=NULL)_obj"+typ + name + "= read"+tempp+"P(*_node);\n";
494  else tes += "if(_node!=NULL)_obj"+typ+"set"+camelCased(name)+"(read"+tempp+"P(*_node));\n";
495  }
496  }
497  }
498  return tes;
499 }
500 
501 
502 string AfcUtil::generateReadVectorObjects(string type, string name, bool priv, bool ptr, string typ, string conttype)
503 {
504  string tes;
505  string act = ((conttype=="set" || conttype=="multiset")?"insert":(conttype=="std::queue"?"push":"push_back"));
506  if(type=="int" || type=="short" || type=="long" || type=="double" || type=="float" || type=="string" || type=="bool")
507  {
508  if(!ptr)
509  {
510  if(!priv)tes += "if(__node!=NULL)_obj"+typ + name + "."+act+"(CastUtil::lexical_cast<"+type+">(__node->getValue()));\n";
511  else tes += "if(__node!=NULL)_obj"+typ+"get"+camelCased(name)+"()."+act+"(CastUtil::lexical_cast<"+type+">(__node->getValue()));\n";
512  }
513  else
514  {
515  if(!priv)tes += "if(__node!=NULL){_obj"+typ + name + " = new "+conttype+"<"+type+">;\n_obj"+typ + name + "->"+act+"(CastUtil::lexical_cast<"+type+">(__node->getValue()));\n}\n";
516  else tes += "if(__node!=NULL){_obj"+typ +"set"+camelCased(name)+"(new "+conttype+"<"+type+">);\n_obj"+typ+"get"+camelCased(name)+"()->"+act+"(CastUtil::lexical_cast<"+type+">(__node->getValue()));\n}\n";
517  }
518  }
519  else if(Reflection::isValidClass(type))
520  {
521  if(!ptr)
522  {
523  if(!priv)tes += "if(__node!=NULL)_obj"+typ + name + "."+act+"(read"+type+"(*__node));\n";
524  else tes += "if(__node!=NULL)_obj"+typ+"get"+camelCased(name)+"()."+act+"(read"+type+"(*__node));\n";
525  }
526  else
527  {
528  if(!priv)tes += "if(__node!=NULL){_obj"+typ + name + " = new "+conttype+"<"+type+">;\n_obj"+typ + name + "->"+act+"(read"+type+"(*__node));\n}\n";
529  else tes += "if(__node!=NULL){_obj"+typ +"set"+camelCased(name)+"(new "+conttype+"<"+type+">);\n_obj"+typ+"get"+camelCased(name)+"()->"+act+"(read"+type+"(*__node));\n}\n";
530  }
531  }
532  return tes;
533 }
534 
535 string AfcUtil::generateToJSONObjects(string type, string name, bool priv, strVec obj, int i, string &retu, string &headers, string path, string &objs, string typ, bool ptr)
536 {
537  string fres;
538  if(type=="int" || type=="short" || type=="long" || type=="double" || type=="float" || type=="string" || type=="bool")
539  {
540  if(type=="string")
541  {
542  if(!ptr)
543  {
544  if(!priv)fres += "json += \"\\\""+name+"\\\" : \\\"\"+_obj"+typ+name+"+\"\\\"\";";
545  else fres += "json += \"\\\""+name+"\\\" : \\\"\"+_obj"+typ+"get"+camelCased(name)+"()+\"\\\"\";";
546  }
547  else
548  {
549  if(!priv)fres += "if(_obj"+typ+name+"!=NULL)json += \"\\\""+name+"\\\" : \\\"\"+*_obj"+typ+name+"+\"\\\"\";\nelse json += \"null\";\n";
550  else fres += "if(_obj"+typ+"get"+camelCased(name)+"()!=NULL)json += \"\\\""+name+"\\\" : \\\"\"+*_obj"+typ+"get"+camelCased(name)+"()+\"\\\"\";\nelse json += \"null\";\n";
551  }
552  }
553  else
554  {
555  if(!ptr)
556  {
557  if(!priv)fres += "json += \"\\\""+name+"\\\" : \"+CastUtil::lexical_cast<string>(_obj"+typ+name+");";
558  else fres += "json += \"\\\""+name+"\\\" : \"+CastUtil::lexical_cast<string>(_obj"+typ+"get"+camelCased(name)+"());";
559  }
560  else
561  {
562  if(!priv)fres += "if(_obj"+typ+name+"!=NULL)json += \"\\\""+name+"\\\" : \"+CastUtil::lexical_cast<string>(*_obj"+typ+name+");\nelse json += \"null\";\n";
563  else fres += "if(_obj"+typ+"get"+camelCased(name)+"()!=NULL)json += \"\\\""+name+"\\\" : \"+CastUtil::lexical_cast<string>(*_obj"+typ+"get"+camelCased(name)+"());\nelse json += \"null\";\n";
564  }
565  }
566 
567  if(i!=((int)obj.size()-1))
568  {
569  fres += "\njson += \",\";\n";
570  }
571  }
572  else
573  {
574  string tempp = StringUtil::replaceAllCopy(type, " ", "");
575  if(tempp.find("vector<")!=string::npos || tempp.find("list<")!=string::npos || tempp.find("set<")!=string::npos
576  || tempp.find("multiset<")!=string::npos || tempp.find("queue<")!=string::npos || tempp.find("deque<")!=string::npos)
577  {
578  string stlcnttyp;
579  if(tempp.find("vector")!=string::npos)
580  stlcnttyp = "vector";
581  else if(tempp.find("queue")!=string::npos)
582  stlcnttyp = "std::queue";
583  else if(tempp.find("deque")!=string::npos)
584  stlcnttyp = "deque";
585  else if(tempp.find("list")!=string::npos)
586  stlcnttyp = "list";
587  else if(tempp.find("multiset")!=string::npos)
588  stlcnttyp = "multiset";
589  else
590  stlcnttyp = "set";
591  tempp = StringUtil::replaceAllCopy(tempp, "vector<", "");
592  tempp = StringUtil::replaceAllCopy(tempp, "multiset<", "");
593  tempp = StringUtil::replaceAllCopy(tempp, "set<", "");
594  tempp = StringUtil::replaceAllCopy(tempp, "queue<", "");
595  tempp = StringUtil::replaceAllCopy(tempp, "deque<", "");
596  tempp = StringUtil::replaceAllCopy(tempp, "list<", "");
597  tempp = StringUtil::replaceAllCopy(tempp, "std::", "");
598  tempp = StringUtil::replaceAllCopy(tempp, ">", "");
599 
600  fres += "json += \"\\\""+name+"\\\" : \";\n";
601  if(!priv)
602  {
603  if(ptr)
604  {
605  fres += "if(_obj"+typ+name+"!=NULL)\n";
606  fres += "json += JSONSerialize::serialize<"+stlcnttyp+"<"+tempp+"> >(*_obj"+typ+name+");\n";
607  }
608  else
609  fres += "json += JSONSerialize::serialize<"+stlcnttyp+"<"+tempp+"> >(_obj"+typ+name+");\n";
610  }
611  else
612  {
613  if(ptr)
614  {
615  fres += "if(_obj"+typ+"get"+camelCased(name)+"()!=NULL)\n";
616  fres += "json += JSONSerialize::serialize<"+stlcnttyp+"<"+tempp+"> >(*_obj"+typ+"get"+camelCased(name)+"());\n";
617  }
618  else
619  fres += "json += JSONSerialize::serialize<"+stlcnttyp+"<"+tempp+"> >(_obj"+typ+"get"+camelCased(name)+"());\n";
620  }
621  //fres += generateToJSONVectorObjects(tempp, name, priv, retu, headers, path, objs, typ, ptr, stlcnttyp);
622  }
623  else if(Reflection::isValidClass(type))
624  {
625  if(!ptr)
626  {
627  if(!priv)fres += "json += \"\\\""+name+"\\\" : \"+from"+type+"ToJSON(_obj"+typ+name+");";
628  else fres += "json += \"\\\""+name+"\\\" : \"+from"+type+"ToJSON(_obj"+typ+"get"+camelCased(name)+"());";
629  }
630  else
631  {
632  if(!priv)fres += "if(_obj"+typ+name+"!=NULL)json += \"\\\""+name+"\\\" : \"+from"+type+"ToJSON(*_obj"+typ+name+");\nelse json += \"null\";\n";
633  else fres += "if(_obj"+typ+"get"+camelCased(name)+"()!=NULL)json += \"\\\""+name+"\\\" : \"+from"+type+"ToJSON(*_obj"+typ+"get"+camelCased(name)+"());\nelse json += \"null\";\n";
634  }
635  }
636  if(i!=((int)obj.size()-1))
637  {
638  fres += "json += \",\";\n";
639  }
640  }
641  return fres;
642 }
643 
644 string AfcUtil::generateToJSONVectorObjects(string type, string name, bool priv, string &retu, string &headers, string path, string &objs, string typ, bool ptr, string stlcnttyp)
645 {
646  string fres = "json += \"\\\""+name+"\\\" : [\";\n";
647  string vtyp = ".";
648  if(ptr)
649  vtyp = "->";
650  //Is list/set then use iterator
651  //if queue then use pop front etc after copying to another queue
652  if(ptr)fres += "if(_obj"+typ+name+"!=NULL)\n";
653  if(!priv)
654  {
655  if(stlcnttyp=="vector")
656  fres += "for(int j=0;j<_obj"+typ+name+vtyp+"size();j++)\n{\n";
657  else if(stlcnttyp=="list" || stlcnttyp=="set" || stlcnttyp=="multiset" || stlcnttyp=="deque")
658  fres += stlcnttyp+"<"+type+">::iterator it"+name+";\nint j"+name+" = 0;\nfor(it"+name+"=_obj"+typ+name+vtyp+"begin();it"+name+"!=_obj"+typ+name+vtyp+"end();it"+name+"++,j"+name+"++)\n{\n";
659  else
660  {
661  if(!ptr)
662  fres += "std::queue<"+type+"> tt"+name+" = _obj"+typ+name+";\nif(!tt"+name+vtyp+"empty()){\n" +
663  "for(int j=0;j<(int)tt"+name+vtyp+"size();j++)\n{\n";
664  else
665  fres += "std::queue<"+type+">* tt"+name+" = _obj"+typ+name+";\nif(!tt"+name+"->empty()){\n" +
666  "for(int j=0;j<(int)tt"+name+"->size();j++)\n{\n";
667  }
668  }
669  else
670  {
671  if(stlcnttyp=="vector")
672  fres += "for(int j=0;j<_obj"+typ+"get"+camelCased(name)+"()"+vtyp+"size();j++)\n{\n";
673  else if(stlcnttyp=="list" || stlcnttyp=="set" || stlcnttyp=="multiset" || stlcnttyp=="deque")
674  fres += stlcnttyp+"<"+type+">::iterator it"+name+";\nint j"+name+" = 0;\nfor(it"+name+"=_obj"+typ+"get"+camelCased(name)+"()"+vtyp+"begin();it"+name+"!=_obj"+typ+"get"+camelCased(name)+"()"+vtyp+"end();it"+name+"++,j"+name+"++)\n{\n";
675  else
676  {
677  if(!ptr)
678  fres += "std::queue<"+type+"> tt"+name+" = _obj"+typ+"get"+camelCased(name)+"();\nif(!tt"+name+vtyp+"empty()){\n" +
679  "for(int j=0;j<(int)tt"+name+vtyp+"size();j++)\n{\n";
680  else
681  fres += "std::queue<"+type+">* tt"+name+" = _obj"+typ+"get"+camelCased(name)+"();\nif(!tt"+name+"!=NULL && !tt"+name+"->empty()){\n" +
682  "for(int j=0;j<(int)tt"+name+"->size();j++)\n{\n";
683  }
684  }
685  if(type=="int" || type=="short" || type=="long" || type=="double" || type=="float" || type=="string" || type=="bool")
686  {
687  if(type=="string")
688  {
689  if(!ptr)
690  {
691  if(stlcnttyp=="vector")
692  {
693  if(!priv)fres += "json += \"\\\"\"+_obj"+typ+name+".at(j)+\"\\\"\";";
694  else fres += "json += \"\\\"\"+_obj"+typ+"get"+camelCased(name)+"().at(j)+\"\\\"\";";
695  }
696  else if(stlcnttyp=="list" || stlcnttyp=="set" || stlcnttyp=="multiset" || stlcnttyp=="deque")
697  {
698  fres += "json += \"\\\"\";json += *it"+name+";json += \"\\\"\";";
699  }
700  else
701  {
702  if(!ptr)
703  fres += "json += \"\\\"\";json += tt"+name+".front();json +=\"\\\"\";\ntt"+name+".pop();\n";
704  else
705  fres += "json += \"\\\"\";json += tt"+name+"->front();json +=\"\\\"\";\ntt"+name+"->pop();\n";
706  }
707  }
708  else
709  {
710  if(stlcnttyp=="vector")
711  {
712  if(!priv)fres += "json += \"\\\"\"+_obj"+typ+name+"->at(j)+\"\\\"\";";
713  else fres += "json += \"\\\"\"+_obj"+typ+"get"+camelCased(name)+"()->at(j)+\"\\\"\";";
714  }
715  else if(stlcnttyp=="list" || stlcnttyp=="set" || stlcnttyp=="multiset" || stlcnttyp=="deque")
716  {
717  fres += "json += \"\\\"\"+*it"+name+"+\"\\\"\";";
718  }
719  else
720  {
721  if(!ptr)
722  fres += "json += \"\\\"\"+tt"+name+".front()+\"\\\"\";\ntt"+name+".pop();\n";
723  else
724  fres += "json += \"\\\"\"+tt"+name+"->front()+\"\\\"\";\ntt"+name+"->pop();\n";
725  }
726  }
727  }
728  else
729  {
730  if(!ptr)
731  {
732  if(stlcnttyp=="vector")
733  {
734  if(!priv)fres += "json += CastUtil::lexical_cast<string>(_obj"+typ+name+".at(j));";
735  else fres += "json += CastUtil::lexical_cast<string>(_obj"+typ+"get"+camelCased(name)+"().at(j));";
736  }
737  else if(stlcnttyp=="list" || stlcnttyp=="set" || stlcnttyp=="multiset" || stlcnttyp=="deque")
738  {
739  fres += "json += CastUtil::lexical_cast<string>(*it"+name+");";
740  }
741  else
742  {
743  if(!ptr)
744  fres += "json += CastUtil::lexical_cast<string>(tt"+name+".front());\ntt"+name+".pop();\n";
745  else
746  fres += "json += CastUtil::lexical_cast<string>(tt"+name+"->front());\ntt"+name+"->pop();\n";
747  }
748  }
749  else
750  {
751  if(stlcnttyp=="vector")
752  {
753  if(!priv)fres += "json += CastUtil::lexical_cast<string>(_obj"+typ+name+"->at(j));";
754  else fres += "json += CastUtil::lexical_cast<string>(_obj"+typ+"get"+camelCased(name)+"()->at(j));";
755  }
756  else if(stlcnttyp=="list" || stlcnttyp=="set" || stlcnttyp=="multiset" || stlcnttyp=="deque")
757  {
758  fres += "json += CastUtil::lexical_cast<string>(*it"+name+");";
759  }
760  else
761  {
762  if(!ptr)
763  fres += "json += CastUtil::lexical_cast<string>(tt"+name+".front());\ntt"+name+".pop();\n";
764  else
765  fres += "json += CastUtil::lexical_cast<string>(tt"+name+"->front());\ntt"+name+"->pop();\n";
766  }
767  }
768  }
769  }
770  else if(Reflection::isValidClass(type))
771  {
772  if(!ptr)
773  {
774  if(stlcnttyp=="vector")
775  {
776  if(!priv)fres += "json += from"+type+"ToJSON(_obj"+typ+name+".at(j));";
777  else fres += "json += from"+type+"ToJSON(_obj"+typ+"get"+camelCased(name)+"().at(j));";
778  }
779  else if(stlcnttyp=="list" || stlcnttyp=="set" || stlcnttyp=="multiset" || stlcnttyp=="deque")
780  {
781  if(!priv)fres += "json += from"+type+"ToJSON(*it"+name+");";
782  else fres += "json += from"+type+"ToJSON(*it"+name+");";
783  }
784  else
785  {
786  if(!ptr)
787  {
788  if(!priv)fres += "json += from"+type+"ToJSON(tt"+name+".front());\ntt"+name+".pop();\n";
789  else fres += "json += from"+type+"ToJSON(tt"+name+".front());\ntt"+name+".pop();\n";
790  }
791  else
792  {
793  if(!priv)fres += "json += from"+type+"ToJSON(tt"+name+"->front());\ntt"+name+"->pop();\n";
794  else fres += "json += from"+type+"ToJSON(tt"+name+"->front());\ntt"+name+"->pop();\n";
795  }
796  }
797  }
798  else
799  {
800  if(stlcnttyp=="vector")
801  {
802  if(!priv)fres += "json += from"+type+"ToJSON(_obj"+typ+name+"->at(j));";
803  else fres += "json += from"+type+"ToJSON(_obj"+typ+"get"+camelCased(name)+"()->at(j));";
804  }
805  else if(stlcnttyp=="list" || stlcnttyp=="set" || stlcnttyp=="multiset" || stlcnttyp=="deque")
806  {
807  if(!priv)fres += "json += from"+type+"ToJSON(*it"+name+");";
808  else fres += "json += from"+type+"ToJSON(*it"+name+");";
809  }
810  else
811  {
812  if(!ptr)
813  {
814  if(!priv)fres += "json += from"+type+"ToJSON(tt"+name+".front());\ntt"+name+".pop();\n";
815  else fres += "json += from"+type+"ToJSON(tt"+name+".front());\ntt"+name+".pop();\n";
816  }
817  else
818  {
819  if(!priv)fres += "json += from"+type+"ToJSON(tt"+name+"->front());\ntt"+name+"->pop();\n";
820  else fres += "json += from"+type+"ToJSON(tt"+name+"->front());\ntt"+name+"->pop();\n";
821  }
822  }
823  }
824  }
825  if(!priv)
826  {
827  if(stlcnttyp=="list" || stlcnttyp=="set" || stlcnttyp=="multiset" || stlcnttyp=="deque")
828  fres += "\nif(j"+name+"!=_obj"+typ+name+vtyp+"size()-1)json += \",\";\n";
829  else
830  fres += "\nif(j!=_obj"+typ+name+vtyp+"size()-1)json += \",\";\n";
831  }
832  else
833  {
834  if(stlcnttyp=="list" || stlcnttyp=="set" || stlcnttyp=="multiset" || stlcnttyp=="deque")
835  fres += "\nif(j"+name+"!=_obj"+typ+"get"+camelCased(name)+"()"+vtyp+"size()-1)json += \",\";\n";
836  else
837  fres += "\nif(j!=_obj"+typ+"get"+camelCased(name)+"()"+vtyp+"size()-1)json += \",\";\n";
838  }
839  if(stlcnttyp=="std::queue")
840  fres += "}\n";
841  fres += "}\njson += \"]\";\n";
842  return fres;
843 }
844 
845 string AfcUtil::generateJsInterfaces(strVec obj,string claz,string &headers,string path,string &infjs,string pv,map<string, string> ajintpthMap)
846 {
847  string test,intf,intff,inc;
848  headers += "#include \"" + claz + ".h\"\n";
849  //writeTofile("/home/sumeet/workspace/inter/AfcInclude.h",inc,false);
850  //inc = "\nextern \"C\"{\n";//string executeAFC(string fn,strVec _inp){\nstring ret;\n";
851  //bool fl = false;
852  test = ("var " + claz + "= {\n");
853  for(unsigned int i=0;i<obj.size();i++)
854  {
855  strVec vemp,emp;
856  string data = obj.at(i);
857  StringUtil::replaceFirst(data,";","");
858  vector<string> delimiters;
859  delimiters.push_back(" ");
860  delimiters.push_back(",");
861  delimiters.push_back(")");
862  delimiters.push_back("(");
863  StringUtil::split(vemp, data, delimiters);
864  for(unsigned int k=0;k<vemp.size();k++)
865  {
866  if(vemp.at(k)!="")
867  emp.push_back(vemp.at(k));
868  //logger << vemp.at(i) << "\n" << flush;
869  }
870  //logger << "\n------------------------------------\n" << flush;
871  if(emp.size()>1)
872  {
873  size_t te = emp.at(1).find("~");
874  if(te==string::npos)
875  {
876  string pars,parswt,types,jsonstr;
877  if(emp.size()==2)
878  {
879  test += emp.at(1) + ": function(_cb,_url,_cntxt){\n";
880  test += "AfcCall(\""+claz+"\",\""+emp.at(1)+"\",new Array("+jsonstr+"),_cb,(_url==null?\""+ajintpthMap[claz]+"\":_url),_cntxt);\n";
881  }
882  else
883  {
884  test += emp.at(1) + ": function(";
885  for(unsigned int j=2;j<emp.size();j++)
886  {
887  stringstream st,st1,st2;
888  //types += ("\"" + emp.at(j) + "\"");
889  st << "_";
890  st << (j-1);
891  if(j!=emp.size()-1)
892  {
893  st << ",";
894  //types += ",";
895  }
896  string h;
897  st >> h;
898  test += h;
899  pars += h;
900  parswt += ("string " + h);
901  //st1 << emp.at(j) << " __" << (j-1);
902  //st1 << " = (" << emp.at(j) << ")_" << (j-1) << "->c_str();\n";
903  string h1;
904  //st1 >> h1;
905  if(emp.at(j)=="string" || emp.at(j)=="int" || emp.at(j)=="float" || emp.at(j)=="double")
906  {
907  types.append(emp.at(j).c_str());
908  types.append(" _");
909  st1 << (j-1);
910  getline(st1,h1);
911  jsonstr += "_"+h1;
912  types.append(h1.c_str());
913  types.append("=CastUtil::lexical_cast<");
914  types.append(emp.at(j).c_str());
915  types.append(">(_inp.at(");
916  st2 << (j-2);
917  getline(st2,h1);
918  types.append(h1.c_str());
919  types.append(").c_str());\n");
920  }
921  else
922  {
923  types.append(emp.at(j).c_str());
924  types.append(" _");
925  st1 << (j-1);
926  getline(st1,h1);
927  jsonstr += "JSON.stringify(_"+h1+")";
928  types.append(h1.c_str());
929  types.append("=to"+emp.at(j)+"(_inp.at(");
930  st2 << (j-2);
931  getline(st2,h1);
932  types.append(h1.c_str());
933  types.append("));\n");
934 
935  }
936  if(j!=emp.size()-1)
937  {
938  jsonstr += ",";
939  }
940  //types += (emp.at(j).c_str() + " __" + (j-1));
941  //logger << vemp.at(i) << "\n" << flush;
942  }
943  //fl = true;
944  test += ",_cb,_url,_cntxt){\n";
945  test += "AfcCall(\""+claz+"\",\""+emp.at(1)+"\",new Array("+jsonstr+"),_cb,(_url==null?\""+pv+"\":_url),_cntxt);\n";
946  }
947  inc += updateAjaxInterface(emp,claz,pars,parswt,types);
948  test += "}\n";
949  if(i!=obj.size()-1)
950  test += ",";
951  }
952  }
953 
954  }
955  test += "};\n";
956  //intff += "return ret;}\n}\n";
957  //intff += intf;
958  //intf = (inc + intf + intff);
959  //inc += "}";
960  //writeTofile("/home/sumeet/server/web/default/_afc_Interfaces.js",test,true);
961  infjs += test;
962  //writeTofile("/home/sumeet/workspace/inter/AjaxInterface.cpp",inc,false);
963  return inc;
964  //logger << test << flush;
965 }
966 
967 string AfcUtil::updateAjaxInterface(strVec emp,string claz,string pars,string parswt,string types)
968 {
969  string test;
970  string retType = emp.at(0);
971  string funcName = emp.at(1);
972  //test = "#include \"" + claz + ".h\"\n#include "CastUtil.h"\n\n";
973  test += ("string invokeAjaxMethodFor" + claz + funcName + "(strVec _inp");
974  test += ")\n{\n" + types;
975  test += (claz + " _obj;\n");
976  if(retType=="void")
977  {
978  test += "return \"\";\n}\n";
979  }
980  else if(retType=="string")
981  {
982  test += "return _obj."+funcName+"("+pars+");\n}\n";
983  }
984  else if(retType=="int" || retType=="float" || retType=="double")
985  {
986  test += "string outp = CastUtil::lexical_cast<string>(_obj."+funcName+"("+pars+"));\nreturn outp;\n}\n";
987  }
988  else
989  test += "return from"+retType+"ToJSON(_obj."+funcName+"("+pars+"));\n}\n";
990  //logger << test << flush;
991 
992  return test;
993 }
994 
995 void AfcUtil::execute(HttpRequest req, HttpResponse* res, string claz)
996 {
997  logger << "Inside Ajax Interface Execute" << endl;
998  strVec vemp;
999  string methName = req.getRequestParam("method");
1000  if(methName=="")
1001  {
1002  res->setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
1003  return;
1004  }
1005  string temp = req.getRequestParam("paramsize");
1006  int paramSize = 0;
1007  if(temp!="")
1008  {
1009  try {
1010  paramSize = CastUtil::lexical_cast<int>(temp.c_str());
1011  } catch(...) {
1012  res->setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
1013  return;
1014  }
1015  }
1016  logger << "Reading Ajax params" << endl;
1017  for(int i=0;i<paramSize;i++)
1018  {
1019  stringstream s;
1020  string ss;
1021  s << (i+1);
1022  s >> ss;
1023  ss = "param_" + ss;
1024  //logger << ss << flush;
1025  string tem = req.getRequestParam(ss);
1026  vemp.push_back(tem);
1027  }
1028  string libName = Constants::INTER_LIB_FILE;
1029  void *dlib = dlopen(libName.c_str(), RTLD_NOW);
1030  if(dlib == NULL){
1031  cerr << dlerror() << endl;
1032  exit(-1);
1033  }
1034  string funcName;
1035  string metn,re;
1036  metn = "invokeAjaxMethodFor"+claz+methName;
1037  void *mkr = dlsym(dlib, metn.c_str());
1038  if(mkr!=NULL)
1039  {
1040  typedef string (*Funptr2) (strVec);
1041  Funptr2 f2 = (Funptr2)mkr;
1042  logger << ("Calling method " + metn) << endl;
1043  re = f2(vemp);
1044  logger << "Completed method call" << endl;
1045  res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
1046  res->setContent_type("text/plain");
1047  res->setContent_str(re);
1048  return;
1049  }
1050  else
1051  {
1052  res->setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
1053  return;
1054  }
1055 }
1056 
1057 
1058 void AfcUtil::writeTofile(string fileName,string data,bool trunc)
1059 {
1060  ofstream myfile1;
1061  if(trunc)
1062  myfile1.open(fileName.c_str(),ios::trunc | ios::binary);
1063  else
1064  myfile1.open(fileName.c_str(),ios::app | ios::binary);
1065  if (myfile1.is_open())
1066  {
1067  myfile1.write(data.c_str(),data.length());
1068  myfile1.close();
1069  }
1070 }
1071 
1072 
1073 string AfcUtil::camelCased(const string& s)
1074 {
1075  string str(s);
1076  str[0] = toupper(str[0]);
1077  return str;
1078 }
1079 
1080 string AfcUtil::reverseCamelCased(const string& s)
1081 {
1082  string str(s);
1083  str[0] = tolower(str[0]);
1084  return str;
1085 }