/DaoGenerator/src-template/content-provider.ftl

https://github.com/greenrobot/greenDAO · Freemarker Template · 221 lines · 170 code · 23 blank · 28 comment · 9 complexity · 30a140dadaab6b9a9e81a7f2acdc1133 MD5 · raw file

  1. <#-- @ftlvariable name="entity" type="org.greenrobot.greendao.generator.Entity" -->
  2. <#-- @ftlvariable name="contentProvider" type="org.greenrobot.greendao.generator.ContentProvider" -->
  3. <#-- @ftlvariable name="schema" type="org.greenrobot.greendao.generator.Schema" -->
  4. package ${contentProvider.javaPackage};
  5. import android.content.ContentProvider;
  6. import android.content.ContentResolver;
  7. import android.content.ContentValues;
  8. import android.content.UriMatcher;
  9. import android.database.Cursor;
  10. import android.database.sqlite.SQLiteQueryBuilder;
  11. import android.net.Uri;
  12. import org.greenrobot.greendao.DaoLog;
  13. import org.greenrobot.greendao.database.StandardDatabase;
  14. import org.greenrobot.greendao.database.Database;
  15. import ${schema.defaultJavaPackageDao}.${schema.prefix}DaoSession;
  16. import ${entity.javaPackageDao}.${entity.classNameDao};
  17. /* Copy this code snippet into your AndroidManifest.xml inside the <application> element:
  18. <provider
  19. android:name="${contentProvider.javaPackage}.${contentProvider.className}"
  20. android:authorities="${contentProvider.authority}" />
  21. */
  22. public class ${contentProvider.className} extends ContentProvider {
  23. public static final String AUTHORITY = "${contentProvider.authority}";
  24. public static final String BASE_PATH = "${contentProvider.basePath}";
  25. public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);
  26. public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
  27. + "/" + BASE_PATH;
  28. public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
  29. + "/" + BASE_PATH;
  30. private static final String TABLENAME = ${entity.classNameDao}.TABLENAME;
  31. private static final String PK = ${entity.classNameDao}.Properties.${entity.pkProperty.propertyName?cap_first}.columnName;
  32. <#assign counter = 0>
  33. private static final int ${entity.className?upper_case}_DIR = ${counter};
  34. private static final int ${entity.className?upper_case}_ID = ${counter+1};
  35. private static final UriMatcher sURIMatcher;
  36. static {
  37. sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  38. sURIMatcher.addURI(AUTHORITY, BASE_PATH, ${entity.className?upper_case}_DIR);
  39. sURIMatcher.addURI(AUTHORITY, BASE_PATH + "/#", ${entity.className?upper_case}_ID);
  40. }
  41. /**
  42. * This must be set from outside, it's recommended to do this inside your Application object.
  43. * Subject to change (static isn't nice).
  44. */
  45. public static ${schema.prefix}DaoSession daoSession;
  46. @Override
  47. public boolean onCreate() {
  48. // if(daoSession == null) {
  49. // throw new IllegalStateException("${schema.prefix}DaoSession must be set before content provider is created");
  50. // }
  51. DaoLog.d("Content Provider started: " + CONTENT_URI);
  52. return true;
  53. }
  54. protected Database getDatabase() {
  55. if(daoSession == null) {
  56. throw new IllegalStateException("${schema.prefix}DaoSession must be set during content provider is active");
  57. }
  58. return daoSession.getDatabase();
  59. }
  60. <#--
  61. ##########################################
  62. ########## Insert ##############
  63. ##########################################
  64. -->
  65. @Override
  66. public Uri insert(Uri uri, ContentValues values) {
  67. <#if contentProvider.isReadOnly()>
  68. throw new UnsupportedOperationException("This content provider is readonly");
  69. <#else>
  70. int uriType = sURIMatcher.match(uri);
  71. long id = 0;
  72. String path = "";
  73. switch (uriType) {
  74. case ${entity.className?upper_case}_DIR:
  75. id = getDatabase().insert(TABLENAME, null, values);
  76. path = BASE_PATH + "/" + id;
  77. break;
  78. default:
  79. throw new IllegalArgumentException("Unknown URI: " + uri);
  80. }
  81. getContext().getContentResolver().notifyChange(uri, null);
  82. return Uri.parse(path);
  83. </#if>
  84. }
  85. <#--
  86. ##########################################
  87. ########## Delete ##############
  88. ##########################################
  89. -->
  90. @Override
  91. public int delete(Uri uri, String selection, String[] selectionArgs) {
  92. <#if contentProvider.isReadOnly()>
  93. throw new UnsupportedOperationException("This content provider is readonly");
  94. <#else>
  95. int uriType = sURIMatcher.match(uri);
  96. Database db = getDatabase();
  97. int rowsDeleted = 0;
  98. String id;
  99. switch (uriType) {
  100. case ${entity.className?upper_case}_DIR:
  101. rowsDeleted = db.delete(TABLENAME, selection, selectionArgs);
  102. break;
  103. case ${entity.className?upper_case}_ID:
  104. id = uri.getLastPathSegment();
  105. if (TextUtils.isEmpty(selection)) {
  106. rowsDeleted = db.delete(TABLENAME, PK + "=" + id, null);
  107. } else {
  108. rowsDeleted = db.delete(TABLENAME, PK + "=" + id + " and "
  109. + selection, selectionArgs);
  110. }
  111. break;
  112. default:
  113. throw new IllegalArgumentException("Unknown URI: " + uri);
  114. }
  115. getContext().getContentResolver().notifyChange(uri, null);
  116. return rowsDeleted;
  117. </#if>
  118. }
  119. <#--
  120. ##########################################
  121. ########## Update ##############
  122. ##########################################
  123. -->
  124. @Override
  125. public int update(Uri uri, ContentValues values, String selection,
  126. String[] selectionArgs) {
  127. <#if contentProvider.isReadOnly()>
  128. throw new UnsupportedOperationException("This content provider is readonly");
  129. <#else>
  130. int uriType = sURIMatcher.match(uri);
  131. Database db = getDatabase();
  132. int rowsUpdated = 0;
  133. String id;
  134. switch (uriType) {
  135. case ${entity.className?upper_case}_DIR:
  136. rowsUpdated = db.update(TABLENAME, values, selection, selectionArgs);
  137. break;
  138. case ${entity.className?upper_case}_ID:
  139. id = uri.getLastPathSegment();
  140. if (TextUtils.isEmpty(selection)) {
  141. rowsUpdated = db.update(TABLENAME, values, PK + "=" + id, null);
  142. } else {
  143. rowsUpdated = db.update(TABLENAME, values, PK + "=" + id
  144. + " and " + selection, selectionArgs);
  145. }
  146. break;
  147. default:
  148. throw new IllegalArgumentException("Unknown URI: " + uri);
  149. }
  150. getContext().getContentResolver().notifyChange(uri, null);
  151. return rowsUpdated;
  152. </#if>
  153. }
  154. <#--
  155. ##########################################
  156. ########## Query ##############
  157. ##########################################
  158. -->
  159. @Override
  160. public Cursor query(Uri uri, String[] projection, String selection,
  161. String[] selectionArgs, String sortOrder) {
  162. SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
  163. int uriType = sURIMatcher.match(uri);
  164. switch (uriType) {
  165. case ${entity.className?upper_case}_DIR:
  166. queryBuilder.setTables(TABLENAME);
  167. break;
  168. case ${entity.className?upper_case}_ID:
  169. queryBuilder.setTables(TABLENAME);
  170. queryBuilder.appendWhere(PK + "="
  171. + uri.getLastPathSegment());
  172. break;
  173. default:
  174. throw new IllegalArgumentException("Unknown URI: " + uri);
  175. }
  176. Database db = getDatabase();
  177. Cursor cursor = queryBuilder.query(((StandardDatabase) db).getSQLiteDatabase(), projection, selection,
  178. selectionArgs, null, null, sortOrder);
  179. cursor.setNotificationUri(getContext().getContentResolver(), uri);
  180. return cursor;
  181. }
  182. <#--
  183. ##########################################
  184. ########## GetType ##############
  185. ##########################################
  186. -->
  187. @Override
  188. public final String getType(Uri uri) {
  189. switch (sURIMatcher.match(uri)) {
  190. case ${entity.className?upper_case}_DIR:
  191. return CONTENT_TYPE;
  192. case ${entity.className?upper_case}_ID:
  193. return CONTENT_ITEM_TYPE;
  194. default :
  195. throw new IllegalArgumentException("Unsupported URI: " + uri);
  196. }
  197. }
  198. }