public void sortAttachments ( ByteArrayInputStream byteArrayInputStream ) { List < String > attachmentNameList = new ArrayList <> ( ) ; List < AttachmentData > attacmentList = getAttachments ( ) ; List < AttachmentData > tempAttacmentList = new ArrayList <> ( ) ; try { DocumentBuilderFactory domParserFactory = DocumentBuilderFactory . newInstance ( ) ; DocumentBuilder domParser = domParserFactory . newDocumentBuilder ( ) ; Document document = domParser . parse ( byteArrayInputStream ) ; byteArrayInputStream . close ( ) ; NodeList fileLocationList = document . getElementsByTagName ( NARRATIVE_ATTACHMENT_FILE_LOCATION ) ; for ( int itemLocation = 0 ; itemLocation < fileLocationList . getLength ( ) ; itemLocation ++ ) { String attachmentName = fileLocationList . item ( itemLocation ) . getAttributes ( ) . item ( 0 ) . getNodeValue ( ) ; String [ ] name = attachmentName . split ( KEY_VALUE_SEPARATOR ) ; String fileName = name [ name . length - 1 ] ; attachmentNameList . add ( fileName ) ; } } catch ( Exception e ) { LOG . error ( e . getMessage ( ) , e ) ; } for ( String attachmentName : attachmentNameList ) { for ( AttachmentData attachment : attacmentList ) { String [ ] names = attachment . getContentId ( ) . split ( KEY_VALUE_SEPARATOR ) ; String fileName = names [ names . length - 1 ] ; if ( fileName . equalsIgnoreCase ( attachmentName ) ) { tempAttacmentList . add ( attachment ) ; } } } if ( tempAttacmentList . size ( ) > 0 ) { attachments . clear ( ) ; for ( AttachmentData tempAttachment : tempAttacmentList ) { attachments . add ( tempAttachment ) ; } } }
Sort the attachments .