<commit_before>/*
 * UniTime 3.4 (University Timetabling Application)
 * Copyright (C) 2012, UniTime LLC, and individual contributors
 * as indicated by the @authors tag.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
*/
package org.unitime.timetable.gwt.client.events;

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

import org.unitime.timetable.gwt.client.ToolBox;
import org.unitime.timetable.gwt.client.widgets.FilterBox;
import org.unitime.timetable.gwt.client.widgets.UniTimeWidget;
import org.unitime.timetable.gwt.client.widgets.FilterBox.Chip;
import org.unitime.timetable.gwt.client.widgets.FilterBox.Suggestion;
import org.unitime.timetable.gwt.client.widgets.FilterBox.SuggestionsProvider;
import org.unitime.timetable.gwt.command.client.GwtRpcService;
import org.unitime.timetable.gwt.command.client.GwtRpcServiceAsync;
import org.unitime.timetable.gwt.resources.GwtMessages;
import org.unitime.timetable.gwt.shared.AcademicSessionProvider;
import org.unitime.timetable.gwt.shared.AcademicSessionProvider.AcademicSessionChangeEvent;
import org.unitime.timetable.gwt.shared.AcademicSessionProvider.AcademicSessionChangeHandler;
import org.unitime.timetable.gwt.shared.EventInterface.FilterRpcRequest;
import org.unitime.timetable.gwt.shared.EventInterface.FilterRpcResponse;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.HasAllKeyHandlers;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.HasValue;

public abstract class UniTimeFilterBox extends Composite implements HasValue<String>, Focusable, HasAllKeyHandlers {
	private static GwtRpcServiceAsync RPC = GWT.create(GwtRpcService.class);
	private static final GwtMessages MESSAGES = GWT.create(GwtMessages.class);
	private AcademicSessionProvider iAcademicSession;
	protected UniTimeWidget<FilterBox> iFilter;
	private boolean iInitialized = false;
	
	public UniTimeFilterBox(AcademicSessionProvider session) {
		iFilter = new UniTimeWidget<FilterBox>(new FilterBox());
		
		iFilter.getWidget().setSuggestionsProvider(new SuggestionsProvider() {
			@Override
			public void getSuggestions(List<Chip> chips, String text, final AsyncCallback<Collection<Suggestion>> callback) {
				Long sessionId = (iAcademicSession == null ? null : iAcademicSession.getAcademicSessionId());
				if (sessionId == null && iAcademicSession != null) {
					callback.onSuccess(null);
					return;
				}
				RPC.execute(createRpcRequest(FilterRpcRequest.Command.SUGGESTIONS, iAcademicSession == null ? null : iAcademicSession.getAcademicSessionId(), chips, text), new AsyncCallback<FilterRpcResponse>() {

					@Override
					public void onFailure(Throwable caught) {
						callback.onFailure(caught);
					}

					@Override
					public void onSuccess(FilterRpcResponse result) {
						List<FilterBox.Suggestion> suggestions = new ArrayList<FilterBox.Suggestion>();
						if (result.hasSuggestions()) {
							for (FilterRpcResponse.Entity s: result.getSuggestions())
								addSuggestion(suggestions, s);
						}
						callback.onSuccess(suggestions);
					}
					
				});
			}
		});
		
		initWidget(iFilter);
		
		iAcademicSession = session;
		
		if (iAcademicSession != null)
			iAcademicSession.addAcademicSessionChangeHandler(new AcademicSessionChangeHandler() {
				@Override
				public void onAcademicSessionChange(AcademicSessionChangeEvent event) {
					if (event.isChanged()) init(true, event.getNewAcademicSessionId(), null);
				}
			});
		
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			@Override
			public void execute() {
				init(true, iAcademicSession == null ? null : iAcademicSession.getAcademicSessionId(), null);
			}
		});
	}
	
	protected void addSuggestion(List<FilterBox.Suggestion> suggestions, FilterRpcResponse.Entity entity) {
		suggestions.add(new FilterBox.Suggestion(entity.getName(), entity.getAbbreviation(), entity.getProperty("hint", null)));
	}
	
	protected void initAsync() {
		setValue(getValue());
	}

	protected void init(final boolean init, Long academicSessionId, final Command onSuccess) {
		if (academicSessionId == null && iAcademicSession != null) {
			iFilter.setHint(MESSAGES.hintNoSession());
		} else {
			if (init) {
				iFilter.setHint(MESSAGES.waitLoadingData(iAcademicSession == null ? "" : iAcademicSession.getAcademicSessionName()));
				iInitialized = false;
			}
			final String value = iFilter.getWidget().getValue();
			RPC.execute(createRpcRequest(FilterRpcRequest.Command.LOAD, academicSessionId, iFilter.getWidget().getChips(null), iFilter.getWidget().getText()), new AsyncCallback<FilterRpcResponse>() {
				@Override
				public void onFailure(Throwable caught) {
					iFilter.setErrorHint(caught.getMessage());
					ToolBox.checkAccess(caught);
				}
				@Override
				public void onSuccess(FilterRpcResponse result) {
					iFilter.clearHint();
					if (!value.equals(iFilter.getWidget().getValue())) return;
					onLoad(result);
					for (FilterBox.Filter filter: iFilter.getWidget().getFilters())
						populateFilter(filter, result.getEntities(filter.getCommand()));<commit_msg>Reservations  - reservations filter rewritten to use the same component as on the Events page<commit_after>/*
 * UniTime 3.4 (University Timetabling Application)
 * Copyright (C) 2012, UniTime LLC, and individual contributors
 * as indicated by the @authors tag.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
*/
package org.unitime.timetable.gwt.client.events;

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

import org.unitime.timetable.gwt.client.ToolBox;
import org.unitime.timetable.gwt.client.widgets.FilterBox;
import org.unitime.timetable.gwt.client.widgets.UniTimeWidget;
import org.unitime.timetable.gwt.client.widgets.FilterBox.Chip;
import org.unitime.timetable.gwt.client.widgets.FilterBox.Suggestion;
import org.unitime.timetable.gwt.client.widgets.FilterBox.SuggestionsProvider;
import org.unitime.timetable.gwt.command.client.GwtRpcService;
import org.unitime.timetable.gwt.command.client.GwtRpcServiceAsync;
import org.unitime.timetable.gwt.resources.GwtMessages;
import org.unitime.timetable.gwt.shared.AcademicSessionProvider;
import org.unitime.timetable.gwt.shared.AcademicSessionProvider.AcademicSessionChangeEvent;
import org.unitime.timetable.gwt.shared.AcademicSessionProvider.AcademicSessionChangeHandler;
import org.unitime.timetable.gwt.shared.EventInterface.FilterRpcRequest;
import org.unitime.timetable.gwt.shared.EventInterface.FilterRpcResponse;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.HasAllKeyHandlers;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.HasValue;

public abstract class UniTimeFilterBox extends Composite implements HasValue<String>, Focusable, HasAllKeyHandlers {
	private static GwtRpcServiceAsync RPC = GWT.create(GwtRpcService.class);
	private static final GwtMessages MESSAGES = GWT.create(GwtMessages.class);
	private AcademicSessionProvider iAcademicSession;
	protected UniTimeWidget<FilterBox> iFilter;
	private boolean iInitialized = false;
	
	public UniTimeFilterBox(AcademicSessionProvider session) {
		iFilter = new UniTimeWidget<FilterBox>(new FilterBox());
		
		iFilter.getWidget().setSuggestionsProvider(new SuggestionsProvider() {
			@Override
			public void getSuggestions(List<Chip> chips, String text, final AsyncCallback<Collection<Suggestion>> callback) {
				Long sessionId = (iAcademicSession == null ? null : iAcademicSession.getAcademicSessionId());
				if (sessionId == null && iAcademicSession != null) {
					callback.onSuccess(null);
					return;
				}
				RPC.execute(createRpcRequest(FilterRpcRequest.Command.SUGGESTIONS, iAcademicSession == null ? null : iAcademicSession.getAcademicSessionId(), chips, text), new AsyncCallback<FilterRpcResponse>() {

					@Override
					public void onFailure(Throwable caught) {
						callback.onFailure(caught);
					}

					@Override
					public void onSuccess(FilterRpcResponse result) {
						List<FilterBox.Suggestion> suggestions = new ArrayList<FilterBox.Suggestion>();
						if (result.hasSuggestions()) {
							for (FilterRpcResponse.Entity s: result.getSuggestions())
								addSuggestion(suggestions, s);
						}
						callback.onSuccess(suggestions);
					}
					
				});
			}
		});
		
		initWidget(iFilter);
		
		iAcademicSession = session;
		
		if (iAcademicSession != null)
			iAcademicSession.addAcademicSessionChangeHandler(new AcademicSessionChangeHandler() {
				@Override
				public void onAcademicSessionChange(AcademicSessionChangeEvent event) {
					if (event.isChanged()) init(true, event.getNewAcademicSessionId(), null);
				}
			});
		
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			@Override
			public void execute() {
				init(true, iAcademicSession == null ? null : iAcademicSession.getAcademicSessionId(), null);
			}
		});
	}
	
	protected void addSuggestion(List<FilterBox.Suggestion> suggestions, FilterRpcResponse.Entity entity) {
		String command = entity.getProperty("command", null);
		if (command == null) {
			suggestions.add(new FilterBox.Suggestion(entity.getName(), entity.getAbbreviation(), entity.getProperty("hint", null)));
		} else {
			Chip old = ("true".equals(entity.getProperty("single", "true")) ? getChip(command) : null);
			suggestions.add(new FilterBox.Suggestion(new Chip(command, entity.getAbbreviation(), entity.getName(), entity.getProperty("hint", null)), old));
		}
	}
	
	protected void initAsync() {
		setValue(getValue());
	}

	protected void init(final boolean init, Long academicSessionId, final Command onSuccess) {
		if (academicSessionId == null && iAcademicSession != null) {
			iFilter.setHint(MESSAGES.hintNoSession());
		} else {
			if (init) {
				iFilter.setHint(MESSAGES.waitLoadingData(iAcademicSession == null ? "" : iAcademicSession.getAcademicSessionName()));
				iInitialized = false;
			}
			final String value = iFilter.getWidget().getValue();
			RPC.execute(createRpcRequest(FilterRpcRequest.Command.LOAD, academicSessionId, iFilter.getWidget().getChips(null), iFilter.getWidget().getText()), new AsyncCallback<FilterRpcResponse>() {
				@Override
				public void onFailure(Throwable caught) {
					iFilter.setErrorHint(caught.getMessage());
					ToolBox.checkAccess(caught);
				}
				@Override
				public void onSuccess(FilterRpcResponse result) {
					iFilter.clearHint();
					if (!value.equals(iFilter.getWidget().getValue())) return;
					onLoad(result);
					for (FilterBox.Filter filter: iFilter.getWidget().getFilters())
						populateFilter(filter, result.getEntities(filter.getCommand()));
{'fraction_non_alphanumeric': 0.1072523765716038, 'fraction_numerical': 0.0010732904017172647, 'mean_word_length': 5.036557149467839, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3524491', 'n_tokens_mistral': 4432, 'n_tokens_neox': 3808, 'n_words': 800}