use of com.abubusoft.kripton.processor.core.ModelAnnotation in project kripton by xcesco.
the class SQLiteModelMethod method getJQLDeclared.
private String getJQLDeclared() {
ModelAnnotation inserAnnotation = this.getAnnotation(BindSqlInsert.class);
ModelAnnotation updateAnnotation = this.getAnnotation(BindSqlUpdate.class);
ModelAnnotation selectAnnotation = this.getAnnotation(BindSqlSelect.class);
ModelAnnotation deleteAnnotation = this.getAnnotation(BindSqlDelete.class);
String jql = null;
int counter = 0;
if (selectAnnotation != null) {
jql = selectAnnotation.getAttribute(AnnotationAttributeType.JQL);
if (StringUtils.hasText(jql)) {
counter++;
AssertKripton.assertTrue(selectAnnotation.getAttributeCount() > 1, "Annotation %s in method %s.%s have more than one annotation with JQL attribute", selectAnnotation.getSimpleName(), this.getParent().getName(), this.getName());
}
}
if (inserAnnotation != null) {
jql = inserAnnotation.getAttribute(AnnotationAttributeType.JQL);
if (StringUtils.hasText(jql)) {
counter++;
AssertKripton.assertTrue(inserAnnotation.getAttributeCount() > 1, "Annotation %s in method %s.%s have more than one annotation with JQL attribute", inserAnnotation.getSimpleName(), this.getParent().getName(), this.getName());
}
}
if (updateAnnotation != null) {
jql = updateAnnotation.getAttribute(AnnotationAttributeType.JQL);
if (StringUtils.hasText(jql)) {
counter++;
AssertKripton.assertTrue(updateAnnotation.getAttributeCount() > 1, "Annotation %s in method %s.%s have more than one annotation with JQL attribute", updateAnnotation.getSimpleName(), this.getParent().getName(), this.getName());
}
}
if (deleteAnnotation != null) {
jql = deleteAnnotation.getAttribute(AnnotationAttributeType.JQL);
if (StringUtils.hasText(jql)) {
counter++;
AssertKripton.assertTrue(deleteAnnotation.getAttributeCount() > 1, "Annotation %s in method %s.%s have more than one annotation with JQL attribute", deleteAnnotation.getSimpleName(), this.getParent().getName(), this.getName());
}
}
AssertKripton.assertTrue(counter <= 1, "Method %s.%s have more than one annotation with JQL attribute", this.getParent().getName(), this.getName());
// remove unscape charater (example \'%\' -> '%')
jql = StringEscapeUtils.unescapeEcmaScript(jql);
return jql;
}
use of com.abubusoft.kripton.processor.core.ModelAnnotation in project kripton by xcesco.
the class AnnotationUtility method getAnnotationAttribute.
static <T> T getAnnotationAttribute(ModelWithAnnotation model, Class<? extends Annotation> annotation, AnnotationAttributeType attribute, T defaultValue, OnAnnotationAttributeListener<T> listener) {
String attributeResult;
ModelAnnotation item = model.getAnnotation(annotation);
if (item != null) {
attributeResult = item.getAttribute(attribute);
return listener.onFound(attributeResult);
}
return defaultValue;
}
use of com.abubusoft.kripton.processor.core.ModelAnnotation in project kripton by xcesco.
the class SqlInsertBuilder method detectInsertType.
public static InsertType detectInsertType(SQLiteModelMethod method) {
SQLiteDaoDefinition daoDefinition = method.getParent();
SQLiteEntity entity = daoDefinition.getEntity();
InsertType insertResultType = null;
// check type of arguments
int count = 0;
for (Pair<String, TypeName> param : method.getParameters()) {
if (TypeUtility.isEquals(param.value1, typeName(entity.getElement()))) {
count++;
}
}
AssertKripton.failWithInvalidMethodSignException(method.getParameters().size() == 0, method, " INSERT operations require at least one parameter");
if (count == 0) {
// method to insert raw data: no bean is used
insertResultType = InsertType.INSERT_RAW;
ModelAnnotation annotation = method.getAnnotation(BindSqlInsert.class);
// check value attribute
AssertKripton.failWithInvalidMethodSignException(AnnotationUtility.extractAsStringArray(method, annotation, AnnotationAttributeType.FIELDS).size() > 0, method, " can not use attribute %s in this kind of query definition", AnnotationAttributeType.FIELDS.getValue());
// check excludeFields attribute
AssertKripton.failWithInvalidMethodSignException(AnnotationUtility.extractAsStringArray(method, annotation, AnnotationAttributeType.EXCLUDED_FIELDS).size() > 0, method, " can not use attribute %s in this kind of query definition", AnnotationAttributeType.EXCLUDED_FIELDS.getValue());
// check if there is only one parameter
AssertKripton.failWithInvalidMethodSignException(method.getParameters().size() != 1 && TypeUtility.isEquals(method.getParameters().get(0).value1, daoDefinition.getEntityClassName()), method);
// check no
AssertKripton.failWithInvalidMethodSignException(annotation.getAttributeAsBoolean(AnnotationAttributeType.INCLUDE_PRIMARY_KEY), method, "attribute '%s' can not be used here", AnnotationAttributeType.INCLUDE_PRIMARY_KEY.getValue());
} else if (count == 1) {
insertResultType = InsertType.INSERT_BEAN;
AssertKripton.failWithInvalidMethodSignException(method.getParameters().size() > 1, method, " aspected only one parameter of %s type", daoDefinition.getEntityClassName());
} else {
throw (new InvalidMethodSignException(method, "only one parameter of type " + typeName(entity.getElement()) + " can be used"));
}
return insertResultType;
}
use of com.abubusoft.kripton.processor.core.ModelAnnotation in project kripton by xcesco.
the class SqlSelectBuilder method generateSelect.
/**
* @param elementUtils
* @param builder
* @param method
* @throws ClassNotFoundException
*/
public static void generateSelect(Builder builder, SQLiteModelMethod method) throws ClassNotFoundException {
SQLiteDaoDefinition daoDefinition = method.getParent();
SQLiteEntity entity = daoDefinition.getEntity();
SelectBuilderUtility.SelectType selectResultType = null;
// if true, field must be associate to ben attributes
TypeName returnTypeName = method.getReturnClass();
ParameterizedTypeName readBeanListener = ParameterizedTypeName.get(ClassName.get(OnReadBeanListener.class), ClassName.get(entity.getElement()));
ClassName readCursorListener = ClassName.get(OnReadCursorListener.class);
ModelAnnotation annotation = method.getAnnotation(BindSqlSelect.class);
int pageSize = annotation.getAttributeAsInt(AnnotationAttributeType.PAGE_SIZE);
AssertKripton.failWithInvalidMethodSignException(pageSize < 0, method, "in @%s(pageSize) must be set with positive number", BindSqlSelect.class.getSimpleName());
AssertKripton.failWithInvalidMethodSignException(pageSize > 0 && method.hasDynamicPageSizeConditions(), method, "can not define @%s(pageSize) and mark a method parameter with @%s ", BindSqlSelect.class.getSimpleName(), BindSqlPageSize.class.getSimpleName());
if (TypeUtility.isTypeIncludedIn(returnTypeName, Void.class, Void.TYPE)) {
// return VOID (in the parameters must be a listener)
if (SqlBuilderHelper.hasParameterOfType(method, readCursorListener)) {
selectResultType = SelectBuilderUtility.SelectType.LISTENER_CURSOR;
} else if (SqlBuilderHelper.hasParameterOfType(method, readBeanListener)) {
selectResultType = SelectBuilderUtility.SelectType.LISTENER_BEAN;
}
} else if (TypeUtility.isTypeIncludedIn(returnTypeName, Cursor.class)) {
// return Cursor (no listener)
selectResultType = SelectBuilderUtility.SelectType.CURSOR;
} else if (returnTypeName instanceof ParameterizedTypeName) {
ParameterizedTypeName returnParameterizedTypeName = (ParameterizedTypeName) returnTypeName;
ClassName returnParameterizedClassName = returnParameterizedTypeName.rawType;
// return List (no listener)
AssertKripton.assertTrueOrInvalidMethodSignException(returnParameterizedTypeName.typeArguments.size() == 1, method, "return type %s is not supported", returnTypeName);
TypeName elementName = returnParameterizedTypeName.typeArguments.get(0);
Class<?> wrapperClazz = Class.forName(returnParameterizedClassName.toString());
if (PaginatedResult.class.isAssignableFrom(wrapperClazz)) {
// method must have pageSize, statically or dynamically
// defined
AssertKripton.assertTrueOrInvalidMethodSignException(method.hasDynamicPageSizeConditions() || pageSize > 0, method, "use of PaginatedResult requires 'pageSize' attribute or a @%s annotated parameter", returnTypeName, BindSqlPageSize.class.getSimpleName());
// paged result
AssertKripton.assertTrueOrInvalidMethodSignException(TypeUtility.isEquals(elementName, entity.getName().toString()), method, "return type %s is not supported", returnTypeName);
selectResultType = SelectBuilderUtility.SelectType.PAGED_RESULT;
// set typeName of paginatedResult
method.paginatedResultName = "paginatedResult";
} else if (Collection.class.isAssignableFrom(wrapperClazz)) {
if (TypeUtility.isEquals(elementName, entity.getName().toString())) {
// entity list
selectResultType = SelectBuilderUtility.SelectType.LIST_BEAN;
} else if (SQLTransformer.isSupportedJDKType(elementName) || TypeUtility.isByteArray(elementName)) {
// scalar list
selectResultType = SelectBuilderUtility.SelectType.LIST_SCALAR;
} else {
AssertKripton.failWithInvalidMethodSignException(true, method, "%s is invalid return type", method.getReturnClass());
}
}
} else if (TypeUtility.isEquals(returnTypeName, entity)) {
// return one element (no listener)
selectResultType = SelectBuilderUtility.SelectType.BEAN;
} else if (SQLTransformer.isSupportedJDKType(returnTypeName) || TypeUtility.isByteArray(returnTypeName)) {
// return single value string, int, long, short, double, float,
// String (no listener)
selectResultType = SelectBuilderUtility.SelectType.SCALAR;
}
AssertKripton.assertTrueOrInvalidMethodSignException(selectResultType != null, method, "'%s' as return type is not supported", returnTypeName);
// generate select method
selectResultType.generate(builder, method);
if (method.hasLiveData()) {
// generate
selectResultType.generateLiveData(builder, method);
}
if (method.contentProviderEntryPathEnabled) {
// we need to generate UPDATE or DELETE for content provider to
generateSelectForContentProvider(builder, method, selectResultType);
}
}
use of com.abubusoft.kripton.processor.core.ModelAnnotation in project kripton by xcesco.
the class InsertBeanHelper method getConflictAlgorithmType.
public static ConflictAlgorithmType getConflictAlgorithmType(SQLiteModelMethod method) {
ModelAnnotation annotation = method.getAnnotation(BindSqlInsert.class);
String value = annotation.getAttribute(AnnotationAttributeType.CONFLICT_ALGORITHM_TYPE);
if (value != null && value.indexOf(".") > -1) {
value = value.substring(value.lastIndexOf(".") + 1);
}
ConflictAlgorithmType conflictAlgorithmType = ConflictAlgorithmType.valueOf(value);
return conflictAlgorithmType;
}
Aggregations