use of com.squareup.javapoet.CodeBlock.Builder in project kripton by xcesco.
the class BindContentProviderBuilder method build.
public void build(Elements elementUtils, Filer filer, SQLiteDatabaseSchema schema) throws Exception {
uriSet.clear();
String dataSourceName = schema.getName();
String dataSourceNameClazz = BindDataSourceBuilder.PREFIX + dataSourceName;
String contentProviderName = PREFIX + dataSourceName.replace(BindDataSourceBuilder.SUFFIX, "") + SUFFIX;
PackageElement pkg = elementUtils.getPackageOf(schema.getElement());
String packageName = pkg.isUnnamed() ? null : pkg.getQualifiedName().toString();
AnnotationProcessorUtilis.infoOnGeneratedClasses(BindContentProvider.class, packageName, contentProviderName);
classBuilder = TypeSpec.classBuilder(contentProviderName).addModifiers(Modifier.PUBLIC).superclass(ContentProvider.class);
classBuilder.addJavadoc("<p>This is the content provider generated for {@link $L}</p>\n\n", dataSourceName);
classBuilder.addJavadoc("<h2>Content provider authority:</h2>\n");
classBuilder.addJavadoc("<pre>$L</pre>\n\n", schema.contentProvider.authority);
generateOnCreate(dataSourceNameClazz);
generateOnShutdown(dataSourceNameClazz);
// define static fields
classBuilder.addField(FieldSpec.builder(String.class, "URI", Modifier.STATIC, Modifier.PUBLIC, Modifier.FINAL).initializer("$S", schema.contentProviderUri()).addJavadoc("<p>content provider's URI.</p>\n<pre>$L</pre>\n", schema.contentProviderUri()).build());
// instance
classBuilder.addField(FieldSpec.builder(className(dataSourceNameClazz), "dataSource", Modifier.PRIVATE, Modifier.STATIC).addJavadoc("<p>datasource singleton</p>\n").build());
classBuilder.addField(FieldSpec.builder(String.class, "AUTHORITY", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).addJavadoc("<p>Content provider authority</p>\n").initializer("$S", schema.contentProvider.authority).build());
classBuilder.addField(FieldSpec.builder(UriMatcher.class, "sURIMatcher", Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL).addJavadoc("<p>URI matcher</p>\n").initializer("new $T($T.NO_MATCH)", UriMatcher.class, UriMatcher.class).build());
Builder staticBuilder = CodeBlock.builder();
Converter<String, String> daoConstantConverter = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.UPPER_UNDERSCORE);
List<FieldSpec> listFieldUri = new ArrayList<>();
List<FieldSpec> listFieldString = new ArrayList<>();
List<FieldSpec> listFieldIndex = new ArrayList<>();
List<FieldSpec> listFieldAlias = new ArrayList<>();
for (SQLiteDaoDefinition daoDefinition : schema.getCollection()) {
String pathConstantName = "PATH_" + daoConstantConverter.convert(daoDefinition.getEntitySimplyClassName());
if (!daoDefinition.contentProviderEnabled)
continue;
// define content provider paths
for (SQLiteModelMethod daoMethod : daoDefinition.getCollection()) {
if (!daoMethod.contentProviderEntryPathEnabled)
continue;
ContentEntry entry = uriSet.get(daoMethod.contentProviderEntryPathTemplate);
if (entry == null) {
entry = new ContentEntry(daoMethod.contentProviderEntryPath);
uriSet.put(daoMethod.contentProviderEntryPathTemplate, entry);
entry.path = daoMethod.contentProviderEntryPath;
entry.uriTemplate = daoMethod.contentProviderEntryPathTemplate;
// we finish later
entry.pathCostant = pathConstantName;
entry.pathIndex = pathConstantName;
}
switch(daoMethod.jql.operationType) {
case INSERT:
AssertKripton.fail(entry.insert != null, String.format("In DAO %s, there are more than one %s statement associated to content provider path '%s'", daoDefinition.getName().toString(), daoMethod.jql.operationType, entry.path));
entry.insert = daoMethod;
break;
case UPDATE:
AssertKripton.fail(entry.update != null, String.format("In DAO %s, there are more than one %s statement associated to content provider path '%s'", daoDefinition.getName().toString(), daoMethod.jql.operationType, entry.path));
entry.update = daoMethod;
break;
case SELECT:
AssertKripton.fail(entry.select != null, String.format("In DAO %s, there are more than one %s statement associated to content provider path '%s'", daoDefinition.getName().toString(), daoMethod.jql.operationType, entry.path));
entry.select = daoMethod;
break;
case DELETE:
AssertKripton.fail(entry.delete != null, String.format("In DAO %s, there are more than one %s statement associated to content provider path '%s'", daoDefinition.getName().toString(), daoMethod.jql.operationType, entry.path));
entry.delete = daoMethod;
break;
}
}
}
// sort uri set by
List<Pair<String, ContentEntry>> listUriSet = new ArrayList<>();
Pair<String, ContentEntry> item;
for (Entry<String, ContentEntry> entry : uriSet.entrySet()) {
item = new Pair<String, ContentEntry>(entry.getKey(), entry.getValue());
listUriSet.add(item);
}
Comparator<Pair<String, ContentEntry>> c = new Comparator<Pair<String, ContentEntry>>() {
@Override
public int compare(Pair<String, ContentEntry> lhs, Pair<String, ContentEntry> rhs) {
return lhs.value0.compareTo(rhs.value0);
}
};
Collections.sort(listUriSet, c);
Set<String> alreadyUsedName = new HashSet<String>();
Converter<String, String> format = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_UNDERSCORE);
int i = 1;
for (Pair<String, ContentEntry> entry : listUriSet) {
// define ordered part of attributes (after sort)
entry.value1.pathCostant += "_" + i;
entry.value1.pathIndex += "_" + i + "_INDEX";
entry.value1.pathValue = i;
// build class attributes
listFieldString.add(FieldSpec.builder(String.class, entry.value1.pathCostant, Modifier.STATIC, Modifier.FINAL).initializer(CodeBlock.of("$S", entry.value1.uriTemplate)).build());
listFieldIndex.add(FieldSpec.builder(Integer.TYPE, entry.value1.pathIndex, Modifier.STATIC, Modifier.FINAL).initializer(CodeBlock.of("$L", entry.value1.pathValue)).build());
listFieldUri.add(FieldSpec.builder(Uri.class, "URI_" + entry.value1.pathCostant, Modifier.STATIC, Modifier.FINAL, Modifier.PRIVATE).addJavadoc("<p>Uri</p>\n").addJavadoc("<pre>$L/$L</pre>\n", schema.contentProviderUri().replace("*", "[*]"), entry.value1.uriTemplate.replace("*", "[*]")).initializer(CodeBlock.of("Uri.parse(URI+\"/$L\")", entry.value1.uriTemplate)).build());
generateURIForMethod(schema, listFieldAlias, alreadyUsedName, format, entry, entry.value1.delete);
generateURIForMethod(schema, listFieldAlias, alreadyUsedName, format, entry, entry.value1.insert);
generateURIForMethod(schema, listFieldAlias, alreadyUsedName, format, entry, entry.value1.select);
generateURIForMethod(schema, listFieldAlias, alreadyUsedName, format, entry, entry.value1.update);
staticBuilder.addStatement("sURIMatcher.addURI(AUTHORITY, $L, $L)", entry.value1.pathCostant, entry.value1.pathIndex);
i++;
}
for (FieldSpec f : listFieldUri) {
classBuilder.addField(f);
}
for (FieldSpec f : listFieldString) {
classBuilder.addField(f);
}
for (FieldSpec f : listFieldIndex) {
classBuilder.addField(f);
}
for (FieldSpec f : listFieldAlias) {
classBuilder.addField(f);
}
classBuilder.addStaticBlock(staticBuilder.build());
generateQuery(schema);
generateInsert(schema);
generateUpdate(schema);
generateDelete(schema);
generateGetType(schema);
TypeSpec typeSpec = classBuilder.build();
JavaWriterHelper.writeJava2File(filer, packageName, typeSpec);
}
Aggregations