<commit_before>package org.sagebionetworks.web.client.widget.table;

import java.util.Arrays;

import org.sagebionetworks.repo.model.EntityBundle;
import org.sagebionetworks.repo.model.EntityType;
import org.sagebionetworks.repo.model.entity.query.Condition;
import org.sagebionetworks.repo.model.entity.query.EntityFieldName;
import org.sagebionetworks.repo.model.entity.query.EntityQuery;
import org.sagebionetworks.repo.model.entity.query.EntityQueryResults;
import org.sagebionetworks.repo.model.entity.query.EntityQueryUtils;
import org.sagebionetworks.repo.model.entity.query.Operator;
import org.sagebionetworks.repo.model.entity.query.Sort;
import org.sagebionetworks.repo.model.entity.query.SortDirection;
import org.sagebionetworks.repo.model.table.EntityView;
import org.sagebionetworks.repo.model.table.TableEntity;
import org.sagebionetworks.web.client.DisplayUtils;
import org.sagebionetworks.web.client.SynapseClientAsync;
import org.sagebionetworks.web.client.cookie.CookieProvider;
import org.sagebionetworks.web.client.utils.Callback;
import org.sagebionetworks.web.client.utils.CallbackP;
import org.sagebionetworks.web.client.widget.entity.controller.PreflightController;
import org.sagebionetworks.web.client.widget.pagination.PageChangeListener;
import org.sagebionetworks.web.client.widget.pagination.PaginationWidget;
import org.sagebionetworks.web.client.widget.table.modal.CreateTableModalWidget;
import org.sagebionetworks.web.client.widget.table.modal.fileview.CreateTableViewWizard;
import org.sagebionetworks.web.client.widget.table.modal.fileview.CreateTableViewWizard.TableType;
import org.sagebionetworks.web.client.widget.table.modal.upload.UploadTableModalWidget;
import org.sagebionetworks.web.client.widget.table.modal.wizard.ModalWizardWidget.WizardCallback;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

/**
 * This widget lists the tables of a given project.
 * 
 * @author John
 *
 */
public class TableListWidget implements TableListWidgetView.Presenter, PageChangeListener, TableCreatedHandler, IsWidget {
	
	public static final Long PAGE_SIZE = 10L;
	public static final Long OFFSET_ZERO = 0L;

	private PreflightController preflightController;
	private TableListWidgetView view;
	private SynapseClientAsync synapseClient;
	private PaginationWidget paginationWidget;
	private CreateTableModalWidget createTableModalWidget;
	private UploadTableModalWidget uploadTableModalWidget;
	private CreateTableViewWizard createTableViewWizard;
	private boolean canEdit;
	private EntityQuery query;
	private EntityBundle parentBundle;
	private CallbackP<String> onTableClickCallback;
	private CookieProvider cookies;
	WizardCallback refreshTablesCallback;
	@Inject
	public TableListWidget(PreflightController preflightController,
			TableListWidgetView view,
			SynapseClientAsync synapseClient,
			CreateTableModalWidget createTableModalWidget,
			PaginationWidget paginationWidget,
			UploadTableModalWidget uploadTableModalWidget,
			CookieProvider cookies,
			CreateTableViewWizard createTableViewWizard) {
		this.preflightController = preflightController;
		this.view = view;
		this.synapseClient = synapseClient;
		this.createTableModalWidget = createTableModalWidget;
		this.uploadTableModalWidget = uploadTableModalWidget;
		this.paginationWidget = paginationWidget;
		this.createTableViewWizard = createTableViewWizard;
		this.cookies = cookies;
		this.view.setPresenter(this);
		this.view.addCreateTableModal(createTableModalWidget);
		this.view.addPaginationWidget(paginationWidget);
		this.view.addUploadTableModal(uploadTableModalWidget);
		this.view.addWizard(createTableViewWizard.asWidget());
		refreshTablesCallback = new WizardCallback() {
			@Override
			public void onFinished() {
				tableCreated();
			}
			
			@Override
			public void onCanceled() {
			}
		};
	}
	
	/**
	 * Configure this widget before use.
	 * @param projectOwnerId
	 * @param canEdit
	 * @param showAddTable
	 */
	public void configure(EntityBundle parentBundle) {
		this.parentBundle = parentBundle;
		this.canEdit = parentBundle.getPermissions().getCanEdit();
		this.createTableModalWidget.configure(parentBundle.getEntity().getId(), this);
		this.uploadTableModalWidget.configure(parentBundle.getEntity().getId(), null);
		this.query = createQuery(parentBundle.getEntity().getId());
		queryForOnePage(OFFSET_ZERO);
	}

	/**
	 * Create a new query.
	 * @param parentId
	 * @return
	 */
	public EntityQuery createQuery(String parentId) {
		EntityQuery newQuery = new EntityQuery();
		Sort sort = new Sort();
		sort.setColumnName(EntityFieldName.createdOn.name());
		sort.setDirection(SortDirection.DESC);
		newQuery.setSort(sort);
		Condition condition = EntityQueryUtils.buildCondition(EntityFieldName.parentId, Operator.EQUALS, parentId);
		Condition typeCondition = EntityQueryUtils.buildCondition(
				EntityFieldName.nodeType, Operator.IN, EntityType.table.name(), EntityType.entityview.name());
		
		newQuery.setConditions(Arrays.asList(condition, typeCondition));
		newQuery.setLimit(PAGE_SIZE);
		newQuery.setOffset(OFFSET_ZERO);
		return newQuery;<commit_msg>SWC-3153: sort by entity name ascending, and increase the page size to 20<commit_after>package org.sagebionetworks.web.client.widget.table;

import java.util.Arrays;

import org.sagebionetworks.repo.model.EntityBundle;
import org.sagebionetworks.repo.model.EntityType;
import org.sagebionetworks.repo.model.entity.query.Condition;
import org.sagebionetworks.repo.model.entity.query.EntityFieldName;
import org.sagebionetworks.repo.model.entity.query.EntityQuery;
import org.sagebionetworks.repo.model.entity.query.EntityQueryResults;
import org.sagebionetworks.repo.model.entity.query.EntityQueryUtils;
import org.sagebionetworks.repo.model.entity.query.Operator;
import org.sagebionetworks.repo.model.entity.query.Sort;
import org.sagebionetworks.repo.model.entity.query.SortDirection;
import org.sagebionetworks.repo.model.table.EntityView;
import org.sagebionetworks.repo.model.table.TableEntity;
import org.sagebionetworks.web.client.DisplayUtils;
import org.sagebionetworks.web.client.SynapseClientAsync;
import org.sagebionetworks.web.client.cookie.CookieProvider;
import org.sagebionetworks.web.client.utils.Callback;
import org.sagebionetworks.web.client.utils.CallbackP;
import org.sagebionetworks.web.client.widget.entity.controller.PreflightController;
import org.sagebionetworks.web.client.widget.pagination.PageChangeListener;
import org.sagebionetworks.web.client.widget.pagination.PaginationWidget;
import org.sagebionetworks.web.client.widget.table.modal.CreateTableModalWidget;
import org.sagebionetworks.web.client.widget.table.modal.fileview.CreateTableViewWizard;
import org.sagebionetworks.web.client.widget.table.modal.fileview.CreateTableViewWizard.TableType;
import org.sagebionetworks.web.client.widget.table.modal.upload.UploadTableModalWidget;
import org.sagebionetworks.web.client.widget.table.modal.wizard.ModalWizardWidget.WizardCallback;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

/**
 * This widget lists the tables of a given project.
 * 
 * @author John
 *
 */
public class TableListWidget implements TableListWidgetView.Presenter, PageChangeListener, TableCreatedHandler, IsWidget {
	
	public static final Long PAGE_SIZE = 20L;
	public static final Long OFFSET_ZERO = 0L;

	private PreflightController preflightController;
	private TableListWidgetView view;
	private SynapseClientAsync synapseClient;
	private PaginationWidget paginationWidget;
	private CreateTableModalWidget createTableModalWidget;
	private UploadTableModalWidget uploadTableModalWidget;
	private CreateTableViewWizard createTableViewWizard;
	private boolean canEdit;
	private EntityQuery query;
	private EntityBundle parentBundle;
	private CallbackP<String> onTableClickCallback;
	private CookieProvider cookies;
	WizardCallback refreshTablesCallback;
	@Inject
	public TableListWidget(PreflightController preflightController,
			TableListWidgetView view,
			SynapseClientAsync synapseClient,
			CreateTableModalWidget createTableModalWidget,
			PaginationWidget paginationWidget,
			UploadTableModalWidget uploadTableModalWidget,
			CookieProvider cookies,
			CreateTableViewWizard createTableViewWizard) {
		this.preflightController = preflightController;
		this.view = view;
		this.synapseClient = synapseClient;
		this.createTableModalWidget = createTableModalWidget;
		this.uploadTableModalWidget = uploadTableModalWidget;
		this.paginationWidget = paginationWidget;
		this.createTableViewWizard = createTableViewWizard;
		this.cookies = cookies;
		this.view.setPresenter(this);
		this.view.addCreateTableModal(createTableModalWidget);
		this.view.addPaginationWidget(paginationWidget);
		this.view.addUploadTableModal(uploadTableModalWidget);
		this.view.addWizard(createTableViewWizard.asWidget());
		refreshTablesCallback = new WizardCallback() {
			@Override
			public void onFinished() {
				tableCreated();
			}
			
			@Override
			public void onCanceled() {
			}
		};
	}
	
	/**
	 * Configure this widget before use.
	 * @param projectOwnerId
	 * @param canEdit
	 * @param showAddTable
	 */
	public void configure(EntityBundle parentBundle) {
		this.parentBundle = parentBundle;
		this.canEdit = parentBundle.getPermissions().getCanEdit();
		this.createTableModalWidget.configure(parentBundle.getEntity().getId(), this);
		this.uploadTableModalWidget.configure(parentBundle.getEntity().getId(), null);
		this.query = createQuery(parentBundle.getEntity().getId());
		queryForOnePage(OFFSET_ZERO);
	}

	/**
	 * Create a new query.
	 * @param parentId
	 * @return
	 */
	public EntityQuery createQuery(String parentId) {
		EntityQuery newQuery = new EntityQuery();
		Sort sort = new Sort();
		sort.setColumnName(EntityFieldName.name.name());
		sort.setDirection(SortDirection.ASC);
		newQuery.setSort(sort);
		Condition condition = EntityQueryUtils.buildCondition(EntityFieldName.parentId, Operator.EQUALS, parentId);
		Condition typeCondition = EntityQueryUtils.buildCondition(
				EntityFieldName.nodeType, Operator.IN, EntityType.table.name(), EntityType.entityview.name());
		
		newQuery.setConditions(Arrays.asList(condition, typeCondition));
		newQuery.setLimit(PAGE_SIZE);
		newQuery.setOffset(OFFSET_ZERO);
		return newQuery;
{'fraction_non_alphanumeric': 0.09145413018652455, 'fraction_numerical': 0.0011419870574800152, 'mean_word_length': 6.328451882845188, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4362976', 'n_tokens_mistral': 3333, 'n_tokens_neox': 3158, 'n_words': 507}