任务实体优化

pull/330/head
AriaLyy 7 years ago
parent 9f6c0d727a
commit fd57d3ffe9
  1. 34
      Aria/src/main/java/com/arialyy/aria/core/UrlMapping.java
  2. 1
      Aria/src/main/java/com/arialyy/aria/core/common/AbsFtpInfoThread.java
  3. 17
      Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java
  4. 1
      Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java
  5. 7
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java
  6. 8
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTaskEntity.java
  7. 15
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java
  8. 1
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  9. 11
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java
  10. 6
      Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java
  11. 1
      Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java
  12. 115
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java
  13. 1
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java
  14. 2
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java
  15. 14
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java
  16. 1
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsUploadTarget.java
  17. 16
      Aria/src/main/java/com/arialyy/aria/core/upload/BaseUListener.java
  18. 5
      Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java
  19. 30
      Aria/src/main/java/com/arialyy/aria/orm/NoNull.java
  20. 22
      Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java
  21. 10
      Aria/src/main/java/com/arialyy/aria/orm/SqlUtil.java
  22. 5
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  23. 9
      aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java

@ -0,0 +1,34 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core;
import com.arialyy.aria.orm.DbEntity;
/**
* Created by lyy on 2017/10/17.
* Url 映射表
*/
public class UrlMapping extends DbEntity {
/**
* 各实体对应的key
*/
public String key;
/**
* 链接
*/
public String url;
}

@ -88,7 +88,6 @@ public abstract class AbsFtpInfoThread<ENTITY extends AbsEntity, TASK_ENTITY ext
if (mSize != 0 && !isUpload) { if (mSize != 0 && !isUpload) {
mEntity.setFileSize(mSize); mEntity.setFileSize(mSize);
} }
mEntity.update();
mTaskEntity.update(); mTaskEntity.update();
onPreComplete(reply); onPreComplete(reply);
} catch (IOException e) { } catch (IOException e) {

@ -143,23 +143,18 @@ class BaseDListener<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<
} }
private void saveData(int state, long location) { private void saveData(int state, long location) {
mEntity.setComplete(state == IEntity.STATE_COMPLETE);
mTaskEntity.state = state; mTaskEntity.state = state;
if (state == IEntity.STATE_CANCEL) {
mEntity.setState(state);
mEntity.deleteData();
} else if (state == IEntity.STATE_COMPLETE) {
mEntity.setState(state); mEntity.setState(state);
mEntity.setComplete(state == IEntity.STATE_COMPLETE);
if (state == IEntity.STATE_CANCEL) {
mTaskEntity.deleteData();
return;
} else if (mEntity.isComplete()) {
mEntity.setCompleteTime(System.currentTimeMillis()); mEntity.setCompleteTime(System.currentTimeMillis());
mEntity.setCurrentProgress(mEntity.getFileSize()); mEntity.setCurrentProgress(mEntity.getFileSize());
mEntity.update(); } else if (location > 0) {
} else {
mEntity.setState(state);
if (location != -1) {
mEntity.setCurrentProgress(location); mEntity.setCurrentProgress(location);
} }
mEntity.update();
}
mTaskEntity.update(); mTaskEntity.update();
} }
} }

@ -55,7 +55,6 @@ abstract class BaseGroupTarget<TARGET extends AbsTarget, TASK_ENTITY extends Abs
entity = new DownloadGroupEntity(); entity = new DownloadGroupEntity();
entity.setGroupName(mGroupName); entity.setGroupName(mGroupName);
entity.setUrls(mUrls); entity.setUrls(mUrls);
entity.insert();
} }
return entity; return entity;
} }

@ -49,13 +49,10 @@ public class DownloadGroupTarget
mTaskEntity = DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", key); mTaskEntity = DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", key);
if (mTaskEntity == null) { if (mTaskEntity == null) {
mTaskEntity = new DownloadGroupTaskEntity(); mTaskEntity = new DownloadGroupTaskEntity();
mTaskEntity.key = key; mTaskEntity.save(getDownloadGroupEntity());
mTaskEntity.entity = getDownloadGroupEntity();
mTaskEntity.insert();
} }
if (mTaskEntity.entity == null || TextUtils.isEmpty(mTaskEntity.entity.getKey())) { if (mTaskEntity.entity == null || TextUtils.isEmpty(mTaskEntity.entity.getKey())) {
mTaskEntity.entity = getDownloadGroupEntity(); mTaskEntity.save(getDownloadGroupEntity());
mTaskEntity.save();
} }
mEntity = mTaskEntity.entity; mEntity = mTaskEntity.entity;
} }

@ -28,4 +28,12 @@ public class DownloadGroupTaskEntity extends AbsGroupTaskEntity<DownloadGroupEnt
@Override public DownloadGroupEntity getEntity() { @Override public DownloadGroupEntity getEntity() {
return entity; return entity;
} }
public void save(DownloadGroupEntity groupEntity){
key = groupEntity.getKey();
entity = groupEntity;
groupEntity.save();
save();
}
} }

@ -61,20 +61,13 @@ public class DownloadTarget
entity.getDownloadPath(), entity.getUrl()); entity.getDownloadPath(), entity.getUrl());
if (mTaskEntity == null) { if (mTaskEntity == null) {
mTaskEntity = new DownloadTaskEntity(); mTaskEntity = new DownloadTaskEntity();
mTaskEntity.url = entity.getUrl(); mTaskEntity.save(entity);
mTaskEntity.key = entity.getDownloadPath();
mTaskEntity.entity = entity;
mTaskEntity.save();
} else if (mTaskEntity.entity == null || TextUtils.isEmpty(mTaskEntity.entity.getUrl())) { } else if (mTaskEntity.entity == null || TextUtils.isEmpty(mTaskEntity.entity.getUrl())) {
mTaskEntity.entity = entity; mTaskEntity.save(entity);
mTaskEntity.save();
} else if (!mTaskEntity.entity.getUrl().equals(entity.getUrl())) { //处理地址切换而保存路径不变 } else if (!mTaskEntity.entity.getUrl().equals(entity.getUrl())) { //处理地址切换而保存路径不变
mTaskEntity.entity.deleteData(); mTaskEntity.save(entity);
mTaskEntity.entity = entity;
entity.save();
mTaskEntity.save();
} }
mEntity = mTaskEntity.entity; mEntity = entity;
} }
/** /**

@ -175,7 +175,6 @@ public class DownloadTask extends AbsNormalTask<DownloadTaskEntity> {
public DownloadTask build() { public DownloadTask build() {
DownloadTask task = new DownloadTask(taskEntity, outHandler); DownloadTask task = new DownloadTask(taskEntity, outHandler);
task.setTargetName(targetName); task.setTargetName(targetName);
taskEntity.getEntity().save();
taskEntity.save(); taskEntity.save();
return task; return task;
} }

@ -18,6 +18,7 @@ package com.arialyy.aria.core.download;
import com.arialyy.aria.core.inf.AbsNormalTaskEntity; import com.arialyy.aria.core.inf.AbsNormalTaskEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.orm.Ignore; import com.arialyy.aria.orm.Ignore;
import com.arialyy.aria.orm.NoNull;
import com.arialyy.aria.orm.OneToOne; import com.arialyy.aria.orm.OneToOne;
/** /**
@ -31,7 +32,7 @@ public class DownloadTaskEntity extends AbsNormalTaskEntity<DownloadEntity> {
/** /**
* 任务的url * 任务的url
*/ */
public String url = ""; @NoNull public String url = "";
/** /**
* 所属的任务组组名如果不属于任务组则为null * 所属的任务组组名如果不属于任务组则为null
@ -49,4 +50,12 @@ public class DownloadTaskEntity extends AbsNormalTaskEntity<DownloadEntity> {
@Override public DownloadEntity getEntity() { @Override public DownloadEntity getEntity() {
return entity; return entity;
} }
public void save(DownloadEntity entity) {
url = entity.getUrl();
key = entity.getDownloadPath();
this.entity = entity;
entity.save();
save();
}
} }

@ -50,12 +50,10 @@ public class FtpDirDownloadTarget
mTaskEntity = DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", key); mTaskEntity = DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", key);
if (mTaskEntity == null) { if (mTaskEntity == null) {
mTaskEntity = new DownloadGroupTaskEntity(); mTaskEntity = new DownloadGroupTaskEntity();
mTaskEntity.key = key; mTaskEntity.save(getDownloadGroupEntity());
mTaskEntity.entity = getDownloadGroupEntity();
mTaskEntity.insert();
} }
if (mTaskEntity.entity == null) { if (mTaskEntity.entity == null) {
mTaskEntity.entity = getDownloadGroupEntity(); mTaskEntity.save(getDownloadGroupEntity());
} }
mEntity = mTaskEntity.entity; mEntity = mTaskEntity.entity;
} }

@ -75,7 +75,6 @@ public class FtpDownloadTarget extends DownloadTarget {
mEntity.setDownloadPath(downloadPath); mEntity.setDownloadPath(downloadPath);
mEntity.setFileName(newFile.getName()); mEntity.setFileName(newFile.getName());
mTaskEntity.key = downloadPath; mTaskEntity.key = downloadPath;
mEntity.update();
mTaskEntity.update(); mTaskEntity.update();
CommonUtil.renameDownloadConfig(oldFile.getName(), newFile.getName()); CommonUtil.renameDownloadConfig(oldFile.getName(), newFile.getName());
} }

@ -169,25 +169,25 @@ public abstract class AbsGroupUtil implements IUtil {
* @param url 子任务下载地址 * @param url 子任务下载地址
*/ */
public void cancelSubTask(String url) { public void cancelSubTask(String url) {
//List<String> urls = mTaskEntity.entity.getUrls(); List<String> urls = mTaskEntity.entity.getUrls();
//if (urls != null && !urls.isEmpty() && urls.contains(url)) { if (urls != null && !urls.isEmpty() && urls.contains(url)) {
// urls.remove(url); urls.remove(url);
// DownloadTaskEntity det = DownloadTaskEntity det =
// DbEntity.findFirst(DownloadTaskEntity.class, "url=? and isGroupTask='true'", url); DbEntity.findFirst(DownloadTaskEntity.class, "url=? and isGroupTask='true'", url);
// if (det != null) { if (det != null) {
// mTotalLen -= det.getEntity().getFileSize(); mTotalLen -= det.getEntity().getFileSize();
// mGroupSize--; mGroupSize--;
// if (mGroupSize == 0) { if (mGroupSize == 0) {
// closeTimer(false); closeTimer(false);
// mListener.onCancel(); mListener.onCancel();
// } }
// } }
// mTaskEntity.getEntity().update(); mTaskEntity.update();
//} }
//Downloader d = getDownloader(url, false); Downloader d = getDownloader(url, false);
//if (d != null) { if (d != null) {
// d.cancel(); d.cancel();
//} }
} }
/** /**
@ -416,19 +416,18 @@ public abstract class AbsGroupUtil implements IUtil {
* 子任务事件监听 * 子任务事件监听
*/ */
private class ChildDownloadListener implements IDownloadListener { private class ChildDownloadListener implements IDownloadListener {
private DownloadTaskEntity subTaskEntity;
DownloadTaskEntity taskEntity; private DownloadEntity subEntity;
DownloadEntity entity;
private int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度 private int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度
private long mLastSaveTime; private long lastSaveTime;
long lastLen = 0; private long lastLen = 0;
ChildDownloadListener(DownloadTaskEntity entity) { ChildDownloadListener(DownloadTaskEntity entity) {
this.taskEntity = entity; subTaskEntity = entity;
this.entity = taskEntity.getEntity(); subEntity = subTaskEntity.getEntity();
this.entity.setFailNum(0); subEntity.setFailNum(0);
lastLen = this.entity.getCurrentProgress(); lastLen = subEntity.getCurrentProgress();
mLastSaveTime = System.currentTimeMillis(); lastSaveTime = System.currentTimeMillis();
} }
@Override public void onPre() { @Override public void onPre() {
@ -436,33 +435,33 @@ public abstract class AbsGroupUtil implements IUtil {
} }
@Override public void onPostPre(long fileSize) { @Override public void onPostPre(long fileSize) {
entity.setFileSize(fileSize); subEntity.setFileSize(fileSize);
entity.setConvertFileSize(CommonUtil.formatFileSize(fileSize)); subEntity.setConvertFileSize(CommonUtil.formatFileSize(fileSize));
saveData(IEntity.STATE_POST_PRE, -1); saveData(IEntity.STATE_POST_PRE, -1);
mListener.onSubPre(entity); mListener.onSubPre(subEntity);
} }
@Override public void onResume(long resumeLocation) { @Override public void onResume(long resumeLocation) {
saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING); saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING);
lastLen = resumeLocation; lastLen = resumeLocation;
mListener.onSubStart(entity); mListener.onSubStart(subEntity);
} }
@Override public void onStart(long startLocation) { @Override public void onStart(long startLocation) {
saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING); saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING);
lastLen = startLocation; lastLen = startLocation;
mListener.onSubStart(entity); mListener.onSubStart(subEntity);
} }
@Override public void onProgress(long currentLocation) { @Override public void onProgress(long currentLocation) {
long speed = currentLocation - lastLen; long speed = currentLocation - lastLen;
mCurrentLocation += speed; mCurrentLocation += speed;
entity.setCurrentProgress(currentLocation); subEntity.setCurrentProgress(currentLocation);
handleSpeed(speed); handleSpeed(speed);
mListener.onSubRunning(entity); mListener.onSubRunning(subEntity);
if (System.currentTimeMillis() - mLastSaveTime >= RUN_SAVE_INTERVAL) { if (System.currentTimeMillis() - lastSaveTime >= RUN_SAVE_INTERVAL) {
saveData(IEntity.STATE_RUNNING, currentLocation); saveData(IEntity.STATE_RUNNING, currentLocation);
mLastSaveTime = System.currentTimeMillis(); lastSaveTime = System.currentTimeMillis();
} }
lastLen = currentLocation; lastLen = currentLocation;
} }
@ -470,7 +469,7 @@ public abstract class AbsGroupUtil implements IUtil {
@Override public void onStop(long stopLocation) { @Override public void onStop(long stopLocation) {
saveData(IEntity.STATE_STOP, stopLocation); saveData(IEntity.STATE_STOP, stopLocation);
handleSpeed(0); handleSpeed(0);
mListener.onSubStop(entity); mListener.onSubStop(subEntity);
synchronized (AbsGroupUtil.class) { synchronized (AbsGroupUtil.class) {
mStopNum++; mStopNum++;
if (mStopNum + mCompleteNum + mInitFailNum + mFailNum >= mGroupSize) { if (mStopNum + mCompleteNum + mInitFailNum + mFailNum >= mGroupSize) {
@ -483,13 +482,13 @@ public abstract class AbsGroupUtil implements IUtil {
@Override public void onCancel() { @Override public void onCancel() {
saveData(IEntity.STATE_CANCEL, -1); saveData(IEntity.STATE_CANCEL, -1);
handleSpeed(0); handleSpeed(0);
mListener.onSubCancel(entity); mListener.onSubCancel(subEntity);
} }
@Override public void onComplete() { @Override public void onComplete() {
saveData(IEntity.STATE_COMPLETE, entity.getFileSize()); saveData(IEntity.STATE_COMPLETE, subEntity.getFileSize());
handleSpeed(0); handleSpeed(0);
mListener.onSubComplete(entity); mListener.onSubComplete(subEntity);
synchronized (AbsGroupUtil.class) { synchronized (AbsGroupUtil.class) {
mCompleteNum++; mCompleteNum++;
//如果子任务完成的数量和总任务数一致,表示任务组任务已经完成 //如果子任务完成的数量和总任务数一致,表示任务组任务已经完成
@ -505,7 +504,7 @@ public abstract class AbsGroupUtil implements IUtil {
} }
@Override public void onFail(boolean needRetry) { @Override public void onFail(boolean needRetry) {
entity.setFailNum(entity.getFailNum() + 1); subEntity.setFailNum(subEntity.getFailNum() + 1);
saveData(IEntity.STATE_FAIL, lastLen); saveData(IEntity.STATE_FAIL, lastLen);
handleSpeed(0); handleSpeed(0);
reTry(needRetry); reTry(needRetry);
@ -516,12 +515,12 @@ public abstract class AbsGroupUtil implements IUtil {
*/ */
private void reTry(boolean needRetry) { private void reTry(boolean needRetry) {
synchronized (AriaManager.LOCK) { synchronized (AriaManager.LOCK) {
if (entity.getFailNum() < 5 && isRunning && needRetry && NetUtils.isConnected( if (subEntity.getFailNum() < 5 && isRunning && needRetry && NetUtils.isConnected(
AriaManager.APP)) { AriaManager.APP)) {
reStartTask(); reStartTask();
} else { } else {
mFailNum++; mFailNum++;
mListener.onSubFail(entity); mListener.onSubFail(subEntity);
//如果失败的任务数大于实际的下载任务数,任务组停止下载 //如果失败的任务数大于实际的下载任务数,任务组停止下载
if (mFailNum >= mActualTaskNum) { if (mFailNum >= mActualTaskNum) {
closeTimer(false); closeTimer(false);
@ -535,28 +534,32 @@ public abstract class AbsGroupUtil implements IUtil {
Timer timer = new Timer(); Timer timer = new Timer();
timer.schedule(new TimerTask() { timer.schedule(new TimerTask() {
@Override public void run() { @Override public void run() {
Downloader dt = mDownloaderMap.get(entity.getUrl()); Downloader dt = mDownloaderMap.get(subEntity.getUrl());
dt.start(); dt.start();
} }
}, 3000); }, 3000);
} }
private void handleSpeed(long speed) { private void handleSpeed(long speed) {
entity.setSpeed(speed); subEntity.setSpeed(speed);
entity.setConvertSpeed(speed <= 0 ? "" : CommonUtil.formatFileSize(speed) + "/s"); subEntity.setConvertSpeed(speed <= 0 ? "" : CommonUtil.formatFileSize(speed) + "/s");
entity.setPercent((int) (entity.getCurrentProgress() * 100 / entity.getFileSize())); subEntity.setPercent((int) (subEntity.getCurrentProgress() * 100 / subEntity.getFileSize()));
} }
private void saveData(int state, long location) { private void saveData(int state, long location) {
entity.setState(state); subTaskEntity.state = state;
entity.setComplete(state == IEntity.STATE_COMPLETE); subEntity.setState(state);
if (entity.isComplete()) { subEntity.setComplete(state == IEntity.STATE_COMPLETE);
entity.setCompleteTime(System.currentTimeMillis()); if (state == IEntity.STATE_CANCEL) {
entity.setCurrentProgress(entity.getFileSize()); subTaskEntity.deleteData();
return;
} else if (subEntity.isComplete()) {
subEntity.setCompleteTime(System.currentTimeMillis());
subEntity.setCurrentProgress(subEntity.getFileSize());
} else if (location > 0) { } else if (location > 0) {
entity.setCurrentProgress(location); subEntity.setCurrentProgress(location);
} }
entity.update(); subTaskEntity.update();
} }
@Override public void supportBreakpoint(boolean support) { @Override public void supportBreakpoint(boolean support) {

@ -118,7 +118,6 @@ class HttpFileInfoThread implements Runnable {
if (onFileInfoListener != null) { if (onFileInfoListener != null) {
onFileInfoListener.onComplete(mEntity.getUrl(), code); onFileInfoListener.onComplete(mEntity.getUrl(), code);
} }
mEntity.update();
mTaskEntity.update(); mTaskEntity.update();
} }
} }

@ -45,7 +45,7 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
* 删除记录 * 删除记录
*/ */
public void removeRecord() { public void removeRecord() {
mEntity.deleteData(); mTaskEntity.deleteData();
} }
/** /**

@ -143,4 +143,18 @@ public abstract class AbsTaskEntity<ENTITY extends AbsEntity> extends DbEntity {
public int getState() { public int getState() {
return getEntity().getState(); return getEntity().getState();
} }
@Override public void deleteData() {
if (getEntity() != null) {
getEntity().deleteData();
}
super.deleteData();
}
@Override public void update() {
if (getEntity() != null) {
getEntity().update();
}
super.update();
}
} }

@ -40,7 +40,6 @@ public abstract class AbsUploadTarget<TARGET extends AbsUploadTarget, ENTITY ext
CheckUtil.checkDownloadUrl(uploadUrl); CheckUtil.checkDownloadUrl(uploadUrl);
if (mEntity.getUrl().equals(uploadUrl)) return (TARGET) this; if (mEntity.getUrl().equals(uploadUrl)) return (TARGET) this;
mEntity.setUrl(uploadUrl); mEntity.setUrl(uploadUrl);
//mEntity.setUrl(CommonUtil.convertUrl(uploadUrl));
mEntity.update(); mEntity.update();
return (TARGET) this; return (TARGET) this;
} }

@ -131,22 +131,18 @@ class BaseUListener<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity<
} }
private void saveData(int state, long location) { private void saveData(int state, long location) {
mEntity.setComplete(state == IEntity.STATE_COMPLETE);
mTaskEntity.state = state; mTaskEntity.state = state;
if (state == IEntity.STATE_CANCEL) {
mEntity.deleteData();
} else if (state == IEntity.STATE_COMPLETE) {
mEntity.setState(state); mEntity.setState(state);
mEntity.setComplete(state == IEntity.STATE_COMPLETE);
if (state == IEntity.STATE_CANCEL) {
mTaskEntity.deleteData();
return;
} else if (mEntity.isComplete()) {
mEntity.setCompleteTime(System.currentTimeMillis()); mEntity.setCompleteTime(System.currentTimeMillis());
mEntity.setCurrentProgress(mEntity.getFileSize()); mEntity.setCurrentProgress(mEntity.getFileSize());
mEntity.update(); } else if (location > 0) {
} else {
mEntity.setState(state);
if (location != -1) {
mEntity.setCurrentProgress(location); mEntity.setCurrentProgress(location);
} }
mEntity.update();
}
mTaskEntity.update(); mTaskEntity.update();
} }
} }

@ -17,6 +17,7 @@ package com.arialyy.aria.orm;
import android.text.TextUtils; import android.text.TextUtils;
import com.arialyy.aria.core.ErrorEntity; import com.arialyy.aria.core.ErrorEntity;
import com.arialyy.aria.core.UrlMapping;
import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
@ -33,7 +34,8 @@ import java.util.Map;
class DBConfig { class DBConfig {
static Map<String, Class> mapping = new HashMap<>(); static Map<String, Class> mapping = new HashMap<>();
static String DB_NAME; static String DB_NAME;
static int VERSION = 16; //static int VERSION = 16;
static int VERSION = 17;
static { static {
if (TextUtils.isEmpty(DB_NAME)) { if (TextUtils.isEmpty(DB_NAME)) {
@ -52,5 +54,6 @@ class DBConfig {
mapping.put("UploadEntity", UploadEntity.class); mapping.put("UploadEntity", UploadEntity.class);
mapping.put("UploadTaskEntity", UploadTaskEntity.class); mapping.put("UploadTaskEntity", UploadTaskEntity.class);
mapping.put("ErrorEntity", ErrorEntity.class); mapping.put("ErrorEntity", ErrorEntity.class);
//mapping.put("UrlMapping", UrlMapping.class);
} }
} }

@ -0,0 +1,30 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.orm;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by lyy on 2015/11/2.
* 非空字段
*/
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface NoNull {
boolean value() default true;
}

@ -26,7 +26,6 @@ import android.util.Log;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
@ -423,6 +422,8 @@ final class SqlHelper extends SQLiteOpenHelper {
db = checkDb(db); db = checkDb(db);
List<Field> fields = CommonUtil.getAllFields(clazz); List<Field> fields = CommonUtil.getAllFields(clazz);
if (fields != null && fields.size() > 0) { if (fields != null && fields.size() > 0) {
//外键Map,在Sqlite3中foreign修饰的字段必须放在最后
final List<Field> foreignArray = new ArrayList<>();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("create table ") sb.append("create table ")
.append(TextUtils.isEmpty(tableName) ? CommonUtil.getClassName(clazz) : tableName) .append(TextUtils.isEmpty(tableName) ? CommonUtil.getClassName(clazz) : tableName)
@ -458,17 +459,26 @@ final class SqlHelper extends SQLiteOpenHelper {
sb.append(" PRIMARY KEY"); sb.append(" PRIMARY KEY");
} }
if (SqlUtil.isForeign(field)) { if (SqlUtil.isForeign(field)) {
foreignArray.add(field);
}
if (SqlUtil.isNoNull(field)) {
sb.append(" NOT NULL");
}
sb.append(",");
}
for (Field field : foreignArray) {
Foreign foreign = field.getAnnotation(Foreign.class); Foreign foreign = field.getAnnotation(Foreign.class);
sb.append(",FOREIGN KEY (") sb.append("FOREIGN KEY (")
.append(field.getName()) .append(field.getName())
.append(") REFERENCES ") .append(") REFERENCES ")
.append(foreign.table()) .append(CommonUtil.getClassName(foreign.table()))
.append("(") .append("(")
.append(foreign.column()) .append(foreign.column())
.append(")"); .append("),");
}
sb.append(",");
} }
String str = sb.toString(); String str = sb.toString();
str = str.substring(0, str.length() - 1) + ");"; str = str.substring(0, str.length() - 1) + ");";
print(CREATE_TABLE, str); print(CREATE_TABLE, str);

@ -184,13 +184,13 @@ final class SqlUtil {
} }
/** /**
* 判断是否是主外键约束 * 判断是否是非空约束
* *
* @return {@code true}主外键约束 * @return {@code true}为非空约束
*/ */
static boolean isPrimaryAndForeign(Field field) { static boolean isNoNull(Field field) {
PrimaryAndForeign fk = field.getAnnotation(PrimaryAndForeign.class); NoNull nn = field.getAnnotation(NoNull.class);
return fk != null; return nn != null;
} }
private static Object checkData(String type, String data) { private static Object checkData(String type, String data) {

@ -247,9 +247,6 @@ public class CommonUtil {
dir.delete(); dir.delete();
} }
} }
if (entity != null) {
entity.deleteData();
}
tEntity.deleteData(); tEntity.deleteData();
} }
@ -271,7 +268,7 @@ public class CommonUtil {
if (config.exists()) { if (config.exists()) {
config.delete(); config.delete();
} }
uEntity.deleteData(); //uEntity.deleteData();
tEntity.deleteData(); tEntity.deleteData();
} }

@ -29,6 +29,7 @@ public class UploadTaskEntity extends AbsNormalTaskEntity<UploadEntity> {
public String contentType = "multipart/form-data"; //上传的文件类型 public String contentType = "multipart/form-data"; //上传的文件类型
public String userAgent = "User-Agent"; public String userAgent = "User-Agent";
@OneToOne(table = UploadEntity.class, key = "filePath") public UploadEntity entity; @OneToOne(table = UploadEntity.class, key = "filePath") public UploadEntity entity;
public String filePath = "";
/** /**
* 文件上传表单 * 文件上传表单
@ -41,4 +42,12 @@ public class UploadTaskEntity extends AbsNormalTaskEntity<UploadEntity> {
@Override public UploadEntity getEntity() { @Override public UploadEntity getEntity() {
return entity; return entity;
} }
public void save(UploadEntity entity) {
filePath = entity.getFilePath();
key = entity.getKey();
this.entity = entity;
entity.save();
save();
}
} }

Loading…
Cancel
Save