Skip to content

Commit 0b792ff

Browse files
committed
Crudable and SelectableById annotations processing.
Solving some bugs.
1 parent e5a7073 commit 0b792ff

File tree

7 files changed

+94
-83
lines changed

7 files changed

+94
-83
lines changed

LivingRoom-compiler/src/main/java/com/pentabin/livingroom/compiler/EntityClass.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import com.pentabin.livingroom.compiler.methods.ArchiveMethod;
66
import com.pentabin.livingroom.compiler.methods.AsyncMethod;
7-
import com.pentabin.livingroom.compiler.methods.CrudMethod;
7+
import com.pentabin.livingroom.compiler.methods.LivingroomMethod;
88
import com.pentabin.livingroom.compiler.methods.DeleteMethod;
99
import com.pentabin.livingroom.compiler.methods.InsertMethod;
1010
import com.pentabin.livingroom.compiler.methods.LiveMethod;
@@ -21,14 +21,13 @@
2121
import javax.lang.model.element.TypeElement;
2222

2323
import static com.pentabin.livingroom.compiler.LivingroomProcessor.dbClassName;
24-
import static com.pentabin.livingroom.compiler.LivingroomProcessor.getLiveDataType;
25-
import static com.pentabin.livingroom.compiler.methods.CrudMethod.CRUD;
26-
import static com.pentabin.livingroom.compiler.methods.CrudMethod.DELETE;
27-
import static com.pentabin.livingroom.compiler.methods.CrudMethod.GET_ALL;
28-
import static com.pentabin.livingroom.compiler.methods.CrudMethod.GET_BY_ID;
29-
import static com.pentabin.livingroom.compiler.methods.CrudMethod.INSERT;
30-
import static com.pentabin.livingroom.compiler.methods.CrudMethod.SOFT_DELETE;
31-
import static com.pentabin.livingroom.compiler.methods.CrudMethod.UPDATE;
24+
import static com.pentabin.livingroom.compiler.methods.LivingroomMethod.CRUD;
25+
import static com.pentabin.livingroom.compiler.methods.LivingroomMethod.DELETE;
26+
import static com.pentabin.livingroom.compiler.methods.LivingroomMethod.GET_ALL;
27+
import static com.pentabin.livingroom.compiler.methods.LivingroomMethod.GET_BY_ID;
28+
import static com.pentabin.livingroom.compiler.methods.LivingroomMethod.INSERT;
29+
import static com.pentabin.livingroom.compiler.methods.LivingroomMethod.SOFT_DELETE;
30+
import static com.pentabin.livingroom.compiler.methods.LivingroomMethod.UPDATE;
3231

3332
public class EntityClass {
3433
private static final String SUFFIX_DAO = "Dao";
@@ -42,7 +41,7 @@ public class EntityClass {
4241
private String daoClassName;
4342
private String repositoryClassName;
4443
private String viewModelClassName;
45-
private Set<CrudMethod> methodsSet;
44+
private Set<LivingroomMethod> methodsSet;
4645

4746
public EntityClass(TypeElement entityClass) {
4847
this.typeElement = entityClass;
@@ -86,16 +85,11 @@ public String getViewModelClassName() {
8685
return viewModelClassName;
8786
}
8887

89-
public Set<? extends CrudMethod> getMethodsSet() {
88+
public Set<? extends LivingroomMethod> getMethodsSet() {
9089
return methodsSet;
9190
}
9291

93-
public void setMethodsSet(Set<CrudMethod> methodsSet) {
94-
this.methodsSet = methodsSet;
95-
}
96-
97-
public void addMethod(CrudMethod method) {
98-
System.err.println("Add Method --->" + method.getClass().getSimpleName());
92+
public void addMethod(LivingroomMethod method) {
9993
methodsSet.add(method);
10094
}
10195

@@ -114,12 +108,23 @@ private void addUpdateMethod() {
114108
private void addArchiveMethod() {
115109
methodsSet.add(new ArchiveMethod(this) );
116110
}
111+
117112
private void addGetAllMethod() {
118113
methodsSet.add(new LiveMethod(GET_ALL, "isDeleted = 0", this, null) );
119114
}
120115

116+
private void addGetByIdMethod() {
117+
String[] params = {"Long id"};
118+
methodsSet.add(new LiveMethod(GET_BY_ID, "id = :id", this, params, false) );
119+
}
120+
121121
private void addCrudMethods() {
122-
//methodsSet.addAll(CrudMethod.basicCrudMethods(typeName));
122+
addInsertMethod(); // TODO display a warning if already exists
123+
addUpdateMethod();
124+
addDeleteMethod();
125+
addArchiveMethod();
126+
addGetAllMethod();
127+
addGetByIdMethod();
123128
}
124129

125130
public void addMethod(String type) {
@@ -143,7 +148,8 @@ public void addMethod(String type) {
143148
case GET_ALL:
144149
addGetAllMethod();
145150
break;
146-
case GET_BY_ID: //TODO
151+
case GET_BY_ID:
152+
addGetByIdMethod();
147153
break;
148154
default:
149155
}
@@ -169,7 +175,7 @@ public TypeSpec generateDaoClass(){
169175
.addModifiers(Modifier.PUBLIC)
170176
.addAnnotation(Dao.class);
171177

172-
for (CrudMethod m: this.getMethodsSet()) {
178+
for (LivingroomMethod m: this.getMethodsSet()) {
173179
daoClass.addMethod(m.generateDaoMethod().build());
174180
}
175181
return daoClass.build();
@@ -191,9 +197,9 @@ public TypeSpec generateRepositoryClass() {
191197
.addField(ClassName.get(this.getPackageName(), this.getDaoClassName()), this.getDaoClassName().toLowerCase(), Modifier.PRIVATE)
192198
.addMethod(constructor);
193199

194-
for (CrudMethod m: this.getMethodsSet()) {
200+
for (LivingroomMethod m: this.getMethodsSet()) {
195201
if (!m.hasParams())
196-
repositoryClass.addField(getLiveDataType(this.getTypeName()), m.getMethodName()+"List", Modifier.PRIVATE);
202+
repositoryClass.addField(((LiveMethod)m).getLiveDataType(), m.getMethodName()+"List", Modifier.PRIVATE);// TODO test if live or not????
197203
repositoryClass.addMethod(m.generateRepositoryMethod(this).build());
198204
if (m instanceof AsyncMethod) {
199205
repositoryClass.addType(
@@ -216,9 +222,9 @@ public TypeSpec generateViewModelClass() {
216222
.addModifiers(Modifier.PUBLIC)
217223
.addField(ClassName.get(this.getPackageName(), this.getRepositoryClassName()), this.getRepositoryClassName().toLowerCase(), Modifier.PRIVATE)
218224
.addMethod(constructor);
219-
for (CrudMethod m: this.getMethodsSet()) {
225+
for (LivingroomMethod m: this.getMethodsSet()) {
220226
if (!m.hasParams()) {
221-
viewModelClass.addField(getLiveDataType(this.getTypeName()), m.getMethodName()+"List", Modifier.PRIVATE);
227+
viewModelClass.addField(((LiveMethod)m).getLiveDataType(), m.getMethodName()+"List", Modifier.PRIVATE);
222228
}
223229
viewModelClass.addMethod(m.generateViewModelMethod(this).build());
224230
}

LivingRoom-compiler/src/main/java/com/pentabin/livingroom/compiler/CrudRoom.java renamed to LivingRoom-compiler/src/main/java/com/pentabin/livingroom/compiler/LivingRoomDatabase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package com.pentabin.livingroom.compiler;
22

3-
public class CrudRoom {
3+
public class LivingRoomDatabase {
44
}

LivingRoom-compiler/src/main/java/com/pentabin/livingroom/compiler/LivingroomProcessor.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import com.pentabin.livingroom.annotations.SelectableWhere;
1111
import com.pentabin.livingroom.annotations.SelectableWheres;
1212
import com.pentabin.livingroom.annotations.Updatable;
13-
import com.pentabin.livingroom.compiler.methods.CrudMethod;
13+
import com.pentabin.livingroom.compiler.methods.LivingroomMethod;
1414
import com.squareup.javapoet.AnnotationSpec;
1515
import com.squareup.javapoet.ClassName;
1616
import com.squareup.javapoet.FieldSpec;
@@ -21,7 +21,6 @@
2121
import com.squareup.javapoet.TypeSpec;
2222

2323
import java.io.IOException;
24-
import java.lang.annotation.Annotation;
2524
import java.util.ArrayList;
2625
import java.util.Collection;
2726
import java.util.HashMap;
@@ -42,9 +41,10 @@
4241
import javax.lang.model.element.TypeElement;
4342
import javax.lang.model.type.DeclaredType;
4443

45-
import static com.pentabin.livingroom.compiler.methods.CrudMethod.GET_ALL;
46-
import static com.pentabin.livingroom.compiler.methods.CrudMethod.GET_BY_ID;
47-
import static com.pentabin.livingroom.compiler.methods.CrudMethod.INSERT;
44+
import static com.pentabin.livingroom.compiler.methods.LivingroomMethod.CRUD;
45+
import static com.pentabin.livingroom.compiler.methods.LivingroomMethod.GET_ALL;
46+
import static com.pentabin.livingroom.compiler.methods.LivingroomMethod.GET_BY_ID;
47+
import static com.pentabin.livingroom.compiler.methods.LivingroomMethod.INSERT;
4848

4949
/**
5050
*
@@ -57,6 +57,8 @@
5757
"com.pentabin.livingroom.annotations.Updatable",
5858
"com.pentabin.livingroom.annotations.Archivable",
5959
"com.pentabin.livingroom.annotations.SelectableWhere",
60+
"com.pentabin.livingroom.annotations.SelectableAll",
61+
"com.pentabin.livingroom.annotations.SelectableById",
6062
"com.pentabin.livingroom.annotations.SelectableWheres",
6163

6264

@@ -66,7 +68,7 @@ public class LivingroomProcessor extends AbstractProcessor {// TODO Rename to Li
6668

6769
private List<TypeName> entities;
6870
private HashMap<TypeElement, EntityClass> entitiesList;
69-
static String packageName; //TODO
71+
static String packageName; //TODO fro the database
7072
static final String SUFFIX_DAO = "Dao";
7173
static final String dbClassName = "CustomRoomDatabase";
7274

@@ -95,13 +97,14 @@ public boolean process(Set<? extends TypeElement> set, RoundEnvironment env) {
9597
System.err.println("The annotated class must inherit from BasicEntity");
9698
}
9799

98-
parseCrudable(crudableElements);
100+
parseCrudable(env);
99101
parseInsertable(env);
100102
parseDeletable(env);
101103
parseUpdatable(env);
102104
parseArchivable(env);
103105
parseSelectable(env);
104106
parseSelectableAll(env);
107+
parseSelectableById(env);
105108

106109
for (Map.Entry<TypeElement, EntityClass> e: entitiesList.entrySet()) {
107110
try {
@@ -132,10 +135,10 @@ private void parseAnnotation(Collection<? extends Element> crudableElements, St
132135
}
133136
}
134137

135-
private void parseCrudable(Collection<? extends Element> crudableElements) { // TODO change like others do
136-
//parseAnnotation(crudableElements, new Cr);
137-
// TODO Crudable.class
138-
}
138+
private void parseCrudable(RoundEnvironment env) { // TODO change like others do
139+
Collection<? extends Element> elements =
140+
env.getElementsAnnotatedWith(Crudable.class);
141+
parseAnnotation(elements, CRUD); }
139142

140143
private void parseInsertable(RoundEnvironment env) {
141144
Collection<? extends Element> insertableElements =
@@ -146,20 +149,20 @@ private void parseInsertable(RoundEnvironment env) {
146149
private void parseDeletable(RoundEnvironment env) {
147150
Collection<? extends Element> deletableElements =
148151
env.getElementsAnnotatedWith(Deletable.class);
149-
parseAnnotation(deletableElements, CrudMethod.DELETE);
152+
parseAnnotation(deletableElements, LivingroomMethod.DELETE);
150153
}
151154

152155
private void parseUpdatable(RoundEnvironment env) {
153156
Collection<? extends Element> updatableElements =
154157
env.getElementsAnnotatedWith(Updatable.class);
155-
parseAnnotation(updatableElements, CrudMethod.UPDATE);
158+
parseAnnotation(updatableElements, LivingroomMethod.UPDATE);
156159
}
157160

158161

159162
private void parseArchivable(RoundEnvironment env) {
160163
Collection<? extends Element> archivableElements =
161164
env.getElementsAnnotatedWith(Updatable.class);
162-
parseAnnotation(archivableElements, CrudMethod.SOFT_DELETE);
165+
parseAnnotation(archivableElements, LivingroomMethod.SOFT_DELETE);
163166
}
164167

165168
private void parseSelectableAll(RoundEnvironment env) {
@@ -188,7 +191,7 @@ private void parseSelectable(RoundEnvironment env) {
188191
a.params());
189192
} else {
190193
EntityClass entityClass = new EntityClass((TypeElement) e);
191-
entitiesList.get(e).addSelectMethod(a.methodName(),
194+
entityClass.addSelectMethod(a.methodName(),
192195
a.where(),
193196
a.params());
194197
entitiesList.put((TypeElement) e, entityClass);
@@ -225,14 +228,6 @@ private void generateViewModelClass(EntityClass clazz) throws IOException {
225228
javaFile.writeTo(filer);
226229
}
227230

228-
public static ParameterizedTypeName getLiveDataType(TypeName clazz){ // TODO put it on utils
229-
ClassName liveDataClass = ClassName.get("androidx.lifecycle", "LiveData");
230-
ClassName listClass = ClassName.get("java.util", "List");
231-
ParameterizedTypeName returnType = ParameterizedTypeName.get(liveDataClass,
232-
ParameterizedTypeName.get(listClass, clazz));
233-
return returnType;
234-
}
235-
236231
private void generateRepositoryClass(EntityClass clazz) throws IOException {
237232
JavaFile javaFile = JavaFile.builder(packageName, clazz.generateRepositoryClass()).build();
238233
Filer filer = processingEnv.getFiler();

LivingRoom-compiler/src/main/java/com/pentabin/livingroom/compiler/methods/AsyncMethod.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99
import com.squareup.javapoet.TypeName;
1010
import com.squareup.javapoet.TypeSpec;
1111

12-
import java.util.Iterator;
13-
import java.util.Map;
14-
1512
import javax.lang.model.element.Modifier;
1613

1714
// TODO this sub class has only one parameter (item of type Entity)
18-
public abstract class AsyncMethod extends CrudMethod {
15+
public abstract class AsyncMethod extends LivingroomMethod {
1916

2017
private static final String asyncTaskSuffix = "AsyncTask";
2118
public static final String ITEM_PARAM = "item"; // TODO replace everywhere
@@ -54,13 +51,14 @@ private ParameterizedTypeName getAsyncTaskType() {
5451
public MethodSpec.Builder generateRepositoryMethod(EntityClass entityClass) {
5552
MethodSpec.Builder builder = super.generateMethod();
5653
CodeBlock.Builder innerCode = CodeBlock.builder();
54+
if (getPreCode() != null) builder.addCode(this.getPreCode());
5755

5856
if (this.isReturnVoid())
59-
innerCode = innerCode
57+
innerCode
6058
.addStatement("new $N().execute($N)",
6159
asyncTaskClassName(entityClass),
6260
this.hasParams() ? "item" : ""); // TODO Maybe pass Dao as parameter to AsyncTask;
63-
else innerCode = CodeBlock.builder()
61+
else innerCode
6462
.beginControlFlow("try")
6563
.addStatement("return new $N().execute($N).get()", asyncTaskClassName(entityClass), hasParams() ? "item" : "")
6664
.nextControlFlow("catch ($T e)", ClassName.get(Throwable.class))
@@ -102,12 +100,11 @@ public TypeSpec.Builder generateAsyncTaskClass(EntityClass entityClass) {
102100
public MethodSpec.Builder generateViewModelMethod(EntityClass entityClass) {
103101
MethodSpec.Builder builder = super.generateMethod();
104102
CodeBlock.Builder innerCode = CodeBlock.builder();
105-
innerCode = innerCode
106-
.addStatement("$N $N.$N($N)",
107-
this.isReturnVoid()?"":"return",
108-
entityClass.getRepositoryClassName().toLowerCase(),
109-
this.getMethodName(),
110-
this.hasParams() ? "item" : "");
103+
innerCode.addStatement("$N $N.$N($N)",
104+
this.isReturnVoid()?"":"return",
105+
entityClass.getRepositoryClassName().toLowerCase(),
106+
this.getMethodName(),
107+
this.hasParams() ? "item" : "");
111108

112109
builder.addCode(innerCode.build());
113110
return builder;

LivingRoom-compiler/src/main/java/com/pentabin/livingroom/compiler/methods/LiveMethod.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,21 @@
1313
import java.util.Iterator;
1414
import java.util.Map;
1515

16-
public class LiveMethod extends CrudMethod {
16+
public class LiveMethod extends LivingroomMethod {
1717
private String where;
1818
private boolean isLiveData; // TODO can either be live or not
19+
private boolean isList;
1920

2021
public LiveMethod(String methodName, String where, EntityClass entityClass, String[] params) {
22+
this(methodName, where, entityClass, params, true);
23+
}
24+
25+
public LiveMethod(String methodName, String where, EntityClass entityClass, String[] params, boolean isList) {
2126
super(entityClass, methodName);
22-
this.setReturnType(getLiveDataType(entityClass.getTypeName()));
23-
this.setAnnotation(Query.class);
27+
this.isList = isList;
2428
this.where = where;
29+
this.setReturnType(getLiveDataType());
30+
this.setAnnotation(Query.class);
2531

2632
if (params != null && params.length>=1) {
2733
for (String s : params) {
@@ -91,11 +97,12 @@ public MethodSpec.Builder generateViewModelMethod(EntityClass entityClass){
9197
return builder;
9298
}
9399

94-
public static ParameterizedTypeName getLiveDataType(TypeName clazz){ // TODO put it on utils + One or List
100+
public ParameterizedTypeName getLiveDataType(){
95101
ClassName liveDataClass = ClassName.get("androidx.lifecycle", "LiveData");
96102
ClassName listClass = ClassName.get("java.util", "List");
97-
ParameterizedTypeName returnType = ParameterizedTypeName.get(liveDataClass,
98-
ParameterizedTypeName.get(listClass, clazz));
103+
ParameterizedTypeName returnType = isList?
104+
ParameterizedTypeName.get(liveDataClass, ParameterizedTypeName.get(listClass, getEntityClass().getTypeName()))
105+
: ParameterizedTypeName.get(liveDataClass,getEntityClass().getTypeName());
99106
return returnType;
100107
}
101108

@@ -110,4 +117,7 @@ private String getParametersString(){
110117
return parameters.toString();
111118
}
112119

120+
public void setList(boolean list) {
121+
isList = list;
122+
}
113123
}

0 commit comments

Comments
 (0)