Source Code Cross Referenced for MailMerge.java in  » J2EE » Sofia » com » salmonllc » mailmerge » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » Sofia » com.salmonllc.mailmerge 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        package com.salmonllc.mailmerge;
0002:
0003:        /////////////////////////
0004:        //$Archive: /JADE/SourceCode/com/salmonllc/mailmerge/MailMerge.java $
0005:        //$Author: Srufle $ 
0006:        //$Revision: 21 $ 
0007:        //$Modtime: 7/31/02 6:15p $ 
0008:        /////////////////////////
0009:
0010:        import java.io.*;
0011:        import java.util.*;
0012:        import com.salmonllc.properties.*;
0013:
0014:        /**
0015:         * Mail Merge for Winword on Server
0016:         * Requirements:
0017:         * Need Winword installed on server.
0018:         * Need the following properties in properties file, preferably system.properties
0019:         *		WinWordPath=d:\\Microsoft Office\\Office
0020:         *		WinWordTemplatePath=d:\\Microsoft Office\\Templates
0021:         * WinWordPath is the Path to winword.exe
0022:         * WinWordTemplatePath is the Path to the location of Salmon.dot which is required to be deployed to the server.
0023:         * Deploy Salmon.dot file to the directory specified by WinWordTemplatePath property.
0024:         * Use mailMergePrint to print the mail merged document on the server printer.
0025:         * Use mailMergePrintToFile to print the mail merged document to a file, useful for generating PDF's see below.
0026:         * Use mailMergeSaveAs to save the mail merged document to a doc file, which can be sent back to a browser for client side printing.
0027:         * If you want to print to pdf, you must have Acrobat installed on the server and setup as the default printer and use
0028:         *   mailMergePrintToFile method to generate the PDF which can be sent back to a browser for client side printing.
0029:         * Creation date: (1/11/02 2:53:36 PM)
0030:         * @author: Fred Cahill
0031:         */
0032:        public class MailMerge implements  Runnable {
0033:            private Thread _wakeupthread;
0034:            private MailMergeException _mme;
0035:            private String[] _saCommandArgs;
0036:            private String[] _saEnvironArgs;
0037:            private Process _process;
0038:            private Hashtable _htTextBookmarks = new Hashtable();
0039:            private Hashtable _htPictureBookmarks = new Hashtable();
0040:            private Hashtable _htFileBookmarks = new Hashtable();
0041:            private Hashtable _htOLEBookmarks = new Hashtable();
0042:            private File _fBackground;
0043:            private Float _fLeftMargin;
0044:            private Float _fRightMargin;
0045:            private Float _fTopMargin;
0046:            private Float _fBottomMargin;
0047:            private Float _fGutter;
0048:            private Float _fHeaderDistance;
0049:            private Float _fFooterDistance;
0050:            private Vector _vExtraEnvironArgs = new Vector();
0051:            private String _sPrinter;
0052:
0053:            /**
0054:             * Creates a MailMerge object.
0055:             * Creation date: (3/15/02 10:44:02 AM)
0056:             */
0057:            public MailMerge() {
0058:            }
0059:
0060:            private MailMerge(String[] commandArgs, String[] environArgs,
0061:                    Thread wakeupThread) {
0062:                _saCommandArgs = commandArgs;
0063:                _saEnvironArgs = environArgs;
0064:                _wakeupthread = wakeupThread;
0065:
0066:            }
0067:
0068:            /**
0069:             * Specifies an OLE File to be inserted at a field location in a word template.
0070:             * Creation date: (3/15/02 10:44:02 AM)
0071:             * @param sField java.lang.String Name of a field in a word template file
0072:             * @param fOleFile java.io.File A File object pointing to a OLE File. Examples are .xls,.doc,.ppt
0073:             */
0074:            public void addOLEFileField(String sField, File fOLEFile)
0075:                    throws MailMergeException {
0076:                if (!fOLEFile.exists())
0077:                    throw new MailMergeException("File "
0078:                            + fOLEFile.getAbsolutePath() + " does not exist.");
0079:                _htOLEBookmarks.put(sField, fOLEFile);
0080:            }
0081:
0082:            /**
0083:             * Specifies a Picture File to be inserted at a field location in a word template.
0084:             * Creation date: (3/15/02 10:44:02 AM)
0085:             * @param sField java.lang.String Name of a field in a word template file
0086:             * @param fPicture java.io.File A File object pointing to a Picture File. Examples are .bmp,.gif,.jpg
0087:             */
0088:            public void addPictureField(String sField, File fPicture)
0089:                    throws MailMergeException {
0090:                if (!fPicture.exists())
0091:                    throw new MailMergeException("File "
0092:                            + fPicture.getAbsolutePath() + " does not exist.");
0093:                _htPictureBookmarks.put(sField, fPicture);
0094:            }
0095:
0096:            /**
0097:             * Specifies a Simple File to be inserted at a field location in a word template.
0098:             * Creation date: (3/15/02 10:44:02 AM)
0099:             * @param sField java.lang.String Name of a field in a word template file
0100:             * @param fSimpleFile java.io.File A File object pointing to a Simple File. Examples are .txt
0101:             */
0102:            public void addSimpleFileField(String sField, File fSimpleFile)
0103:                    throws MailMergeException {
0104:                if (!fSimpleFile.exists())
0105:                    throw new MailMergeException("File "
0106:                            + fSimpleFile.getAbsolutePath()
0107:                            + " does not exist.");
0108:                _htFileBookmarks.put(sField, fSimpleFile);
0109:            }
0110:
0111:            /**
0112:             * Specifies text to be inserted at a field location in a word template.
0113:             * Creation date: (3/15/02 10:44:02 AM)
0114:             * @param sField java.lang.String Name of a field in a word template file
0115:             * @param sText java.lang.String The text to be inserted
0116:             */
0117:            public void addTextField(String sField, String sText) {
0118:                _htTextBookmarks.put(sField, sText);
0119:            }
0120:
0121:            /**
0122:             * Concatenates two word files and prints the result.
0123:             * Creation date: (3/15/02 10:44:02 AM)
0124:             * @param sFile1 String Primary word document
0125:             * @param sFile2 String Document to append to primary one.
0126:             */
0127:            public static void concatFilesPrint(String sFile1, String sFile2)
0128:                    throws MailMergeException {
0129:                String sWinWordTemplatePath = Props.getSystemProps()
0130:                        .getProperty(Props.WINWORD_TEMPLATE_PATH);
0131:                String sWinWordPath = Props.getSystemProps().getProperty(
0132:                        Props.WINWORD_PATH);
0133:                if (sWinWordTemplatePath == null)
0134:                    throw new MailMergeException(Props.WINWORD_TEMPLATE_PATH
0135:                            + " is missing from properties file.");
0136:                if (sWinWordPath == null)
0137:                    throw new MailMergeException(Props.WINWORD_PATH
0138:                            + " is missing from properties file.");
0139:                if (!sWinWordPath.endsWith(File.separator))
0140:                    sWinWordPath += File.separator;
0141:                if (!sWinWordTemplatePath.endsWith(File.separator))
0142:                    sWinWordTemplatePath += File.separator;
0143:                File fSalmonTemplate = new File(sWinWordTemplatePath
0144:                        + "Salmon.dot");
0145:                if (!fSalmonTemplate.exists())
0146:                    throw new MailMergeException(
0147:                            "Salmon.dot was not deployed to winword template directory specified by "
0148:                                    + Props.WINWORD_TEMPLATE_PATH
0149:                                    + " in properties file.");
0150:                File fFile1 = new File(sFile1);
0151:                File fFile2 = new File(sFile2);
0152:                if (!fFile1.exists())
0153:                    throw new MailMergeException(sFile1 + " does not exist.");
0154:                if (!fFile2.exists())
0155:                    throw new MailMergeException(sFile2 + " does not exist.");
0156:                String[] saCommandArgs = new String[3];
0157:                String[] saEnvArgs = new String[2];
0158:                saCommandArgs[0] = "\"" + sWinWordPath + "winword\"";
0159:                saCommandArgs[1] = sWinWordTemplatePath + "Salmon.dot";
0160:                saCommandArgs[2] = "-mConcatFiles";
0161:                saEnvArgs[0] = "MMFILE1=" + sFile1;
0162:                saEnvArgs[1] = "MMFILE2=" + sFile2;
0163:                MailMerge mm = new MailMerge(saCommandArgs, saEnvArgs, Thread
0164:                        .currentThread());
0165:                Thread thmm = new Thread(mm);
0166:                thmm.start();
0167:                boolean bSuccessful = false;
0168:                try {
0169:                    Thread.sleep(60000);
0170:                } catch (Exception e) {
0171:                    if (mm.getMailMergeException() == null)
0172:                        bSuccessful = true;
0173:                }
0174:                if (!bSuccessful) {
0175:                    Process pMm = mm.getMailMergeProcess();
0176:                    if (pMm != null)
0177:                        pMm.destroy();
0178:                    throw mm.getMailMergeException();
0179:                }
0180:            }
0181:
0182:            /**
0183:             * Concatenates two word files and prints the result to a file.
0184:             * Creation date: (3/15/02 10:44:02 AM)
0185:             * @param sFile1 String Primary word document
0186:             * @param sFile2 String Document to append to primary one.
0187:             * @param sPrintFileName String Name of the generated print file.
0188:             */
0189:            public static void concatFilesPrintToFile(String sFile1,
0190:                    String sFile2, String sPrintFileName)
0191:                    throws MailMergeException {
0192:                String sWinWordTemplatePath = Props.getSystemProps()
0193:                        .getProperty(Props.WINWORD_TEMPLATE_PATH);
0194:                String sWinWordPath = Props.getSystemProps().getProperty(
0195:                        Props.WINWORD_PATH);
0196:                if (sWinWordTemplatePath == null)
0197:                    throw new MailMergeException(Props.WINWORD_TEMPLATE_PATH
0198:                            + " is missing from properties file.");
0199:                if (sWinWordPath == null)
0200:                    throw new MailMergeException(Props.WINWORD_PATH
0201:                            + " is missing from properties file.");
0202:                if (!sWinWordPath.endsWith(File.separator))
0203:                    sWinWordPath += File.separator;
0204:                if (!sWinWordTemplatePath.endsWith(File.separator))
0205:                    sWinWordTemplatePath += File.separator;
0206:                File fSalmonTemplate = new File(sWinWordTemplatePath
0207:                        + "Salmon.dot");
0208:                if (!fSalmonTemplate.exists())
0209:                    throw new MailMergeException(
0210:                            "Salmon.dot was not deployed to winword template directory specified by "
0211:                                    + Props.WINWORD_TEMPLATE_PATH
0212:                                    + " in properties file.");
0213:                File fFile1 = new File(sFile1);
0214:                File fFile2 = new File(sFile2);
0215:                if (!fFile1.exists())
0216:                    throw new MailMergeException(sFile1 + " does not exist.");
0217:                if (!fFile2.exists())
0218:                    throw new MailMergeException(sFile2 + " does not exist.");
0219:                String[] saCommandArgs = new String[3];
0220:                String[] saEnvArgs = new String[3];
0221:                saCommandArgs[0] = "\"" + sWinWordPath + "winword\"";
0222:                saCommandArgs[1] = sWinWordTemplatePath + "Salmon.dot";
0223:                saCommandArgs[2] = "-mConcatFiles";
0224:                saEnvArgs[0] = "MMFILE1=" + sFile1;
0225:                saEnvArgs[1] = "MMFILE2=" + sFile2;
0226:                saEnvArgs[2] = "MMPRINTFILE=" + sPrintFileName;
0227:                MailMerge mm = new MailMerge(saCommandArgs, saEnvArgs, Thread
0228:                        .currentThread());
0229:                Thread thmm = new Thread(mm);
0230:                thmm.start();
0231:                boolean bSuccessful = false;
0232:                try {
0233:                    Thread.sleep(60000);
0234:                } catch (Exception e) {
0235:                    if (mm.getMailMergeException() == null)
0236:                        bSuccessful = true;
0237:                }
0238:                if (!bSuccessful) {
0239:                    Process pMm = mm.getMailMergeProcess();
0240:                    if (pMm != null)
0241:                        pMm.destroy();
0242:                    throw mm.getMailMergeException();
0243:                }
0244:            }
0245:
0246:            /**
0247:             * Concatenates two word files and saves the result to a file.
0248:             * Creation date: (3/15/02 10:44:02 AM)
0249:             * @param sFile1 String Primary word document
0250:             * @param sFile2 String Document to append to primary one.
0251:             * @param sSaveAs String Name of the generated file.
0252:             */
0253:            public static void concatFilesSaveAs(String sFile1, String sFile2,
0254:                    String sSaveAsFileName) throws MailMergeException {
0255:                String sWinWordTemplatePath = Props.getSystemProps()
0256:                        .getProperty(Props.WINWORD_TEMPLATE_PATH);
0257:                String sWinWordPath = Props.getSystemProps().getProperty(
0258:                        Props.WINWORD_PATH);
0259:                if (sWinWordTemplatePath == null)
0260:                    throw new MailMergeException(Props.WINWORD_TEMPLATE_PATH
0261:                            + " is missing from properties file.");
0262:                if (sWinWordPath == null)
0263:                    throw new MailMergeException(Props.WINWORD_PATH
0264:                            + " is missing from properties file.");
0265:                if (!sWinWordPath.endsWith(File.separator))
0266:                    sWinWordPath += File.separator;
0267:                if (!sWinWordTemplatePath.endsWith(File.separator))
0268:                    sWinWordTemplatePath += File.separator;
0269:                File fSalmonTemplate = new File(sWinWordTemplatePath
0270:                        + "Salmon.dot");
0271:                if (!fSalmonTemplate.exists())
0272:                    throw new MailMergeException(
0273:                            "Salmon.dot was not deployed to winword template directory specified by "
0274:                                    + Props.WINWORD_TEMPLATE_PATH
0275:                                    + " in properties file.");
0276:                File fFile1 = new File(sFile1);
0277:                File fFile2 = new File(sFile2);
0278:                if (!fFile1.exists())
0279:                    throw new MailMergeException(sFile1 + " does not exist.");
0280:                if (!fFile2.exists())
0281:                    throw new MailMergeException(sFile2 + " does not exist.");
0282:                String[] saCommandArgs = new String[3];
0283:                String[] saEnvArgs = new String[3];
0284:                saCommandArgs[0] = "\"" + sWinWordPath + "winword\"";
0285:                saCommandArgs[1] = sWinWordTemplatePath + "Salmon.dot";
0286:                saCommandArgs[2] = "-mConcatFiles";
0287:                saEnvArgs[0] = "MMFILE1=" + sFile1;
0288:                saEnvArgs[1] = "MMFILE2=" + sFile2;
0289:                saEnvArgs[2] = "MMSAVEAS=" + sSaveAsFileName;
0290:                MailMerge mm = new MailMerge(saCommandArgs, saEnvArgs, Thread
0291:                        .currentThread());
0292:                Thread thmm = new Thread(mm);
0293:                thmm.start();
0294:                boolean bSuccessful = false;
0295:                try {
0296:                    Thread.sleep(60000);
0297:                } catch (Exception e) {
0298:                    if (mm.getMailMergeException() == null)
0299:                        bSuccessful = true;
0300:                }
0301:                if (!bSuccessful) {
0302:                    Process pMm = mm.getMailMergeProcess();
0303:                    if (pMm != null)
0304:                        pMm.destroy();
0305:                    throw mm.getMailMergeException();
0306:                }
0307:            }
0308:
0309:            private MailMergeException getMailMergeException() {
0310:                return _mme;
0311:            }
0312:
0313:            private Process getMailMergeProcess() {
0314:                return _process;
0315:            }
0316:
0317:            /**
0318:             * Mail Merges a template file with the values in DataSource File and prints the result.
0319:             * Creation date: (3/15/02 10:44:02 AM)
0320:             * @param sTemplateFileName java.lang.String Name of a word template file
0321:             * @param sDataSourceFileName java.lang.String Name of a DataSoure File usually a .csv file.
0322:             */
0323:            public static void mailMergePrint(String sTemplateFileName,
0324:                    String sDataSourceFileName) throws MailMergeException {
0325:                mailMergePrint(sTemplateFileName, sDataSourceFileName, null);
0326:            }
0327:
0328:            /**
0329:             * Mail Merges a template file with the values in DataSource File and a Hashtable and prints the result.
0330:             * Creation date: (3/15/02 10:44:02 AM)
0331:             * @param sTemplateFileName java.lang.String Name of a word template file
0332:             * @param sDataSourceFileName java.lang.String Name of a DataSoure File usually a .csv file.
0333:             * @param htFields java.util.Hashtable A hashtable of field names & values to be used to populate template.
0334:             */
0335:            public static void mailMergePrint(String sTemplateFileName,
0336:                    String sDataSourceFileName, Hashtable htFields)
0337:                    throws MailMergeException {
0338:                String sWinWordTemplatePath = Props.getSystemProps()
0339:                        .getProperty(Props.WINWORD_TEMPLATE_PATH);
0340:                String sWinWordPath = Props.getSystemProps().getProperty(
0341:                        Props.WINWORD_PATH);
0342:                if (sWinWordTemplatePath == null)
0343:                    throw new MailMergeException(Props.WINWORD_TEMPLATE_PATH
0344:                            + " is missing from properties file.");
0345:                if (sWinWordPath == null)
0346:                    throw new MailMergeException(Props.WINWORD_PATH
0347:                            + " is missing from properties file.");
0348:                if (!sWinWordPath.endsWith(File.separator))
0349:                    sWinWordPath += File.separator;
0350:                if (!sWinWordTemplatePath.endsWith(File.separator))
0351:                    sWinWordTemplatePath += File.separator;
0352:                File fSalmonTemplate = new File(sWinWordTemplatePath
0353:                        + "Salmon.dot");
0354:                if (!fSalmonTemplate.exists())
0355:                    throw new MailMergeException(
0356:                            "Salmon.dot was not deployed to winword template directory specified by "
0357:                                    + Props.WINWORD_TEMPLATE_PATH
0358:                                    + " in properties file.");
0359:                File fTemplate = new File(sTemplateFileName);
0360:                File fDatasource = null;
0361:                if (sDataSourceFileName != null
0362:                        && !sDataSourceFileName.trim().equals(""))
0363:                    fDatasource = new File(sDataSourceFileName);
0364:                if (!fTemplate.exists())
0365:                    throw new MailMergeException(sTemplateFileName
0366:                            + " does not exist.");
0367:                if (fDatasource != null && !fDatasource.exists())
0368:                    throw new MailMergeException(sDataSourceFileName
0369:                            + " does not exist.");
0370:                Vector vBookmarks = new Vector();
0371:                Vector vBookmarkValues = new Vector();
0372:                if (htFields != null) {
0373:                    Enumeration enumBookmarks = htFields.keys();
0374:                    int i = 1;
0375:                    while (enumBookmarks.hasMoreElements()) {
0376:                        String sBookmark = (String) enumBookmarks.nextElement();
0377:                        String sValue = (String) htFields.get(sBookmark);
0378:                        vBookmarks.addElement("MMBOOKMARK" + i + "="
0379:                                + sBookmark);
0380:                        vBookmarkValues.addElement("MMBOOKMARKVALUE" + i + "="
0381:                                + sValue);
0382:                        i++;
0383:                    }
0384:                }
0385:                int iStartIndex = fDatasource == null ? 1 : 2;
0386:                String[] saCommandArgs = new String[3];
0387:                String[] saEnvArgs = new String[iStartIndex
0388:                        + (vBookmarks.size() * 2)];
0389:                saCommandArgs[0] = "\"" + sWinWordPath + "winword\"";
0390:                saCommandArgs[1] = sWinWordTemplatePath + "Salmon.dot";
0391:                saCommandArgs[2] = "-mMailMerge";
0392:                if (fDatasource != null)
0393:                    saEnvArgs[0] = "MMDATASOURCE=" + sDataSourceFileName;
0394:                saEnvArgs[iStartIndex - 1] = "MMTEMPLATE=" + sTemplateFileName;
0395:                for (int k = iStartIndex; k < saEnvArgs.length; k += 2) {
0396:                    saEnvArgs[k] = (String) vBookmarks
0397:                            .elementAt((k - iStartIndex) / 2);
0398:                    saEnvArgs[k + 1] = (String) vBookmarkValues
0399:                            .elementAt((k - iStartIndex) / 2);
0400:                }
0401:                MailMerge mm = new MailMerge(saCommandArgs, saEnvArgs, Thread
0402:                        .currentThread());
0403:                Thread thmm = new Thread(mm);
0404:                thmm.start();
0405:                boolean bSuccessful = false;
0406:                try {
0407:                    Thread.sleep(60000);
0408:                } catch (Exception e) {
0409:                    if (mm.getMailMergeException() == null)
0410:                        bSuccessful = true;
0411:                }
0412:                if (!bSuccessful) {
0413:                    Process pMm = mm.getMailMergeProcess();
0414:                    if (pMm != null)
0415:                        pMm.destroy();
0416:                    throw mm.getMailMergeException();
0417:                }
0418:            }
0419:
0420:            /**
0421:             * Mail Merges a template file with the values in the Hastable and prints the result.
0422:             * Creation date: (3/15/02 10:44:02 AM)
0423:             * @param sTemplateFileName java.lang.String Name of a word template file
0424:             * @param htFields java.util.Hashtable A hashtable of field names & values to be used to populate template.
0425:             */
0426:            public static void mailMergePrint(String sTemplateFileName,
0427:                    Hashtable htFields) throws MailMergeException {
0428:                mailMergePrint(sTemplateFileName, null, htFields);
0429:            }
0430:
0431:            /**
0432:             * Mail Merges a template file with the values in DataSource File and prints the result to a file.
0433:             * Creation date: (3/15/02 10:44:02 AM)
0434:             * @param sTemplateFileName java.lang.String Name of a word template file
0435:             * @param sDataSourceFileName java.lang.String Name of a DataSoure File usually a .csv file.
0436:             * @param sPrintFileName java.lang.String Name of the printed file output
0437:             */
0438:            public static void mailMergePrintToFile(String sTemplateFileName,
0439:                    String sDataSourceFileName, String sPrintFileName)
0440:                    throws MailMergeException {
0441:                mailMergePrintToFile(sTemplateFileName, sDataSourceFileName,
0442:                        sPrintFileName, null);
0443:            }
0444:
0445:            /**
0446:             * Mail Merges a template file with the values in DataSource File and Hashtable and prints the result to a file.
0447:             * Creation date: (3/15/02 10:44:02 AM)
0448:             * @param sTemplateFileName java.lang.String Name of a word template file
0449:             * @param sDataSourceFileName java.lang.String Name of a DataSoure File usually a .csv file.
0450:             * @param sPrintFileName java.lang.String Name of the printed file output
0451:             * @param htFields java.util.Hashtable A hashtable of field names & values to be used to populate template.
0452:             */
0453:            public static void mailMergePrintToFile(String sTemplateFileName,
0454:                    String sDataSourceFileName, String sPrintFileName,
0455:                    Hashtable htFields) throws MailMergeException {
0456:                String sWinWordTemplatePath = Props.getSystemProps()
0457:                        .getProperty(Props.WINWORD_TEMPLATE_PATH);
0458:                String sWinWordPath = Props.getSystemProps().getProperty(
0459:                        Props.WINWORD_PATH);
0460:                if (sWinWordTemplatePath == null)
0461:                    throw new MailMergeException(Props.WINWORD_TEMPLATE_PATH
0462:                            + " is missing from properties file.");
0463:                if (sWinWordPath == null)
0464:                    throw new MailMergeException(Props.WINWORD_PATH
0465:                            + " is missing from properties file.");
0466:                if (!sWinWordPath.endsWith(File.separator))
0467:                    sWinWordPath += File.separator;
0468:                if (!sWinWordTemplatePath.endsWith(File.separator))
0469:                    sWinWordTemplatePath += File.separator;
0470:                File fSalmonTemplate = new File(sWinWordTemplatePath
0471:                        + "Salmon.dot");
0472:                if (!fSalmonTemplate.exists())
0473:                    throw new MailMergeException(
0474:                            "Salmon.dot was not deployed to winword template directory specified by "
0475:                                    + Props.WINWORD_TEMPLATE_PATH
0476:                                    + " in properties file.");
0477:                File fTemplate = new File(sTemplateFileName);
0478:                File fDatasource = null;
0479:                if (sDataSourceFileName != null
0480:                        && !sDataSourceFileName.trim().equals(""))
0481:                    fDatasource = new File(sDataSourceFileName);
0482:                if (!fTemplate.exists())
0483:                    throw new MailMergeException(sTemplateFileName
0484:                            + " does not exist.");
0485:                if (fDatasource != null && !fDatasource.exists())
0486:                    throw new MailMergeException(sDataSourceFileName
0487:                            + " does not exist.");
0488:                Vector vBookmarks = new Vector();
0489:                Vector vBookmarkValues = new Vector();
0490:                if (htFields != null) {
0491:                    Enumeration enumBookmarks = htFields.keys();
0492:                    int i = 1;
0493:                    while (enumBookmarks.hasMoreElements()) {
0494:                        String sBookmark = (String) enumBookmarks.nextElement();
0495:                        String sValue = (String) htFields.get(sBookmark);
0496:                        vBookmarks.addElement("MMBOOKMARK" + i + "="
0497:                                + sBookmark);
0498:                        vBookmarkValues.addElement("MMBOOKMARKVALUE" + i + "="
0499:                                + sValue);
0500:                        i++;
0501:                    }
0502:                }
0503:                int iStartIndex = fDatasource == null ? 2 : 3;
0504:                String[] saCommandArgs = new String[3];
0505:                String[] saEnvArgs = new String[iStartIndex
0506:                        + (vBookmarks.size() * 2)];
0507:                saCommandArgs[0] = "\"" + sWinWordPath + "winword\"";
0508:                saCommandArgs[1] = sWinWordTemplatePath + "Salmon.dot";
0509:                saCommandArgs[2] = "-mMailMerge";
0510:                if (fDatasource != null)
0511:                    saEnvArgs[0] = "MMDATASOURCE=" + sDataSourceFileName;
0512:                saEnvArgs[iStartIndex - 2] = "MMPRINTFILE=" + sPrintFileName;
0513:                saEnvArgs[iStartIndex - 1] = "MMTEMPLATE=" + sTemplateFileName;
0514:                for (int k = iStartIndex; k < saEnvArgs.length; k += 2) {
0515:                    saEnvArgs[k] = (String) vBookmarks
0516:                            .elementAt((k - iStartIndex) / 2);
0517:                    saEnvArgs[k + 1] = (String) vBookmarkValues
0518:                            .elementAt((k - iStartIndex) / 2);
0519:                }
0520:                MailMerge mm = new MailMerge(saCommandArgs, saEnvArgs, Thread
0521:                        .currentThread());
0522:                Thread thmm = new Thread(mm);
0523:                thmm.start();
0524:                boolean bSuccessful = false;
0525:                try {
0526:                    Thread.sleep(60000);
0527:                } catch (Exception e) {
0528:                    if (mm.getMailMergeException() == null)
0529:                        bSuccessful = true;
0530:                }
0531:                if (!bSuccessful) {
0532:                    Process pMm = mm.getMailMergeProcess();
0533:                    if (pMm != null)
0534:                        pMm.destroy();
0535:                    throw mm.getMailMergeException();
0536:                }
0537:            }
0538:
0539:            /**
0540:             * Mail Merges a template file with the values in the Hastable and prints the result to a file.
0541:             * Creation date: (3/15/02 10:44:02 AM)
0542:             * @param sTemplateFileName java.lang.String Name of a word template file
0543:             * @param sPrintFileName java.lang.String Name of the printed file output
0544:             * @param htFields java.util.Hashtable A hashtable of field names & values to be used to populate template.
0545:             */
0546:            public static void mailMergePrintToFile(String sTemplateFileName,
0547:                    String sPrintFileName, Hashtable htFields)
0548:                    throws MailMergeException {
0549:                mailMergePrintToFile(sTemplateFileName, null, sPrintFileName,
0550:                        htFields);
0551:            }
0552:
0553:            /**
0554:             * Mail Merges a template file with the values in DataSource File and saves the result to a file.
0555:             * Creation date: (3/15/02 10:44:02 AM)
0556:             * @param sTemplateFileName java.lang.String Name of a word template file
0557:             * @param sDataSourceFileName java.lang.String Name of a DataSoure File usually a .csv file.
0558:             * @param sSaveAsFileName java.lang.String Name of the saved file
0559:             */
0560:            public static void mailMergeSaveAs(String sTemplateFileName,
0561:                    String sDataSourceFileName, String sSaveAsFileName)
0562:                    throws MailMergeException {
0563:                mailMergeSaveAs(sTemplateFileName, sDataSourceFileName,
0564:                        sSaveAsFileName, null);
0565:            }
0566:
0567:            /**
0568:             * Mail Merges a template file with the values in DataSource File and Hashtable and saves the result to a file.
0569:             * Creation date: (3/15/02 10:44:02 AM)
0570:             * @param sTemplateFileName java.lang.String Name of a word template file
0571:             * @param sDataSourceFileName java.lang.String Name of a DataSoure File usually a .csv file.
0572:             * @param sSaveAsFileName java.lang.String Name of the saved file
0573:             * @param htFields java.util.Hashtable A hashtable of field names & values to be used to populate template.
0574:             */
0575:            public static void mailMergeSaveAs(String sTemplateFileName,
0576:                    String sDataSourceFileName, String sSaveAsFileName,
0577:                    Hashtable htFields) throws MailMergeException {
0578:                String sWinWordTemplatePath = Props.getSystemProps()
0579:                        .getProperty(Props.WINWORD_TEMPLATE_PATH);
0580:                String sWinWordPath = Props.getSystemProps().getProperty(
0581:                        Props.WINWORD_PATH);
0582:                if (sWinWordTemplatePath == null)
0583:                    throw new MailMergeException(Props.WINWORD_TEMPLATE_PATH
0584:                            + " is missing from properties file.");
0585:                if (sWinWordPath == null)
0586:                    throw new MailMergeException(Props.WINWORD_PATH
0587:                            + " is missing from properties file.");
0588:                if (!sWinWordPath.endsWith(File.separator))
0589:                    sWinWordPath += File.separator;
0590:                if (!sWinWordTemplatePath.endsWith(File.separator))
0591:                    sWinWordTemplatePath += File.separator;
0592:                File fSalmonTemplate = new File(sWinWordTemplatePath
0593:                        + "Salmon.dot");
0594:                if (!fSalmonTemplate.exists())
0595:                    throw new MailMergeException(
0596:                            "Salmon.dot was not deployed to winword template directory specified by "
0597:                                    + Props.WINWORD_TEMPLATE_PATH
0598:                                    + " in properties file.");
0599:                File fTemplate = new File(sTemplateFileName);
0600:                File fDatasource = null;
0601:                if (sDataSourceFileName != null
0602:                        && !sDataSourceFileName.trim().equals(""))
0603:                    fDatasource = new File(sDataSourceFileName);
0604:                if (!fTemplate.exists())
0605:                    throw new MailMergeException(sTemplateFileName
0606:                            + " does not exist.");
0607:                if (fDatasource != null && !fDatasource.exists())
0608:                    throw new MailMergeException(sDataSourceFileName
0609:                            + " does not exist.");
0610:                Vector vBookmarks = new Vector();
0611:                Vector vBookmarkValues = new Vector();
0612:                if (htFields != null) {
0613:                    Enumeration enumBookmarks = htFields.keys();
0614:                    int i = 1;
0615:                    while (enumBookmarks.hasMoreElements()) {
0616:                        String sBookmark = (String) enumBookmarks.nextElement();
0617:                        String sValue = (String) htFields.get(sBookmark);
0618:                        vBookmarks.addElement("MMBOOKMARK" + i + "="
0619:                                + sBookmark);
0620:                        vBookmarkValues.addElement("MMBOOKMARKVALUE" + i + "="
0621:                                + sValue);
0622:                        i++;
0623:                    }
0624:                }
0625:                int iStartIndex = fDatasource == null ? 2 : 3;
0626:                String[] saCommandArgs = new String[3];
0627:                String[] saEnvArgs = new String[iStartIndex
0628:                        + (vBookmarks.size() * 2)];
0629:                saCommandArgs[0] = "\"" + sWinWordPath + "winword\"";
0630:                saCommandArgs[1] = sWinWordTemplatePath + "Salmon.dot";
0631:                saCommandArgs[2] = "-mMailMerge";
0632:                if (fDatasource != null)
0633:                    saEnvArgs[0] = "MMDATASOURCE=" + sDataSourceFileName;
0634:                saEnvArgs[iStartIndex - 2] = "MMSAVEAS=" + sSaveAsFileName;
0635:                saEnvArgs[iStartIndex - 1] = "MMTEMPLATE=" + sTemplateFileName;
0636:                for (int k = iStartIndex; k < saEnvArgs.length; k += 2) {
0637:                    saEnvArgs[k] = (String) vBookmarks
0638:                            .elementAt((k - iStartIndex) / 2);
0639:                    saEnvArgs[k + 1] = (String) vBookmarkValues
0640:                            .elementAt((k - iStartIndex) / 2);
0641:                }
0642:
0643:                MailMerge mm = new MailMerge(saCommandArgs, saEnvArgs, Thread
0644:                        .currentThread());
0645:                Thread thmm = new Thread(mm);
0646:                thmm.start();
0647:                boolean bSuccessful = false;
0648:                try {
0649:                    Thread.sleep(60000);
0650:                } catch (Exception e) {
0651:                    if (mm.getMailMergeException() == null)
0652:                        bSuccessful = true;
0653:                }
0654:                if (!bSuccessful) {
0655:                    Process pMm = mm.getMailMergeProcess();
0656:                    if (pMm != null)
0657:                        pMm.destroy();
0658:                    throw mm.getMailMergeException();
0659:                }
0660:            }
0661:
0662:            /**
0663:             * Mail Merges a template file with the values in the Hastable and saves the result to a file.
0664:             * Creation date: (3/15/02 10:44:02 AM)
0665:             * @param sTemplateFileName java.lang.String Name of a word template file
0666:             * @param sSaveAsFileName java.lang.String Name of the saved file
0667:             * @param htFields java.util.Hashtable A hashtable of field names & values to be used to populate template.
0668:             */
0669:            public static void mailMergeSaveAs(String sTemplateFileName,
0670:                    String sSaveAsFileName, Hashtable htFields)
0671:                    throws MailMergeException {
0672:                mailMergeSaveAs(sTemplateFileName, null, sSaveAsFileName,
0673:                        htFields);
0674:            }
0675:
0676:            /**
0677:             * Prints a Template File after Mail Merging with a Datasource.
0678:             * Creation date: (3/15/02 10:44:02 AM)
0679:             * @param sTemplateFileName java.lang.String Name of the word template file
0680:             * @param sDataSourceFileName java.lang.String Name of the Data Source File usually a .csv file
0681:             */
0682:            public void print(String sTemplateFileName,
0683:                    String sDataSourceFileName) throws MailMergeException {
0684:                String sWinWordTemplatePath = Props.getSystemProps()
0685:                        .getProperty(Props.WINWORD_TEMPLATE_PATH);
0686:                String sWinWordPath = Props.getSystemProps().getProperty(
0687:                        Props.WINWORD_PATH);
0688:                if (sWinWordTemplatePath == null)
0689:                    throw new MailMergeException(Props.WINWORD_TEMPLATE_PATH
0690:                            + " is missing from properties file.");
0691:                if (sWinWordPath == null)
0692:                    throw new MailMergeException(Props.WINWORD_PATH
0693:                            + " is missing from properties file.");
0694:                if (!sWinWordPath.endsWith(File.separator))
0695:                    sWinWordPath += File.separator;
0696:                if (!sWinWordTemplatePath.endsWith(File.separator))
0697:                    sWinWordTemplatePath += File.separator;
0698:                File fSalmonTemplate = new File(sWinWordTemplatePath
0699:                        + "Salmon.dot");
0700:                if (!fSalmonTemplate.exists())
0701:                    throw new MailMergeException(
0702:                            "Salmon.dot was not deployed to winword template directory specified by "
0703:                                    + Props.WINWORD_TEMPLATE_PATH
0704:                                    + " in properties file.");
0705:                File fTemplate = new File(sTemplateFileName);
0706:                File fDatasource = null;
0707:                if (sDataSourceFileName != null
0708:                        && !sDataSourceFileName.trim().equals(""))
0709:                    fDatasource = new File(sDataSourceFileName);
0710:                if (!fTemplate.exists())
0711:                    throw new MailMergeException(sTemplateFileName
0712:                            + " does not exist.");
0713:                if (fDatasource != null && !fDatasource.exists())
0714:                    throw new MailMergeException(sDataSourceFileName
0715:                            + " does not exist.");
0716:                Vector vBookmarks = new Vector();
0717:                Vector vBookmarkValues = new Vector();
0718:                if (_htTextBookmarks != null) {
0719:                    Enumeration enumBookmarks = _htTextBookmarks.keys();
0720:                    int i = 1;
0721:                    while (enumBookmarks.hasMoreElements()) {
0722:                        String sBookmark = (String) enumBookmarks.nextElement();
0723:                        String sValue = (String) _htTextBookmarks
0724:                                .get(sBookmark);
0725:                        vBookmarks.addElement("MMBOOKMARK" + i + "="
0726:                                + sBookmark);
0727:                        vBookmarkValues.addElement("MMBOOKMARKVALUE" + i + "="
0728:                                + sValue);
0729:                        i++;
0730:                    }
0731:                }
0732:                if (_htPictureBookmarks != null) {
0733:                    Enumeration enumBookmarks = _htPictureBookmarks.keys();
0734:                    int i = 1;
0735:                    while (enumBookmarks.hasMoreElements()) {
0736:                        String sBookmark = (String) enumBookmarks.nextElement();
0737:                        File fValue = (File) _htPictureBookmarks.get(sBookmark);
0738:                        vBookmarks.addElement("MMPICTUREBOOKMARK" + i + "="
0739:                                + sBookmark);
0740:                        vBookmarkValues.addElement("MMPICTUREBOOKMARKVALUE" + i
0741:                                + "=" + fValue.getAbsolutePath());
0742:                        i++;
0743:                    }
0744:                }
0745:                if (_htFileBookmarks != null) {
0746:                    Enumeration enumBookmarks = _htFileBookmarks.keys();
0747:                    int i = 1;
0748:                    while (enumBookmarks.hasMoreElements()) {
0749:                        String sBookmark = (String) enumBookmarks.nextElement();
0750:                        File fValue = (File) _htFileBookmarks.get(sBookmark);
0751:                        vBookmarks.addElement("MMFILEBOOKMARK" + i + "="
0752:                                + sBookmark);
0753:                        vBookmarkValues.addElement("MMFILEBOOKMARKVALUE" + i
0754:                                + "=" + fValue.getAbsolutePath());
0755:                        i++;
0756:                    }
0757:                }
0758:                if (_htOLEBookmarks != null) {
0759:                    Enumeration enumBookmarks = _htOLEBookmarks.keys();
0760:                    int i = 1;
0761:                    while (enumBookmarks.hasMoreElements()) {
0762:                        String sBookmark = (String) enumBookmarks.nextElement();
0763:                        File fValue = (File) _htOLEBookmarks.get(sBookmark);
0764:                        vBookmarks.addElement("MMOLEFILEBOOKMARK" + i + "="
0765:                                + sBookmark);
0766:                        vBookmarkValues.addElement("MMOLEFILEBOOKMARKVALUE" + i
0767:                                + "=" + fValue.getAbsolutePath());
0768:                        i++;
0769:                    }
0770:                }
0771:                int iStartIndex = fDatasource == null ? 1 : 2;
0772:                String[] saCommandArgs = new String[3];
0773:                int iSize = iStartIndex + (vBookmarks.size() * 2)
0774:                        + _vExtraEnvironArgs.size();
0775:                String[] saEnvArgs = new String[iSize];
0776:                saCommandArgs[0] = "\"" + sWinWordPath + "winword\"";
0777:                saCommandArgs[1] = sWinWordTemplatePath + "Salmon.dot";
0778:                saCommandArgs[2] = "-mMailMerge";
0779:                if (fDatasource != null)
0780:                    saEnvArgs[0] = "MMDATASOURCE=" + sDataSourceFileName;
0781:                saEnvArgs[iStartIndex - 1] = "MMTEMPLATE=" + sTemplateFileName;
0782:                int k = iStartIndex;
0783:                for (; k < iSize - _vExtraEnvironArgs.size(); k += 2) {
0784:                    saEnvArgs[k] = (String) vBookmarks
0785:                            .elementAt((k - iStartIndex) / 2);
0786:                    saEnvArgs[k + 1] = (String) vBookmarkValues
0787:                            .elementAt((k - iStartIndex) / 2);
0788:                }
0789:                for (; k < iSize; k++) {
0790:                    saEnvArgs[k] = (String) _vExtraEnvironArgs.elementAt(k
0791:                            - (iSize - _vExtraEnvironArgs.size()));
0792:                }
0793:                MailMerge mm = new MailMerge(saCommandArgs, saEnvArgs, Thread
0794:                        .currentThread());
0795:                Thread thmm = new Thread(mm);
0796:                thmm.start();
0797:                boolean bSuccessful = false;
0798:                try {
0799:                    Thread.sleep(60000);
0800:                } catch (Exception e) {
0801:                    if (mm.getMailMergeException() == null)
0802:                        bSuccessful = true;
0803:                }
0804:                if (!bSuccessful) {
0805:                    Process pMm = mm.getMailMergeProcess();
0806:                    if (pMm != null)
0807:                        pMm.destroy();
0808:                    throw mm.getMailMergeException();
0809:                }
0810:            }
0811:
0812:            /**
0813:             * Prints to a File a Template File after Mail Merging with a Datasource.
0814:             * Creation date: (3/15/02 10:44:02 AM)
0815:             * @param sTemplateFileName java.lang.String Name of the word template file
0816:             * @param sDataSourceFileName java.lang.String Name of the Data Source File usually a .csv file
0817:             * @param sPrintFileName java.lang.String Name of the Print File Output.
0818:             */
0819:            public void printToFile(String sTemplateFileName,
0820:                    String sDataSourceFileName, String sPrintFileName)
0821:                    throws MailMergeException {
0822:                String sWinWordTemplatePath = Props.getSystemProps()
0823:                        .getProperty(Props.WINWORD_TEMPLATE_PATH);
0824:                String sWinWordPath = Props.getSystemProps().getProperty(
0825:                        Props.WINWORD_PATH);
0826:                if (sWinWordTemplatePath == null)
0827:                    throw new MailMergeException(Props.WINWORD_TEMPLATE_PATH
0828:                            + " is missing from properties file.");
0829:                if (sWinWordPath == null)
0830:                    throw new MailMergeException(Props.WINWORD_PATH
0831:                            + " is missing from properties file.");
0832:                if (!sWinWordPath.endsWith(File.separator))
0833:                    sWinWordPath += File.separator;
0834:                if (!sWinWordTemplatePath.endsWith(File.separator))
0835:                    sWinWordTemplatePath += File.separator;
0836:                File fSalmonTemplate = new File(sWinWordTemplatePath
0837:                        + "Salmon.dot");
0838:                if (!fSalmonTemplate.exists())
0839:                    throw new MailMergeException(
0840:                            "Salmon.dot was not deployed to winword template directory specified by "
0841:                                    + Props.WINWORD_TEMPLATE_PATH
0842:                                    + " in properties file.");
0843:                File fTemplate = new File(sTemplateFileName);
0844:                File fDatasource = null;
0845:                if (sDataSourceFileName != null
0846:                        && !sDataSourceFileName.trim().equals(""))
0847:                    fDatasource = new File(sDataSourceFileName);
0848:                if (!fTemplate.exists())
0849:                    throw new MailMergeException(sTemplateFileName
0850:                            + " does not exist.");
0851:                if (fDatasource != null && !fDatasource.exists())
0852:                    throw new MailMergeException(sDataSourceFileName
0853:                            + " does not exist.");
0854:                Vector vBookmarks = new Vector();
0855:                Vector vBookmarkValues = new Vector();
0856:                if (_htTextBookmarks != null) {
0857:                    Enumeration enumBookmarks = _htTextBookmarks.keys();
0858:                    int i = 1;
0859:                    while (enumBookmarks.hasMoreElements()) {
0860:                        String sBookmark = (String) enumBookmarks.nextElement();
0861:                        String sValue = (String) _htTextBookmarks
0862:                                .get(sBookmark);
0863:                        vBookmarks.addElement("MMBOOKMARK" + i + "="
0864:                                + sBookmark);
0865:                        vBookmarkValues.addElement("MMBOOKMARKVALUE" + i + "="
0866:                                + sValue);
0867:                        i++;
0868:                    }
0869:                }
0870:                if (_htPictureBookmarks != null) {
0871:                    Enumeration enumBookmarks = _htPictureBookmarks.keys();
0872:                    int i = 1;
0873:                    while (enumBookmarks.hasMoreElements()) {
0874:                        String sBookmark = (String) enumBookmarks.nextElement();
0875:                        File fValue = (File) _htPictureBookmarks.get(sBookmark);
0876:                        vBookmarks.addElement("MMPICTUREBOOKMARK" + i + "="
0877:                                + sBookmark);
0878:                        vBookmarkValues.addElement("MMPICTUREBOOKMARKVALUE" + i
0879:                                + "=" + fValue.getAbsolutePath());
0880:                        i++;
0881:                    }
0882:                }
0883:                if (_htFileBookmarks != null) {
0884:                    Enumeration enumBookmarks = _htFileBookmarks.keys();
0885:                    int i = 1;
0886:                    while (enumBookmarks.hasMoreElements()) {
0887:                        String sBookmark = (String) enumBookmarks.nextElement();
0888:                        File fValue = (File) _htFileBookmarks.get(sBookmark);
0889:                        vBookmarks.addElement("MMFILEBOOKMARK" + i + "="
0890:                                + sBookmark);
0891:                        vBookmarkValues.addElement("MMFILEBOOKMARKVALUE" + i
0892:                                + "=" + fValue.getAbsolutePath());
0893:                        i++;
0894:                    }
0895:                }
0896:                if (_htOLEBookmarks != null) {
0897:                    Enumeration enumBookmarks = _htOLEBookmarks.keys();
0898:                    int i = 1;
0899:                    while (enumBookmarks.hasMoreElements()) {
0900:                        String sBookmark = (String) enumBookmarks.nextElement();
0901:                        File fValue = (File) _htOLEBookmarks.get(sBookmark);
0902:                        vBookmarks.addElement("MMOLEFILEBOOKMARK" + i + "="
0903:                                + sBookmark);
0904:                        vBookmarkValues.addElement("MMOLEFILEBOOKMARKVALUE" + i
0905:                                + "=" + fValue.getAbsolutePath());
0906:                        i++;
0907:                    }
0908:                }
0909:                int iStartIndex = fDatasource == null ? 2 : 3;
0910:                String[] saCommandArgs = new String[3];
0911:                int iSize = iStartIndex + (vBookmarks.size() * 2)
0912:                        + _vExtraEnvironArgs.size();
0913:                String[] saEnvArgs = new String[iSize];
0914:                saCommandArgs[0] = "\"" + sWinWordPath + "winword\"";
0915:                saCommandArgs[1] = sWinWordTemplatePath + "Salmon.dot";
0916:                saCommandArgs[2] = "-mMailMerge";
0917:                if (fDatasource != null)
0918:                    saEnvArgs[0] = "MMDATASOURCE=" + sDataSourceFileName;
0919:                saEnvArgs[iStartIndex - 2] = "MMPRINTFILE=" + sPrintFileName;
0920:                saEnvArgs[iStartIndex - 1] = "MMTEMPLATE=" + sTemplateFileName;
0921:                int k = iStartIndex;
0922:                for (; k < iSize - _vExtraEnvironArgs.size(); k += 2) {
0923:                    saEnvArgs[k] = (String) vBookmarks
0924:                            .elementAt((k - iStartIndex) / 2);
0925:                    saEnvArgs[k + 1] = (String) vBookmarkValues
0926:                            .elementAt((k - iStartIndex) / 2);
0927:                }
0928:                for (; k < iSize; k++) {
0929:                    saEnvArgs[k] = (String) _vExtraEnvironArgs.elementAt(k
0930:                            - (iSize - _vExtraEnvironArgs.size()));
0931:                }
0932:                MailMerge mm = new MailMerge(saCommandArgs, saEnvArgs, Thread
0933:                        .currentThread());
0934:                Thread thmm = new Thread(mm);
0935:                thmm.start();
0936:                boolean bSuccessful = false;
0937:                try {
0938:                    Thread.sleep(60000);
0939:                } catch (Exception e) {
0940:                    if (mm.getMailMergeException() == null)
0941:                        bSuccessful = true;
0942:                }
0943:                if (!bSuccessful) {
0944:                    Process pMm = mm.getMailMergeProcess();
0945:                    if (pMm != null)
0946:                        pMm.destroy();
0947:                    throw mm.getMailMergeException();
0948:                }
0949:            }
0950:
0951:            public void run() {
0952:                try {
0953:                    _process = Runtime.getRuntime().exec(_saCommandArgs,
0954:                            _saEnvironArgs);
0955:                    _process.waitFor();
0956:                    _process = null;
0957:                } catch (Exception e) {
0958:                    StringBuffer sbCommand = new StringBuffer();
0959:                    for (int i = 0; i < _saCommandArgs.length; i++)
0960:                        sbCommand.append(_saCommandArgs[i] + " ");
0961:                    _mme = new MailMergeException("Unable to execute "
0962:                            + sbCommand.toString() + " for processing: "
0963:                            + e.getMessage());
0964:                    if (_process != null)
0965:                        _process.destroy();
0966:                }
0967:                _wakeupthread.interrupt();
0968:            }
0969:
0970:            /**
0971:             * Saves to a File a Template File after Mail Merging with a Datasource.
0972:             * Creation date: (3/15/02 10:44:02 AM)
0973:             * @param sTemplateFileName java.lang.String Name of the word template file
0974:             * @param sDataSourceFileName java.lang.String Name of the Data Source File usually a .csv file
0975:             * @param sSaveAsFileName java.lang.String Name of the Saved File.
0976:             */
0977:            public void saveAs(String sTemplateFileName,
0978:                    String sDataSourceFileName, String sSaveAsFileName)
0979:                    throws MailMergeException {
0980:                String sWinWordTemplatePath = Props.getSystemProps()
0981:                        .getProperty(Props.WINWORD_TEMPLATE_PATH);
0982:                String sWinWordPath = Props.getSystemProps().getProperty(
0983:                        Props.WINWORD_PATH);
0984:                if (sWinWordTemplatePath == null)
0985:                    throw new MailMergeException(Props.WINWORD_TEMPLATE_PATH
0986:                            + " is missing from properties file.");
0987:                if (sWinWordPath == null)
0988:                    throw new MailMergeException(Props.WINWORD_PATH
0989:                            + " is missing from properties file.");
0990:                if (!sWinWordPath.endsWith(File.separator))
0991:                    sWinWordPath += File.separator;
0992:                if (!sWinWordTemplatePath.endsWith(File.separator))
0993:                    sWinWordTemplatePath += File.separator;
0994:                File fSalmonTemplate = new File(sWinWordTemplatePath
0995:                        + "Salmon.dot");
0996:                if (!fSalmonTemplate.exists())
0997:                    throw new MailMergeException(
0998:                            "Salmon.dot was not deployed to winword template directory specified by "
0999:                                    + Props.WINWORD_TEMPLATE_PATH
1000:                                    + " in properties file.");
1001:                File fTemplate = new File(sTemplateFileName);
1002:                File fDatasource = null;
1003:                if (sDataSourceFileName != null
1004:                        && !sDataSourceFileName.trim().equals(""))
1005:                    fDatasource = new File(sDataSourceFileName);
1006:                if (!fTemplate.exists())
1007:                    throw new MailMergeException(sTemplateFileName
1008:                            + " does not exist.");
1009:                if (fDatasource != null && !fDatasource.exists())
1010:                    throw new MailMergeException(sDataSourceFileName
1011:                            + " does not exist.");
1012:                Vector vBookmarks = new Vector();
1013:                Vector vBookmarkValues = new Vector();
1014:                if (_htTextBookmarks != null) {
1015:                    Enumeration enumBookmarks = _htTextBookmarks.keys();
1016:                    int i = 1;
1017:                    while (enumBookmarks.hasMoreElements()) {
1018:                        String sBookmark = (String) enumBookmarks.nextElement();
1019:                        String sValue = (String) _htTextBookmarks
1020:                                .get(sBookmark);
1021:                        vBookmarks.addElement("MMBOOKMARK" + i + "="
1022:                                + sBookmark);
1023:                        vBookmarkValues.addElement("MMBOOKMARKVALUE" + i + "="
1024:                                + sValue);
1025:                        i++;
1026:                    }
1027:                }
1028:                if (_htPictureBookmarks != null) {
1029:                    Enumeration enumBookmarks = _htPictureBookmarks.keys();
1030:                    int i = 1;
1031:                    while (enumBookmarks.hasMoreElements()) {
1032:                        String sBookmark = (String) enumBookmarks.nextElement();
1033:                        File fValue = (File) _htPictureBookmarks.get(sBookmark);
1034:                        vBookmarks.addElement("MMPICTUREBOOKMARK" + i + "="
1035:                                + sBookmark);
1036:                        vBookmarkValues.addElement("MMPICTUREBOOKMARKVALUE" + i
1037:                                + "=" + fValue.getAbsolutePath());
1038:                        i++;
1039:                    }
1040:                }
1041:                if (_htFileBookmarks != null) {
1042:                    Enumeration enumBookmarks = _htFileBookmarks.keys();
1043:                    int i = 1;
1044:                    while (enumBookmarks.hasMoreElements()) {
1045:                        String sBookmark = (String) enumBookmarks.nextElement();
1046:                        File fValue = (File) _htFileBookmarks.get(sBookmark);
1047:                        vBookmarks.addElement("MMFILEBOOKMARK" + i + "="
1048:                                + sBookmark);
1049:                        vBookmarkValues.addElement("MMFILEBOOKMARKVALUE" + i
1050:                                + "=" + fValue.getAbsolutePath());
1051:                        i++;
1052:                    }
1053:                }
1054:                if (_htOLEBookmarks != null) {
1055:                    Enumeration enumBookmarks = _htOLEBookmarks.keys();
1056:                    int i = 1;
1057:                    while (enumBookmarks.hasMoreElements()) {
1058:                        String sBookmark = (String) enumBookmarks.nextElement();
1059:                        File fValue = (File) _htOLEBookmarks.get(sBookmark);
1060:                        vBookmarks.addElement("MMOLEFILEBOOKMARK" + i + "="
1061:                                + sBookmark);
1062:                        vBookmarkValues.addElement("MMOLEFILEBOOKMARKVALUE" + i
1063:                                + "=" + fValue.getAbsolutePath());
1064:                        i++;
1065:                    }
1066:                }
1067:                int iStartIndex = fDatasource == null ? 2 : 3;
1068:                String[] saCommandArgs = new String[3];
1069:                int iSize = iStartIndex + (vBookmarks.size() * 2)
1070:                        + _vExtraEnvironArgs.size();
1071:                String[] saEnvArgs = new String[iSize];
1072:                saCommandArgs[0] = "\"" + sWinWordPath + "winword\"";
1073:                saCommandArgs[1] = sWinWordTemplatePath + "Salmon.dot";
1074:                saCommandArgs[2] = "-mMailMerge";
1075:                if (fDatasource != null)
1076:                    saEnvArgs[0] = "MMDATASOURCE=" + sDataSourceFileName;
1077:                saEnvArgs[iStartIndex - 2] = "MMSAVEAS=" + sSaveAsFileName;
1078:                saEnvArgs[iStartIndex - 1] = "MMTEMPLATE=" + sTemplateFileName;
1079:                int k = iStartIndex;
1080:                for (; k < iSize - _vExtraEnvironArgs.size(); k += 2) {
1081:                    saEnvArgs[k] = (String) vBookmarks
1082:                            .elementAt((k - iStartIndex) / 2);
1083:                    saEnvArgs[k + 1] = (String) vBookmarkValues
1084:                            .elementAt((k - iStartIndex) / 2);
1085:                }
1086:                for (; k < iSize; k++) {
1087:                    saEnvArgs[k] = (String) _vExtraEnvironArgs.elementAt(k
1088:                            - (iSize - _vExtraEnvironArgs.size()));
1089:                }
1090:                MailMerge mm = new MailMerge(saCommandArgs, saEnvArgs, Thread
1091:                        .currentThread());
1092:                Thread thmm = new Thread(mm);
1093:                thmm.start();
1094:                boolean bSuccessful = false;
1095:                try {
1096:                    Thread.sleep(60000);
1097:                } catch (Exception e) {
1098:                    if (mm.getMailMergeException() == null)
1099:                        bSuccessful = true;
1100:                }
1101:                if (!bSuccessful) {
1102:                    Process pMm = mm.getMailMergeProcess();
1103:                    if (pMm != null)
1104:                        pMm.destroy();
1105:                    throw mm.getMailMergeException();
1106:                }
1107:            }
1108:
1109:            /**
1110:             * Indicates that if the document is view that it should auto print.
1111:             * Creation date: (3/15/02 10:44:02 AM)
1112:             * @param bAutoPrint boolean If true document will automatically print if viewed.
1113:             */
1114:            public void setAutoPrint(boolean bAutoPrint)
1115:                    throws MailMergeException {
1116:                String sWinWordTemplatePath = Props.getSystemProps()
1117:                        .getProperty(Props.WINWORD_TEMPLATE_PATH);
1118:                String sWinWordPath = Props.getSystemProps().getProperty(
1119:                        Props.WINWORD_PATH);
1120:                if (sWinWordTemplatePath == null)
1121:                    throw new MailMergeException(Props.WINWORD_TEMPLATE_PATH
1122:                            + " is missing from properties file.");
1123:                if (sWinWordPath == null)
1124:                    throw new MailMergeException(Props.WINWORD_PATH
1125:                            + " is missing from properties file.");
1126:                if (!sWinWordPath.endsWith(File.separator))
1127:                    sWinWordPath += File.separator;
1128:                if (!sWinWordTemplatePath.endsWith(File.separator))
1129:                    sWinWordTemplatePath += File.separator;
1130:                File fSalmonAutoMacrosTemplate = new File(sWinWordTemplatePath
1131:                        + "SalmonAutoMacros.dot");
1132:                if (!fSalmonAutoMacrosTemplate.exists())
1133:                    throw new MailMergeException(
1134:                            "SalmonAutoMacros.dot was not deployed to winword template directory specified by "
1135:                                    + Props.WINWORD_TEMPLATE_PATH
1136:                                    + " in properties file.");
1137:                if (!bAutoPrint) {
1138:                    int iIndex = _vExtraEnvironArgs.indexOf("MMAUTOPRINT="
1139:                            + sWinWordTemplatePath + "SalmonAutoMacros.dot");
1140:                    if (iIndex >= 0)
1141:                        _vExtraEnvironArgs
1142:                                .remove("MMAUTOPRINT=" + sWinWordTemplatePath
1143:                                        + "SalmonAutoMacros.dot");
1144:                    return;
1145:                }
1146:                _vExtraEnvironArgs.addElement("MMAUTOPRINT="
1147:                        + sWinWordTemplatePath + "SalmonAutoMacros.dot");
1148:            }
1149:
1150:            /**
1151:             * Sets the Background of a Word Document.
1152:             * Creation date: (3/15/02 10:44:02 AM)
1153:             * @param fBackground java.io.File A File pointing to a background for the document
1154:             */
1155:            public void setBackground(File fBackground)
1156:                    throws MailMergeException {
1157:                if (!fBackground.exists())
1158:                    throw new MailMergeException("File "
1159:                            + fBackground.getAbsolutePath()
1160:                            + " does not exist.");
1161:                _fBackground = fBackground;
1162:                _vExtraEnvironArgs.addElement("MMBACKGROUND="
1163:                        + fBackground.getAbsolutePath());
1164:            }
1165:
1166:            /**
1167:             * Sets the Bottom Margin of a Word Document.
1168:             * Creation date: (3/15/02 10:44:02 AM)
1169:             * @param fBottomMargin float Bottom Margin in Inches
1170:             */
1171:            public void setBottomMargin(float fBottomMargin) {
1172:                _fBottomMargin = new Float(fBottomMargin);
1173:                _vExtraEnvironArgs
1174:                        .addElement("MMBOTTOMMARGIN=" + fBottomMargin);
1175:            }
1176:
1177:            /**
1178:             * Sets the Footer Distance of a Word Document.
1179:             * Creation date: (3/15/02 10:44:02 AM)
1180:             * @param fFooterDistance float Footer Distance in Inches
1181:             */
1182:            public void setFooterDistance(float fFooterDistance) {
1183:                _fFooterDistance = new Float(fFooterDistance);
1184:                _vExtraEnvironArgs.addElement("MMFOOTERDISTANCE="
1185:                        + fFooterDistance);
1186:            }
1187:
1188:            /**
1189:             * Sets the Gutter of a Word Document.
1190:             * Creation date: (3/15/02 10:44:02 AM)
1191:             * @param fGutter float Gutter in Inches
1192:             */
1193:            public void setGutter(float fGutter) {
1194:                _fGutter = new Float(fGutter);
1195:                _vExtraEnvironArgs.addElement("MMGUTTER=" + fGutter);
1196:            }
1197:
1198:            /**
1199:             * Sets the Header Distance of a Word Document.
1200:             * Creation date: (3/15/02 10:44:02 AM)
1201:             * @param fHeaderDistance float Header Distance in Inches
1202:             */
1203:            public void setHeaderDistance(float fHeaderDistance) {
1204:                _fHeaderDistance = new Float(fHeaderDistance);
1205:                _vExtraEnvironArgs.addElement("MMHEADERDISTANCE="
1206:                        + fHeaderDistance);
1207:            }
1208:
1209:            /**
1210:             * Sets the Left Margin of a Word Document.
1211:             * Creation date: (3/15/02 10:44:02 AM)
1212:             * @param fLeftMargin float Left Margin in Inches
1213:             */
1214:            public void setLeftMargin(float fLeftMargin) {
1215:                _fLeftMargin = new Float(fLeftMargin);
1216:                _vExtraEnvironArgs.addElement("MMLEFTMARGIN=" + fLeftMargin);
1217:            }
1218:
1219:            /**
1220:             * Sets the Printer for a Word Document.
1221:             * Creation date: (3/15/02 10:44:02 AM)
1222:             * @param sPrinter java.lang.String A String representing the printer to print to.
1223:             */
1224:            public void setPrinter(String sPrinter) {
1225:                _sPrinter = sPrinter;
1226:                _vExtraEnvironArgs.addElement("MMPRINTER=" + sPrinter);
1227:            }
1228:
1229:            /**
1230:             * Sets the Right Margin of a Word Document.
1231:             * Creation date: (3/15/02 10:44:02 AM)
1232:             * @param fRightMargin float Right Margin in Inches
1233:             */
1234:            public void setRightMargin(float fRightMargin) {
1235:                _fRightMargin = new Float(fRightMargin);
1236:                _vExtraEnvironArgs.addElement("MMRIGHTMARGIN=" + fRightMargin);
1237:            }
1238:
1239:            /**
1240:             * Sets the Top Margin of a Word Document.
1241:             * Creation date: (3/15/02 10:44:02 AM)
1242:             * @param fTopMargin float Top Margin in Inches
1243:             */
1244:            public void setTopMargin(float fTopMargin) {
1245:                _fTopMargin = new Float(fTopMargin);
1246:                _vExtraEnvironArgs.addElement("MMTOPMARGIN=" + fTopMargin);
1247:            }
1248:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.