<commit_before>/**
 * Copyright (C) 2014 yvolk (<NAME>), http://yurivolkov.com
 *
 * 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 org.andstatus.app.data;

import android.database.Cursor;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.provider.BaseColumns;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import org.andstatus.app.context.MyContextHolder;
import org.andstatus.app.context.MyPreferences;
import org.andstatus.app.context.UserInTimeline;
import org.andstatus.app.database.DownloadTable;
import org.andstatus.app.database.FriendshipTable;
import org.andstatus.app.database.MsgOfUserTable;
import org.andstatus.app.database.MsgTable;
import org.andstatus.app.database.UserTable;
import org.andstatus.app.origin.Origin;
import org.andstatus.app.origin.OriginType;
import org.andstatus.app.timeline.Timeline;
import org.andstatus.app.timeline.TimelineType;
import org.andstatus.app.util.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class TimelineSql {
    private TimelineSql() {
        // Empty
    }

    /**
     * @param uri the same as uri for
     *            {@link MyProvider#query(Uri, String[], String, String[], String)}
     * @param projection Projection
     * @return String for {@link SQLiteQueryBuilder#setTables(String)}
     */
    static String tablesForTimeline(Uri uri, String[] projection) {
        Timeline timeline = Timeline.fromParsedUri(MyContextHolder.get(), ParsedUri.fromUri(uri), "");
        SelectedUserIds selectedAccounts = new SelectedUserIds(timeline);
    
        Collection<String> columns = new java.util.HashSet<>(Arrays.asList(projection));
    
        String msgTable = MsgTable.TABLE_NAME;
        SqlWhere where = new SqlWhere();

        boolean linkedUserDefined = false;
        boolean authorNameDefined = false;
        String authorTableName = "";
        switch (timeline.getTimelineType()) {
            case FOLLOWERS:
            case MY_FOLLOWERS:
            case FRIENDS:
            case MY_FRIENDS:
                String fUserIdColumnName = FriendshipTable.FRIEND_ID;
                String fUserLinkedUserIdColumnName = FriendshipTable.USER_ID;
                if (timeline.getTimelineType() == TimelineType.FOLLOWERS ||
                        timeline.getTimelineType() == TimelineType.MY_FOLLOWERS) {
                    fUserIdColumnName = FriendshipTable.USER_ID;
                    fUserLinkedUserIdColumnName = FriendshipTable.FRIEND_ID;
                }
                msgTable = "(SELECT " + fUserIdColumnName + " AS fUserId, "
                        + fUserLinkedUserIdColumnName + " AS " + UserTable.LINKED_USER_ID
                        + " FROM " + FriendshipTable.TABLE_NAME
                        + " WHERE (" + UserTable.LINKED_USER_ID + selectedAccounts.getSql()
                        + " AND " + FriendshipTable.FOLLOWED + "=1 )"
                        + ") as fUser";
                linkedUserDefined = true;
                boolean defineAuthorName = columns.contains(UserTable.AUTHOR_NAME);
                if (defineAuthorName) {
                    authorNameDefined = true;
                    authorTableName = "u1";
                }
                String userTable = "(SELECT "
                        + BaseColumns._ID
                        + (defineAuthorName ? ", " + UserTable.USERNAME + " AS " + UserTable.AUTHOR_NAME : "")
                        + ", " + UserTable.USER_MSG_ID
                        + " FROM " + UserTable.TABLE_NAME + ")";
                msgTable += " INNER JOIN " + userTable + " as u1"
                        + " ON (fUserId=u1." + BaseColumns._ID + ")";
                /**
                 * Select only the latest message from each Friend's timeline
                 */
                msgTable  += " LEFT JOIN " + MsgTable.TABLE_NAME + " AS " + ProjectionMap.MSG_TABLE_ALIAS
                        + " ON ("
                        + ProjectionMap.MSG_TABLE_ALIAS + "." + MsgTable.ACTOR_ID
                        + "=fUserId"
                        + " AND " + ProjectionMap.MSG_TABLE_ALIAS + "." + BaseColumns._ID
                        + "=u1." + UserTable.USER_MSG_ID
                        + ")";<commit_msg>Fixed: In the Friends and Followers timelines Username was always shown as "@username" disregarding settings<commit_after>/**
 * Copyright (C) 2014 yvolk (<NAME>), http://yurivolkov.com
 *
 * 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 org.andstatus.app.data;

import android.database.Cursor;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.provider.BaseColumns;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import org.andstatus.app.context.MyContextHolder;
import org.andstatus.app.context.MyPreferences;
import org.andstatus.app.context.UserInTimeline;
import org.andstatus.app.database.DownloadTable;
import org.andstatus.app.database.FriendshipTable;
import org.andstatus.app.database.MsgOfUserTable;
import org.andstatus.app.database.MsgTable;
import org.andstatus.app.database.UserTable;
import org.andstatus.app.origin.Origin;
import org.andstatus.app.origin.OriginType;
import org.andstatus.app.timeline.Timeline;
import org.andstatus.app.timeline.TimelineType;
import org.andstatus.app.util.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class TimelineSql {
    private TimelineSql() {
        // Empty
    }

    /**
     * @param uri the same as uri for
     *            {@link MyProvider#query(Uri, String[], String, String[], String)}
     * @param projection Projection
     * @return String for {@link SQLiteQueryBuilder#setTables(String)}
     */
    static String tablesForTimeline(Uri uri, String[] projection) {
        Timeline timeline = Timeline.fromParsedUri(MyContextHolder.get(), ParsedUri.fromUri(uri), "");
        SelectedUserIds selectedAccounts = new SelectedUserIds(timeline);
    
        Collection<String> columns = new java.util.HashSet<>(Arrays.asList(projection));
    
        String msgTable = MsgTable.TABLE_NAME;
        SqlWhere where = new SqlWhere();

        boolean linkedUserDefined = false;
        boolean authorNameDefined = false;
        String authorTableName = "";
        switch (timeline.getTimelineType()) {
            case FOLLOWERS:
            case MY_FOLLOWERS:
            case FRIENDS:
            case MY_FRIENDS:
                String fUserIdColumnName = FriendshipTable.FRIEND_ID;
                String fUserLinkedUserIdColumnName = FriendshipTable.USER_ID;
                if (timeline.getTimelineType() == TimelineType.FOLLOWERS ||
                        timeline.getTimelineType() == TimelineType.MY_FOLLOWERS) {
                    fUserIdColumnName = FriendshipTable.USER_ID;
                    fUserLinkedUserIdColumnName = FriendshipTable.FRIEND_ID;
                }
                msgTable = "(SELECT " + fUserIdColumnName + " AS fUserId, "
                        + fUserLinkedUserIdColumnName + " AS " + UserTable.LINKED_USER_ID
                        + " FROM " + FriendshipTable.TABLE_NAME
                        + " WHERE (" + UserTable.LINKED_USER_ID + selectedAccounts.getSql()
                        + " AND " + FriendshipTable.FOLLOWED + "=1 )"
                        + ") as fUser";
                linkedUserDefined = true;
                boolean defineAuthorName = columns.contains(UserTable.AUTHOR_NAME);
                if (defineAuthorName) {
                    authorNameDefined = true;
                    authorTableName = "u1";
                }
                String userTable = "(SELECT "
                        + BaseColumns._ID
                        + (defineAuthorName ? ", " + TimelineSql.userNameField() + " AS " + UserTable.AUTHOR_NAME : "")
                        + ", " + UserTable.USER_MSG_ID
                        + " FROM " + UserTable.TABLE_NAME + ")";
                msgTable += " INNER JOIN " + userTable + " as u1"
                        + " ON (fUserId=u1." + BaseColumns._ID + ")";
                /**
                 * Select only the latest message from each Friend's timeline
                 */
                msgTable  += " LEFT JOIN " + MsgTable.TABLE_NAME + " AS " + ProjectionMap.MSG_TABLE_ALIAS
                        + " ON ("
                        + ProjectionMap.MSG_TABLE_ALIAS + "." + MsgTable.ACTOR_ID
                        + "=fUserId"
                        + " AND " + ProjectionMap.MSG_TABLE_ALIAS + "." + BaseColumns._ID
                        + "=u1." + UserTable.USER_MSG_ID
                        + ")";
{'fraction_non_alphanumeric': 0.09626759127065063, 'fraction_numerical': 0.0026514378951662245, 'mean_word_length': 2.2080471050049066, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6984319', 'n_tokens_mistral': 2698, 'n_tokens_neox': 2612, 'n_words': 596}