Search in sources :

Example 1 with Builder

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);
}
Also used : ContentProvider(android.content.ContentProvider) BindContentProvider(com.abubusoft.kripton.android.annotation.BindContentProvider) Builder(com.squareup.javapoet.CodeBlock.Builder) ArrayList(java.util.ArrayList) Uri(android.net.Uri) FieldSpec(com.squareup.javapoet.FieldSpec) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) Comparator(java.util.Comparator) SQLiteModelMethod(com.abubusoft.kripton.processor.sqlite.model.SQLiteModelMethod) PackageElement(javax.lang.model.element.PackageElement) Pair(com.abubusoft.kripton.common.Pair) HashSet(java.util.HashSet) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

ContentProvider (android.content.ContentProvider)1 Uri (android.net.Uri)1 BindContentProvider (com.abubusoft.kripton.android.annotation.BindContentProvider)1 Pair (com.abubusoft.kripton.common.Pair)1 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)1 SQLiteModelMethod (com.abubusoft.kripton.processor.sqlite.model.SQLiteModelMethod)1 Builder (com.squareup.javapoet.CodeBlock.Builder)1 FieldSpec (com.squareup.javapoet.FieldSpec)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 HashSet (java.util.HashSet)1 PackageElement (javax.lang.model.element.PackageElement)1