orm 添加外键注解
This commit is contained in:
@@ -77,7 +77,7 @@ public class TaskManager {
|
||||
/**
|
||||
* 通过key获取任务
|
||||
*
|
||||
* @return 入梅找不到任务,返回null,否则返回key对应的任务
|
||||
* @return 如果找不到任务,返回null,否则返回key对应的任务
|
||||
*/
|
||||
public AbsTask getTask(String key) {
|
||||
return map.get(CommonUtil.keyToHashKey(key));
|
||||
|
@@ -21,6 +21,7 @@ import android.os.Parcelable;
|
||||
import android.text.TextUtils;
|
||||
import com.arialyy.aria.core.inf.AbsNormalEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
import com.arialyy.aria.orm.Foreign;
|
||||
import com.arialyy.aria.orm.Primary;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
|
||||
@@ -34,7 +35,12 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable {
|
||||
/**
|
||||
* 所属任务组
|
||||
*/
|
||||
private String groupName = "";
|
||||
@Foreign(table = DownloadGroupEntity.class, column = "groupName") private String groupName = "";
|
||||
|
||||
/**
|
||||
* 下载任务实体的key
|
||||
*/
|
||||
@Foreign(table = DownloadTaskEntity.class, column = "key") private String taskKey = "";
|
||||
|
||||
/**
|
||||
* 通过{@link AbsTaskEntity#md5Key}从服务器的返回信息中获取的文件md5信息,如果服务器没有返回,则不会设置该信息
|
||||
|
@@ -168,11 +168,26 @@ public abstract class AbsGroupUtil implements IUtil {
|
||||
*
|
||||
* @param url 子任务下载地址
|
||||
*/
|
||||
public void cancelSunTask(String url) {
|
||||
Downloader d = getDownloader(url, false);
|
||||
if (d != null) {
|
||||
d.cancel();
|
||||
}
|
||||
public void cancelSubTask(String url) {
|
||||
//List<String> urls = mTaskEntity.entity.getUrls();
|
||||
//if (urls != null && !urls.isEmpty() && urls.contains(url)) {
|
||||
// urls.remove(url);
|
||||
// DownloadTaskEntity det =
|
||||
// DbEntity.findFirst(DownloadTaskEntity.class, "url=? and isGroupTask='true'", url);
|
||||
// if (det != null) {
|
||||
// mTotalLen -= det.getEntity().getFileSize();
|
||||
// mGroupSize--;
|
||||
// if (mGroupSize == 0) {
|
||||
// closeTimer(false);
|
||||
// mListener.onCancel();
|
||||
// }
|
||||
// }
|
||||
// mTaskEntity.getEntity().update();
|
||||
//}
|
||||
//Downloader d = getDownloader(url, false);
|
||||
//if (d != null) {
|
||||
// d.cancel();
|
||||
//}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -17,6 +17,8 @@ package com.arialyy.aria.core.inf;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
|
||||
import com.arialyy.aria.orm.Foreign;
|
||||
import com.arialyy.aria.orm.NormalList;
|
||||
import com.arialyy.aria.orm.Primary;
|
||||
import java.util.ArrayList;
|
||||
@@ -29,7 +31,8 @@ public abstract class AbsGroupEntity extends AbsEntity implements Parcelable {
|
||||
/**
|
||||
* 组名,组名为任务地址相加的urlMd5
|
||||
*/
|
||||
@Primary protected String groupName = "";
|
||||
@Primary @Foreign(table = DownloadGroupTaskEntity.class, column = "key") protected String
|
||||
groupName = "";
|
||||
|
||||
/**
|
||||
* 任务组别名
|
||||
|
@@ -59,7 +59,7 @@ public abstract class AbsGroupTask<TASK_ENTITY extends AbsGroupTaskEntity>
|
||||
*/
|
||||
public void cancelSubTask(String url) {
|
||||
if (mUtil != null) {
|
||||
mUtil.cancelSunTask(url);
|
||||
mUtil.cancelSubTask(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -18,17 +18,17 @@ package com.arialyy.aria.core.upload;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import com.arialyy.aria.core.inf.AbsNormalEntity;
|
||||
import com.arialyy.aria.orm.Foreign;
|
||||
import com.arialyy.aria.orm.Ignore;
|
||||
import com.arialyy.aria.orm.Primary;
|
||||
import com.arialyy.aria.orm.PrimaryAndForeign;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2017/2/9.
|
||||
* 上传文件实体
|
||||
*/
|
||||
public class UploadEntity extends AbsNormalEntity implements Parcelable {
|
||||
@Primary
|
||||
private String filePath; //文件路径
|
||||
|
||||
@Primary @Foreign(table = UploadTaskEntity.class, column = "key") private String filePath; //文件路径
|
||||
|
||||
public String getFilePath() {
|
||||
return filePath;
|
||||
|
36
Aria/src/main/java/com/arialyy/aria/orm/Foreign.java
Normal file
36
Aria/src/main/java/com/arialyy/aria/orm/Foreign.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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 AriaL on 2017/7/4.
|
||||
* 外键约束
|
||||
*/
|
||||
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Foreign {
|
||||
/**
|
||||
* 关联的表
|
||||
*/
|
||||
Class<? extends DbEntity> table();
|
||||
/**
|
||||
* 关联的列
|
||||
*/
|
||||
String column();
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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 AriaL on 2017/7/4.
|
||||
* 主键和外键约束
|
||||
*/
|
||||
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface PrimaryAndForeign {
|
||||
/**
|
||||
* 关联的表
|
||||
*/
|
||||
Class<? extends DbEntity> table();
|
||||
/**
|
||||
* 关联的列
|
||||
*/
|
||||
String column();
|
||||
}
|
@@ -251,6 +251,18 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
String... expression) {
|
||||
db = checkDb(db);
|
||||
CheckUtil.checkSqlExpression(expression);
|
||||
|
||||
//List<Field> fields = CommonUtil.getAllFields(clazz);
|
||||
//for (Field field : fields) {
|
||||
// if (SqlUtil.isOneToOne(field)) {
|
||||
// OneToOne oto = field.getAnnotation(OneToOne.class);
|
||||
// delData(db, oto.table(), oto.key() + "=?", );
|
||||
// } else if (SqlUtil.isOneToMany(field)) {
|
||||
// OneToMany otm = field.getAnnotation(OneToMany.class);
|
||||
// delData(db, otm.table(), otm.key() + "=?", otm.key());
|
||||
// }
|
||||
//}
|
||||
|
||||
String sql = "DELETE FROM " + CommonUtil.getClassName(clazz) + " WHERE " + expression[0] + " ";
|
||||
sql = sql.replace("?", "%s");
|
||||
Object[] params = new String[expression.length - 1];
|
||||
@@ -422,11 +434,8 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
}
|
||||
Class<?> type = field.getType();
|
||||
sb.append(field.getName());
|
||||
if (type == String.class
|
||||
|| type == Map.class
|
||||
|| type == List.class
|
||||
|| SqlUtil.isOneToOne(field)
|
||||
|| type.isEnum()) {
|
||||
if (type == String.class || type == Map.class || type == List.class || SqlUtil.isOneToOne(
|
||||
field) || type.isEnum()) {
|
||||
sb.append(" varchar");
|
||||
} else if (type == int.class || type == Integer.class) {
|
||||
sb.append(" interger");
|
||||
@@ -446,8 +455,17 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
continue;
|
||||
}
|
||||
if (SqlUtil.isPrimary(field)) {
|
||||
//sb.append(" PRIMARY KEY");
|
||||
sb.append(" NOT NULL");
|
||||
sb.append(" PRIMARY KEY");
|
||||
}
|
||||
if (SqlUtil.isForeign(field)) {
|
||||
Foreign foreign = field.getAnnotation(Foreign.class);
|
||||
sb.append(",FOREIGN KEY (")
|
||||
.append(field.getName())
|
||||
.append(") REFERENCES ")
|
||||
.append(foreign.table())
|
||||
.append("(")
|
||||
.append(foreign.column())
|
||||
.append(")");
|
||||
}
|
||||
sb.append(",");
|
||||
}
|
||||
@@ -587,7 +605,7 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
return findData(db, params[0], params[1] + "=?", primary);
|
||||
}
|
||||
|
||||
private static void closeCursor(Cursor cursor){
|
||||
private static void closeCursor(Cursor cursor) {
|
||||
if (cursor != null && !cursor.isClosed()) {
|
||||
try {
|
||||
cursor.close();
|
||||
|
@@ -15,9 +15,7 @@
|
||||
*/
|
||||
package com.arialyy.aria.orm;
|
||||
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
@@ -136,8 +134,6 @@ final class SqlUtil {
|
||||
return TextUtils.isEmpty(str) ? str : str.substring(0, str.length() - 1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return true 忽略该字段
|
||||
*/
|
||||
@@ -146,13 +142,9 @@ final class SqlUtil {
|
||||
Ignore ignore = field.getAnnotation(Ignore.class);
|
||||
int modifiers = field.getModifiers();
|
||||
String fieldName = field.getName();
|
||||
return (ignore != null && ignore.value())
|
||||
|| fieldName.equals("rowID")
|
||||
|| fieldName.equals("shadow$_klass_")
|
||||
|| fieldName.equals("shadow$_monitor_")
|
||||
|| field.isSynthetic()
|
||||
|| Modifier.isStatic(modifiers)
|
||||
|| Modifier.isFinal(modifiers);
|
||||
return (ignore != null && ignore.value()) || fieldName.equals("rowID") || fieldName.equals(
|
||||
"shadow$_klass_") || fieldName.equals("shadow$_monitor_") || field.isSynthetic() || Modifier
|
||||
.isStatic(modifiers) || Modifier.isFinal(modifiers);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -172,27 +164,48 @@ final class SqlUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否是主键
|
||||
* 判断是否是主键约束
|
||||
*
|
||||
* @return {@code true}主键约束
|
||||
*/
|
||||
static boolean isPrimary(Field field) {
|
||||
Primary pk = field.getAnnotation(Primary.class);
|
||||
return pk != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否是外键约束
|
||||
*
|
||||
* @return {@code true}外键约束
|
||||
*/
|
||||
static boolean isForeign(Field field) {
|
||||
Foreign fk = field.getAnnotation(Foreign.class);
|
||||
return fk != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否是主外键约束
|
||||
*
|
||||
* @return {@code true}主外键约束
|
||||
*/
|
||||
static boolean isPrimaryAndForeign(Field field) {
|
||||
PrimaryAndForeign fk = field.getAnnotation(PrimaryAndForeign.class);
|
||||
return fk != null;
|
||||
}
|
||||
|
||||
private static Object checkData(String type, String data) {
|
||||
if (type.equalsIgnoreCase("java.lang.String")){
|
||||
if (type.equalsIgnoreCase("java.lang.String")) {
|
||||
return data;
|
||||
}else if (type.equalsIgnoreCase("int") || type.equals("java.lang.Integer")){
|
||||
} else if (type.equalsIgnoreCase("int") || type.equals("java.lang.Integer")) {
|
||||
return Integer.parseInt(data);
|
||||
}else if (type.equalsIgnoreCase("double") || type.equals("java.lang.Double")){
|
||||
} else if (type.equalsIgnoreCase("double") || type.equals("java.lang.Double")) {
|
||||
return Double.parseDouble(data);
|
||||
}else if (type.equalsIgnoreCase("float") || type.equals("java.lang.Float")){
|
||||
} else if (type.equalsIgnoreCase("float") || type.equals("java.lang.Float")) {
|
||||
return Float.parseFloat(data);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查找class的主键字段
|
||||
*
|
||||
|
@@ -301,7 +301,8 @@ public class CommonUtil {
|
||||
if (config.exists()) {
|
||||
config.delete();
|
||||
}
|
||||
dEntity.deleteData();
|
||||
//dEntity.deleteData();
|
||||
//tEntity.deleteData();
|
||||
tEntity.deleteData();
|
||||
}
|
||||
|
||||
|
@@ -128,12 +128,12 @@ import java.lang.annotation.Target;
|
||||
String[] value() default { AriaConstance.NO_URL };
|
||||
}
|
||||
|
||||
///**
|
||||
// * 任务组子任务删除的注解
|
||||
// */
|
||||
//@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) @interface onSubTaskCancel {
|
||||
// String[] value() default { AriaConstance.NO_URL };
|
||||
//}
|
||||
/**
|
||||
* 任务组子任务删除的注解
|
||||
*/
|
||||
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) @interface onSubTaskCancel {
|
||||
String[] value() default { AriaConstance.NO_URL };
|
||||
}
|
||||
|
||||
/**
|
||||
* 任务组子任务失败的注解
|
||||
|
@@ -18,6 +18,7 @@ package com.arialyy.simple.download.group;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
@@ -109,6 +110,7 @@ import java.util.List;
|
||||
|
||||
//@DownloadGroup.onSubTaskCancel void onSubTaskCancel(DownloadGroupTask groupTask,
|
||||
// DownloadEntity subEntity) {
|
||||
// Log.d(TAG, "new Size: " + groupTask.getConvertFileSize());
|
||||
// mSub.setText("子任务:" + mChildName + ",状态:取消下载");
|
||||
//}
|
||||
|
||||
@@ -129,7 +131,7 @@ import java.util.List;
|
||||
return R.layout.dialog_sub_task_handler;
|
||||
}
|
||||
|
||||
@OnClick({ R.id.start, R.id.stop }) void onClick(View view) {
|
||||
@OnClick({ R.id.start, R.id.stop, R.id.cancel }) void onClick(View view) {
|
||||
switch (view.getId()) {
|
||||
case R.id.start:
|
||||
Aria.download(this).load(mUrls).getSubTaskManager().startSubTask(mChildEntity.getUrl());
|
||||
|
@@ -66,8 +66,8 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
|
||||
dialog.show(getSupportFragmentManager(), "download_num");
|
||||
break;
|
||||
case R.id.stop_all:
|
||||
Aria.download(this).stopAllTask();
|
||||
//Aria.download(this).removeAllTask(false);
|
||||
//Aria.download(this).stopAllTask();
|
||||
Aria.download(this).removeAllTask(false);
|
||||
break;
|
||||
case R.id.turn:
|
||||
startActivity(new Intent(this, MultiDownloadActivity.class));
|
||||
|
Reference in New Issue
Block a user