-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGenerateOrderDetails.java
324 lines (293 loc) · 9.52 KB
/
GenerateOrderDetails.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import java.io.FileInputStream;
import com.syncfusion.docio.*;
import com.syncfusion.javahelper.system.RefSupport;
import com.syncfusion.javahelper.system.StringSupport;
import com.syncfusion.javahelper.system.collections.DictionaryEntrySupport;
import com.syncfusion.javahelper.system.collections.generic.DictionarySupport;
import com.syncfusion.javahelper.system.collections.generic.IDictionarySupport;
import com.syncfusion.javahelper.system.collections.generic.IEnumeratorSupport;
import com.syncfusion.javahelper.system.collections.generic.ListSupport;
import com.syncfusion.javahelper.system.io.*;
import com.syncfusion.javahelper.system.xml.XmlDocumentSupport;
import com.syncfusion.javahelper.system.xml.XmlNodeSupport;
import com.syncfusion.javahelper.system.xml.XmlNodeType;
import com.syncfusion.javahelper.system.xml.XmlReaderSupport;
import java.io.File;
public class GenerateOrderDetails
{
public static void main(String[] args) throws Exception
{
//Creates new Word document instance for Word processing.
WordDocument document = new WordDocument();
//Opens the template Word document.
String basePath = getDataDir("Template.docx");
document.open(basePath, FormatType.Docx);
//Retrieves the mail merge data.
MailMergeDataTable dataTable = getMailMergeDataTable();
//Executes nested Mail merge using implicit relational data.
document.getMailMerge().executeNestedGroup(dataTable);
//Removes empty page at the end of Word document.
removeEmptyPage(document);
//Save the document in the given name and format.
document.save("Sample.docx", FormatType.Docx);
//Release the resources occupied by the WordDocument instance.
document.close();
System.out.println("Word document generated successfully.");
}
/**
*
* Gets the mail merge data table.
*
* @return
*/
private static MailMergeDataTable getMailMergeDataTable() throws Exception
{
//Gets the customer details implicit as "IEnumerable" collection.
ListSupport<CustomerDetails> customers = new ListSupport<CustomerDetails>(CustomerDetails.class);
FileStreamSupport stream = new FileStreamSupport(getDataDir("CustomerDetails.xml"), FileMode.OpenOrCreate);
XmlReaderSupport reader = XmlReaderSupport.create(stream);
while (reader.getNodeType().getEnumValue() != XmlNodeType.Element.getEnumValue())
reader.read();
reader.read();
while (reader.getNodeType().getEnumValue() == XmlNodeType.Whitespace.getEnumValue())
reader.read();
while (!(reader.getLocalName() == "CustomerDetails"))
{
if (reader.getNodeType().getEnumValue() == XmlNodeType.Element.getEnumValue())
{
switch ((reader.getLocalName()) == null ? "string_null_value" : (reader.getLocalName()))
{
case "Customers":
customers.add(getCustomer(reader));
break;
}
}
else
{
reader.read();
if ((reader.getLocalName() == "CustomerDetails")
&& reader.getNodeType().getEnumValue() == XmlNodeType.EndElement.getEnumValue())
break;
}
}
reader.close();
stream.close();
// Creates an instance of "MailMergeDataTable" by specifying mail merge group
// name and "IEnumerable" collection.
MailMergeDataTable dataTable = new MailMergeDataTable("Customers", customers);
return dataTable;
}
/**
*
* Gets the customer.
*
* @param reader The reader.
* @return
*/
private static CustomerDetails getCustomer(XmlReaderSupport reader) throws Exception
{
while (reader.getNodeType().getEnumValue() != XmlNodeType.Element.getEnumValue())
reader.read();
reader.read();
while (reader.getNodeType().getEnumValue() == XmlNodeType.Whitespace.getEnumValue())
reader.read();
CustomerDetails customer = new CustomerDetails();
while (!(reader.getLocalName() == "Customers"))
{
if (reader.getNodeType().getEnumValue() == XmlNodeType.Element.getEnumValue())
{
switch ((reader.getLocalName()) == null ? "string_null_value" : (reader.getLocalName()))
{
case "CustomerName":
customer.setCustomerName(reader.readContentAsString());
break;
case "Address":
customer.setAddress(reader.readContentAsString());
break;
case "City":
customer.setCity(reader.readContentAsString());
break;
case "PostalCode":
customer.setPostalCode(reader.readContentAsString());
break;
case "Country":
customer.setCountry(reader.readContentAsString());
break;
case "Phone":
customer.setPhone(reader.readContentAsString());
break;
case "Orders":
customer.getOrders().add(getOrder(reader));
break;
default:
reader.skip();
break;
}
}
else
{
reader.read();
if ((reader.getLocalName() == "Customers")
&& reader.getNodeType().getEnumValue() == XmlNodeType.EndElement.getEnumValue())
break;
}
}
return customer;
}
/**
*
* Gets the order.
*
* @param reader The reader.
* @return
*/
private static OrderDetails getOrder(XmlReaderSupport reader) throws Exception
{
while (reader.getNodeType().getEnumValue() != XmlNodeType.Element.getEnumValue())
reader.read();
reader.read();
while (reader.getNodeType().getEnumValue() == XmlNodeType.Whitespace.getEnumValue())
reader.read();
OrderDetails order = new OrderDetails();
while (!(reader.getLocalName() == "Orders"))
{
if (reader.getNodeType().getEnumValue() == XmlNodeType.Element.getEnumValue())
{
switch ((reader.getLocalName()) == null ? "string_null_value" : (reader.getLocalName()))
{
case "CustomerName":
order.setCustomerName(reader.readContentAsString());
break;
case "OrderID":
order.setOrderID(reader.readContentAsString());
break;
case "OrderDate":
order.setOrderDate(reader.readContentAsString());
break;
case "ExpectedDeliveryDate":
order.setExpectedDeliveryDate(reader.readContentAsString());
break;
case "ShippedDate":
order.setShippedDate(reader.readContentAsString());
break;
case "Products":
order.getProducts().add(getProduct(reader));
break;
}
reader.read();
}
else
{
reader.read();
if ((reader.getLocalName() == "Orders")
&& reader.getNodeType().getEnumValue() == XmlNodeType.EndElement.getEnumValue())
break;
}
}
return order;
}
/**
*
* Gets the product.
*
* @param reader The reader.
* @return
*/
private static ProductDetails getProduct(XmlReaderSupport reader) throws Exception
{
while (reader.getNodeType().getEnumValue() != XmlNodeType.Element.getEnumValue())
reader.read();
reader.read();
while (reader.getNodeType().getEnumValue() == XmlNodeType.Whitespace.getEnumValue())
reader.read();
ProductDetails product = new ProductDetails();
while (!(reader.getLocalName() == "Products"))
{
if (reader.getNodeType().getEnumValue() != XmlNodeType.EndElement.getEnumValue())
{
switch ((reader.getLocalName()) == null ? "string_null_value" : (reader.getLocalName()))
{
case "OrderID":
product.setOrderID(reader.readContentAsString());
break;
case "Product":
product.setProduct(reader.readContentAsString());
break;
case "UnitPrice":
product.setUnitPrice(reader.readContentAsString());
break;
case "Quantity":
product.setQuantity(reader.readContentAsString());
break;
}
reader.read();
}
else
{
reader.read();
if ((reader.getLocalName() == "Products")
&& reader.getNodeType().getEnumValue() == XmlNodeType.EndElement.getEnumValue())
break;
}
}
return product;
}
/**
*
* Removes empty paragraphs from the end of Word document.
*
* @param document The Word document
*/
private static void removeEmptyPage(WordDocument document) throws Exception
{
WTextBody textBody = document.getLastSection().getBody();
//A flag to determine any renderable item found in the Word document.
boolean IsRenderableItem = false;
//Iterates text body items.
for (int itemIndex = textBody.getChildEntities().getCount() - 1; itemIndex >= 0 && !IsRenderableItem; itemIndex--)
{
//Check item is empty paragraph and removes it.
if (textBody.getChildEntities().get(itemIndex) instanceof WParagraph)
{
WParagraph paragraph = (WParagraph) textBody.getChildEntities().get(itemIndex);
//Iterates into paragraph
for (int pIndex = paragraph.getItems().getCount() - 1; pIndex >= 0; pIndex--)
{
ParagraphItem paragraphItem = paragraph.getItems().get(pIndex);
//If page break found in end of document, then remove it to preserve contents in same page
if ((paragraphItem instanceof Break
&& ((Break) paragraphItem).getBreakType().getEnumValue() == BreakType.PageBreak.getEnumValue()))
{
paragraph.getItems().removeAt(pIndex);
}
//Check paragraph contains any renderable items.
else if (!(paragraphItem instanceof BookmarkStart || paragraphItem instanceof BookmarkEnd))
{
IsRenderableItem = true;
//Found renderable item and break the iteration.
break;
}
}
//Remove empty paragraph and the paragraph with bookmarks only
if (paragraph.getItems().getCount() == 0 || !IsRenderableItem)
textBody.getChildEntities().removeAt(itemIndex);
}
}
}
/**
* Get the file path
*
* @param path specifies the file path
*/
public static String getDataDir(String path)
{
File dir = new File(System.getProperty("user.dir"));
if (!(dir.toString().endsWith("samples")))
dir = dir.getParentFile();
dir = new File(dir, "resources");
dir = new File(dir, path);
if (dir.isDirectory() == false)
dir.mkdir();
return dir.toString();
}
}