private void syncNewFiles(List<SubAwardAttachments> attachments) {
    assert attachments != null : "the attachments was null";

    for (SubAwardAttachments attachment : attachments) {
      if (SubAwardAttachmentFormBean.doesNewFileExist(attachment)) {
        final AttachmentFile newFile = AttachmentFile.createFromFormFile(attachment.getNewFile());
        // setting the sequence number to the old file sequence number
        if (attachment.getFile() != null) {
          newFile.setSequenceNumber(attachment.getFile().getSequenceNumber());
        }
        attachment.setFile(newFile);
      }
    }
  }
  protected void syncNewFiles(List<CoiDisclosureAttachment> coiDisclosureAttachments) {
    assert coiDisclosureAttachments != null : "the attachments was null";

    for (final CoiDisclosureAttachment attachment : coiDisclosureAttachments) {
      if (CoiNotesAndAttachmentsHelper.doesNewFileExist(attachment)) {
        final AttachmentFile newFile = AttachmentFile.createFromFormFile(attachment.getNewFile());
        // setting the sequence number to the old file sequence number
        if (attachment.getFile() != null) {
          newFile.setSequenceNumber(attachment.getFile().getSequenceNumber());
        }
        attachment.setFile(newFile);
        // set to null, so the subsequent post will not creating new file again
        attachment.setNewFile(null);
      }
    }
  }
  /**
   * Tests that the factory method works correctly with normal input.
   *
   * @throws Exception if bad happens.
   */
  @Test
  public void createFromFormFileBasic() throws Exception {

    final String fileName = "fileName";
    final String fileType = "fileType";
    final byte[] data =
        new byte[] {
          1,
        };

    FormFile formFile = createMockFormFileWithExpectations(fileName, fileType, data);
    AttachmentFile file = AttachmentFile.createFromFormFile(formFile);

    this.context.assertIsSatisfied();

    Assert.assertThat(file.getName(), is(fileName));
    Assert.assertThat(file.getType(), is(fileType));
    Assert.assertThat(file.getData(), is(data));
  }
  /**
   * Test that confirms proper handling of long file names.
   *
   * @throws Exception if bad happens.
   */
  @Test
  public void createFromFormFileLongName() throws Exception {
    final String fileName =
        "fileNamethrows Exceptionthrows Exceptionthrows Exceptionthrows Exceptionthrows Exceptionthrows Exceptionthrows Exceptionthrows Exceptionthrows Exceptionthrows Exception";
    final String fileType = "fileType";
    final byte[] data =
        new byte[] {
          1,
        };

    FormFile formFile = createMockFormFileWithExpectations(fileName, fileType, data);
    AttachmentFile file = AttachmentFile.createFromFormFile(formFile);

    this.context.assertIsSatisfied();

    Assert.assertThat(
        file.getName(),
        is(fileName.substring(fileName.length() - AttachmentFile.MAX_FILE_NAME_LENGTH)));
    Assert.assertThat(file.getType(), is(fileType));
    Assert.assertThat(file.getData(), is(data));
  }
 public void fixReloadedAttachments(Map parameterMap) {
   Iterator keys = parameterMap.keySet().iterator();
   while (keys.hasNext()) {
     String key = keys.next().toString();
     String fieldNameStarter = "coiDisclosureRefreshButtonClicked";
     try {
       if (key.indexOf(fieldNameStarter) > -1) {
         // we have a refresh button checker field
         String fieldValue = ((String[]) parameterMap.get(key))[0];
         if ("T".equals(fieldValue)) {
           // a refresh button has been clicked, now we just need to update the appropriate
           // attachment status code
           int numericVal = Integer.valueOf(key.substring(fieldNameStarter.length()));
           CoiDisclosureAttachment attachment = retrieveExistingAttachmentByType(numericVal);
           FormFile file = attachment.getNewFile();
           if (file == null) return;
           byte[] fileData;
           try {
             fileData = file.getFileData();
             if (fileData.length > 0) {
               AttachmentFile newFile = new AttachmentFile();
               newFile.setType(file.getContentType());
               newFile.setName(file.getFileName());
               newFile.setData(file.getFileData());
               attachment.setFile(newFile);
               getBusinessObjectService().save(attachment);
             }
           } catch (Exception e) {
             LOG.error(e.getMessage(), e);
           }
         }
       }
     } catch (Exception e) {
       LOG.error(e.getMessage(), e);
     }
   }
 }
  /**
   * Tests that IOExceptions are properly handled by wrapping in a CreateException.
   *
   * @throws Exception if bad happens.
   */
  @Test(expected = CreateException.class)
  public void testCreateException() throws Exception {
    final FormFile formFile = this.context.mock(FormFile.class);
    final String fileName = "fileName";
    final String fileType = "fileType";

    this.context.checking(
        new Expectations() {
          {
            one(formFile).getFileName();
            will(returnValue(fileName));

            one(formFile).getContentType();
            will(returnValue(fileType));

            one(formFile).getFileData();
            will(throwException(new IOException()));
          }
        });

    AttachmentFile.createFromFormFile(formFile);
    this.context.assertIsSatisfied();
  }
 /** Tests that factory method properly handles a null form file. */
 @Test(expected = IllegalArgumentException.class)
 public void createFromFormFileNullFormFile() {
   AttachmentFile.createFromFormFile(null);
 }