public void renameAttachmentTo ( String newAttachmentName ) throws Exception { JSONObject doc = documentDescriptor . getJson ( ) ; JSONObject _attachments = doc . optJSONObject ( "_attachments" ) ; JSONObject nunaliit_attachments = doc . getJSONObject ( UploadConstants . KEY_DOC_ATTACHMENTS ) ; JSONObject files = nunaliit_attachments . getJSONObject ( "files" ) ; // Verify no collision within nunaliit_attachments.files { Object obj = files . opt ( newAttachmentName ) ; if ( null != obj ) { throw new Exception ( "Can not rename attachment because of name collision" ) ; } } // Verify no collision within _attachments if ( null != _attachments ) { Object obj = _attachments . opt ( newAttachmentName ) ; if ( null != obj ) { throw new Exception ( "Can not rename attachment because of name collision" ) ; } } // Not allowed to move a stub if ( null != _attachments ) { JSONObject att = _attachments . optJSONObject ( attName ) ; if ( null != att ) { throw new Exception ( "Can not rename attachment descriptor because file is already attached" ) ; } } // Move descriptor to new name String oldAttachmentName = attName ; JSONObject descriptor = files . getJSONObject ( oldAttachmentName ) ; files . remove ( oldAttachmentName ) ; files . put ( newAttachmentName , descriptor ) ; attName = newAttachmentName ; setStringAttribute ( UploadConstants . ATTACHMENT_NAME_KEY , newAttachmentName ) ; // Loop through all attachment descriptors, updating "source", "thumbnail" // and "original" attributes { Iterator < ? > it = files . keys ( ) ; while ( it . hasNext ( ) ) { Object objKey = it . next ( ) ; if ( objKey instanceof String ) { String key = ( String ) objKey ; JSONObject att = files . getJSONObject ( key ) ; { String value = att . optString ( "source" , null ) ; if ( oldAttachmentName . equals ( value ) ) { att . put ( "source" , newAttachmentName ) ; } } { String value = att . optString ( "thumbnail" , null ) ; if ( oldAttachmentName . equals ( value ) ) { att . put ( "thumbnail" , newAttachmentName ) ; } } { String value = att . optString ( "originalAttachment" , null ) ; if ( oldAttachmentName . equals ( value ) ) { att . put ( "original" , newAttachmentName ) ; } } } } } }
Renames an attachment . This ensures that there is no collision and that all references to this attachment are updated properly within this document .