ASF Bugzilla – Attachment 36687 Details for
Bug 63618
Support changing MailMerge ConnectString
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
Mail merge editing example
MailMergeHelper.java (text/x-csrc), 3.12 KB, created by
Rabea Gransberger
on 2019-07-29 15:53:08 UTC
(
hide
)
Description:
Mail merge editing example
Filename:
MIME Type:
Creator:
Rabea Gransberger
Created:
2019-07-29 15:53:08 UTC
Size:
3.12 KB
patch
obsolete
>/* > * @(#)MailMergeHelper.java > * =========================================================================== > * (C) Copyright MEKO-S GmbH 2019 > * =========================================================================== > * Created on Jul 29, 2019 > */ > > >import java.io.File; >import java.io.IOException; >import java.util.Arrays; >import java.util.List; > >import org.apache.poi.ooxml.POIXMLDocumentPart; >import org.apache.poi.openxml4j.opc.PackageRelationship; >import org.apache.poi.xwpf.usermodel.XWPFDocument; >import org.apache.poi.xwpf.usermodel.XWPFSettings; >import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTMailMerge; >import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSettings; >import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString; > >import de.mekos.tools.StringTools; >import de.mekos.tools.reflect.ReflectionTools; > >/** > * @author Rabea Gransberger > */ >public class MailMergeHelper { > > public static void updateFormLetterWord(XWPFDocument doc, File formLetterInput, > boolean relativePath) throws IOException { > boolean found = false; > List<POIXMLDocumentPart> relations = doc.getRelations(); > for (POIXMLDocumentPart relation : relations) { > if (relation instanceof XWPFSettings) { > found = true; > XWPFSettings settings = (XWPFSettings) relation; > CTSettings s = (CTSettings) ReflectionTools.getFieldValue(settings, "ctSettings"); > CTMailMerge merge = s.getMailMerge(); > > if (merge == null) { > throw new IOException("Mail Merge not enabled in this document"); > } > changeConnectString(formLetterInput, merge.getConnectString()); > changeConnectString(formLetterInput, merge.getOdso().getUdl()); > > final String firstId = merge.getDataSource().getId(); > final String secondId = merge.getOdso().getSrc().getId(); > > PackageRelationship first = settings.getPackagePart().getRelationship(firstId); > PackageRelationship second = settings.getPackagePart().getRelationship(secondId); > > settings.getPackagePart().removeRelationship(first.getId()); > settings.getPackagePart().removeRelationship(second.getId()); > > final String uriString = formLetterInput.toURI().toString(); > > settings.getPackagePart().addExternalRelationship(relativePath ? formLetterInput.getName() > : uriString, first.getRelationshipType(), first.getId()); > settings.getPackagePart().addExternalRelationship(relativePath ? formLetterInput.getName() > : uriString, second.getRelationshipType(), second.getId()); > break; > } > } > if (!found) { > throw new IllegalArgumentException("No settings found"); > } > } > > private static void changeConnectString(File formLetterInput, final CTString connectString) { > final String connectVal = connectString.getVal(); > final String[] splits = connectVal.split(";"); > for (int i = 0; i < splits.length; i++) { > final String split = splits[i]; > if (split.startsWith("Data Source=")) { > splits[i] = "Data Source=" + formLetterInput.getAbsolutePath(); > } > } > final String newConnectValue = StringTools.concat(Arrays.asList(splits), ";"); > connectString.setVal(newConnectValue); > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 63618
: 36687