@@ -413,11 +413,11 @@ public String readAttachment(
413413 Response response = httpClient .newCall (request ).execute ();
414414 if (!response .isSuccessful ()) {
415415 System .out .println (
416- "Read Attachment failed in the"
416+ "Read Attachment failed in the "
417417 + facetName
418418 + " section. Error :"
419419 + response .body ().string ());
420- throw new IOException ("Read Attachment failed in the" + facetName + " section" );
420+ throw new IOException ("Read Attachment failed in the " + facetName + " section" );
421421 }
422422 return "OK" ;
423423 } catch (IOException e ) {
@@ -489,7 +489,7 @@ public String deleteAttachment(
489489 try (Response deleteResponse = httpClient .newCall (request ).execute ()) {
490490 if (deleteResponse .code () != 204 ) {
491491 System .out .println (
492- "Delete Attachment failed in the"
492+ "Delete Attachment failed in the "
493493 + facetName
494494 + " section. Error :"
495495 + deleteResponse .body ().string ());
@@ -532,7 +532,7 @@ public String renameAttachment(
532532 try (Response renameResponse = httpClient .newCall (request ).execute ()) {
533533 if (renameResponse .code () != 200 ) {
534534 System .out .println (
535- "Rename Attachment failed in the"
535+ "Rename Attachment failed in the "
536536 + facetName
537537 + " section. Error : "
538538 + renameResponse .body ().string ());
@@ -631,6 +631,51 @@ public String updateInvalidSecondaryProperty(
631631 }
632632 }
633633
634+ public String copyAttachment (
635+ String appUrl ,
636+ String entityName ,
637+ String facetName ,
638+ String entityID ,
639+ List <String > sourceObjectIds )
640+ throws IOException {
641+ String objectIds = String .join ("," , sourceObjectIds );
642+ String url =
643+ "https://"
644+ + appUrl
645+ + "/odata/v4/"
646+ + serviceName
647+ + "/"
648+ + entityName
649+ + "(ID="
650+ + entityID
651+ + ",IsActiveEntity=false)/"
652+ + facetName
653+ + "/"
654+ + serviceName
655+ + ".copyAttachments" ;
656+
657+ MediaType mediaType = MediaType .parse ("application/json" );
658+
659+ String jsonPayload =
660+ "{" + "\" up__ID\" : \" " + entityID + "\" ," + "\" objectIds\" : \" " + objectIds + "\" " + "}" ;
661+
662+ RequestBody body = RequestBody .create (mediaType , jsonPayload );
663+
664+ Request request =
665+ new Request .Builder ().url (url ).post (body ).addHeader ("Authorization" , token ).build ();
666+
667+ try (Response response = httpClient .newCall (request ).execute ()) {
668+ if (!response .isSuccessful ()) {
669+ throw new IOException (
670+ "Could not copy attachments: " + response .code () + " - " + response .body ().string ());
671+ }
672+ return "Attachments copied successfully" ;
673+ } catch (IOException e ) {
674+ System .out .println ("Error while copying attachments: " + e .getMessage ());
675+ throw new IOException (e );
676+ }
677+ }
678+
634679 public Map <String , Object > fetchMetadata (
635680 String appUrl , String entityName , String facetName , String entityID , String ID )
636681 throws IOException {
@@ -671,4 +716,48 @@ public Map<String, Object> fetchMetadata(
671716 }
672717 }
673718 }
719+
720+ public List <Map <String , Object >> fetchEntityMetadata (
721+ String appUrl , String entityName , String facetName , String entityID ) throws IOException {
722+
723+ // Construct the URL for fetching attachment metadata
724+ String url =
725+ "https://"
726+ + appUrl
727+ + "/odata/v4/"
728+ + serviceName
729+ + "/"
730+ + entityName
731+ + "(ID="
732+ + entityID
733+ + ",IsActiveEntity=true)/"
734+ + facetName ;
735+
736+ // Make a GET request to fetch the attachment metadata
737+ Request request =
738+ new Request .Builder ().url (url ).get ().addHeader ("Authorization" , token ).build ();
739+
740+ try (Response response = httpClient .newCall (request ).execute ()) {
741+ if (response .code () != 200 ) {
742+ System .out .println ("Response code: " + response .code ());
743+ System .out .println (
744+ "Fetch metadata failed for "
745+ + facetName
746+ + " Section. Error: "
747+ + response .body ().string ());
748+ throw new IOException ("Could not fetch " + facetName + " metadata" );
749+ } else {
750+ ObjectMapper objectMapper = new ObjectMapper ();
751+ Map <String , Object > entityData =
752+ objectMapper .readValue (
753+ response .body ().string (), new com .fasterxml .jackson .core .type .TypeReference <>() {});
754+ Object value = entityData .get ("value" );
755+ List <Map <String , Object >> result =
756+ objectMapper .convertValue (
757+ value ,
758+ new com .fasterxml .jackson .core .type .TypeReference <List <Map <String , Object >>>() {});
759+ return result ;
760+ }
761+ }
762+ }
674763}
0 commit comments