com.activeandroid.Model Java Examples

The following examples show how to use com.activeandroid.Model. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContentProvider.java    From clear-todolist with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
	final Class<? extends Model> type = getModelType(uri);
	final Cursor cursor = Cache.openDatabase().query(
			Cache.getTableName(type),
			projection,
			selection,
			selectionArgs,
			null,
			null,
			sortOrder);

	cursor.setNotificationUri(getContext().getContentResolver(), uri);

	return cursor;
}
 
Example #2
Source File: ModelTest.java    From clear-todolist with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Two rows in a table should have different hashcodes.
 */
public void testHashCodeDifferentRows() {
	Set<Model> set = new HashSet<Model>();
	Model m1 = new MockModel();
	Model m2 = new MockModel();
	Model m3;

	m1.save();
	m2.save();
	m3 = Model.load(MockModel.class, m1.getId());

	assertEquals(m1.hashCode(), m3.hashCode());
	assertFalse(m1.hashCode() == m2.hashCode());
	set.add(m1);
	set.add(m2);
	set.add(m3);
	assertEquals(2, set.size());
}
 
Example #3
Source File: ModelTest.java    From clear-todolist with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Tests hashcode for new instances.
 */	
public void testHashCode() {
	Set<Model> set = new HashSet<Model>();
	Model m1 = new MockModel();
	Model m2 = new MockModel();
	Model m3 = new AnotherMockModel();

	assertFalse(m1.hashCode() == m2.hashCode()); // hashes for unsaved models must not match
	set.add(m1);
	set.add(m2);
	assertEquals(2, set.size()); //try in a set

	assertFalse(m1.hashCode() == m3.hashCode());
	set.add(m3);
	assertEquals(3, set.size());
}
 
Example #4
Source File: ModelTest.java    From clear-todolist with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Two different rows in a table should not be equal (different ids).
 */	
public void testEqualsDifferentRows() {
	MockModel model1 = new MockModel();
	MockModel model2 = new MockModel();
	MockModel model3;

	model1.save();
	model2.save();
	model3 = Model.load(MockModel.class, model1.getId());

       // Not equal to each other.
	assertFalse(model1.equals(model2));
	assertFalse(model2.equals(model1));

       // Equal to each other when loaded.
	assertTrue(model1.equals(model3));
	assertTrue(model1.equals(model3));

       // Loaded model is not equal to a different model.
	assertFalse(model3.equals(model2));
	assertFalse(model2.equals(model3));
}
 
Example #5
Source File: ContentProvider.java    From mobile-android-survey-app with MIT License 6 votes vote down vote up
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
	final Class<? extends Model> type = getModelType(uri);
	final Cursor cursor = Cache.openDatabase().query(
			Cache.getTableName(type),
			projection,
			selection,
			selectionArgs,
			null,
			null,
			sortOrder);

	cursor.setNotificationUri(getContext().getContentResolver(), uri);

	return cursor;
}
 
Example #6
Source File: FeedDBAPIImpl.java    From umeng_community_android with MIT License 6 votes vote down vote up
private void removeFeedRelativeItems() throws ClassNotFoundException {
    // 获取所有缓存的Feed
    List<FeedItem> cacheFeedItems = new Select().from(FeedItem.class)
            .where("category='NORMAL'").execute();

    Class<? extends Model> feedLikeClass = refectModelClz("com.umeng.comm.core.beans.relation.FeedLike");
    Class<? extends Model> feedCommentClass = refectModelClz("com.umeng.comm.core.beans.relation.FeedComment");
    Class<? extends Model> feedTopicClass = refectModelClz("com.umeng.comm.core.beans.relation.FeedTopic");
    Class<? extends Model> feedCreatorClass = refectModelClz("com.umeng.comm.core.beans.relation.FeedCreator");
    Class<? extends Model> feedFriendClass = refectModelClz("com.umeng.comm.core.beans.relation.FeedFriends");

    for (FeedItem feedItem : cacheFeedItems) {
        // 移除like相关
        removeRelativeLike(feedItem.id, feedLikeClass);
        // 移除Feed相关的Comment
        removeRelativeComment(feedItem.id, feedCommentClass);
        new Delete().from(feedTopicClass).where("feed_id=?", feedItem.id)
                .execute();
        new Delete().from(feedCreatorClass).where("feed_id=?", feedItem.id)
                .execute();
        new Delete().from(feedFriendClass).where("feed_id=?", feedItem.id)
                .execute();
    }
}
 
Example #7
Source File: UploaderQueue.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static UploaderQueue newEntryForWatch(String action, Model obj) {
    UserError.Log.d(TAG, "new entry called for watch");
    final UploaderQueue result = new UploaderQueue();
    result.bitfield_wanted = DEFAULT_UPLOAD_CIRCUITS
            | (Pref.getBooleanDefaultFalse("wear_sync") ? WATCH_WEARAPI : 0);
    if (result.bitfield_wanted == 0) return null; // no queue required
    result.timestamp = JoH.tsl();
    result.reference_id = obj.getId();
    // TODO this probably could be neater
    if (result.reference_uuid == null)
        result.reference_uuid = obj instanceof BgReading ? ((BgReading) obj).uuid : null;
    if (result.reference_uuid == null)
        result.reference_uuid = obj instanceof Treatments ? ((Treatments) obj).uuid : null;
    if (result.reference_uuid == null)
        result.reference_uuid = obj instanceof Calibration ? ((Calibration) obj).uuid : null;
    if (result.reference_uuid == null)
        result.reference_uuid = obj instanceof BloodTest ? ((BloodTest) obj).uuid : null;

    if (result.reference_uuid == null) {
        Log.d(TAG, "reference_uuid was null so refusing to create new entry");
        return null;
    }

    if (result.reference_id < 0) {
        UserError.Log.wtf(TAG, "Watch ERROR ref id was: " + result.reference_id + " for uuid: " + result.reference_uuid + " refusing to create");
        return null;
    }

    result.action = action;

    result.bitfield_complete = 0;
    result.type = obj.getClass().getSimpleName();
    result.saveit();
    if (d) UserError.Log.d(TAG, result.toS());
    last_new_entry = JoH.tsl();
    return result;
}
 
Example #8
Source File: ContentProvider.java    From clear-todolist with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Uri insert(Uri uri, ContentValues values) {
	final Class<? extends Model> type = getModelType(uri);
	final Long id = Cache.openDatabase().insert(Cache.getTableName(type), null, values);

	if (id != null && id > 0) {
		Uri retUri = createUri(type, id);
		notifyChange(retUri);

		return retUri;
	}

	return null;
}
 
Example #9
Source File: ContentProvider.java    From clear-todolist with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String getType(Uri uri) {
	final int match = URI_MATCHER.match(uri);

	String cachedMimeType = sMimeTypeCache.get(match);
	if (cachedMimeType != null) {
		return cachedMimeType;
	}

	final Class<? extends Model> type = getModelType(uri);
	final boolean single = ((match % 2) == 0);

	StringBuilder mimeType = new StringBuilder();
	mimeType.append("vnd");
	mimeType.append(".");
	mimeType.append(sAuthority);
	mimeType.append(".");
	mimeType.append(single ? "item" : "dir");
	mimeType.append("/");
	mimeType.append("vnd");
	mimeType.append(".");
	mimeType.append(sAuthority);
	mimeType.append(".");
	mimeType.append(Cache.getTableName(type));

	sMimeTypeCache.append(match, mimeType.toString());

	return mimeType.toString();
}
 
Example #10
Source File: UploaderQueue.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void newTransmitterDataEntry(String action, Model obj) {
	if(!Pref.getBooleanDefaultFalse("mongo_load_transmitter_data")) {
		return;
	}
	newEntry(action, obj);
	// For libre us sensors, we have a reading, it might not create a BG entry, but we still need
	// to upload it.
	startSyncService(3000); // sync in 3 seconds
}
 
Example #11
Source File: FeedDBAPIImpl.java    From umeng_community_android with MIT License 5 votes vote down vote up
private void removeRelativeComment(String feedId,
        Class<? extends Model> feedCommentClass)
        throws ClassNotFoundException {
    List<Comment> comments = new Select().from(Comment.class)
            .innerJoin(feedCommentClass)
            .on("comment._id=feed_comment.comment_id")
            .where("feed_comment.feed_id=?", feedId)
            .execute();
    for (Comment comment : comments) {
        new Delete().from(feedCommentClass).where("comment_id=?", comment.id).execute();
        new Delete().from(Comment.class).where("_id=?", comment.id).execute();
    }
}
 
Example #12
Source File: From.java    From clear-todolist with GNU General Public License v3.0 5 votes vote down vote up
public <T extends Model> List<T> execute() {
	if (mQueryBase instanceof Select) {
		return SQLiteUtils.rawQuery(mType, toSql(), getArguments());
		
	} else {
		SQLiteUtils.execSql(toSql(), getArguments());
		Cache.getContext().getContentResolver().notifyChange(ContentProvider.createUri(mType, null), null);
		return null;
		
	}
}
 
Example #13
Source File: ReflectionUtils.java    From mobile-android-survey-app with MIT License 5 votes vote down vote up
public static Set<Field> getDeclaredColumnFields(Class<?> type) {
	Set<Field> declaredColumnFields = Collections.emptySet();
	
	if (ReflectionUtils.isSubclassOf(type, Model.class) || Model.class.equals(type)) {
		declaredColumnFields = new LinkedHashSet<Field>();
		
		Field[] fields = type.getDeclaredFields();
		Arrays.sort(fields, new Comparator<Field>() {
			@Override
			public int compare(Field field1, Field field2) {
				return field2.getName().compareTo(field1.getName());
			}
		});
		for (Field field : fields) {
			if (field.isAnnotationPresent(Column.class)) {
				declaredColumnFields.add(field);
			}
		}

		Class<?> parentType = type.getSuperclass();
		if (parentType != null) {
			declaredColumnFields.addAll(getDeclaredColumnFields(parentType));
		}
	}
	
	return declaredColumnFields;		
}
 
Example #14
Source File: ContentProvider.java    From clear-todolist with GNU General Public License v3.0 5 votes vote down vote up
public static Uri createUri(Class<? extends Model> type, Long id) {
	final StringBuilder uri = new StringBuilder();
	uri.append("content://");
	uri.append(sAuthority);
	uri.append("/");
	uri.append(Cache.getTableName(type).toLowerCase());

	if (id != null) {
		uri.append("/");
		uri.append(id.toString());
	}

	return Uri.parse(uri.toString());
}
 
Example #15
Source File: UploaderQueue.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static UploaderQueue newEntryForWatch(String action, Model obj) {
    UserError.Log.d(TAG, "new entry called for watch");
    final UploaderQueue result = new UploaderQueue();
    result.bitfield_wanted = DEFAULT_UPLOAD_CIRCUITS
            | (Pref.getBooleanDefaultFalse("wear_sync") ? WATCH_WEARAPI : 0);
    if (result.bitfield_wanted == 0) return null; // no queue required
    result.timestamp = JoH.tsl();
    result.reference_id = obj.getId();
    // TODO this probably could be neater
    if (result.reference_uuid == null)
        result.reference_uuid = obj instanceof BgReading ? ((BgReading) obj).uuid : null;
    if (result.reference_uuid == null)
        result.reference_uuid = obj instanceof Treatments ? ((Treatments) obj).uuid : null;
    if (result.reference_uuid == null)
        result.reference_uuid = obj instanceof Calibration ? ((Calibration) obj).uuid : null;
    if (result.reference_uuid == null)
        result.reference_uuid = obj instanceof BloodTest ? ((BloodTest) obj).uuid : null;

    if (result.reference_uuid == null) {
        Log.d(TAG, "reference_uuid was null so refusing to create new entry");
        return null;
    }

    if (result.reference_id < 0) {
        UserError.Log.wtf(TAG, "Watch ERROR ref id was: " + result.reference_id + " for uuid: " + result.reference_uuid + " refusing to create");
        return null;
    }

    result.action = action;

    result.bitfield_complete = 0;
    result.type = obj.getClass().getSimpleName();
    result.saveit();
    if (d) UserError.Log.d(TAG, result.toS());
    last_new_entry = JoH.tsl();
    return result;
}
 
Example #16
Source File: SQLiteUtils.java    From mobile-android-survey-app with MIT License 5 votes vote down vote up
public static <T extends Model> T rawQuerySingle(Class<? extends Model> type, String sql, String[] selectionArgs) {
	List<T> entities = rawQuery(type, sql, selectionArgs);

	if (entities.size() > 0) {
		return entities.get(0);
	}

	return null;
}
 
Example #17
Source File: UploaderQueue.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static void newTransmitterDataEntry(String action, Model obj) {
	if(!Pref.getBooleanDefaultFalse("mongo_load_transmitter_data")) {
		return;
	}
	newEntry(action, obj);
	// For libre us sensors, we have a reading, it might not create a BG entry, but we still need
	// to upload it.
	startSyncService(3000); // sync in 3 seconds
}
 
Example #18
Source File: FeedDBAPIImpl.java    From umeng_community_android with MIT License 5 votes vote down vote up
private void removeRelativeLike(String feedId, Class<? extends Model> feedLikeClass)
        throws ClassNotFoundException {
    List<Like> likes = new Select().from(Like.class).innerJoin(feedLikeClass)
            .on("like._id=feed_like.like_id").where("feed_like.feed_id=?", feedId)
            .execute();
    // 删除feed_like表中的记录

    new Delete().from(feedLikeClass).where("feed_id=?", feedId);
    Class<? extends Model> likeCreatorClz = feedLikeClass = refectModelClz("com.umeng.comm.core.beans.relation.LikeCreator");
    for (Like like : likes) {
        new Delete().from(likeCreatorClz).where("like_id=?", like.id).execute();
        new Delete().from(Like.class).where("_id=?", like.id).execute();
    }
}
 
Example #19
Source File: ContentProvider.java    From clear-todolist with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
	final Class<? extends Model> type = getModelType(uri);
	final int count = Cache.openDatabase().update(Cache.getTableName(type), values, selection, selectionArgs);

	notifyChange(uri);

	return count;
}
 
Example #20
Source File: ContentProvider.java    From clear-todolist with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
	final Class<? extends Model> type = getModelType(uri);
	final int count = Cache.openDatabase().delete(Cache.getTableName(type), selection, selectionArgs);

	notifyChange(uri);

	return count;
}
 
Example #21
Source File: From.java    From clear-todolist with GNU General Public License v3.0 5 votes vote down vote up
public <T extends Model> T executeSingle() {
	if (mQueryBase instanceof Select) {
		limit(1);
		return (T) SQLiteUtils.rawQuerySingle(mType, toSql(), getArguments());
		
	} else {
		limit(1);
		SQLiteUtils.rawQuerySingle(mType, toSql(), getArguments()).delete();
		return null;
		
	}
}
 
Example #22
Source File: ContentProvider.java    From clear-todolist with GNU General Public License v3.0 5 votes vote down vote up
private Class<? extends Model> getModelType(Uri uri) {
	final int code = URI_MATCHER.match(uri);
	if (code != UriMatcher.NO_MATCH) {
		return TYPE_CODES.get(code);
	}

	return null;
}
 
Example #23
Source File: From.java    From mobile-android-survey-app with MIT License 5 votes vote down vote up
public <T extends Model> List<T> execute() {
	if (mQueryBase instanceof Select) {
		return SQLiteUtils.rawQuery(mType, toSql(), getArguments());
		
	} else {
		SQLiteUtils.execSql(toSql(), getArguments());
		Cache.getContext().getContentResolver().notifyChange(ContentProvider.createUri(mType, null), null);
		return null;
		
	}
}
 
Example #24
Source File: ReflectionUtils.java    From clear-todolist with GNU General Public License v3.0 5 votes vote down vote up
public static Set<Field> getDeclaredColumnFields(Class<?> type) {
	Set<Field> declaredColumnFields = Collections.emptySet();
	
	if (ReflectionUtils.isSubclassOf(type, Model.class) || Model.class.equals(type)) {
		declaredColumnFields = new LinkedHashSet<Field>();
		
		Field[] fields = type.getDeclaredFields();
		Arrays.sort(fields, new Comparator<Field>() {
			@Override
			public int compare(Field field1, Field field2) {
				return field2.getName().compareTo(field1.getName());
			}
		});
		for (Field field : fields) {
			if (field.isAnnotationPresent(Column.class)) {
				declaredColumnFields.add(field);
			}
		}

		Class<?> parentType = type.getSuperclass();
		if (parentType != null) {
			declaredColumnFields.addAll(getDeclaredColumnFields(parentType));
		}
	}
	
	return declaredColumnFields;		
}
 
Example #25
Source File: SQLiteUtils.java    From clear-todolist with GNU General Public License v3.0 5 votes vote down vote up
public static <T extends Model> List<T> rawQuery(Class<? extends Model> type, String sql, String[] selectionArgs) {
	Cursor cursor = Cache.openDatabase().rawQuery(sql, selectionArgs);
	List<T> entities = processCursor(type, cursor);
	cursor.close();

	return entities;
}
 
Example #26
Source File: SQLiteUtils.java    From clear-todolist with GNU General Public License v3.0 5 votes vote down vote up
public static <T extends Model> T rawQuerySingle(Class<? extends Model> type, String sql, String[] selectionArgs) {
	List<T> entities = rawQuery(type, sql, selectionArgs);

	if (entities.size() > 0) {
		return entities.get(0);
	}

	return null;
}
 
Example #27
Source File: ConfigurationTest.java    From clear-todolist with GNU General Public License v3.0 5 votes vote down vote up
public void testCreateConfigurationWithMockModel() {
    Configuration conf = new Configuration.Builder(getContext())
            .addModelClass(ConfigurationTestModel.class)
            .create();
    List<Class<? extends Model>> modelClasses = conf.getModelClasses();
    assertEquals(1, modelClasses.size());
    assertTrue(conf.isValid());
}
 
Example #28
Source File: ModelTest.java    From clear-todolist with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Equals should not be true for different model classes.
 */	
public void testEqualsDifferentModel() {
	Model model1 = new MockModel();
	Model model2 = new AnotherMockModel();

	assertFalse(model1.equals(model2));
}
 
Example #29
Source File: InsertTest.java    From AirData with MIT License 5 votes vote down vote up
public void testActiveAndroidSelect(){
        ActiveAndroid.initialize(getContext());
//        List<Model> list = new com.activeandroid.query.Select().from(Student2.class).execute();
        List<Model> list = new com.activeandroid.query.Select().from(Student2.class).limit("0, 100000").execute();
//        List<Student2> list = new com.activeandroid.query.Select().from(Student2.class).where("Score > ? and Score < 50000", "1000").limit("1000, 100").execute();
//        assertEquals(list.size(), new com.activeandroid.query.Select().from(Student2.class).count());
    }
 
Example #30
Source File: From.java    From mobile-android-survey-app with MIT License 5 votes vote down vote up
public From(Class<? extends Model> table, Sqlable queryBase) {
	mType = table;
	mJoins = new ArrayList<Join>();
	mQueryBase = queryBase;

	mJoins = new ArrayList<Join>();
	mArguments = new ArrayList<Object>();
}