Search in sources :

Example 6 with ClientPhoto

use of org.mifos.framework.image.domain.ClientPhoto in project head by mifos.

the class ClientPhotoServiceDatabase method update.

@Override
public boolean update(Long clientId, InputStream in) {
    boolean transactionStarted = false;
    if (in == null) {
        return false;
    }
    try {
        final ClientPhoto clientPhoto = this.read(clientId);
        if (clientPhoto == null) {
            //create new picture
            return this.create(clientId, in);
        } else {
            //get picture
            final Blob blob = this.createBlob(in);
            final String contentType = this.determineContentType(in);
            //update data
            final ImageInfo imageInfo = clientPhoto.getImageInfo();
            imageInfo.setContentType(contentType);
            imageInfo.setLength(blob.length());
            imageInfo.getCustomerPictureEntity().setPicture(blob);
            this.hibernateTransactionHelper.startTransaction();
            transactionStarted = true;
            this.genericDao.createOrUpdate(clientPhoto);
            this.hibernateTransactionHelper.commitTransaction();
        }
    } catch (Exception ex) {
        if (transactionStarted) {
            hibernateTransactionHelper.rollbackTransaction();
        }
        LOG.error("Unable to update picture", ex);
        return false;
    }
    return true;
}
Also used : ClientPhoto(org.mifos.framework.image.domain.ClientPhoto) Blob(java.sql.Blob) ImageInfo(org.mifos.framework.image.domain.ImageInfo) IOException(java.io.IOException) SQLException(java.sql.SQLException)

Example 7 with ClientPhoto

use of org.mifos.framework.image.domain.ClientPhoto in project head by mifos.

the class ClientPhotoServiceFileSystem method create.

@Override
public boolean create(Long clientId, InputStream in) {
    try {
        ImageInfo imInfo = ImageStorageManager.createImage(in, clientId.toString());
        if (imInfo == null) {
            return false;
        }
        hibernateTransactionHelper.startTransaction();
        ClientPhoto clientPhoto = new ClientPhoto();
        clientPhoto.setClientId(clientId);
        clientPhoto.setImageInfo(imInfo);
        genericDao.getSession().save(clientPhoto);
        hibernateTransactionHelper.commitTransaction();
    } catch (IOException e) {
        LOG.error("Unable to persist", e);
        return false;
    } catch (Exception e) {
        hibernateTransactionHelper.rollbackTransaction();
        throw new BusinessRuleException(ClientConstants.INVALID_PHOTO, new Object[] { e.getMessage() }, e);
    }
    return true;
}
Also used : ClientPhoto(org.mifos.framework.image.domain.ClientPhoto) BusinessRuleException(org.mifos.service.BusinessRuleException) IOException(java.io.IOException) ImageInfo(org.mifos.framework.image.domain.ImageInfo) BusinessRuleException(org.mifos.service.BusinessRuleException) IOException(java.io.IOException)

Example 8 with ClientPhoto

use of org.mifos.framework.image.domain.ClientPhoto in project head by mifos.

the class PictureFormFile method retrievePicture.

@TransactionDemarcate(joinToken = true)
public ActionForward retrievePicture(ActionMapping mapping, @SuppressWarnings("unused") ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    ClientBO clientBO = getClientFromSession(request);
    ClientPhotoService cps = ApplicationContextProvider.getBean(ClientPhotoService.class);
    ClientPhoto cp = cps.read(clientBO.getCustomerId().longValue());
    InputStream in = null;
    if (cp != null) {
        in = new ByteArrayInputStream(cps.getData(cp));
        response.setContentType(cp.getImageInfo().getContentType());
    } else {
        in = ClientPhotoService.class.getResourceAsStream("/org/mifos/image/nopicture.png");
        response.setContentType("image/png");
    }
    in.mark(0);
    BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
    // 4K buffer buf, 0, buf.length
    byte[] by = new byte[1024 * 4];
    int index = in.read(by, 0, 1024 * 4);
    while (index != -1) {
        out.write(by, 0, index);
        index = in.read(by, 0, 1024 * 4);
    }
    out.flush();
    out.close();
    in.reset();
    String forward = ClientConstants.CUSTOMER_PICTURE_PAGE;
    return mapping.findForward(forward);
}
Also used : ClientPhoto(org.mifos.framework.image.domain.ClientPhoto) ClientPhotoService(org.mifos.framework.image.service.ClientPhotoService) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ClientBO(org.mifos.customers.client.business.ClientBO) BufferedOutputStream(java.io.BufferedOutputStream) TransactionDemarcate(org.mifos.framework.util.helpers.TransactionDemarcate)

Example 9 with ClientPhoto

use of org.mifos.framework.image.domain.ClientPhoto in project head by mifos.

the class ClientPhotoServiceDatabaseIntegrationTest method TestCRUD.

@Test
public void TestCRUD() {
    final Long clientId = (long) this.testClient.getCustomerId().intValue();
    /* Create */
    final String data = "test string";
    InputStream in = new ByteArrayInputStream(data.getBytes());
    this.clientPhotoService.create(clientId, in);
    ClientPhoto cp = this.clientPhotoService.read(clientId);
    Assert.assertEquals(clientId, cp.getClientId());
    ImageInfo imageInfo = cp.getImageInfo();
    String path = imageInfo.getPath();
    Assert.assertNotNull(imageInfo.getCustomerPictureEntity().getPicture());
    Assert.assertNotNull(imageInfo.getContentType());
    Assert.assertEquals(data.length(), imageInfo.getLength().intValue());
    Assert.assertEquals(data, new String(this.clientPhotoService.getData(cp)));
    /* Update */
    final String otherData = "other test string";
    in = new ByteArrayInputStream(otherData.getBytes());
    this.clientPhotoService.update(clientId, in);
    cp = this.clientPhotoService.read(clientId);
    Assert.assertEquals(path, imageInfo.getPath());
    Assert.assertNotNull(imageInfo.getContentType());
    Assert.assertEquals(otherData.length(), imageInfo.getLength().intValue());
    Assert.assertEquals(otherData, new String(this.clientPhotoService.getData(cp)));
    Assert.assertTrue(this.clientPhotoService.delete(clientId));
    /* Delete */
    this.clientPhotoService.delete(clientId);
    cp = this.clientPhotoService.read(clientId);
    Assert.assertNull(cp);
}
Also used : ClientPhoto(org.mifos.framework.image.domain.ClientPhoto) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ImageInfo(org.mifos.framework.image.domain.ImageInfo) Test(org.junit.Test)

Example 10 with ClientPhoto

use of org.mifos.framework.image.domain.ClientPhoto in project head by mifos.

the class ClientPhotoServiceIntegrationTest method testCRUD.

@Test
public void testCRUD() {
    String data = "test string";
    InputStream in = new ByteArrayInputStream(data.getBytes());
    Long clientId = 2342L;
    clientPhotoService.create(clientId, in);
    ClientPhoto cp = clientPhotoService.read(2342L);
    Assert.assertEquals(clientId, cp.getClientId());
    ImageInfo imageInfo = cp.getImageInfo();
    String path = imageInfo.getPath();
    Assert.assertNotNull(path);
    Assert.assertNotNull(imageInfo.getContentType());
    Assert.assertEquals(data.length(), imageInfo.getLength().intValue());
    Assert.assertEquals(data, new String(clientPhotoService.getData(cp)));
    String otherData = "other test string";
    in = new ByteArrayInputStream(otherData.getBytes());
    clientPhotoService.update(clientId, in);
    Assert.assertEquals(path, imageInfo.getPath());
    Assert.assertNotNull(imageInfo.getContentType());
    Assert.assertEquals(otherData.length(), imageInfo.getLength().intValue());
    Assert.assertEquals(otherData, new String(clientPhotoService.getData(cp)));
    Assert.assertTrue(clientPhotoService.delete(clientId));
}
Also used : ClientPhoto(org.mifos.framework.image.domain.ClientPhoto) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ImageInfo(org.mifos.framework.image.domain.ImageInfo) Test(org.junit.Test)

Aggregations

ClientPhoto (org.mifos.framework.image.domain.ClientPhoto)10 ImageInfo (org.mifos.framework.image.domain.ImageInfo)7 IOException (java.io.IOException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 SQLException (java.sql.SQLException)3 Blob (java.sql.Blob)2 Test (org.junit.Test)2 BusinessRuleException (org.mifos.service.BusinessRuleException)2 BufferedOutputStream (java.io.BufferedOutputStream)1 ClientBO (org.mifos.customers.client.business.ClientBO)1 CustomerPictureEntity (org.mifos.customers.client.business.CustomerPictureEntity)1 ClientPhotoDto (org.mifos.dto.screen.ClientPhotoDto)1 ClientPhotoService (org.mifos.framework.image.service.ClientPhotoService)1 TransactionDemarcate (org.mifos.framework.util.helpers.TransactionDemarcate)1