}
if(result.hasErrors()){
return new AjaxResult(AjaxResultCode.INVALID,
BindingErrors.from(result));
}
Activity activity = activityRepository.findOne(bean.getActId());
if(activity==null){
throw new RuntimeException("Invalid activity id:" + bean.getActId());
}
// save comment
Comment cmt = Comment.from(bean, signInUser);
cmt.setAct(activity);
cmt = commentRepository.save(cmt);
// incr commented count of orignal activity
activityRepository.inc(activity.getId(), "commentedCount", 1);
// incr comment count of sign in user
userRepository.inc(signInUser.getId(), "commentCount", 1);
if(activity.getTargetSpot()!=null){
Spot spot = spotRepository.findOne(activity.getTargetSpot());
// incr commented count of target spot
spotRepository.inc(spot.getId(), "commentedCount", 1);
}
// save cmt activity
activity = new Activity();
activity.setOwner(signInUser.getId());
activity.setCreatedAt(new Date());
activity.setTargetSpot(activity.getTargetSpot());
activity.setTargetUser(activity.getOwner());
activity.setContent(bean.getContent());
activity.setType(ActivityType.COMMENT);
activity.setBasedOn(bean.getActId());
activity.setBy(sessionUtil.getBy(session));
activityRepository.save(activity);
return new AjaxResult(AjaxResultCode.SUCCESS, cmt);
}