<commit_before>/*******************************************************************************
 * Copyright (c) 2004 - 2005 University Of British Columbia and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylar.bugzilla.ui.tasklist;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.security.auth.login.LoginException;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.mylar.bugzilla.core.BugzillaPlugin;
import org.eclipse.mylar.bugzilla.core.IBugzillaConstants;
import org.eclipse.mylar.bugzilla.core.search.BugzillaSearchHit;
import org.eclipse.mylar.bugzilla.ui.BugzillaImages;
import org.eclipse.mylar.bugzilla.ui.BugzillaUiPlugin;
import org.eclipse.mylar.bugzilla.ui.search.BugzillaResultCollector;
import org.eclipse.mylar.bugzilla.ui.tasklist.BugzillaCategorySearchOperation.ICategorySearchListener;
import org.eclipse.mylar.tasklist.IQuery;
import org.eclipse.mylar.tasklist.IQueryHit;
import org.eclipse.mylar.tasklist.ITaskListElement;
import org.eclipse.mylar.tasklist.TaskListImages;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.PlatformUI;

/**
 * @author <NAME>
 */
public class BugzillaQueryCategory implements IQuery {
	
	private static final long serialVersionUID = 5517146402031743253L;	
	private String queryString;
	private int maxHits;
	private List<IQueryHit> hits = new ArrayList<IQueryHit>();
	private boolean hasBeenRefreshed = false;
	
	protected Date lastRefresh;
	
	protected String description = "";
	private String handle = "";
	
	private ICategorySearchListener listener = new BugzillaQueryCategorySearchListener();
	private boolean isMaxReached = false;
	
	public class BugzillaQueryCategorySearchListener implements ICategorySearchListener { 

		Map<Integer, BugzillaSearchHit> hits = new HashMap<Integer, BugzillaSearchHit>();
		
		public void searchCompleted(BugzillaResultCollector collector) {
			for(BugzillaSearchHit hit: collector.getResults()){
		
				// HACK need the server name and handle properly
				addHit(new BugzillaHit(hit.getId() + ": " + hit.getDescription(), hit.getPriority(), hit.getId(), null, hit.getState()));
			}
		}

	}	
		
	public BugzillaQueryCategory(String label, String url, String maxHits) {
		this.description = label;
		this.queryString = url;
		try{
			this.maxHits = Integer.parseInt(maxHits);
		} catch (Exception e){
			this.maxHits = -1;
		}
	}

	public String getDescription(boolean label) {
		if (hits.size() > 0 || !label) {
			if(!hasBeenRefreshed && label){
				return description + " <needs refresh>";	
			}else if(isMaxReached && label){
				return description + " <first "+ maxHits +" hits>";
			} else {
				return description;
			}
		} else if (!hasBeenRefreshed) {
			return description + " <needs refresh>";
		} else {
			return description + " <no hits>";
		}
	}
	
	
	public Image getIcon() {
		return TaskListImages.getImage(BugzillaImages.CATEGORY_QUERY);
	}
	
	public String getQueryString() {
		return queryString;
	}
	
	
	
	public List<IQueryHit> getChildren() {
		return hits;
	}
	
	public void addHit(IQueryHit hit) {
		BugzillaTask task = BugzillaUiPlugin.getDefault().getBugzillaTaskListManager().getFromBugzillaTaskRegistry(hit.getHandle());
		hit.setAssociatedTask(task);
		hits.add(hit);
	}

	public void removeHit(BugzillaHit hit) {
		hits.remove(hit);
	}
	
	public void refreshBugs() {
		hits.clear();
		final BugzillaCategorySearchOperation catSearch = new BugzillaCategorySearchOperation(
				getQueryString(), maxHits);
		catSearch.addResultsListener(listener);
		final IStatus[] status = new IStatus[1];

		try {
			// execute the search operation
			catSearch.execute(new NullProgressMonitor());
			isMaxReached = catSearch.isMaxReached();
			hasBeenRefreshed = true;
			lastRefresh = new Date();

			// get the status of the search operation
			status[0] = catSearch.getStatus();

			// determine if there was an error, if it was cancelled, or if it is
			// ok<commit_msg>make sure that the tasklist is refreshed so that it shows that there are currently no hits<commit_after>/*******************************************************************************
 * Copyright (c) 2004 - 2005 University Of British Columbia and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylar.bugzilla.ui.tasklist;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.security.auth.login.LoginException;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.mylar.bugzilla.core.BugzillaPlugin;
import org.eclipse.mylar.bugzilla.core.IBugzillaConstants;
import org.eclipse.mylar.bugzilla.core.search.BugzillaSearchHit;
import org.eclipse.mylar.bugzilla.ui.BugzillaImages;
import org.eclipse.mylar.bugzilla.ui.BugzillaUiPlugin;
import org.eclipse.mylar.bugzilla.ui.search.BugzillaResultCollector;
import org.eclipse.mylar.bugzilla.ui.tasklist.BugzillaCategorySearchOperation.ICategorySearchListener;
import org.eclipse.mylar.tasklist.IQuery;
import org.eclipse.mylar.tasklist.IQueryHit;
import org.eclipse.mylar.tasklist.ITaskListElement;
import org.eclipse.mylar.tasklist.TaskListImages;
import org.eclipse.mylar.tasklist.ui.views.TaskListView;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;

/**
 * @author <NAME>
 */
public class BugzillaQueryCategory implements IQuery {
	
	private static final long serialVersionUID = 5517146402031743253L;	
	private String queryString;
	private int maxHits;
	private List<IQueryHit> hits = new ArrayList<IQueryHit>();
	private boolean hasBeenRefreshed = false;
	
	protected Date lastRefresh;
	
	protected String description = "";
	private String handle = "";
	
	private ICategorySearchListener listener = new BugzillaQueryCategorySearchListener();
	private boolean isMaxReached = false;
	
	public class BugzillaQueryCategorySearchListener implements ICategorySearchListener { 

		Map<Integer, BugzillaSearchHit> hits = new HashMap<Integer, BugzillaSearchHit>();
		
		public void searchCompleted(BugzillaResultCollector collector) {
			for(BugzillaSearchHit hit: collector.getResults()){
		
				// HACK need the server name and handle properly
				addHit(new BugzillaHit(hit.getId() + ": " + hit.getDescription(), hit.getPriority(), hit.getId(), null, hit.getState()));
			}
		}

	}	
		
	public BugzillaQueryCategory(String label, String url, String maxHits) {
		this.description = label;
		this.queryString = url;
		try{
			this.maxHits = Integer.parseInt(maxHits);
		} catch (Exception e){
			this.maxHits = -1;
		}
	}

	public String getDescription(boolean label) {
		if (hits.size() > 0 || !label) {
			if(!hasBeenRefreshed && label){
				return description + " <needs refresh>";	
			}else if(isMaxReached && label){
				return description + " <first "+ maxHits +" hits>";
			} else {
				return description;
			}
		} else if (!hasBeenRefreshed) {
			return description + " <needs refresh>";
		} else {
			return description + " <no hits>";
		}
	}
	
	
	public Image getIcon() {
		return TaskListImages.getImage(BugzillaImages.CATEGORY_QUERY);
	}
	
	public String getQueryString() {
		return queryString;
	}
	
	
	
	public List<IQueryHit> getChildren() {
		return hits;
	}
	
	public void addHit(IQueryHit hit) {
		BugzillaTask task = BugzillaUiPlugin.getDefault().getBugzillaTaskListManager().getFromBugzillaTaskRegistry(hit.getHandle());
		hit.setAssociatedTask(task);
		hits.add(hit);
	}

	public void removeHit(BugzillaHit hit) {
		hits.remove(hit);
	}
	
	public void refreshBugs() {
		hits.clear();
		// refresh the view to show that the results are gone
		Display.getDefault().asyncExec(new Runnable(){
			public void run() {
				if(TaskListView.getDefault() != null)
					TaskListView.getDefault().getViewer().refresh();
			}
		});
		final BugzillaCategorySearchOperation catSearch = new BugzillaCategorySearchOperation(
				getQueryString(), maxHits);
		catSearch.addResultsListener(listener);
		final IStatus[] status = new IStatus[1];

		try {
			// execute the search operation
			catSearch.execute(new NullProgressMonitor());
			isMaxReached = catSearch.isMaxReached();
			hasBeenRefreshed = true;
			lastRefresh = new Date();

			// get the status of the search operation
			status[0] = catSearch.getStatus();

			// determine if there was an error, if it was cancelled, or if it is
			// ok
{'fraction_non_alphanumeric': 0.1335662180211651, 'fraction_numerical': 0.0071920271242165825, 'mean_word_length': 4.804412641621944, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 25, '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': '6992808', 'n_tokens_mistral': 3208, 'n_tokens_neox': 2832, 'n_words': 683}