Java Code Examples for com.j256.ormlite.stmt.QueryBuilder#join()

The following examples show how to use com.j256.ormlite.stmt.QueryBuilder#join() . 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: ObservationLoadTask.java    From mage-android with Apache License 2.0 6 votes vote down vote up
private CloseableIterator<Observation> iterator() throws SQLException {
    Dao<Observation, Long> dao = DaoStore.getInstance(context).getObservationDao();
    QueryBuilder<Observation, Long> query = dao.queryBuilder();
    Where<Observation, Long> where = query
            .orderBy("timestamp", false)
            .where()
            .ge("last_modified", observationCollection.getLatestDate())
            .and()
            .eq("event_id", currentEventId);

    for (Filter filter : filters) {
        QueryBuilder<?, ?> filterQuery = filter.query();
        if (filterQuery != null) {
            query.join(filterQuery);
        }

        filter.and(where);
    }

    return dao.iterator(query.prepare());
}
 
Example 2
Source File: DialogOccupantDataManager.java    From q-municate-android with Apache License 2.0 6 votes vote down vote up
public DialogOccupant getDialogOccupantForPrivateChat(int userId) {
    DialogOccupant dialogOccupant = null;

    try {
        QueryBuilder<DialogOccupant, Long> dialogOccupantQueryBuilder = dao.queryBuilder();
        dialogOccupantQueryBuilder.where().eq(QMUserColumns.ID, userId);

        QueryBuilder<Dialog, Long> dialogQueryBuilder = dialogDao.queryBuilder();
        dialogQueryBuilder.where().eq(Dialog.Column.TYPE, Dialog.Type.PRIVATE);

        dialogOccupantQueryBuilder.join(dialogQueryBuilder);

        PreparedQuery<DialogOccupant> preparedQuery = dialogOccupantQueryBuilder.prepare();
        dialogOccupant = dao.queryForFirst(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return dialogOccupant;
}
 
Example 3
Source File: DialogNotificationDataManager.java    From q-municate-android with Apache License 2.0 6 votes vote down vote up
public List<DialogNotification> getDialogNotificationsByDialogId(String dialogId) {
    List<DialogNotification> dialogNotificationsList = new ArrayList<>();

    try {
        QueryBuilder<DialogNotification, Long> messageQueryBuilder = dao
                .queryBuilder();

        QueryBuilder<DialogOccupant, Long> dialogOccupantQueryBuilder = dialogOccupantDao
                .queryBuilder();

        QueryBuilder<Dialog, Long> dialogQueryBuilder = dialogDao.queryBuilder();
        dialogQueryBuilder.where().eq(Dialog.Column.ID, dialogId);

        dialogOccupantQueryBuilder.join(dialogQueryBuilder);
        messageQueryBuilder.join(dialogOccupantQueryBuilder);

        PreparedQuery<DialogNotification> preparedQuery = messageQueryBuilder.prepare();
        dialogNotificationsList = dao.query(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return dialogNotificationsList;
}
 
Example 4
Source File: FriendDataManager.java    From q-municate-android with Apache License 2.0 6 votes vote down vote up
public List<Friend> getAllSorted() {
    List<Friend> friendsList = Collections.emptyList();

    try {
        QueryBuilder<Friend, Long> friendQueryBuilder = dao.queryBuilder();

        QueryBuilder<QMUser, Long> userQueryBuilder = userDao.queryBuilder();
        userQueryBuilder.orderByRaw(QMUserColumns.FULL_NAME + " COLLATE NOCASE");

        friendQueryBuilder.join(userQueryBuilder);

        PreparedQuery<Friend> preparedQuery = friendQueryBuilder.prepare();

        friendsList = dao.query(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return friendsList;
}
 
Example 5
Source File: MessageDataManager.java    From q-municate-android with Apache License 2.0 6 votes vote down vote up
public List<Message> getMessagesByDialogId(String dialogId) {
    List<Message> messagesList = new ArrayList<>();

    try {
        QueryBuilder<Message, Long> messageQueryBuilder = dao.queryBuilder();

        QueryBuilder<DialogOccupant, Long> dialogOccupantQueryBuilder = dialogOccupantDao.queryBuilder();

        QueryBuilder<Dialog, Long> dialogQueryBuilder = dialogDao.queryBuilder();
        dialogQueryBuilder.where().eq(Dialog.Column.ID, dialogId);

        dialogOccupantQueryBuilder.join(dialogQueryBuilder);
        messageQueryBuilder.join(dialogOccupantQueryBuilder);

        PreparedQuery<Message> preparedQuery = messageQueryBuilder.prepare();
        messagesList = dao.query(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return messagesList;
}
 
Example 6
Source File: MessageDataManager.java    From q-municate-android with Apache License 2.0 6 votes vote down vote up
public List<Message> getMessagesByDialogIdLimeted(String dialogId, long limit) {
    List<Message> messagesList = new ArrayList<>();

    try {
        QueryBuilder<Message, Long> messageQueryBuilder = dao.queryBuilder();

        QueryBuilder<DialogOccupant, Long> dialogOccupantQueryBuilder = dialogOccupantDao.queryBuilder();

        QueryBuilder<Dialog, Long> dialogQueryBuilder = dialogDao.queryBuilder();
        dialogQueryBuilder.where().eq(Dialog.Column.ID, dialogId);

        dialogOccupantQueryBuilder.join(dialogQueryBuilder);
        messageQueryBuilder
                .join(dialogOccupantQueryBuilder)
                .orderBy(Message.Column.CREATED_DATE, false)
                .limit(limit);

        PreparedQuery<Message> preparedQuery = messageQueryBuilder.prepare();
        messagesList = dao.query(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return messagesList;
}
 
Example 7
Source File: ContentsIdExtension.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Get by contents data type
 * 
 * @param type
 *            contents data type
 * @return contents ids
 */
public List<ContentsId> getIds(String type) {

	List<ContentsId> contentsIds = null;

	ContentsDao contentsDao = geoPackage.getContentsDao();

	try {

		if (contentsIdDao.isTableExists()) {

			QueryBuilder<Contents, String> contentsQueryBuilder = contentsDao
					.queryBuilder();
			QueryBuilder<ContentsId, Long> contentsIdQueryBuilder = contentsIdDao
					.queryBuilder();

			contentsQueryBuilder.where().eq(Contents.COLUMN_DATA_TYPE,
					type);
			contentsIdQueryBuilder.join(contentsQueryBuilder);

			contentsIds = contentsIdQueryBuilder.query();

		} else {
			contentsIds = new ArrayList<>();
		}
	} catch (SQLException e) {
		throw new GeoPackageException(
				"Failed to query for contents id by contents data type. GeoPackage: "
						+ geoPackage.getName() + ", Type: " + type,
				e);
	}

	return contentsIds;
}
 
Example 8
Source File: DialogNotificationDataManager.java    From q-municate-android with Apache License 2.0 5 votes vote down vote up
public List<DialogNotification> getDialogNotificationsByDialogId(String dialogId, long limit) {
    List<DialogNotification> dialogNotificationsList = new ArrayList<>();

    try {
        QueryBuilder<DialogNotification, Long> messageQueryBuilder = dao
                .queryBuilder();

        QueryBuilder<DialogOccupant, Long> dialogOccupantQueryBuilder = dialogOccupantDao
                .queryBuilder();

        QueryBuilder<Dialog, Long> dialogQueryBuilder = dialogDao.queryBuilder();
        dialogQueryBuilder.where().eq(Dialog.Column.ID, dialogId);

        dialogOccupantQueryBuilder.join(dialogQueryBuilder);
        messageQueryBuilder
                .join(dialogOccupantQueryBuilder)
                .orderBy(DialogNotification.Column.CREATED_DATE, false)
                .limit(limit);

        PreparedQuery<DialogNotification> preparedQuery = messageQueryBuilder.prepare();
        dialogNotificationsList = dao.query(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return dialogNotificationsList;
}
 
Example 9
Source File: DialogNotificationDataManager.java    From q-municate-android with Apache License 2.0 5 votes vote down vote up
public List<DialogNotification> getDialogNotificationsByDialogIdAndDate(String dialogId, long createdDate, boolean moreDate) {
    List<DialogNotification> dialogNotificationsList = new ArrayList<>();

    try {
        QueryBuilder<DialogNotification, Long> messageQueryBuilder = dao.queryBuilder();

        Where<DialogNotification, Long> where = messageQueryBuilder.where();
        where.and(where.ne(DialogNotification.Column.STATE, State.TEMP_LOCAL),
                where.ne(DialogNotification.Column.STATE, State.TEMP_LOCAL_UNREAD),
                moreDate
                        ? where.gt(DialogNotification.Column.CREATED_DATE, createdDate)
                        : where.lt(DialogNotification.Column.CREATED_DATE, createdDate));

        QueryBuilder<DialogOccupant, Long> dialogOccupantQueryBuilder = dialogOccupantDao
                .queryBuilder();

        QueryBuilder<Dialog, Long> dialogQueryBuilder = dialogDao.queryBuilder();
        dialogQueryBuilder.where().eq(Dialog.Column.ID, dialogId);

        dialogOccupantQueryBuilder.join(dialogQueryBuilder);
        messageQueryBuilder.join(dialogOccupantQueryBuilder);

        PreparedQuery<DialogNotification> preparedQuery = messageQueryBuilder.prepare();
        dialogNotificationsList = dao.query(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return dialogNotificationsList;
}
 
Example 10
Source File: DialogNotificationDataManager.java    From q-municate-android with Apache License 2.0 5 votes vote down vote up
public List<DialogNotification> getDialogNotificationsByDialogIdAndDate(String dialogId, long createdDate, boolean moreDate, long limit) {
    List<DialogNotification> dialogNotificationsList = new ArrayList<>();

    try {
        QueryBuilder<DialogNotification, Long> messageQueryBuilder = dao.queryBuilder();

        Where<DialogNotification, Long> where = messageQueryBuilder.where();
        where.and(where.ne(DialogNotification.Column.STATE, State.TEMP_LOCAL),
                where.ne(DialogNotification.Column.STATE, State.TEMP_LOCAL_UNREAD),
                moreDate
                        ? where.gt(DialogNotification.Column.CREATED_DATE, createdDate)
                        : where.lt(DialogNotification.Column.CREATED_DATE, createdDate));

        QueryBuilder<DialogOccupant, Long> dialogOccupantQueryBuilder = dialogOccupantDao
                .queryBuilder();

        QueryBuilder<Dialog, Long> dialogQueryBuilder = dialogDao.queryBuilder();
        dialogQueryBuilder.where().eq(Dialog.Column.ID, dialogId);

        dialogOccupantQueryBuilder.join(dialogQueryBuilder);
        messageQueryBuilder
                .join(dialogOccupantQueryBuilder)
                .orderBy(DialogNotification.Column.CREATED_DATE, false)
                .limit(limit);

        PreparedQuery<DialogNotification> preparedQuery = messageQueryBuilder.prepare();
        dialogNotificationsList = dao.query(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return dialogNotificationsList;
}
 
Example 11
Source File: MessageDataManager.java    From q-municate-android with Apache License 2.0 5 votes vote down vote up
public List<Message> getMessagesByDialogIdAndDate(String dialogId, long createdDate, boolean moreDate){
    List<Message> messagesList = new ArrayList<>();

    try {
        QueryBuilder<Message, Long> messageQueryBuilder = dao.queryBuilder();

        Where<Message, Long> where = messageQueryBuilder.where();
        where.and(where.ne(Message.Column.STATE, State.TEMP_LOCAL),
                where.ne(Message.Column.STATE, State.TEMP_LOCAL_UNREAD),
                moreDate
                        ? where.gt(Message.Column.CREATED_DATE, createdDate)
                        : where.lt(Message.Column.CREATED_DATE, createdDate));

        QueryBuilder<DialogOccupant, Long> dialogOccupantQueryBuilder = dialogOccupantDao.queryBuilder();

        QueryBuilder<Dialog, Long> dialogQueryBuilder = dialogDao.queryBuilder();
        dialogQueryBuilder.where().eq(Dialog.Column.ID, dialogId);

        dialogOccupantQueryBuilder.join(dialogQueryBuilder);
        messageQueryBuilder.join(dialogOccupantQueryBuilder);

        PreparedQuery<Message> preparedQuery = messageQueryBuilder.prepare();
        messagesList = dao.query(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return messagesList;
}
 
Example 12
Source File: MessageDataManager.java    From q-municate-android with Apache License 2.0 5 votes vote down vote up
public List<Message> getMessagesByDialogIdAndDate(String dialogId, long createdDate, boolean moreDate, long limit){
    List<Message> messagesList = new ArrayList<>();

    try {
        QueryBuilder<Message, Long> messageQueryBuilder = dao.queryBuilder();

        Where<Message, Long> where = messageQueryBuilder.where();
        where.and(where.ne(Message.Column.STATE, State.TEMP_LOCAL),
                where.ne(Message.Column.STATE, State.TEMP_LOCAL_UNREAD),
                moreDate
                        ? where.gt(Message.Column.CREATED_DATE, createdDate)
                        : where.lt(Message.Column.CREATED_DATE, createdDate));

        QueryBuilder<DialogOccupant, Long> dialogOccupantQueryBuilder = dialogOccupantDao.queryBuilder();

        QueryBuilder<Dialog, Long> dialogQueryBuilder = dialogDao.queryBuilder();
        dialogQueryBuilder.where().eq(Dialog.Column.ID, dialogId);

        dialogOccupantQueryBuilder.join(dialogQueryBuilder);
        messageQueryBuilder
                .join(dialogOccupantQueryBuilder)
                .orderBy(Message.Column.CREATED_DATE, false)
                .limit(limit);

        PreparedQuery<Message> preparedQuery = messageQueryBuilder.prepare();
        messagesList = dao.query(preparedQuery);
    } catch (SQLException e) {
        ErrorUtils.logError(e);
    }

    return messagesList;
}
 
Example 13
Source File: ObservationFeedFragment.java    From mage-android with Apache License 2.0 4 votes vote down vote up
private PreparedQuery<Observation> buildQuery(Dao<Observation, Long> oDao, int filterId) throws SQLException {
	QueryBuilder<Observation, Long> qb = oDao.queryBuilder();
	Calendar c = Calendar.getInstance();
	List<String> filters = new ArrayList<>();
	String footerText = "All observations have been returned";

	if (filterId == getResources().getInteger(R.integer.time_filter_last_month)) {
		filters.add("Last Month");
		footerText = "End of results for Last Month filter";
		c.add(Calendar.MONTH, -1);
	} else if (filterId == getResources().getInteger(R.integer.time_filter_last_week)) {
		filters.add("Last Week");
		footerText = "End of results for Last Week filter";
		c.add(Calendar.DAY_OF_MONTH, -7);
	} else if (filterId == getResources().getInteger(R.integer.time_filter_last_24_hours)) {
		filters.add("Last 24 Hours");
		footerText = "End of results for Last 24 Hours filter";
		c.add(Calendar.HOUR, -24);
	} else if (filterId == getResources().getInteger(R.integer.time_filter_today)) {
		filters.add("Since Midnight");
		footerText = "End of results for Today filter";
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
	}  else if (filterId == getResources().getInteger(R.integer.time_filter_custom)) {
		String customFilterTimeUnit = getCustomTimeUnit();
		int customTimeNumber = getCustomTimeNumber();

		filters.add("Last " + customTimeNumber + " " + customFilterTimeUnit);
		footerText = "End of results for custom filter";
		switch (customFilterTimeUnit) {
			case "Hours":
				c.add(Calendar.HOUR, -1 * customTimeNumber);
				break;
			case "Days":
				c.add(Calendar.DAY_OF_MONTH, -1 * customTimeNumber);
				break;
			case "Months":
				c.add(Calendar.MONTH, -1 * customTimeNumber);
				break;
			default:
				c.add(Calendar.MINUTE, -1 * customTimeNumber);
				break;
		}

	} else {
		// no filter
		c.setTime(new Date(0));
	}

	requeryTime = c.getTimeInMillis();
	adapter.setFooterText(footerText);
	qb.where()
		.ne("state", State.ARCHIVE)
		.and()
		.ge("timestamp", c.getTime())
		.and()
		.eq("event_id", EventHelper.getInstance(context).getCurrentEvent().getId());

	List<String> actionFilters = new ArrayList<>();

	boolean favorites = preferences.getBoolean(getResources().getString(R.string.activeFavoritesFilterKey), false);
	if (favorites && currentUser != null) {
		Dao<ObservationFavorite, Long> observationFavoriteDao = DaoStore.getInstance(context).getObservationFavoriteDao();
		QueryBuilder<ObservationFavorite, Long> favoriteQb = observationFavoriteDao.queryBuilder();
		favoriteQb.where()
			.eq("user_id", currentUser.getRemoteId())
			.and()
			.eq("is_favorite", true);

		qb.join(favoriteQb);

		actionFilters.add("Favorites");
	}

	boolean important = preferences.getBoolean(getResources().getString(R.string.activeImportantFilterKey), false);
	if (important) {
		Dao<ObservationImportant, Long> observationImportantDao = DaoStore.getInstance(context).getObservationImportantDao();
		QueryBuilder<ObservationImportant, Long> importantQb = observationImportantDao.queryBuilder();
		importantQb.where().eq("is_important", true);

		qb.join(importantQb);

		actionFilters.add("Important");
	}

	qb.orderBy("timestamp", false);

	if (!actionFilters.isEmpty()) {
		filters.add(StringUtils.join(actionFilters, " & "));
	}

	((AppCompatActivity) getActivity()).getSupportActionBar().setSubtitle(StringUtils.join(filters, ", "));

	return qb.prepare();
}