From 634a7772787857bf3b1534fb35d042393bce0dad Mon Sep 17 00:00:00 2001 From: Daniel Sikeler Date: Wed, 25 Mar 2020 15:12:26 +0100 Subject: [PATCH] trac#30220 --- .../itd51/wollmux/core/util/PropertyName.java | 2 + .../mailmerge/print/OOoBasedMailMerge.java | 80 ++++++++++++++++--- 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/muenchen/allg/itd51/wollmux/core/util/PropertyName.java b/src/main/java/de/muenchen/allg/itd51/wollmux/core/util/PropertyName.java index cd2f0f992..70daccb6f 100644 --- a/src/main/java/de/muenchen/allg/itd51/wollmux/core/util/PropertyName.java +++ b/src/main/java/de/muenchen/allg/itd51/wollmux/core/util/PropertyName.java @@ -6,6 +6,8 @@ public final class PropertyName { + public static final String BOOKMARK_CONDITION = "BookmarkCondition"; + public static final String BOOKMARK_HIDDEN = "BookmarkHidden"; public static final String CHAR_BACK_COLOR = "CharBackColor"; public static final String CHAR_FONT_NAME = "CharFontName"; public static final String CHAR_HEIGHT = "CharHeight"; diff --git a/src/main/java/de/muenchen/allg/itd51/wollmux/mailmerge/print/OOoBasedMailMerge.java b/src/main/java/de/muenchen/allg/itd51/wollmux/mailmerge/print/OOoBasedMailMerge.java index 7efe6aab1..5052780f2 100644 --- a/src/main/java/de/muenchen/allg/itd51/wollmux/mailmerge/print/OOoBasedMailMerge.java +++ b/src/main/java/de/muenchen/allg/itd51/wollmux/mailmerge/print/OOoBasedMailMerge.java @@ -12,6 +12,7 @@ import java.util.List; import java.util.Map; import java.util.Random; +import java.util.function.Predicate; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -24,6 +25,8 @@ 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.container.XEnumeration; @@ -32,6 +35,7 @@ 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; @@ -59,6 +63,7 @@ import de.muenchen.allg.afid.UnoHelperException; import de.muenchen.allg.afid.UnoProps; import de.muenchen.allg.itd51.wollmux.XPrintModel; +import de.muenchen.allg.itd51.wollmux.core.document.Bookmark; import de.muenchen.allg.itd51.wollmux.core.document.FormFieldFactory; import de.muenchen.allg.itd51.wollmux.core.document.FormFieldFactory.FormField; import de.muenchen.allg.itd51.wollmux.core.document.FormFieldFactory.FormFieldType; @@ -70,6 +75,9 @@ import de.muenchen.allg.itd51.wollmux.core.document.commands.DocumentCommand; import de.muenchen.allg.itd51.wollmux.core.document.commands.DocumentCommand.InsertFormValue; import de.muenchen.allg.itd51.wollmux.core.document.commands.DocumentCommands; +import de.muenchen.allg.itd51.wollmux.core.parser.ConfigThingy; +import de.muenchen.allg.itd51.wollmux.core.parser.NodeNotFoundException; +import de.muenchen.allg.itd51.wollmux.core.parser.SyntaxErrorException; import de.muenchen.allg.itd51.wollmux.core.util.L; import de.muenchen.allg.itd51.wollmux.core.util.PropertyName; import de.muenchen.allg.itd51.wollmux.core.util.Utils; @@ -379,7 +387,7 @@ private void createAndAdjustInputFile() throws PrintException * 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)); @@ -445,9 +453,8 @@ 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. @@ -462,24 +469,34 @@ 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(); XNameAccess xna = UNO.XBookmarksSupplier(tmpDoc).getBookmarks(); for (String name : xna.getElementNames()) { - try - { - XTextContent bookmark = UNO.XTextContent(xna.getByName(name)); - if (bookmark != null) + try + { + if (setGroups.test(name)) { - bookmark.getAnchor().getText().removeTextContent(bookmark); + updateSetGroupsBookmark(tmpDoc, name); + } else + { + XTextContent bookmark = UNO.XTextContent(xna.getByName(name)); + if (bookmark != null) + { + bookmark.getAnchor().getText().removeTextContent(bookmark); + } } } catch (NoSuchElementException e) @@ -494,6 +511,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<>(); + + UNO.setPropertyToDefault(bookmark.getAnchor(), PropertyName.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(PropertyName.BOOKMARK_HIDDEN, true); + ps.setPropertyValue(PropertyName.BOOKMARK_CONDITION, condition); + bookmark.rename(StringUtils.join(names, "_")); + } catch (NodeNotFoundException | java.io.IOException | SyntaxErrorException + | WrappedTargetException | UnknownPropertyException + | PropertyVetoException ex) + { + LOGGER.debug("", ex); + } + } + /** * Replace all insertFormValue-Bookmarks with mail merge fields. *