}
@Override
public T execute(final TaskHandlerActionContext<PrincipalActionContext> inActionContext)
{
ResizeAvatarRequest request = (ResizeAvatarRequest) inActionContext.getActionContext().getParams();
Integer x = request.getX();
Integer y = request.getY();
Integer size = request.getSize();
Boolean refreshFiles = request.getRefreshFiles();
T avatarEntity;
try
{
avatarEntity = finder.findEntity(inActionContext.getActionContext().getPrincipal(), request.getEntityId());
}
catch (Exception e1)
{
throw new ExecutionException(e1);
}
String avatarId = avatarEntity.getAvatarId();
if (refreshFiles)
{
// Delete the three files there now.
imageWriter.delete("n" + avatarId);
imageWriter.delete("s" + avatarId);
String originalPath = "o" + avatarId;
avatarId = hasher.hash(((Long) avatarEntity.getId()).toString());
avatarEntity.setAvatarId(avatarId);
imageWriter.rename(originalPath, "o" + avatarId);
}
avatarEntity.setAvatarCropX(x);
avatarEntity.setAvatarCropY(y);
avatarEntity.setAvatarCropSize(size);
mapper.flush();
try
{
BufferedImage bufferedImage = (BufferedImage) imageWriter.read("o" + avatarId);
imageWriter.write(createResizedCopy(bufferedImage, x, y, NORMAL_AVATAR_SIZE, size), "n" + avatarId);
imageWriter.write(createResizedCopy(bufferedImage, x, y, SMALL_AVATAR_SIZE, size), "s" + avatarId);
}
catch (Exception e)
{
log.error("Error resizing avatar", e);
return null;
}
// if we have a cache updating strategy, call it.
if (cacheUpdaterStategy != null)
{
inActionContext.getUserActionRequests().addAll(
cacheUpdaterStategy.getUpdateCacheRequests(inActionContext.getActionContext().getPrincipal(),
request.getEntityId()));
}
return avatarEntity;
}