<commit_before>package ch.elexis.core.ui.medication.views;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.State;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;

import ch.elexis.core.model.prescription.EntryType;
import ch.elexis.core.ui.medication.handlers.ApplyCustomSortingHandler;
import ch.elexis.data.Artikel;
import ch.elexis.data.Prescription;
import ch.elexis.data.Query;
import ch.rgw.tools.ExHandler;
import ch.rgw.tools.Money;
import ch.rgw.tools.TimeTool;

public class MedicationViewHelper {
	private static final int FILTER_PRESCRIPTION_AFTER_N_DAYS = 30;
	
	public static ViewerSortOrder getSelectedComparator(){
		ICommandService service =
			(ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
		Command command = service.getCommand(ApplyCustomSortingHandler.CMD_ID);
		State state = command.getState(ApplyCustomSortingHandler.STATE_ID);
		
		if ((Boolean) state.getValue()) {
			return ViewerSortOrder.getSortOrderPerValue(ViewerSortOrder.MANUAL.val);
		} else {
			return ViewerSortOrder.getSortOrderPerValue(ViewerSortOrder.DEFAULT.val);
		}
	}
	
	public static String calculateDailyCostAsString(List<Prescription> pres){
		String TTCOST = Messages.FixMediDisplay_DailyCost;
		
		double cost = 0.0;
		boolean canCalculate = true;
		
		for (Prescription pr : pres) {
			float num = Prescription.calculateTagesDosis(pr.getDosis());
			try {
				Artikel art = pr.getArtikel();
				if (art != null) {
					int ve = art.guessVE();
					if (ve != 0) {
						Money price = pr.getArtikel().getVKPreis();
						cost += num * price.getAmount() / ve;
					} else {
						canCalculate = false;
					}
				} else {
					canCalculate = false;
				}
			} catch (Exception ex) {
				ExHandler.handle(ex);
				canCalculate = false;
			}
		}
		
		double rounded = Math.round(100.0 * cost) / 100.0;
		if (canCalculate) {
			return TTCOST +" "+Double.toString(rounded);
		} else {
			if (rounded == 0.0) {
				return TTCOST + " ?";
			} else {
				return TTCOST + " >" + Double.toString(rounded);
			}
		}
	}
	
	/**
	 * <pre>
	 * SELECT * FROM PATIENT_ARTIKEL_JOINT 
	 * WHERE deleted='0' AND PatientId='C7dc8b102d96407ed0632' 
	 * AND (
	 * 	(DateFrom >= '20150922' AND RezeptID is not null)
	 * 	OR
	 * 	# FIXED MEDICATION
	 * 	(RezeptID is null AND DateUntil is null)
	 * )
	 * </pre>
	 * 
	 * @param patId
	 * @return
	 */
	public static List<Prescription> loadInputData(boolean loadFullHistory, String patId){
		if (patId == null)
			return Collections.emptyList();
			
		if (loadFullHistory) {
			return loadAllHistorical(patId);
		}
		return loadNonHistorical(patId);
	}
	
	private static List<Prescription> loadNonHistorical(String patId){
		// make sure just now closed are not included
		Query<Prescription> qbe = new Query<Prescription>(Prescription.class);
		qbe.add(Prescription.FLD_PATIENT_ID, Query.EQUALS, patId);
		List<Prescription> tmpPrescs = qbe.execute();
		// make sure just now closed are not included
		TimeTool now = new TimeTool();
		now.add(TimeTool.SECOND, 5);
		tmpPrescs =
			tmpPrescs.parallelStream().filter(p -> !p.isStopped(now)).collect(Collectors.toList());
		
		List<Prescription> result = new ArrayList<Prescription>();
		for (Prescription p : tmpPrescs) {
			if (p.getArtikel() != null && p.getArtikel().getATC_code() != null) {
				if (p.getArtikel().getATC_code().toUpperCase().startsWith("J07")) {
					continue;
				}
				if (p.getEntryType() == EntryType.RECIPE
					|| p.getEntryType() == EntryType.SELF_DISPENSED) {
					continue;
				}
			}
			
			result.add(p);
		}
		return result;
	}
	
	private static List<Prescription> loadAllHistorical(String patId){
		Query<Prescription> qbe = new Query<Prescription>(Prescription.class);
		qbe.add(Prescription.FLD_PATIENT_ID, Query.EQUALS, patId);
		return qbe.execute();
	}
}<commit_msg>[5645] updated core ui medication loading<commit_after>package ch.elexis.core.ui.medication.views;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.State;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;

import ch.elexis.core.model.prescription.EntryType;
import ch.elexis.core.ui.medication.handlers.ApplyCustomSortingHandler;
import ch.elexis.data.Artikel;
import ch.elexis.data.Patient;
import ch.elexis.data.Prescription;
import ch.elexis.data.Query;
import ch.rgw.tools.ExHandler;
import ch.rgw.tools.Money;

public class MedicationViewHelper {
	private static final int FILTER_PRESCRIPTION_AFTER_N_DAYS = 30;
	
	public static ViewerSortOrder getSelectedComparator(){
		ICommandService service =
			(ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
		Command command = service.getCommand(ApplyCustomSortingHandler.CMD_ID);
		State state = command.getState(ApplyCustomSortingHandler.STATE_ID);
		
		if ((Boolean) state.getValue()) {
			return ViewerSortOrder.getSortOrderPerValue(ViewerSortOrder.MANUAL.val);
		} else {
			return ViewerSortOrder.getSortOrderPerValue(ViewerSortOrder.DEFAULT.val);
		}
	}
	
	public static String calculateDailyCostAsString(List<Prescription> pres){
		String TTCOST = Messages.FixMediDisplay_DailyCost;
		
		double cost = 0.0;
		boolean canCalculate = true;
		
		for (Prescription pr : pres) {
			float num = Prescription.calculateTagesDosis(pr.getDosis());
			try {
				Artikel art = pr.getArtikel();
				if (art != null) {
					int ve = art.guessVE();
					if (ve != 0) {
						Money price = pr.getArtikel().getVKPreis();
						cost += num * price.getAmount() / ve;
					} else {
						canCalculate = false;
					}
				} else {
					canCalculate = false;
				}
			} catch (Exception ex) {
				ExHandler.handle(ex);
				canCalculate = false;
			}
		}
		
		double rounded = Math.round(100.0 * cost) / 100.0;
		if (canCalculate) {
			return TTCOST +" "+Double.toString(rounded);
		} else {
			if (rounded == 0.0) {
				return TTCOST + " ?";
			} else {
				return TTCOST + " >" + Double.toString(rounded);
			}
		}
	}
	
	/**
	 * <pre>
	 * SELECT * FROM PATIENT_ARTIKEL_JOINT 
	 * WHERE deleted='0' AND PatientId='C7dc8b102d96407ed0632' 
	 * AND (
	 * 	(DateFrom >= '20150922' AND RezeptID is not null)
	 * 	OR
	 * 	# FIXED MEDICATION
	 * 	(RezeptID is null AND DateUntil is null)
	 * )
	 * </pre>
	 * 
	 * @param patId
	 * @return
	 */
	public static List<Prescription> loadInputData(boolean loadFullHistory, String patId){
		if (patId == null)
			return Collections.emptyList();
			
		if (loadFullHistory) {
			return loadAllHistorical(patId);
		}
		return loadNonHistorical(patId);
	}
	
	private static List<Prescription> loadNonHistorical(String patId){
		List<Prescription> tmpPrescs = Patient.load(patId).getMedication(null);
		List<Prescription> result = new ArrayList<Prescription>();
		
		for (Prescription p : tmpPrescs) {
			if (p.getEntryType() == EntryType.RECIPE
				|| p.getEntryType() == EntryType.SELF_DISPENSED) {
				continue;
			}
			
			if (p.getArtikel() != null && p.getArtikel().getATC_code() != null) {
				if (p.getArtikel().getATC_code().toUpperCase().startsWith("J07")) {
					continue;
				}
			}
			
			result.add(p);
		}
		return result;
	}
	
	private static List<Prescription> loadAllHistorical(String patId){
		// prefetch the values needed for filter operations
		Query<Prescription> qbe = new Query<Prescription>(Prescription.class, null, null,
			Prescription.TABLENAME, new String[] {
				Prescription.FLD_DATE_UNTIL, Prescription.FLD_REZEPT_ID,
				Prescription.FLD_PRESC_TYPE, Prescription.FLD_ARTICLE
			});
		qbe.add(Prescription.FLD_PATIENT_ID, Query.EQUALS, patId);
		return qbe.execute();
	}
}
{'fraction_non_alphanumeric': 0.1228451027965777, 'fraction_numerical': 0.010854297024645639, 'mean_word_length': 3.8980612883051906, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 32, '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': '538882', 'n_tokens_mistral': 3121, 'n_tokens_neox': 2626, 'n_words': 517}