use of com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext in project dal by ctripcorp.
the class JavaCodeGeneratorOfSpProcessor method generateSpDao.
private List<Callable<ExecuteResult>> generateSpDao(CodeGenContext codeGenCtx, final File mavenLikeDir) {
JavaCodeGenContext ctx = (JavaCodeGenContext) codeGenCtx;
final Progress progress = ctx.getProgress();
Map<String, SpDbHost> _spHostMaps = ctx.getSpHostMaps();
List<Callable<ExecuteResult>> results = new ArrayList<Callable<ExecuteResult>>();
for (final SpDbHost host : _spHostMaps.values()) {
Callable<ExecuteResult> worker = new Callable<ExecuteResult>() {
@Override
public ExecuteResult call() throws Exception {
ExecuteResult result = new ExecuteResult("Generate SP[" + host.getDbSetName() + "] Dao, Pojo, Test");
progress.setOtherMessage(result.getTaskName());
try {
VelocityContext context = GenUtils.buildDefaultVelocityContext();
context.put("host", host);
GenUtils.mergeVelocityContext(context, String.format("%s/Dao/%sSpDao.java", mavenLikeDir.getAbsolutePath(), host.getDbSetName()), "templates/java/DAOBySp.java.tpl");
GenUtils.mergeVelocityContext(context, String.format("%s/Test/%sSpDaoUnitTest.java", mavenLikeDir.getAbsolutePath(), host.getDbSetName()), "templates/java/test/DAOBySpUnitTest.java.tpl");
for (SpHost sp : host.getSpHosts()) {
sp.setDbSetName(host.getDbSetName());
context.put("host", sp);
GenUtils.mergeVelocityContext(context, String.format("%s/Entity/%s.java", mavenLikeDir.getAbsolutePath(), sp.getPojoClassName()), "templates/java/Pojo.java.tpl");
}
result.setSuccessal(true);
} catch (Exception e) {
log.error(result.getTaskName() + " exception", e);
}
return result;
}
};
results.add(worker);
}
return results;
}
use of com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext in project dal by ctripcorp.
the class JavaCodeGeneratorOfTableProcessor method process.
@Override
public void process(CodeGenContext context) throws Exception {
JavaCodeGenContext ctx = (JavaCodeGenContext) context;
int projectId = ctx.getProjectId();
File dir = new File(String.format("%s/%s/java", ctx.getGeneratePath(), projectId));
List<Callable<ExecuteResult>> tableCallables = generateTableDao(ctx, dir);
TaskUtils.invokeBatch(log, tableCallables);
}
use of com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext in project dal by ctripcorp.
the class JavaCodeGeneratorOfTableProcessor method generateTableDao.
private List<Callable<ExecuteResult>> generateTableDao(CodeGenContext codeGenCtx, final File mavenLikeDir) {
JavaCodeGenContext ctx = (JavaCodeGenContext) codeGenCtx;
final Progress progress = ctx.getProgress();
Queue<JavaTableHost> _tableHosts = ctx.getTableHosts();
List<Callable<ExecuteResult>> results = new ArrayList<>();
for (final JavaTableHost host : _tableHosts) {
Callable<ExecuteResult> worker = new Callable<ExecuteResult>() {
@Override
public ExecuteResult call() throws Exception {
ExecuteResult result = new ExecuteResult("Generate Table[" + host.getDbSetName() + "." + host.getTableName() + "] Dao, Pojo, Test");
progress.setOtherMessage(result.getTaskName());
try {
VelocityContext context = GenUtils.buildDefaultVelocityContext();
context.put("host", host);
GenUtils.mergeVelocityContext(context, String.format("%s/Dao/%sDao.java", mavenLikeDir.getAbsolutePath(), host.getPojoClassName()), "templates/java/dao/standard/DAO.java.tpl");
GenUtils.mergeVelocityContext(context, String.format("%s/Entity/%s.java", mavenLikeDir.getAbsolutePath(), host.getPojoClassName()), "templates/java/Pojo.java.tpl");
GenUtils.mergeVelocityContext(context, String.format("%s/Test/%sDaoUnitTest.java", mavenLikeDir.getAbsolutePath(), host.getPojoClassName()), "templates/java/test/DaoUnitTests.java.tpl");
result.setSuccessal(true);
} catch (Exception e) {
log.error(result.getTaskName() + " exception", e);
}
return result;
}
};
results.add(worker);
}
return results;
}
use of com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext in project dal by ctripcorp.
the class AbstractJavaDataPreparer method addDatabaseSet.
protected void addDatabaseSet(CodeGenContext codeGenCtx, String databaseSetName) {
DaoOfDatabaseSet daoOfDatabaseSet = SpringBeanGetter.getDaoOfDatabaseSet();
List<DatabaseSet> sets = daoOfDatabaseSet.getAllDatabaseSetByName(databaseSetName);
if (null == sets || sets.isEmpty()) {
// databaseSetName));
return;
}
JavaCodeGenContext ctx = (JavaCodeGenContext) codeGenCtx;
ContextHost contextHost = ctx.getContextHost();
DalConfigHost dalConfigHost = ctx.getDalConfigHost();
dalConfigHost.addDatabaseSet(sets);
for (DatabaseSet databaseSet : sets) {
List<DatabaseSetEntry> entries = daoOfDatabaseSet.getAllDatabaseSetEntryByDbsetid(databaseSet.getId());
if (entries == null || entries.isEmpty()) {
continue;
}
dalConfigHost.addDatabaseSetEntry(entries);
Map<String, DatabaseSetEntry> map = dalConfigHost.getDatabaseSetEntryMap();
for (DatabaseSetEntry entry : entries) {
String key = entry.getConnectionString();
if (map.containsKey(key)) {
DatabaseSetEntry value = map.get(key);
Resource resource = new Resource(value.getConnectionString(), value.getUserName(), value.getPassword(), value.getDbAddress(), value.getDbPort(), value.getDbCatalog(), value.getProviderName());
contextHost.addResource(resource);
}
}
}
}
use of com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext in project dal by ctripcorp.
the class AbstractJavaDataPreparer method buildTableHost.
protected JavaTableHost buildTableHost(CodeGenContext codeGenCtx, GenTaskByTableViewSp tableViewSp, String tableName, DatabaseCategory dbCategory) throws Exception {
JavaCodeGenContext ctx = (JavaCodeGenContext) codeGenCtx;
if (!DbUtils.tableExists(tableViewSp.getAllInOneName(), tableName)) {
throw new Exception(String.format("The table[%s,%s] doesn't exist, pls check", tableViewSp.getAllInOneName(), tableName));
}
JavaTableHost tableHost = new JavaTableHost();
tableHost.setPackageName(ctx.getNamespace());
tableHost.setDatabaseCategory(getDatabaseCategory(tableViewSp.getAllInOneName()));
tableHost.setDbSetName(tableViewSp.getDatabaseSetName());
tableHost.setTableName(tableName);
tableHost.setPojoClassName(getPojoClassName(tableViewSp.getPrefix(), tableViewSp.getSuffix(), tableName));
tableHost.setSp(tableViewSp.isCud_by_sp());
tableHost.setApi_list(tableViewSp.getApi_list());
// 主键及所有列
List<String> primaryKeyNames = DbUtils.getPrimaryKeyNames(tableViewSp.getAllInOneName(), tableName);
List<AbstractParameterHost> allColumnsAbstract = DbUtils.getAllColumnNames(tableViewSp.getAllInOneName(), tableName, new JavaColumnNameResultSetExtractor(tableViewSp.getAllInOneName(), tableName, dbCategory));
if (null == allColumnsAbstract) {
throw new Exception(String.format("The column names of tabel[%s, %s] is null", tableViewSp.getAllInOneName(), tableName));
}
List<JavaParameterHost> allColumns = new ArrayList<>();
for (AbstractParameterHost h : allColumnsAbstract) {
allColumns.add((JavaParameterHost) h);
}
List<JavaParameterHost> primaryKeys = new ArrayList<>();
boolean hasIdentity = false;
String identityColumnName = null;
for (JavaParameterHost h : allColumns) {
if (!hasIdentity && h.isIdentity()) {
hasIdentity = true;
identityColumnName = h.getName();
}
if (primaryKeyNames.contains(h.getName())) {
h.setPrimary(true);
primaryKeys.add(h);
}
}
List<GenTaskBySqlBuilder> currentTableBuilders = filterExtraMethods(ctx, tableViewSp.getAllInOneName(), tableName);
List<JavaMethodHost> methods = buildSqlBuilderMethodHost(allColumns, currentTableBuilders);
tableHost.setFields(allColumns);
tableHost.setPrimaryKeys(primaryKeys);
tableHost.setHasIdentity(hasIdentity);
tableHost.setIdentityColumnName(identityColumnName);
tableHost.setMethods(methods);
if (tableHost.isSp()) {
tableHost.setSpInsert(getSpaOperation(tableViewSp.getAllInOneName(), tableName, "i"));
tableHost.setSpUpdate(getSpaOperation(tableViewSp.getAllInOneName(), tableName, "u"));
tableHost.setSpDelete(getSpaOperation(tableViewSp.getAllInOneName(), tableName, "d"));
}
return tableHost;
}
Aggregations