Skip to content

Commit

Permalink
trac#30220
Browse files Browse the repository at this point in the history
  • Loading branch information
sikelerd committed Mar 25, 2020
1 parent 6fa78e5 commit 634a777
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
Expand Up @@ -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";
Expand Down
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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.
Expand All @@ -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<String> 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)
Expand All @@ -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<String> conditions = new ArrayList<>();
List<String> 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.
*
Expand Down

0 comments on commit 634a777

Please sign in to comment.