use of ru.curs.celesta.score.DataGrainElement in project celesta by CourseOrchestra.
the class CursorGenerator method generateCursor.
/**
* Generate code for schema (grain) element.
* @param ge Schema (grain) element
* @param scorePath path to CelestaSQL file
*/
public void generateCursor(GrainElement ge, String scorePath) {
final String sourcePackage = calcSourcePackage(ge, scorePath);
if (sourcePackage.isEmpty()) {
throw new CelestaException("Couldn't generate class file for %s.%s without package", ge.getGrain().getName(), ge.getName());
}
final String className = calcClassName(ge);
final String columnsClassName = "Columns";
boolean isVersionedGe = ge instanceof VersionedElement && ((VersionedElement) ge).isVersioned();
ClassName classType = ClassName.bestGuess(className);
TypeSpec.Builder cursorClass = buildClassDefinition(ge, classType);
ClassName columnsClassType = classType.nestedClass(columnsClassName);
cursorClass.addFields(buildMetaFields(ge));
cursorClass.addMethods(buildConstructors(ge, columnsClassType));
// FIELDS
if (ge instanceof DataGrainElement) {
DataGrainElement dge = (DataGrainElement) ge;
FieldSpec columnsField = buildColumnsField(columnsClassType);
cursorClass.addField(columnsField);
cursorClass.addInitializerBlock(buildColumnsFiledInitializer(columnsField));
List<FieldSpec> fieldSpecs = buildDataFields(dge);
cursorClass.addFields(fieldSpecs);
cursorClass.addMethods(generateGettersAndSetters(fieldSpecs, classType));
cursorClass.addMethod(buildGetFieldValue(dge.getColumns()));
cursorClass.addMethod(buildSetFieldValue(dge.getColumns()));
StringBuilder parseResultOverridingMethodNameBuilder = new StringBuilder("_parseResult");
Set<Column<?>> pk = Collections.emptySet();
if (dge instanceof TableElement && !(dge instanceof ReadOnlyTable)) {
TableElement te = (TableElement) dge;
pk = new LinkedHashSet<>(te.getPrimaryKey().values());
cursorClass.addMethod(buildCurrentKeyValues(pk));
cursorClass.addMethod(buildTryGet(pk));
cursorClass.addMethod(buildGet(pk));
if (te instanceof Table) {
parseResultOverridingMethodNameBuilder.append("Internal");
}
}
final Map<String, ? extends ColumnMeta<?>> columns = dge.getColumns();
MethodSpec buildParseResultMethod = buildParseResult(columns, parseResultOverridingMethodNameBuilder.toString(), isVersionedGe);
cursorClass.addMethod(buildParseResultMethod);
cursorClass.addMethod(buildClearBuffer(columns, pk));
cursorClass.addMethod(buildCurrentValues(columns));
cursorClass.addType(buildCursorColumnsAsInnerStaticClass(dge, columnsClassType));
if (dge instanceof BasicTable) {
BasicTable t = (BasicTable) dge;
if (t instanceof Table) {
cursorClass.addMethods(buildCalcBlobs(columns, className));
cursorClass.addMethod(buildSetAutoIncrement(columns));
cursorClass.addMethods(buildTriggerRegistration(classType));
}
cursorClass.addTypes(buildOptionFieldsAsInnerStaticClasses(t.getColumns().values()));
}
cursorClass.addMethods(buildCompileCopying(ge, classType, columns.keySet(), isVersionedGe));
cursorClass.addMethod(buildIterator(classType));
}
cursorClass.addMethods(buildGrainNameAndObjectName(ge));
JavaFile javaFile = JavaFile.builder(sourcePackage, cursorClass.build()).skipJavaLangImports(true).indent(" ").build();
try {
javaFile.writeTo(srcDir);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of ru.curs.celesta.score.DataGrainElement in project celesta by CourseOrchestra.
the class CursorGenerator method buildCursorColumnsAsInnerStaticClass.
private TypeSpec buildCursorColumnsAsInnerStaticClass(DataGrainElement dge, ClassName columnsClassType) {
TypeSpec.Builder builder = TypeSpec.classBuilder(columnsClassType).addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "unchecked").build()).addAnnotation(buildGeneratedAnnotation()).addAnnotation(CelestaGenerated.class);
FieldSpec elementField = FieldSpec.builder(dge.getClass(), "element", Modifier.PRIVATE, Modifier.FINAL).build();
builder.addField(elementField);
builder.addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(ICelesta.class, "celesta").addStatement("this.$N = celesta.getScore().getGrains().get($L).getElements($T.class).get($L)", elementField, GRAIN_FIELD_NAME, elementField.type, OBJECT_FIELD_NAME).build());
dge.getColumns().entrySet().stream().filter(e -> !BinaryColumn.CELESTA_TYPE.equals(e.getValue().getCelestaType())).map(e -> {
final String columnName = e.getKey();
final TypeName columnType = ParameterizedTypeName.get(ColumnMeta.class, e.getValue().getJavaClass());
return MethodSpec.methodBuilder(camelize(columnName)).addModifiers(Modifier.PUBLIC).returns(columnType).addStatement("return ($T) this.$N.getColumns().get($S)", columnType, elementField, columnName).build();
}).forEach(builder::addMethod);
return builder.build();
}
use of ru.curs.celesta.score.DataGrainElement in project celesta by CourseOrchestra.
the class BasicCursor method getFrom.
// TODO:Must be refactored by new util class FromClauseGenerator
protected FromClause getFrom() {
FromClause result = new FromClause();
DataGrainElement ge = meta();
result.setGe(ge);
result.setExpression(db().tableString(ge.getGrain().getName(), ge.getName()));
return result;
}
use of ru.curs.celesta.score.DataGrainElement in project celesta by CourseOrchestra.
the class ParameterizedViewCursor method getFrom.
@Override
protected FromClause getFrom() {
FromClause result = new FromClause();
DataGrainElement ge = meta();
result.setGe(ge);
result.setExpression(db().getCallFunctionSql(meta));
List<Object> paramValues = meta().getParameters().keySet().stream().map(pName -> parameters.get(pName)).collect(Collectors.toList());
result.setParameters(paramValues);
return result;
}
Aggregations