// snippet-sourcedescription:[StartDocumentAnalysis.java demonstrates how to start the asynchronous analysis of a document.]
// snippet-keyword:[AWS SDK for Java v2]
// snippet-service:[Amazon Textract]
// snippet-keyword:[Code Sample]
// snippet-sourcetype:[full-example]
// snippet-sourcedate:[09/29/2021]
// snippet-sourceauthor:[scmacdon - AWS]

/*
   Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
   SPDX-License-Identifier: Apache-2.0
*/


package com.example.textract;

// snippet-start:[textract.java2._start_doc_analysis.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.textract.model.S3Object;
import software.amazon.awssdk.services.textract.TextractClient;
import software.amazon.awssdk.services.textract.model.StartDocumentAnalysisRequest;
import software.amazon.awssdk.services.textract.model.DocumentLocation;
import software.amazon.awssdk.services.textract.model.TextractException;
import software.amazon.awssdk.services.textract.model.StartDocumentAnalysisResponse;
import software.amazon.awssdk.services.textract.model.GetDocumentAnalysisRequest;
import software.amazon.awssdk.services.textract.model.GetDocumentAnalysisResponse;
import software.amazon.awssdk.services.textract.model.FeatureType;
import java.util.ArrayList;
import java.util.List;
// snippet-end:[textract.java2._start_doc_analysis.import]

public class StartDocumentAnalysis {

    public static void main(String[] args) {

        final String USAGE = "\n" +
                "Usage:\n" +
                "    <bucketName> <docName> \n\n" +
                "Where:\n" +
                "    bucketName - the name of the Amazon S3 bucket that contains the document. \n\n" +
                "    docName - the document name (must be an image, for example, book.png). \n";

        if (args.length != 2) {
            System.out.println(USAGE);
            System.exit(1);
        }

        Region region = Region.US_WEST_2;
        TextractClient textractClient = TextractClient.builder()
                .region(region)
                .build();

        String bucketName = args[0];
        String docName = args[1];

        startDocAnalysisS3 (textractClient, bucketName, docName);
        textractClient.close();
    }

    // snippet-start:[textract.java2._start_doc_analysis.main]
    public static void startDocAnalysisS3 (TextractClient textractClient, String bucketName, String docName) {

        try {

            List<FeatureType> myList = new ArrayList<FeatureType>();
            myList.add(FeatureType.TABLES);
            myList.add(FeatureType.FORMS);

            S3Object s3Object = S3Object.builder()
                    .bucket(bucketName)
                    .name(docName)
                    .build();

            DocumentLocation location = DocumentLocation.builder()
                    .s3Object(s3Object)
                    .build();

            StartDocumentAnalysisRequest documentAnalysisRequest = StartDocumentAnalysisRequest.builder()
                    .documentLocation(location)
                    .featureTypes(myList)
                    .build();

            StartDocumentAnalysisResponse response = textractClient.startDocumentAnalysis(documentAnalysisRequest);

            // Get the job ID
            String jobId = response.jobId();

            String result = getJobResults(textractClient,jobId);

            System.out.println("The status of the job is: "+result);

        } catch (TextractException e) {

            System.err.println(e.getMessage());
            System.exit(1);
        }
    }

    private static String getJobResults(TextractClient textractClient, String jobId) {

        GetDocumentAnalysisRequest analysisRequest = GetDocumentAnalysisRequest.builder()
                .jobId(jobId)
                .maxResults(1000)
                .build();

        GetDocumentAnalysisResponse response = textractClient.getDocumentAnalysis(analysisRequest);
        String status = response.jobStatus().toString();

        return status;
    }
    // snippet-end:[textract.java2._start_doc_analysis.main]
}
@@ -1967,24 +1967,97 @@
        %7D%0D%0A%0D%0A
+        String bucketName = args%5B0%5D;%0D%0A        String docName = args%5B1%5D;%0D%0A
         Regi
@@ -2219,83 +2219,8 @@
 %0D%0A%0D%0A
-        String bucketName = args%5B0%5D;%0D%0A        String docName = args%5B1%5D;%0D%0A%0D%0A
     
@@ -3408,26 +3408,24 @@
 e.jobId();%0D%0A
-%0D%0A
             
@@ -3474,26 +3474,24 @@
 nt,jobId);%0D%0A
-%0D%0A
             
@@ -3518,26 +3518,19 @@
 The 
+job 
 status 
-of the job 
 is: 
@@ -3770,24 +3770,163 @@
 jobId) %7B%0D%0A%0D%0A
+        boolean finished = false;%0D%0A        int index = 0 ;%0D%0A        String status = %22%22 ;%0D%0A%0D%0A       try %7B%0D%0A        while (!finished) %7B%0D%0A    
         GetD
@@ -4020,16 +4020,20 @@
         
+    
 .jobId(j
@@ -4039,16 +4039,20 @@
 jobId)%0D%0A
+    
         
@@ -4086,32 +4086,36 @@
                 
+    
 .build();%0D%0A%0D%0A   
@@ -4103,32 +4103,36 @@
    .build();%0D%0A%0D%0A
+    
         GetDocum
@@ -4216,38 +4216,35 @@
 uest);%0D%0A        
+  
-String
+ 
  status = respon
@@ -4285,21 +4285,411 @@
     
-return status
+    if (status.compareTo(%22SUCCEEDED%22) == 0)%0D%0A                finished = true;%0D%0A            else %7B%0D%0A                System.out.println(index + %22 status is: %22 + status);%0D%0A                Thread.sleep(1000);%0D%0A            %7D%0D%0A        %7D%0D%0A        return status;%0D%0A%0D%0A       %7D catch( InterruptedException e) %7B%0D%0A           System.out.println(e.getMessage());%0D%0A           System.exit(1);%0D%0A       %7D%0D%0A       return %22%22
 ;%0D%0A 
@@ -4757,8 +4757,10 @@
 main%5D%0D%0A%7D
+%0D%0A

Update StartDocumentAnalysis.java