|
| 1 | +package com.nuix.javaenginesimple.examples; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.File; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.io.Reader; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Properties; |
| 11 | +import java.util.Set; |
| 12 | +import java.util.function.Consumer; |
| 13 | + |
| 14 | +import org.apache.log4j.Logger; |
| 15 | +import org.apache.log4j.PropertyConfigurator; |
| 16 | +import org.joda.time.DateTime; |
| 17 | + |
| 18 | +import com.nuix.javaenginesimple.EngineWrapper; |
| 19 | +import com.nuix.javaenginesimple.LicenseFilter; |
| 20 | +import com.nuix.javaenginesimple.NuixDiagnostics; |
| 21 | + |
| 22 | +import nuix.Case; |
| 23 | +import nuix.Item; |
| 24 | +import nuix.ReaderReadLogic; |
| 25 | +import nuix.Text; |
| 26 | +import nuix.Utilities; |
| 27 | + |
| 28 | +public class UsingTextExample { |
| 29 | + // Obtain a logger instance for this class |
| 30 | + private final static Logger logger = Logger.getLogger(BasicSearchAndTagExample.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 = BasicSearchAndTagExample.class.getResourceAsStream("/log4j.properties"); |
| 38 | + props.load(log4jSettingsStream); |
| 39 | + PropertyConfigurator.configure(props); |
| 40 | + |
| 41 | + EngineWrapper wrapper = new EngineWrapper("D:\\engine-releases\\8.8.1.131"); |
| 42 | + |
| 43 | + LicenseFilter licenseFilter = wrapper.getLicenseFilter(); |
| 44 | + licenseFilter.setMinWorkers(4); |
| 45 | + licenseFilter.addRequiredFeature("CASE_CREATION"); |
| 46 | + |
| 47 | + String licenseUserName = System.getProperty("License.UserName"); |
| 48 | + String licensePassword = System.getProperty("License.Password"); |
| 49 | + |
| 50 | + if(licenseUserName != null && !licenseUserName.trim().isEmpty()) { |
| 51 | + logger.info(String.format("License username was provided via argument -DLicense.UserName: %s",licenseUserName)); |
| 52 | + } |
| 53 | + |
| 54 | + if(licensePassword != null && !licensePassword.trim().isEmpty()) { |
| 55 | + logger.info("License password was provided via argument -DLicense.Password"); |
| 56 | + } |
| 57 | + |
| 58 | + String query = "flag:audited AND content:*"; |
| 59 | + |
| 60 | + // Contrived example where we will iterate each line of the item's text and when a given |
| 61 | + // line is blank after trimming whitespace, we add 1 to our blank line count, ultimately |
| 62 | + // returning the number of blank lines we encountered. |
| 63 | + ReaderReadLogic<Integer> textOperation = new ReaderReadLogic<Integer>() { |
| 64 | + @Override |
| 65 | + public Integer withReader(Reader reader) throws IOException { |
| 66 | + int blankLineCount = 0; |
| 67 | + BufferedReader buffer = new BufferedReader(reader); |
| 68 | + String line; |
| 69 | + while((line = buffer.readLine()) != null) { |
| 70 | + if(line.trim().isEmpty()) { |
| 71 | + blankLineCount++; |
| 72 | + } |
| 73 | + } |
| 74 | + return blankLineCount; |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + try { |
| 79 | + wrapper.trustAllCertificates(); |
| 80 | + wrapper.withCloudLicense(licenseUserName, licensePassword, new Consumer<Utilities>() { |
| 81 | + public void accept(Utilities utilities) { |
| 82 | + File caseDirectory = new File("D:\\Cases\\MyNuixCase"); |
| 83 | + Case nuixCase = null; |
| 84 | + |
| 85 | + try { |
| 86 | + // Attempt to open the case |
| 87 | + logger.info(String.format("Opening case: %s",caseDirectory.toString())); |
| 88 | + nuixCase = utilities.getCaseFactory().open(caseDirectory); |
| 89 | + logger.info("Case opened"); |
| 90 | + |
| 91 | + Set<Item> items = nuixCase.searchUnsorted(query); |
| 92 | + for(Item item : items) { |
| 93 | + Text itemTextObject = item.getTextObject(); |
| 94 | + // Have our text operation do something with the items text. Since this operation is handed a |
| 95 | + // Reader rather than attempting to construct one solitary string in memory, this operation should |
| 96 | + // behave better when an item has an especially large text value. |
| 97 | + int blankLineCount = itemTextObject.usingText(textOperation); |
| 98 | + |
| 99 | + // Record the number of blank lines we encountered as custom metadata |
| 100 | + item.getCustomMetadata().putInteger("ContentBlankLines", blankLineCount); |
| 101 | + |
| 102 | + logger.info(String.format("%s has %s blank lines in its content text", item.getGuid(), blankLineCount)); |
| 103 | + } |
| 104 | + |
| 105 | + // Note that nuixCase is closed in finally block below |
| 106 | + } catch (IOException exc) { |
| 107 | + logger.error(String.format("Error while opening case: %s",caseDirectory.toString()),exc); |
| 108 | + } finally { |
| 109 | + // Make sure we close the case |
| 110 | + if(nuixCase != null) { |
| 111 | + logger.info(String.format("Closing case: %s",caseDirectory.toString())); |
| 112 | + nuixCase.close(); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + } catch (Exception e) { |
| 119 | + logger.error("Unhandled exception",e); |
| 120 | + NuixDiagnostics.saveDiagnostics("C:\\EngineDiagnostics"); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments