Skip to content

Commit 28d8d2f

Browse files
committed
Create BasicExportExample.java
Add example demonstrating export
1 parent 84db4e9 commit 28d8d2f

1 file changed

Lines changed: 249 additions & 0 deletions

File tree

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
package com.nuix.javaenginesimple.examples;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.Properties;
10+
import java.util.concurrent.atomic.AtomicInteger;
11+
import java.util.function.Consumer;
12+
13+
import org.apache.log4j.Logger;
14+
import org.apache.log4j.PropertyConfigurator;
15+
import org.joda.time.DateTime;
16+
17+
import com.nuix.javaenginesimple.EngineWrapper;
18+
import com.nuix.javaenginesimple.LicenseFilter;
19+
import com.nuix.javaenginesimple.NuixDiagnostics;
20+
21+
import nuix.BatchExporter;
22+
import nuix.Case;
23+
import nuix.Item;
24+
import nuix.ItemEventCallback;
25+
import nuix.ItemEventInfo;
26+
import nuix.Utilities;
27+
28+
public class BasicExportExample {
29+
// Obtain a logger instance for this class
30+
private final static Logger logger = Logger.getLogger(BasicExportExample.class);
31+
32+
public static void main(String[] args) throws Exception {
33+
String logDirectory = String.format("C:\\NuixEngineLogs\\%s",DateTime.now().toString("YYYYMMDD_HHmmss"));
34+
System.getProperties().put("nuix.logdir", logDirectory);
35+
36+
Properties props = new Properties();
37+
InputStream log4jSettingsStream = OpenCaseExample.class.getResourceAsStream("/log4j.properties");
38+
props.load(log4jSettingsStream);
39+
PropertyConfigurator.configure(props);
40+
41+
EngineWrapper wrapper = new EngineWrapper("D:\\engine-releases\\8.4.2.466");
42+
43+
LicenseFilter licenseFilter = wrapper.getLicenseFilter();
44+
licenseFilter.setMinWorkers(4);
45+
licenseFilter.addRequiredFeature("CASE_CREATION");
46+
licenseFilter.addRequiredFeature("EXPORT_ITEMS");
47+
48+
String licenseUserName = System.getProperty("License.UserName");
49+
String licensePassword = System.getProperty("License.Password");
50+
51+
if(licenseUserName != null && !licenseUserName.trim().isEmpty()) {
52+
logger.info(String.format("License username was provided via argument -DLicense.UserName: %s",licenseUserName));
53+
}
54+
55+
if(licensePassword != null && !licensePassword.trim().isEmpty()) {
56+
logger.info("License password was provided via argument -DLicense.Password");
57+
}
58+
59+
try {
60+
wrapper.trustAllCertificates();
61+
wrapper.withCloudLicense(licenseUserName, licensePassword, new Consumer<Utilities>() {
62+
public void accept(Utilities utilities) {
63+
File caseDirectory = new File("D:\\Cases\\MyNuixCase");
64+
File exportDirectory = new File("D:\\Exports\\MyNuixExport");
65+
66+
Case nuixCase = null;
67+
68+
try {
69+
// Attempt to open the case
70+
logger.info(String.format("Opening case: %s",caseDirectory.toString()));
71+
nuixCase = utilities.getCaseFactory().open(caseDirectory);
72+
logger.info("Case opened");
73+
74+
BatchExporter exporter = utilities.createBatchExporter(exportDirectory);
75+
76+
// We will using the same naming type for all products, possible choices:
77+
// - "document_id" (e.g. "ABC-000000001.pdf"), requires license feature EXPORT_LEGAL
78+
// - "document_id_with_page" (e.g. "ABC-000000001_11.pdf"), requires license feature EXPORT_LEGAL
79+
// - "page_only" (e.g. "0001.pdf"), requires license feature EXPORT_LEGAL
80+
// - "full" (e.g. "ABC0010010001.pdf"), requires license feature EXPORT_LEGAL
81+
// - "full_with_periods" (e.g. "ABC.001.001.0001.pdf"), requires license feature EXPORT_LEGAL
82+
// - "item_name" (e.g. "original-name.pdf"), requires license feature EXPORT_ITEMS
83+
// - "item_name_with_path" (e.g. "mailbox/inbox/original-name.pdf"), requires license feature EXPORT_ITEMS
84+
// - "guid" (e.g. "04d/04dd8e72-f087-4f66-848a-6585bce732d5.pdf"), requires license feature EXPORT_ITEMS
85+
// - "md5" (e.g. "790/79054025255fb1a26e4bc422aef54eb4.pdf"), requires license feature EXPORT_ITEMS
86+
String productNaming = "guid";
87+
88+
// ======================================
89+
// * Add and Configure Native Exporting *
90+
// ======================================
91+
92+
logger.info("Configuring settings for NATIVES...");
93+
94+
Map<String,Object> nativeSettings = new HashMap<String,Object>();
95+
nativeSettings.put("naming",productNaming);
96+
nativeSettings.put("path","NATIVES");
97+
nativeSettings.put("mailFormat","eml");
98+
nativeSettings.put("includeAttachments",true);
99+
100+
// Add native product to our exporter
101+
exporter.addProduct("native", nativeSettings);
102+
103+
// =========================================
104+
// * Add and Configure Text File Exporting *
105+
// =========================================
106+
107+
logger.info("Configuring settings for TEXT...");
108+
109+
Map<String,Object> textFileSettings = new HashMap<String,Object>();
110+
textFileSettings.put("naming",productNaming);
111+
textFileSettings.put("path","TEXT");
112+
113+
// Add native product to our exporter
114+
exporter.addProduct("text", textFileSettings);
115+
116+
// ===================================
117+
// * Add and Configure PDF Exporting *
118+
// ===================================
119+
120+
logger.info("Configuring settings for PDFs...");
121+
122+
Map<String,Object> pdfSettings = new HashMap<String,Object>();
123+
pdfSettings.put("naming",productNaming);
124+
pdfSettings.put("path","PDF");
125+
pdfSettings.put("regenerateStored",false);
126+
127+
// Add native product to our exporter
128+
exporter.addProduct("pdf", pdfSettings);
129+
130+
// ====================================
131+
// * Add and Configure TIFF Exporting *
132+
// ====================================
133+
134+
logger.info("Configuring settings for TIFFs...");
135+
136+
Map<String,Object> tiffSettings = new HashMap<String,Object>();
137+
tiffSettings.put("naming",productNaming);
138+
tiffSettings.put("path","TIFF");
139+
tiffSettings.put("regenerateStored",false);
140+
tiffSettings.put("multiPageTiff",false);
141+
tiffSettings.put("tiffDpi",300);
142+
tiffSettings.put("tiffFormat","MONOCHROME_CCITT_T6_G4");
143+
144+
// Add native product to our exporter
145+
exporter.addProduct("tiff", tiffSettings);
146+
147+
// ================================
148+
// * Add Concordance DAT Loadfile *
149+
// ================================
150+
151+
logger.info("Configuring settings for Concordance DAT...");
152+
153+
Map<String,Object> concordanceLoadfileSettings = new HashMap<String,Object>();
154+
concordanceLoadfileSettings.put("metadataProfile","Default");
155+
concordanceLoadfileSettings.put("encoding","UTF-8");
156+
157+
exporter.addLoadFile("concordance",concordanceLoadfileSettings);
158+
159+
// =============================
160+
// * Configure Worker Settings *
161+
// =============================
162+
163+
// Now we are going to instruct the processor how many workers to use to load the data. See API documentation for
164+
// ParallelProcessingConfigurable.setParallelProcessingSettings for list of settings and what they do.
165+
Map<String,Object> parallelProcessingSettings = new HashMap<String,Object>();
166+
int licenseWorkerCount = utilities.getLicence().getWorkers();
167+
parallelProcessingSettings.put("workerCount",licenseWorkerCount);
168+
parallelProcessingSettings.put("workerTemp","D:\\WorkerTemp");
169+
170+
exporter.setParallelProcessingSettings(parallelProcessingSettings);
171+
172+
// =============================
173+
// * Hook Up Progress Callback *
174+
// =============================
175+
176+
logger.info("Setting up progress callback...");
177+
178+
// Track error count
179+
AtomicInteger errorCount = new AtomicInteger();
180+
181+
exporter.whenItemEventOccurs(new ItemEventCallback() {
182+
183+
// Use this to track when we reported progress last
184+
long lastProgressMillis = System.currentTimeMillis();
185+
186+
@Override
187+
public void itemProcessed(ItemEventInfo info) {
188+
// Report progress if it has been at least 5 seconds (5000 milliseconds) since
189+
// the last time we reported progress
190+
long currentTimeMillis = System.currentTimeMillis();
191+
if(currentTimeMillis - lastProgressMillis > 5 * 1000) {
192+
String progressMessage = String.format(
193+
"Stage: %s, Progress: %s, Errors: %s",
194+
info.getStage(), info.getStageCount(), errorCount.get());
195+
logger.info(progressMessage);
196+
lastProgressMillis = System.currentTimeMillis();
197+
}
198+
199+
// If this particular item had an error we always record this
200+
Exception possibleItemException = info.getFailure();
201+
if(possibleItemException != null) {
202+
Item item = info.getItem();
203+
errorCount.incrementAndGet();
204+
String errorMessage = String.format(
205+
"Error while exporting item %s/%s: %s",
206+
item.getGuid(), item.getLocalisedName(), possibleItemException.getMessage());
207+
logger.error(errorMessage);
208+
}
209+
}
210+
});
211+
212+
// ===================
213+
// * Begin Exporting *
214+
// ===================
215+
216+
String itemsToExportQuery = "kind:email AND flag:top_level";
217+
218+
logger.info(String.format("Searching: %s",itemsToExportQuery));
219+
List<Item> itemsToExport = nuixCase.search(itemsToExportQuery);
220+
logger.info(String.format("Responsive Items: %s",itemsToExport.size()));
221+
222+
logger.info("Beginning export...");
223+
exporter.exportItems(itemsToExport);
224+
logger.info("Export completed");
225+
226+
logger.info(String.format("Errors: %s",errorCount.get()));
227+
if(errorCount.get() > 0) {
228+
logger.info("Review logs for more details regarding export errors");
229+
}
230+
231+
// Note that nuixCase is closed in finally block below
232+
} catch (IOException exc) {
233+
logger.error(String.format("Error while opening case: %s",caseDirectory.toString()),exc);
234+
} finally {
235+
// Make sure we close the case
236+
if(nuixCase != null) {
237+
logger.info(String.format("Closing case: %s",caseDirectory.toString()));
238+
nuixCase.close();
239+
}
240+
}
241+
}
242+
});
243+
244+
} catch (Exception e) {
245+
logger.error("Unhandled exception",e);
246+
NuixDiagnostics.saveDiagnostics("C:\\EngineDiagnostics");
247+
}
248+
}
249+
}

0 commit comments

Comments
 (0)