Files
Work/proto/junk/main.cpp.saf
2024-08-07 09:24:33 -04:00

305 lines
9.4 KiB
Plaintext

#include <common/file.hpp>
#include <common/profile.hpp>
#include <common/string.hpp>
#include <common/block.hpp>
#include <ClassGen/NameValuePair.hpp>
class ClassMapper
{
public:
ClassMapper();
virtual ~ClassMapper();
bool createClass(const String &packageName,String className,const String &databaseName,const String &tableName,const String &strPathMapFile,const String &strPathTable);
private:
bool loadMapping(const String &strPathMapFile);
bool mapType(const String &type,String &mapped);
bool createClass(const String &packageName,const String &className,Block<NameValuePair> &variables);
bool createDataAccess(const String &packageName,const String &className,const String &instanceName,const String &databaseName,const String &tableName,Block<NameValuePair> &variables,Block<NameValuePair> &originals);
String makeHungarian(const String &name);
String makeAccessor(const String &name);
String makeMutator(const String &name);
String makeFirstUpper(const String &name);
String makeFirstLower(const String &name);
Block<NameValuePair> mNameValuePairs;
};
ClassMapper::ClassMapper()
{
}
ClassMapper::~ClassMapper()
{
}
bool ClassMapper::createClass(const String &packageName,String className,const String &databaseName,const String &tableName,const String &strPathMapFile,const String &strPathTable)
{
File inFile;
String strLine;
String name;
String value;
String instanceName;
Block<NameValuePair> variables;
Block<NameValuePair> originals;
int errors=0;
if(!loadMapping(strPathMapFile))return false;
if(!inFile.open(strPathTable,"rb"))return false;
while(true)
{
inFile.readLine(strLine);
if(strLine.isNull())break;
name=strLine.betweenString(0,' ');
value=strLine.betweenString(' ',' ');
if(!mapType(value,value))
{
printf("Don't know how to map %s\n",value.str());
errors++;
continue;
}
variables.insert(&NameValuePair(makeHungarian(name),value));
originals.insert(&NameValuePair(name,value));
}
inFile.close();
if(errors)return false;
if(!variables.size())return false;
instanceName=makeFirstLower(className);
className=makeFirstUpper(className);
createClass(packageName,className,variables);
createDataAccess(packageName,className,instanceName,databaseName,tableName,variables,originals);
printf("generation completed.\n");
return true;
}
bool ClassMapper::createClass(const String &packageName,const String &className,Block<NameValuePair> &variables)
{
File outFile;
if(!outFile.open(className+String(".java"),"wb"))return false;
if(!packageName.isNull())outFile.writeLine(String("package ")+packageName);
outFile.writeLine("\t");
outFile.writeLine("\t");
outFile.writeLine(String("public class ")+className);
outFile.writeLine("{");
for(int index=0;index<variables.size();index++)
{
NameValuePair &nameValuePair=variables[index];
outFile.writeLine(String(" private ")+nameValuePair.getValue()+String(" ")+nameValuePair.getName()+String(";"));
}
for(index=0;index<variables.size();index++)
{
NameValuePair &nameValuePair=variables[index];
outFile.writeLine(String(" public ")+nameValuePair.getValue()+String(" ")+makeAccessor(nameValuePair.getName())+String("()"));
outFile.writeLine(" {");
outFile.writeLine(String(" return ")+nameValuePair.getName()+String(";"));
outFile.writeLine(" }");
outFile.writeLine(String(" public void ")+makeMutator(nameValuePair.getName())+String("(")+nameValuePair.getValue()+String(" ")+nameValuePair.getName()+String(")"));
outFile.writeLine(" {");
outFile.writeLine(String(" this.")+nameValuePair.getName()+String("=")+nameValuePair.getName()+String(";"));
outFile.writeLine(" }");
}
outFile.writeLine("};");
outFile.close();
return true;
}
bool ClassMapper::createDataAccess(const String &packageName,const String &className,const String &instanceName,const String &databaseName,const String &tableName,Block<NameValuePair> &variables,Block<NameValuePair> &originals)
{
File outFile;
String select;
if(!outFile.open(className+String("DA")+String(".java"),"wb"))return false;
if(!packageName.isNull())outFile.writeLine(String("package ")+packageName);
outFile.writeLine("\t");
outFile.writeLine("\t");
outFile.writeLine(String("public class ")+className+String("DA"));
outFile.writeLine("{");
outFile.writeLine(" public List readAll()throws SQLException");
outFile.writeLine(" {");
outFile.writeLine(" Statement statement=null;");
outFile.writeLine(" ResultSet rs=null;");
outFile.writeLine(" Connection connection=null;");
outFile.writeLine(" String strQuery=null;");
outFile.writeLine(" List list=null;");
outFile.writeLine("\t");
outFile.writeLine(" try");
outFile.writeLine(" {");
outFile.writeLine(String(" ")+className+String(" ")+instanceName+String(" = new ")+className+String("();"));
outFile.writeLine(" connection=getConnection();");
outFile.writeLine(" statement=connection.createStatement();");
outFile.writeLine(" list=new ArrayList();");
select=" strQuery=\"select ";
for(int index=0;index<originals.size();index++)
{
select+=originals[index].getName();
if(index<originals.size()-1)select+=String(", ");
}
select+=String(" from ")+tableName+String("\";");
outFile.writeLine(select);
outFile.writeLine(" rs=statement.executeQuery(strQuery);");
outFile.writeLine(" while(rs.next())");
outFile.writeLine(" {");
for(index=0;index<variables.size();index++)
{
NameValuePair &original=originals[index];
NameValuePair &variable=variables[index];
outFile.writeLine(String(" ")+instanceName+String(".")+makeMutator(variable.getName())+String("(rs.get")+makeFirstUpper(variable.getValue())+String("(\"")+original.getName()+String("\"));"));
}
outFile.writeLine(String(" list.add(")+instanceName+String(");"));
outFile.writeLine(" }");
outFile.writeLine(" return list;");
outFile.writeLine(" }");
outFile.writeLine(" finally");
outFile.writeLine(" {");
outFile.writeLine(" if(null!=rs)rs.close();");
outFile.writeLine(" if(null!=statement)statement.close();");
outFile.writeLine(" }");
outFile.writeLine(" }");
outFile.writeLine(" private Connection getConnection()throws SQLException");
outFile.writeLine(" {");
outFile.writeLine(" try");
outFile.writeLine(" {");
outFile.writeLine(" InitialContext jndiCntx=new InitialContext();");
outFile.writeLine(String(" DataSource ds=(DataSource)jndiCntx.lookup(\"java:") +databaseName+String("\")"));
outFile.writeLine(" jndiCntx.close();");
outFile.writeLine(" return ds.getConnection();");
outFile.writeLine(" }");
outFile.writeLine(" catch(NamingException exception)");
outFile.writeLine(" {");
outFile.writeLine(" message(\"[getConnection] Object not found\");");
outFile.writeLine(" throw new EJBException(exception);");
outFile.writeLine(" }");
outFile.writeLine(" }");
outFile.writeLine("};");
outFile.close();
return true;
}
bool ClassMapper::mapType(const String &type,String &mapped)
{
for(int index=0;index<mNameValuePairs.size();index++)
{
NameValuePair &nameValuePair=mNameValuePairs[index];
if(nameValuePair.getName()==type)return mapped=nameValuePair.getValue(),true;
}
return false;
}
bool ClassMapper::loadMapping(const String &strPathMapFile)
{
File inFile;
String strLine;
String key;
String value;
mNameValuePairs.remove();
if(!inFile.open(strPathMapFile,"rb"))return false;
while(true)
{
inFile.readLine(strLine);
if(strLine.isNull())break;
key=strLine.betweenString(0,'=');
value=strLine.betweenString('=',0);
mNameValuePairs.insert(&NameValuePair(key,value));
}
return true;
}
String ClassMapper::makeHungarian(const String &name)
{
String hungarianName;
int length=name.length();
for(int index=0;index<length;index++)
{
char ch=name.charAt(index);
if(ch=='_')
{
if(index+1>=length)break;
index++;
ch=toupper(name.charAt(index));
hungarianName+=ch;
}
else hungarianName+=ch;
}
return hungarianName;
}
String ClassMapper::makeAccessor(const String &name)
{
String str;
str+="get";
str+=toupper(name.charAt(0));
str+=name.substr(1);
return str;
}
String ClassMapper::makeMutator(const String &name)
{
String str;
str+="set";
str+=toupper(name.charAt(0));
str+=name.substr(1);
return str;
}
String ClassMapper::makeFirstUpper(const String &name)
{
String str;
str+=toupper(name.charAt(0));
str+=name.substr(1);
return str;
}
String ClassMapper::makeFirstLower(const String &name)
{
String str;
str+=tolower(name.charAt(0));
str+=name.substr(1);
return str;
}
#include <common/filemap.hpp>
#include <common/pview.hpp>
extern "C"
{
bool PASCAL GetTicks(char *pTickers,SYSTEMTIME *pStartDate,SYSTEMTIME *pEndDate,char *pHandle);
}
int main(int argc,char **argv)
{
SYSTEMTIME startTime;
SYSTEMTIME endTime;
char buffer[128];
::memset(buffer,0,sizeof(buffer));
GetTicks("IBM",&startTime,&endTime,buffer);
FileMap fileMap;
if(!fileMap.open(buffer,0,128))return 0;
PureViewOfFile pureView(fileMap);
int size(0);
pureView.read(size);
if(7!=argc)
{
printf("USAGE: mapclass <packagename><classname><databasename><tablename><pathtomappingtable><pathtotable>\n");
printf("(ie) mapclass zbi.risk.server.vhi.mapped Historic DRMS dt_main_positions c:\\work\\classgen\\mapping.txt c:\\work\\classgen\\table.txt\n");
return 0;
}
ClassMapper classMapper;
if(!classMapper.createClass(argv[1],argv[2],argv[3],argv[4],argv[5],argv[6]))return 1;
return 0;
}