From ae4e9c6653cc8f6a9316731e05de3d5410910261 Mon Sep 17 00:00:00 2001 From: Daniel Sikeler Date: Wed, 25 Mar 2020 15:12:26 +0100 Subject: [PATCH] trac#30220 use conditional on LO bookmarks The WollMux command 'setGroups' is changed in a conditional on the bookmark. Also we rename the bookmark, so that WollMux doesn't care about it. --- .../mailmerge/print/OOoBasedMailMerge.java | 78 +++++++++++++++++-- 1 file changed, 70 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/de/muenchen/allg/itd51/wollmux/mailmerge/print/OOoBasedMailMerge.java b/core/src/main/java/de/muenchen/allg/itd51/wollmux/mailmerge/print/OOoBasedMailMerge.java index a4ccd6846..019bf431f 100644 --- a/core/src/main/java/de/muenchen/allg/itd51/wollmux/mailmerge/print/OOoBasedMailMerge.java +++ b/core/src/main/java/de/muenchen/allg/itd51/wollmux/mailmerge/print/OOoBasedMailMerge.java @@ -35,6 +35,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Random; +import java.util.function.Predicate; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -47,11 +48,15 @@ import com.sun.star.beans.NamedValue; import com.sun.star.beans.PropertyValue; +import com.sun.star.beans.PropertyVetoException; +import com.sun.star.beans.UnknownPropertyException; import com.sun.star.beans.XPropertySet; +import com.sun.star.container.NoSuchElementException; import com.sun.star.frame.XModel; import com.sun.star.frame.XStorable; import com.sun.star.io.IOException; import com.sun.star.lang.IllegalArgumentException; +import com.sun.star.lang.WrappedTargetException; import com.sun.star.lang.XMultiServiceFactory; import com.sun.star.lang.XSingleServiceFactory; import com.sun.star.sdb.CommandType; @@ -80,6 +85,10 @@ import de.muenchen.allg.afid.UnoDictionary; import de.muenchen.allg.afid.UnoHelperException; import de.muenchen.allg.afid.UnoProps; +import de.muenchen.allg.document.text.Bookmark; +import de.muenchen.allg.itd51.wollmux.config.ConfigThingy; +import de.muenchen.allg.itd51.wollmux.config.NodeNotFoundException; +import de.muenchen.allg.itd51.wollmux.config.SyntaxErrorException; import de.muenchen.allg.itd51.wollmux.dialog.InfoDialog; import de.muenchen.allg.itd51.wollmux.document.DocumentManager; import de.muenchen.allg.itd51.wollmux.document.FormFieldFactory; @@ -357,11 +366,11 @@ private void createAndAdjustInputFile() throws PrintException adjustDatabaseAndInputUserFields(tmpDoc); /* * Bookmarks make LO mail merge slow. So we delete all of the. - * + * * If at some time we need bookmarks at least WollMux document commands have to be removed * so that they are not processed twice. */ - removeAllBookmarks(tmpDoc); + updateBookmarks(tmpDoc); ContentBasedDirectiveModel.createModel(UNO.XTextDocument(tmpDoc)).renameTextStyles(); removeWollMuxMetadata(UNO.XTextDocument(tmpDoc)); @@ -420,7 +429,7 @@ private void updateTextSections(XTextDocument doc) } /** - * Remove all non informational meta data of wollmux from the document. + * Remove all non informational meta data of WollMux from the document. * * @param doc * The document. @@ -437,25 +446,35 @@ private void removeWollMuxMetadata(XTextDocument doc) } /** - * Remove all bookmarks from the document. + * Remove all bookmarks except setGroups-commands from the document. setGroups + * commands are converted, so that they can be interpreted by LibreOffice mail + * merge. * * @param tmpDoc * The document. */ - private void removeAllBookmarks(XTextDocument tmpDoc) + private void updateBookmarks(XTextDocument tmpDoc) { if (UNO.XBookmarksSupplier(tmpDoc) != null) { + Predicate setGroups = DocumentCommands.getPatternForCommand("setGroups").asMatchPredicate(); UnoDictionary bookmarks = UnoDictionary .create(UNO.XBookmarksSupplier(tmpDoc).getBookmarks(), XTextContent.class); - for (XTextContent bookmark : bookmarks.values()) + for (Map.Entry bookmark : bookmarks.entrySet()) { try { - if (bookmark != null) + if (setGroups.test(bookmark.getKey())) { - bookmark.getAnchor().getText().removeTextContent(bookmark); + updateSetGroupsBookmark(tmpDoc, bookmark.getKey()); + } else if (bookmark.getValue() != null) + { + bookmark.getValue().getAnchor().getText().removeTextContent(bookmark.getValue()); } + } + catch (NoSuchElementException e) + { + continue; } catch (Exception e) { LOGGER.error("", e); @@ -464,6 +483,49 @@ private void removeAllBookmarks(XTextDocument tmpDoc) } } + /** + * Sets a condition on the given book mark according to its name. All + * mentioned groups are part of the condition. Renames the book mark. + * + * @param tmpDoc + * The document which has the book mark + * @param name + * The name of the book mark. + * @throws NoSuchElementException + * Couldn't find the book mark. + */ + private void updateSetGroupsBookmark(XTextDocument tmpDoc, String name) + throws NoSuchElementException + { + try + { + Bookmark bookmark = new Bookmark(name, UNO.XBookmarksSupplier(tmpDoc)); + ConfigThingy groups = new ConfigThingy("cmd", name).get("GROUPS"); + List conditions = new ArrayList<>(); + List names = new ArrayList<>(); + + UnoProperty.setPropertyToDefault(bookmark.getAnchor(), UnoProperty.CHAR_HIDDEN); + for (ConfigThingy groupName : groups) + { + conditions.add(String.format("([%s] != \"true\")", + COLUMN_PREFIX_TEXTSECTION + groupName.toString())); + names.add(groupName.toString()); + } + String condition = StringUtils.join(conditions, " or "); + + XPropertySet ps = UNO.XPropertySet( + UNO.XBookmarksSupplier(tmpDoc).getBookmarks().getByName(name)); + ps.setPropertyValue(UnoProperty.BOOKMARK_HIDDEN, true); + ps.setPropertyValue(UnoProperty.BOOKMARK_CONDITION, condition); + bookmark.rename(StringUtils.join(names, "_")); + } catch (NodeNotFoundException | java.io.IOException | SyntaxErrorException + | WrappedTargetException | UnknownPropertyException + | PropertyVetoException | UnoHelperException ex) + { + LOGGER.debug("", ex); + } + } + /** * Replace all insertFormValue-Bookmarks with mail merge fields. *