Commit 6133841b authored by Marco Descher's avatar Marco Descher
Browse files

[16258] Introduce ITextReplacementService

parent 27093007
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" activate="activate" name="ch.elexis.core.services.TextReplacementService">
   <service>
      <provide interface="ch.elexis.core.services.ITextReplacementService"/>
   </service>
   <reference cardinality="0..n" field="placeholderResolvers" interface="ch.elexis.core.text.ITextPlaceholderResolver" name="placeholderResolvers"/>
   <implementation class="ch.elexis.core.services.TextReplacementService"/>
</scr:component>
 No newline at end of file
+7 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="ch.elexis.core.services.internal.text.PatientTextPlaceholderResolver">
   <service>
      <provide interface="ch.elexis.core.text.ITextPlaceholderResolver"/>
   </service>
   <implementation class="ch.elexis.core.services.internal.text.PatientTextPlaceholderResolver"/>
</scr:component>
 No newline at end of file
+7 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="ch.elexis.core.services.internal.text.TerminTextPlaceholderResolver">
   <service>
      <provide interface="ch.elexis.core.text.ITextPlaceholderResolver"/>
   </service>
   <implementation class="ch.elexis.core.services.internal.text.TerminTextPlaceholderResolver"/>
</scr:component>
 No newline at end of file
+62 −0
Original line number Diff line number Diff line
package ch.elexis.core.services;

import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;

import ch.elexis.core.text.ITextPlaceholderResolver;

@Component
public class TextReplacementService implements ITextReplacementService {
	
	@Reference(cardinality = ReferenceCardinality.MULTIPLE)
	private List<ITextPlaceholderResolver> placeholderResolvers;
	
	private static final String DONT_SHOW_REPLACEMENT_ERRORS = "*";
	public static final String MATCH_TEMPLATE = "\\[[" + DONT_SHOW_REPLACEMENT_ERRORS //$NON-NLS-1$
		+ "]?[-a-zA-ZäöüÄÖÜéàè_ ]+\\.[-a-zA-Z0-9äöüÄÖÜéàè_ ]+\\]";
	
	private Pattern matchTemplate;
	
	@Activate
	public void activate(){
		matchTemplate = Pattern.compile(MATCH_TEMPLATE);
	}
	
	@Override
	public String performReplacement(IContext context, String template){
		Matcher matcher = matchTemplate.matcher(template);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			String found = matcher.group();
			matcher.appendReplacement(sb,
				Matcher.quoteReplacement((String) replacePlaceholder(context, found)));
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
	
	private String replacePlaceholder(IContext context, String placeholder){
		String substring = placeholder.substring(1, placeholder.length() - 1);
		String[] split = substring.split("\\.");
		if (split.length > 1) {
			for (ITextPlaceholderResolver resolver : placeholderResolvers) {
				if (resolver.getSupportedType().equalsIgnoreCase(split[0])) {
					Optional<String> result = resolver.replaceByTypeAndAttribute(context,
						substring.substring(split[0].length() + 1));
					if (result.isPresent()) {
						return result.get();
					}
				}
			}
		}
		return "?";
	}
	
}
+69 −0
Original line number Diff line number Diff line
package ch.elexis.core.services.internal.text;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.osgi.service.component.annotations.Component;

import ch.elexis.core.interfaces.ILocalizedEnum;
import ch.elexis.core.model.IPatient;
import ch.elexis.core.services.IContext;
import ch.elexis.core.text.ITextPlaceholderResolver;
import ch.elexis.core.text.PlaceholderAttribute;

@Component
public class PatientTextPlaceholderResolver implements ITextPlaceholderResolver {
	
	@Override
	public String getSupportedType(){
		return "Patient";
	}
	
	@Override
	public List<PlaceholderAttribute> getSupportedAttributes(){
		return Arrays.asList(PatientAttribute.values()).stream()
			.map(m -> new PlaceholderAttribute(getSupportedType(), m.name(), m.getLocaleText()))
			.collect(Collectors.toList());
	}
	
	@Override
	public Optional<String> replaceByTypeAndAttribute(IContext context, String attribute){
		IPatient patient = context.getTyped(IPatient.class).orElse(null);
		if (patient != null) {
			return Optional.ofNullable(replace(patient, attribute.toLowerCase()));
		}
		return Optional.empty();
	}
	
	private String replace(IPatient patient, String lcAttribute){
		
		PatientAttribute patientAttribut = searchEnum(PatientAttribute.class, lcAttribute);
		switch (patientAttribut) {
		case Name:
			return patient.getLastName();
		case Vorname:
			return patient.getFirstName();
		default:
			break;
		}
		return null;
	}
	
	private enum PatientAttribute implements ILocalizedEnum {
			Name("Tag des Termins im Format dd.MM.yyyy"), Vorname("Zugehöriger Bereich");
		
		final String description;
		
		private PatientAttribute(String description){
			this.description = description;
		}
		
		@Override
		public String getLocaleText(){
			return description;
		}
	}
	
}
Loading