From 5a223e4b0888b69f3ecaaa475383d0e937c60640 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Wed, 5 Jul 2017 10:57:58 +0800 Subject: [PATCH] =?UTF-8?q?orm=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/command/normal/ResumeAllCmd.java | 3 +- .../core/download/DownloadGroupTarget.java | 3 +- .../download/DownloadGroupTaskEntity.java | 4 -- .../aria/core/download/DownloadTarget.java | 3 +- .../core/download/DownloadTaskEntity.java | 4 -- .../downloader/DownloadGroupUtil.java | 3 +- .../download/downloader/FileInfoThread.java | 4 +- .../arialyy/aria/core/inf/AbsTaskEntity.java | 3 +- .../core/queue/DownloadGroupTaskQueue.java | 1 + .../aria/core/queue/DownloadTaskQueue.java | 1 + .../aria/core/queue/UploadTaskQueue.java | 2 +- .../aria/core/upload/UploadReceiver.java | 2 +- .../aria/core/upload/UploadTarget.java | 3 +- .../java/com/arialyy/aria/orm/DbEntity.java | 15 +++++++- .../java/com/arialyy/aria/orm/SqlHelper.java | 38 ++++++++++++------- .../aria/core/upload/UploadTaskEntity.java | 3 +- 16 files changed, 56 insertions(+), 36 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java index 58b26bdc..2a019bb7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java @@ -41,7 +41,8 @@ final class ResumeAllCmd extends AbsNormalCmd { private AbsTask createTask(DownloadEntity entity) { AbsTask task = mQueue.getTask(entity); if (task == null) { - DownloadTaskEntity taskEntity = new DownloadTaskEntity(entity); + DownloadTaskEntity taskEntity = new DownloadTaskEntity(); + taskEntity.entity = entity; task = mQueue.createTask(mTargetName, taskEntity); } else { Log.w(TAG, "添加命令执行失败,【该任务已经存在】"); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java index f76ee2f2..9983edad 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java @@ -32,7 +32,8 @@ public class DownloadGroupTarget this.mEntity = entity; this.mTargetName = targetName; this.mUrls = urls; - mTaskEntity = new DownloadGroupTaskEntity(entity); + mTaskEntity = new DownloadGroupTaskEntity(); + mTaskEntity.entity = entity; } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTaskEntity.java index 8ef08664..4987a1d0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTaskEntity.java @@ -25,10 +25,6 @@ public class DownloadGroupTaskEntity extends AbsTaskEntity @OneToOne(table = DownloadGroupEntity.class, key = "groupName") public DownloadGroupEntity entity; - public DownloadGroupTaskEntity(DownloadGroupEntity entity) { - this.entity = entity; - } - @Override public DownloadGroupEntity getEntity() { return entity; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java index e9f4295a..e6bab790 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java @@ -31,7 +31,8 @@ public class DownloadTarget extends DownloadTarget(DownloadEntity entity, String targetName) { mEntity = entity; mTargetName = targetName; - mTaskEntity = new DownloadTaskEntity(entity); + mTaskEntity = new DownloadTaskEntity(); + mTaskEntity.entity = mEntity; } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java index 09e06d7e..13d1c6fd 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java @@ -29,10 +29,6 @@ public class DownloadTaskEntity extends AbsTaskEntity { public DownloadTaskEntity() { } - public DownloadTaskEntity(DownloadEntity entity) { - this.entity = entity; - } - @Override public DownloadEntity getEntity() { return entity; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java index 01e3c6bb..7dbd2585 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java @@ -242,7 +242,8 @@ public class DownloadGroupUtil implements IDownloadUtil { if (taskEntity != null) { return taskEntity; } - taskEntity = new DownloadTaskEntity(entity); + taskEntity = new DownloadTaskEntity(); + taskEntity.entity = entity; taskEntity.headers = mTaskEntity.headers; taskEntity.requestEnum = mTaskEntity.requestEnum; taskEntity.redirectUrlKey = mTaskEntity.redirectUrlKey; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FileInfoThread.java index 71e33f78..d47a1d3b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FileInfoThread.java @@ -117,8 +117,8 @@ class FileInfoThread implements Runnable { if (onFileInfoListener != null) { onFileInfoListener.onComplete(mEntity.getDownloadUrl(), code); } - mEntity.update(); - mTaskEntity.update(); + mEntity.save(); + mTaskEntity.save(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java index 26459feb..ba341fdc 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java @@ -18,6 +18,7 @@ package com.arialyy.aria.core.inf; import com.arialyy.aria.core.RequestEnum; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.Ignore; +import com.arialyy.aria.orm.Primary; import java.util.HashMap; import java.util.Map; @@ -29,7 +30,7 @@ public abstract class AbsTaskEntity extends DbEntity { /** * Task实体对应的key */ - public String key; + @Primary public String key = ""; /** * http 请求头 diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadGroupTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadGroupTaskQueue.java index 6699ef41..325284ed 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadGroupTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadGroupTaskQueue.java @@ -53,6 +53,7 @@ public class DownloadGroupTaskQueue if (!TextUtils.isEmpty(targetName)) { task = (DownloadGroupTask) TaskFactory.getInstance() .createTask(targetName, entity, DownloadGroupSchedulers.getInstance()); + entity.key = entity.getEntity().getGroupName(); mCachePool.putTask(task); } else { Log.e(TAG, "target name 为 null!!"); diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java index 169a9839..7cf4d0ed 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java @@ -118,6 +118,7 @@ public class DownloadTaskQueue if (!TextUtils.isEmpty(target)) { task = (DownloadTask) TaskFactory.getInstance() .createTask(target, entity, DownloadSchedulers.getInstance()); + entity.key = entity.getEntity().getDownloadUrl(); mCachePool.putTask(task); } else { Log.e(TAG, "target name 为 null!!"); diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java index 0d36f105..b58013f9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java @@ -54,12 +54,12 @@ public class UploadTaskQueue extends AbsTaskQueue { for (UploadEntity entity : allEntity) { if (entity.getState() == IEntity.STATE_RUNNING) { stopCmds.add( - CommonUtil.createCmd(targetName, new UploadTaskEntity(entity), NormalCmdFactory.TASK_STOP)); + CommonUtil.createCmd(targetName, new UploadTaskEntity(), NormalCmdFactory.TASK_STOP)); } } AriaManager.getInstance(AriaManager.APP).setCmds(stopCmds).exe(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java index cdbde0e1..1e6e481b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java @@ -27,7 +27,8 @@ public class UploadTarget extends AbsNormalTarget fields = CommonUtil.getAllFields(getClass()); List where = new ArrayList<>(); List values = new ArrayList<>(); for (Field field : fields) { @@ -165,7 +167,16 @@ public class DbEntity { continue; } where.add(field.getName()); - values.add(field.get(this) + ""); + Type type = field.getType(); + if (SqlHelper.isOneToOne(field)) { + values.add(SqlHelper.getOneToOneParams(field)); + } else if (type == List.class) { + values.add(SqlHelper.getListElementParams(field)); + } else if (type == Map.class) { + values.add(SqlHelper.map2Str((Map) field.get(this))); + } else { + values.add(field.get(this) + ""); + } } DbEntity entity = findData(getClass(), where.toArray(new String[where.size()]), values.toArray(new String[values.size()])); diff --git a/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java b/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java index 46541a94..4eeaa338 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java @@ -51,10 +51,6 @@ final class SqlHelper extends SQLiteOpenHelper { private static final int FIND_ALL_DATA = 5; private static final int DEL_DATA = 6; - private static final String MAP_FIELD = "map$$"; - private static final String LIST_FIELD = "list$$"; - private static final String GENERIC_FIELD = "Generic$$"; - private static volatile SqlHelper INSTANCE = null; static SqlHelper init(Context context) { @@ -380,7 +376,7 @@ final class SqlHelper extends SQLiteOpenHelper { /** * 获取一对一参数 */ - private static String getOneToOneParams(Field field) { + static String getOneToOneParams(Field field) { OneToOne oneToOne = field.getAnnotation(OneToOne.class); if (oneToOne == null) { throw new IllegalArgumentException("@OneToOne注解的对象必须要有@Primary注解的字段"); @@ -393,7 +389,7 @@ final class SqlHelper extends SQLiteOpenHelper { * * @param field list反射字段 */ - private static String getListElementParams(Field field) { + static String getListElementParams(Field field) { OneToMany oneToMany = field.getAnnotation(OneToMany.class); if (oneToMany == null) { throw new IllegalArgumentException("List中元素必须被@OneToMany注解"); @@ -600,15 +596,26 @@ final class SqlHelper extends SQLiteOpenHelper { throw new IllegalArgumentException("List中的元素对象必须需要@Primary注解的字段"); } //list字段保存的数据 + int kc = cursor.getColumnIndex(primaryKey); String params = cursor.getString(column); + String primaryData = cursor.getString(kc); + if (TextUtils.isEmpty(primaryData)) continue; + List list = findForeignData(db, primaryData, params); + if (list == null) continue; field.set(entity, findForeignData(db, primaryKey, params)); } else if (isOneToOne(field)) { String primaryKey = getPrimaryName(clazz); if (TextUtils.isEmpty(primaryKey)) { throw new IllegalArgumentException("@OneToOne的注解对象必须需要@Primary注解的字段"); } + int kc = cursor.getColumnIndex(primaryKey); String params = cursor.getString(column); - field.set(entity, findForeignData(db, primaryKey, params).get(0)); + String primaryData = cursor.getString(kc); + if (TextUtils.isEmpty(primaryData) || primaryData.equalsIgnoreCase("null")) continue; + List list = findForeignData(db, primaryData, params); + if (list != null && list.size() > 1) { + field.set(entity, list.get(0)); + } } } entity.rowID = cursor.getInt(cursor.getColumnIndex("rowid")); @@ -632,7 +639,7 @@ final class SqlHelper extends SQLiteOpenHelper { */ private static List findForeignData(SQLiteDatabase db, String primary, String childParams) { - String[] params = childParams.split("$$"); + String[] params = childParams.split("\\$\\$"); return findData(db, params[0], params[1] + "=?", primary); } @@ -643,10 +650,13 @@ final class SqlHelper extends SQLiteOpenHelper { * */ private static Map str2Map(String str) { - String[] element = str.split(","); Map map = new HashMap<>(); + if (TextUtils.isEmpty(str)) { + return map; + } + String[] element = str.split(","); for (String data : element) { - String[] s = data.split("$"); + String[] s = data.split("\\$"); map.put(s[0], s[1]); } return map; @@ -658,7 +668,7 @@ final class SqlHelper extends SQLiteOpenHelper { * {@code Map} * */ - private static String map2Str(Map map) { + static String map2Str(Map map) { StringBuilder sb = new StringBuilder(); Set keys = map.keySet(); for (String key : keys) { @@ -696,7 +706,7 @@ final class SqlHelper extends SQLiteOpenHelper { /** * 判断是否一对多注解 */ - private static boolean isOneToMany(Field field) { + static boolean isOneToMany(Field field) { OneToMany oneToMany = field.getAnnotation(OneToMany.class); return oneToMany != null; } @@ -704,7 +714,7 @@ final class SqlHelper extends SQLiteOpenHelper { /** * 判断是否是一对一注解 */ - private static boolean isOneToOne(Field field) { + static boolean isOneToOne(Field field) { OneToOne oneToOne = field.getAnnotation(OneToOne.class); return oneToOne != null; } @@ -712,7 +722,7 @@ final class SqlHelper extends SQLiteOpenHelper { /** * 判断是否是主键 */ - private static boolean isPrimary(Field field) { + static boolean isPrimary(Field field) { Primary pk = field.getAnnotation(Primary.class); return pk != null; } diff --git a/aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java b/aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java index 48f17ee7..93edfe96 100644 --- a/aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java +++ b/aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java @@ -38,8 +38,7 @@ public class UploadTaskEntity extends AbsTaskEntity { */ public Map formFields = new HashMap<>(); - public UploadTaskEntity(UploadEntity entity) { - this.entity = entity; + public UploadTaskEntity() { } @Override public UploadEntity getEntity() {