<commit_before>/*******************************************************************************
 * Copyright (c) 2006-2009, <NAME>, <NAME>, <NAME> and Elexis
 * 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:
 *    <NAME> - initial implementation
 *    
 *******************************************************************************/

package ch.elexis.core.ui.actions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.viewers.IFilter;

import ch.elexis.core.text.model.Samdas;
import ch.elexis.data.Konsultation;
import ch.rgw.tools.TimeTool;
import ch.rgw.tools.VersionedResource;

/**
 * Texte früherer Konsultationen asynchron nachladen.
 * 
 * @author Gerry
 * 
 */
public class HistoryLoader extends BackgroundJob {
	StringBuilder sb;
	List<Konsultation> lKons;
	KonsFilter filter;
	IFilter globalFilter;
	
	boolean multiline = false;
	
	public void setFilter(final KonsFilter kf){
		filter = kf;
	}
	
	/*
	 * multine == true: show Konsultation text with newlines
	 */
	public HistoryLoader(final StringBuilder sb, final ArrayList<Konsultation> lKons){
		this(sb, lKons, false);
	}
	
	public HistoryLoader(final StringBuilder sb, final ArrayList<Konsultation> lKons,
		final boolean multiline){
		super(Messages.HistoryLoader_LoadKonsMessage); //$NON-NLS-1$
		this.sb = sb;
		this.lKons = new ArrayList<Konsultation>(lKons);
		this.multiline = multiline;
		this.setPriority(Job.DECORATE);
		this.setUser(false);
	}
	
	@Override
	public IStatus execute(final IProgressMonitor monitor){
		synchronized (lKons) {
			monitor.beginTask(Messages.HistoryLoader_LoadKonsMessage, lKons.size() + 100); //$NON-NLS-1$
			monitor.subTask(Messages.HistoryLoader_Sorting); //$NON-NLS-1$
			if (lKons.isEmpty()) {
				return Status.OK_STATUS;
			}
			Collections.sort(lKons, new Comparator<Konsultation>() {
				TimeTool t1 = new TimeTool();
				TimeTool t2 = new TimeTool();
				
				public int compare(final Konsultation o1, final Konsultation o2){
					if ((o1 == null) || (o2 == null)) {
						return 0;
					}
					t1.set(o1.getDatum());
					t2.set(o2.getDatum());
					if (t1.isBefore(t2)) {
						return 1;
					}
					if (t1.isAfter(t2)) {
						return -1;
					}
					return 0;
				}
			});
			monitor.worked(50);
			
			List<Konsultation> konsList = new ArrayList<Konsultation>(lKons);
			HashSet<Konsultation> hsKons = new HashSet<Konsultation>();
			hsKons.addAll(konsList);
			konsList.clear();
			konsList.addAll(hsKons);
			
			Iterator<Konsultation> it = konsList.iterator();
			sb.append("<form>"); //$NON-NLS-1$
			globalFilter = ObjectFilterRegistry.getInstance().getFilterFor(Konsultation.class);
			while (!monitor.isCanceled()) {
				if (!it.hasNext()) {
					sb.append("</form>"); //$NON-NLS-1$
					result = sb.toString();
					monitor.worked(1);
					monitor.done();
					return Status.OK_STATUS;
				}
				Konsultation k = it.next();
				if (filter != null) {
					if (filter.pass(k) == false) {
						continue;
					}
				}
				if (globalFilter != null) {
					if (globalFilter.select(k) == false) {
						continue;
					}
				}
				VersionedResource vr = k.getEintrag();
				String s = vr.getHead();
				if (s != null) {
					if (s.startsWith("<")) { //$NON-NLS-1$
						Samdas samdas = new Samdas(s);
						s = samdas.getRecordText();
					}
					s = maskHTML(s);
					if (multiline) {
						// TODO use system line separator
						// replace Windows line separator
						s = s.replaceAll("\r\n", "<br/>"); //$NON-NLS-1$ //$NON-NLS-2$
						// replace remaining "manual" line separators
						s = s.replaceAll("\n", "<br/>"); //$NON-NLS-1$ //$NON-NLS-2$
					}
					
				} else {
					s = ""; //$NON-NLS-1$
				}
				String label = maskHTML(k.getLabel());
				// label+="<br/>"+k.getFall().getLabel();
				sb.append("<p><a href=\"") //$NON-NLS-1$
					.append(maskHTML(k.getId())).append("\">") //$NON-NLS-1$
					.append(label).append("</a><br/>") //$NON-NLS-1$
					.append("<span color=\"gruen\">") //$NON-NLS-1$
					.append(maskHTML(k.getFall().getLabel())).append("</span><br/>") //$NON-NLS-1$
					.append(s).append("</p>"); //$NON-NLS-1$
				monitor.worked(1);
				
			}
			sb.setLength(0);
			monitor.done();
		}
		return Status.CANCEL_STATUS;
	}
	
	private String maskHTML(String input){
		String s = input.replaceAll("<", "&lt;"); //$NON-NLS-1$ //$NON-NLS-2$
		s = s.replaceAll(">", "&gt;"); //$NON-NLS-1$ //$NON-NLS-2$
		s = s.replaceAll("&", "&amp;"); //$NON-NLS-1$ //$NON-NLS-2$
		return s;
	}
	
	@Override
	public int getSize(){
		return lKons.size();
	}
	
}
<commit_msg>[4087] sort konsList(by date) for history just before further processing<commit_after>/*******************************************************************************
 * Copyright (c) 2006-2009, <NAME>, <NAME>, <NAME> and Elexis
 * 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:
 *    <NAME> - initial implementation
 *    
 *******************************************************************************/

package ch.elexis.core.ui.actions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.viewers.IFilter;

import ch.elexis.core.text.model.Samdas;
import ch.elexis.data.Konsultation;
import ch.rgw.tools.TimeTool;
import ch.rgw.tools.VersionedResource;

/**
 * Texte früherer Konsultationen asynchron nachladen.
 * 
 * @author Gerry
 * 
 */
public class HistoryLoader extends BackgroundJob {
	StringBuilder sb;
	List<Konsultation> lKons;
	KonsFilter filter;
	IFilter globalFilter;
	
	boolean multiline = false;
	
	public void setFilter(final KonsFilter kf){
		filter = kf;
	}
	
	/*
	 * multine == true: show Konsultation text with newlines
	 */
	public HistoryLoader(final StringBuilder sb, final ArrayList<Konsultation> lKons){
		this(sb, lKons, false);
	}
	
	public HistoryLoader(final StringBuilder sb, final ArrayList<Konsultation> lKons,
		final boolean multiline){
		super(Messages.HistoryLoader_LoadKonsMessage); //$NON-NLS-1$
		this.sb = sb;
		this.lKons = new ArrayList<Konsultation>(lKons);
		this.multiline = multiline;
		this.setPriority(Job.DECORATE);
		this.setUser(false);
	}
	
	@Override
	public IStatus execute(final IProgressMonitor monitor){
		synchronized (lKons) {
			monitor.beginTask(Messages.HistoryLoader_LoadKonsMessage, lKons.size() + 100); //$NON-NLS-1$
			monitor.subTask(Messages.HistoryLoader_Sorting); //$NON-NLS-1$
			if (lKons.isEmpty()) {
				return Status.OK_STATUS;
			}
			
			List<Konsultation> konsList = new ArrayList<Konsultation>(lKons);
			HashSet<Konsultation> hsKons = new HashSet<Konsultation>();
			hsKons.addAll(konsList);
			konsList.clear();
			konsList.addAll(hsKons);
			
			//sort konsList by date
			Collections.sort(konsList, new Comparator<Konsultation>() {
				TimeTool t1 = new TimeTool();
				TimeTool t2 = new TimeTool();
				
				public int compare(final Konsultation o1, final Konsultation o2){
					if ((o1 == null) || (o2 == null)) {
						return 0;
					}
					t1.set(o1.getDatum());
					t2.set(o2.getDatum());
					if (t1.isBefore(t2)) {
						return 1;
					}
					if (t1.isAfter(t2)) {
						return -1;
					}
					return 0;
				}
			});
			monitor.worked(50);
			
			Iterator<Konsultation> it = konsList.iterator();
			sb.append("<form>"); //$NON-NLS-1$
			globalFilter = ObjectFilterRegistry.getInstance().getFilterFor(Konsultation.class);
			while (!monitor.isCanceled()) {
				if (!it.hasNext()) {
					sb.append("</form>"); //$NON-NLS-1$
					result = sb.toString();
					monitor.worked(1);
					monitor.done();
					return Status.OK_STATUS;
				}
				Konsultation k = it.next();
				if (filter != null) {
					if (filter.pass(k) == false) {
						continue;
					}
				}
				if (globalFilter != null) {
					if (globalFilter.select(k) == false) {
						continue;
					}
				}
				VersionedResource vr = k.getEintrag();
				String s = vr.getHead();
				if (s != null) {
					if (s.startsWith("<")) { //$NON-NLS-1$
						Samdas samdas = new Samdas(s);
						s = samdas.getRecordText();
					}
					s = maskHTML(s);
					if (multiline) {
						// TODO use system line separator
						// replace Windows line separator
						s = s.replaceAll("\r\n", "<br/>"); //$NON-NLS-1$ //$NON-NLS-2$
						// replace remaining "manual" line separators
						s = s.replaceAll("\n", "<br/>"); //$NON-NLS-1$ //$NON-NLS-2$
					}
					
				} else {
					s = ""; //$NON-NLS-1$
				}
				String label = maskHTML(k.getLabel());
				// label+="<br/>"+k.getFall().getLabel();
				sb.append("<p><a href=\"") //$NON-NLS-1$
					.append(maskHTML(k.getId())).append("\">") //$NON-NLS-1$
					.append(label).append("</a><br/>") //$NON-NLS-1$
					.append("<span color=\"gruen\">") //$NON-NLS-1$
					.append(maskHTML(k.getFall().getLabel())).append("</span><br/>") //$NON-NLS-1$
					.append(s).append("</p>"); //$NON-NLS-1$
				monitor.worked(1);
				
			}
			sb.setLength(0);
			monitor.done();
		}
		return Status.CANCEL_STATUS;
	}
	
	private String maskHTML(String input){
		String s = input.replaceAll("<", "&lt;"); //$NON-NLS-1$ //$NON-NLS-2$
		s = s.replaceAll(">", "&gt;"); //$NON-NLS-1$ //$NON-NLS-2$
		s = s.replaceAll("&", "&amp;"); //$NON-NLS-1$ //$NON-NLS-2$
		return s;
	}
	
	@Override
	public int getSize(){
		return lKons.size();
	}
	
}

{'fraction_non_alphanumeric': 0.1947477131897315, 'fraction_numerical': 0.012393036293892004, 'mean_word_length': 3.2313774448605908, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 59, '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': '4740454', 'n_tokens_mistral': 4293, 'n_tokens_neox': 3568, 'n_words': 707}