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;
}
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;
}
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);
}
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);
}
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));
}
Aggregations