Commit b81cbb0f authored by Thomas Huster's avatar Thomas Huster

[16427] reworked finding existing text templates

parent 85522fd8
Pipeline #11218 passed with stages
in 2 minutes and 40 seconds
......@@ -17,7 +17,6 @@ import org.eclipse.ui.handlers.HandlerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.elexis.core.constants.StringConstants;
import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.core.data.events.ElexisEvent;
import ch.elexis.core.data.events.ElexisEventDispatcher;
......@@ -27,7 +26,6 @@ import ch.elexis.core.ui.text.MimeTypeUtil;
import ch.elexis.core.ui.views.textsystem.TextTemplateView;
import ch.elexis.core.ui.views.textsystem.model.TextTemplate;
import ch.elexis.data.Brief;
import ch.elexis.data.Query;
import ch.rgw.tools.ExHandler;
public class ImportSelectedTemplateCommand extends AbstractHandler {
......@@ -71,16 +69,16 @@ public class ImportSelectedTemplateCommand extends AbstractHandler {
fis.read(contentToStore);
fis.close();
List<Brief> existing = findExistingEquivalentTemplates(
List<Brief> existing = TextTemplate.findExistingTemplates(
textTemplate.isSystemTemplate(),
textTemplate.getName(),
textTemplate.getName(), (String) null,
textTemplate.getMandant() != null ? textTemplate.getMandant().getId() : "");
if(!existing.isEmpty()) {
if (MessageDialog.openQuestion(HandlerUtil.getActiveShell(event),
"Vorlagen existieren",
String.format(
"Sollen die existierenden %s Vorlagen überschrieben werden?",
textTemplate.getName()))) {
"Sollen die (%d) existierenden %s Vorlagen überschrieben werden?",
existing.size(), textTemplate.getName()))) {
for (Brief brief : existing) {
brief.delete();
}
......@@ -120,23 +118,4 @@ public class ImportSelectedTemplateCommand extends AbstractHandler {
}
return null;
}
private List<Brief> findExistingEquivalentTemplates(boolean isSysTemplate, String name, String mandantId){
Query<Brief> qbe = new Query<Brief>(Brief.class);
qbe.add(Brief.FLD_SUBJECT, Query.EQUALS, name);
qbe.add(Brief.FLD_TYPE, Query.EQUALS, Brief.TEMPLATE);
// treat as system template
if (isSysTemplate) {
qbe.startGroup();
qbe.addToken(Brief.FLD_DESTINATION_ID + " is NULL");
qbe.or();
qbe.add(Brief.FLD_DESTINATION_ID, Query.EQUALS, StringConstants.EMPTY);
qbe.endGroup();
} else {
qbe.add(Brief.FLD_DESTINATION_ID, Query.EQUALS, mandantId);
}
return qbe.execute();
}
}
......@@ -15,7 +15,6 @@ import org.eclipse.ui.PlatformUI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.elexis.core.constants.StringConstants;
import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.core.data.events.ElexisEvent;
import ch.elexis.core.data.events.ElexisEventDispatcher;
......@@ -27,7 +26,6 @@ import ch.elexis.core.ui.views.textsystem.TextTemplateView;
import ch.elexis.core.ui.views.textsystem.model.TextTemplate;
import ch.elexis.data.Brief;
import ch.elexis.data.Mandant;
import ch.elexis.data.Query;
import ch.rgw.tools.ExHandler;
public class ImportTemplatesCommand extends AbstractHandler {
......@@ -75,7 +73,8 @@ public class ImportTemplatesCommand extends AbstractHandler {
// check existence of same template
List<Brief> equivalentTemplates =
findExistingEquivalentTemplates(sysTemplate != null, name, mimeType);
TextTemplate.findExistingTemplates(sysTemplate != null, name, mimeType,
mandantId);
boolean replaceExisting = false;
if (equivalentTemplates != null && !equivalentTemplates.isEmpty()) {
TextTemplateImportConflictDialog ttiConflictDialog =
......@@ -148,26 +147,4 @@ public class ImportTemplatesCommand extends AbstractHandler {
}
return null;
}
private List<Brief> findExistingEquivalentTemplates(boolean isSysTemplate, String name,
String mimeType){
Query<Brief> qbe = new Query<Brief>(Brief.class);
qbe.add(Brief.FLD_SUBJECT, Query.EQUALS, name);
qbe.add(Brief.FLD_MIME_TYPE, Query.EQUALS, mimeType);
qbe.add(Brief.FLD_TYPE, Query.EQUALS, Brief.TEMPLATE);
// treat as system template
if (isSysTemplate) {
qbe.startGroup();
qbe.addToken(Brief.FLD_DESTINATION_ID + " is NULL");
qbe.or();
qbe.add(Brief.FLD_DESTINATION_ID, Query.EQUALS, StringConstants.EMPTY);
qbe.endGroup();
} else {
// treat as form template
qbe.add(Brief.FLD_DESTINATION_ID, Query.EQUALS, mandantId);
}
return qbe.execute();
}
}
......@@ -78,6 +78,7 @@ import ch.elexis.core.ui.dialogs.DocumentSelectDialog;
import ch.elexis.core.ui.dialogs.KontaktSelektor;
import ch.elexis.core.ui.preferences.TextTemplatePreferences;
import ch.elexis.core.ui.util.SWTHelper;
import ch.elexis.core.ui.views.textsystem.model.TextTemplate;
import ch.elexis.data.Brief;
import ch.elexis.data.Fall;
import ch.elexis.data.Konsultation;
......@@ -179,34 +180,9 @@ public class TextContainer {
}
private Brief loadTemplate(String name){
Mandant mandantor = ElexisEventDispatcher.getSelectedMandator();
Query<Brief> qbe = new Query<Brief>(Brief.class);
qbe.add(Brief.FLD_TYPE, Query.EQUALS, Brief.TEMPLATE);
qbe.and();
qbe.add(Brief.FLD_SUBJECT, Query.EQUALS, name);
qbe.startGroup();
if(mandantor != null) {
qbe.add(Brief.FLD_DESTINATION_ID, Query.EQUALS, mandantor.getId());
qbe.or();
}
qbe.add(Brief.FLD_DESTINATION_ID, Query.EQUALS, StringTool.leer);
qbe.endGroup();
List<Brief> list = qbe.execute();
Brief ret = null;
if (list != null && !list.isEmpty()) {
ret = list.get(0);
if (list.size() > 1) {
if(mandantor != null) {
for (Brief brief : list) {
if (mandantor.getId().equals(brief.get(Brief.FLD_DESTINATION_ID))) {
ret = brief;
break;
}
}
}
}
}
return ret;
Mandant mandator = ElexisEventDispatcher.getSelectedMandator();
return TextTemplate.findExistingTemplate(name,
(mandator != null ? mandator.getId() : null));
}
/**
......
package ch.elexis.core.ui.views.textsystem.model;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.core.model.ISticker;
import ch.elexis.core.ui.dialogs.DocumentSelectDialog;
......@@ -8,6 +12,8 @@ import ch.elexis.core.ui.views.textsystem.TextTemplatePrintSettings;
import ch.elexis.data.Brief;
import ch.elexis.data.Kontakt;
import ch.elexis.data.Mandant;
import ch.elexis.data.Query;
import ch.rgw.tools.StringTool;
public class TextTemplate {
private enum UPDATE_TYPE {
......@@ -283,4 +289,80 @@ public class TextTemplate {
setTray(tray);
}
/**
* Find an existing template with matching name. If the mandantId is provided, and a template
* with matching mandantId is found it is preferred to templates without mandantId.
*
* @param name
* @param mandantId
* @return
*/
public static Brief findExistingTemplate(String name, String mandantId){
Query<Brief> qbe = new Query<Brief>(Brief.class);
qbe.add(Brief.FLD_TYPE, Query.EQUALS, Brief.TEMPLATE);
qbe.and();
qbe.add(Brief.FLD_SUBJECT, Query.EQUALS, name);
qbe.startGroup();
if (mandantId != null) {
qbe.add(Brief.FLD_DESTINATION_ID, Query.EQUALS, mandantId);
qbe.or();
}
qbe.add(Brief.FLD_DESTINATION_ID, Query.EQUALS, StringTool.leer);
qbe.endGroup();
List<Brief> list = qbe.execute();
Brief ret = null;
if (list != null && !list.isEmpty()) {
ret = list.get(0);
if (list.size() > 1) {
if (mandantId != null) {
for (Brief brief : list) {
if (mandantId.equals(brief.get(Brief.FLD_DESTINATION_ID))) {
ret = brief;
break;
} else if (ret == null) {
if (StringUtils.isEmpty(brief.get(Brief.FLD_DESTINATION_ID))) {
ret = brief;
}
}
}
}
}
}
return ret;
}
/**
* Find existing templates matching the provided parameters.
*
* @param isSysTemplate
* @param name
* @param mimeType
* @param mandantId
* @return
*/
public static List<Brief> findExistingTemplates(boolean isSysTemplate, String name,
String mimeType, String mandantId){
Query<Brief> qbe = new Query<Brief>(Brief.class);
qbe.add(Brief.FLD_SUBJECT, Query.EQUALS, name);
qbe.add(Brief.FLD_TYPE, Query.EQUALS, Brief.TEMPLATE);
if (mimeType != null) {
qbe.add(Brief.FLD_MIME_TYPE, Query.EQUALS, mimeType);
}
if (isSysTemplate) {
qbe.add(Brief.FLD_KONSULTATION_ID, Query.EQUALS, Brief.SYS_TEMPLATE);
} else {
qbe.add(Brief.FLD_KONSULTATION_ID, Query.NOT_EQUAL, Brief.SYS_TEMPLATE);
}
if (mandantId != null) {
qbe.add(Brief.FLD_DESTINATION_ID, Query.EQUALS, mandantId);
} else {
qbe.startGroup();
qbe.add(Brief.FLD_DESTINATION_ID, Query.EQUALS, StringTool.leer);
qbe.or();
qbe.addToken(Brief.FLD_DESTINATION_ID + " is NULL");
qbe.endGroup();
}
return qbe.execute();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment