@@ -192,131 +192,6 @@ static void setup() throws IOException {
192192 }
193193 }
194194
195- /**
196- * Helper method to wait for attachment upload to complete. Polls the attachment metadata until
197- * uploadStatus is "Success" or "Failed", or timeout is reached.
198- *
199- * @param chapterId The chapter ID containing the attachment
200- * @param attachmentId The attachment ID to wait for
201- * @param timeoutSeconds Maximum time to wait in seconds
202- * @param facetName The facet name (attachments, references, footnotes)
203- * @return true if upload completed successfully, false if failed or timed out
204- */
205- private static boolean waitForUploadCompletion (
206- String chapterId , String attachmentId , int timeoutSeconds , String facetName ) {
207- int pollIntervalSeconds = 2 ;
208- int maxAttempts = timeoutSeconds / pollIntervalSeconds ;
209-
210- for (int attempt = 0 ; attempt < maxAttempts ; attempt ++) {
211- try {
212- // Fetch metadata for the attachment in draft mode
213- Map <String , Object > metadata = null ;
214- boolean metadataFetched = false ;
215-
216- try {
217- // First try fetchMetadataDraft for draft entities
218- metadata =
219- api .fetchMetadataDraft (appUrl , chapterEntityName , facetName , chapterId , attachmentId );
220- metadataFetched = true ;
221- } catch (IOException e ) {
222- // If draft fetch fails, entity might be active, try regular fetch
223- try {
224- metadata =
225- api .fetchMetadata (appUrl , chapterEntityName , facetName , chapterId , attachmentId );
226- metadataFetched = true ;
227- } catch (IOException e2 ) {
228- // If both fail, attachment might not exist yet or has been deleted
229- // Wait and retry
230- Thread .sleep (pollIntervalSeconds * 1000 );
231- continue ;
232- }
233- }
234-
235- if (!metadataFetched || metadata == null ) {
236- Thread .sleep (pollIntervalSeconds * 1000 );
237- continue ;
238- }
239-
240- // Check upload status
241- if (metadata .containsKey ("uploadStatus" )) {
242- String uploadStatus = (String ) metadata .get ("uploadStatus" );
243-
244- if ("Success" .equals (uploadStatus )) {
245- return true ;
246- } else if ("Failed" .equals (uploadStatus )) {
247- System .err .println (
248- "Upload failed for attachment "
249- + attachmentId
250- + " in chapter "
251- + chapterId
252- + ". Status: "
253- + uploadStatus );
254- return false ;
255- }
256- // If status is "uploading" or any other status, continue waiting
257- }
258-
259- // Wait before next poll
260- Thread .sleep (pollIntervalSeconds * 1000 );
261-
262- } catch (InterruptedException e ) {
263- Thread .currentThread ().interrupt ();
264- System .err .println ("Wait interrupted for attachment " + attachmentId );
265- return false ;
266- }
267- }
268-
269- // Timeout reached
270- System .err .println (
271- "Timeout waiting for upload completion of attachment "
272- + attachmentId
273- + " in chapter "
274- + chapterId );
275- return false ;
276- }
277-
278- private static boolean waitForAllUploadsCompletion (
279- String chapterId , String facetName , int timeoutSeconds ) {
280- int maxIterations = timeoutSeconds / 2 ; // Check every 2 seconds
281- for (int i = 0 ; i < maxIterations ; i ++) {
282- try {
283- List <Map <String , Object >> attachmentsMetadata =
284- api .fetchEntityMetadataDraft (appUrl , chapterEntityName , facetName , chapterId );
285-
286- boolean allComplete = true ;
287- boolean anyFailed = false ;
288-
289- for (Map <String , Object > metadata : attachmentsMetadata ) {
290- String uploadStatus = (String ) metadata .get ("uploadStatus" );
291- if (uploadStatus == null || "InProgress" .equals (uploadStatus )) {
292- allComplete = false ;
293- } else if ("Failed" .equals (uploadStatus )) {
294- anyFailed = true ;
295- System .err .println ("Upload failed for attachment: " + metadata .get ("ID" ));
296- }
297- }
298-
299- if (anyFailed ) {
300- return false ;
301- }
302-
303- if (allComplete ) {
304- return true ;
305- }
306-
307- // Still uploading, wait before checking again
308- Thread .sleep (5000 );
309- } catch (Exception e ) {
310- System .err .println (
311- "Error checking upload status for chapter " + chapterId + ": " + e .getMessage ());
312- return false ;
313- }
314- }
315-
316- System .err .println ("Upload timed out for chapter: " + chapterId );
317- return false ;
318- }
319-
320195 private String CreateandReturnFacetID (
321196 String appUrl ,
322197 String serviceName ,
0 commit comments