/** @author Dzyuba Orest */
public class ChangePasswordCommand implements ActionCommand {

  private ServiceFactory factory = ServiceFactory.getInstance();
  private UserService service = factory.create(UserService.class);

  @Override
  public void execute(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String email = request.getParameter("email");
    String password = request.getParameter("password");
    String confirmPassword = request.getParameter("confirm_password");
    HttpSession session = request.getSession();
    ResourceBundle bundle = (ResourceBundle) session.getAttribute("bundle");
    if (password.equals(confirmPassword)) {
      User user = service.getByEmail(email);
      if (user != null) {
        user.setPassword(new HashMD5().hash(password));
        service.update(user.getId(), user);
        session.setAttribute(
            "toastr_notification",
            "success|" + UTF8.encoding(bundle.getString("restore" + ".notification.success")));
        response.sendRedirect(ServerLocationUtils.getServerPath(request) + "/login");
        return;
      }
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
    } else {
      request.setAttribute("email", email);
      request.setAttribute(
          "toastr_notification",
          "error|" + UTF8.encoding(bundle.getString("restore" + ".notification.match")));
      request.getRequestDispatcher("jsp/user/pwrestore_newpw.jsp").forward(request, response);
    }
  }
}
/** @author Boyarsky Vitaliy */
public class GetPostedDateCommand implements ActionCommand {

  private static ServiceFactory factory = ServiceFactory.getInstance();
  private static PostingTaskService service = factory.create(PostingTaskService.class);

  @Override
  public void execute(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    int postId = Integer.parseInt(request.getParameter("post_id"));
    PostingTask post = service.getById(postId);
    SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat time = new SimpleDateFormat("HH:mm");
    JSONObject json = new JSONObject();
    {
      json.put("now", date.format(new Date()));
      json.put("date", date.format(post.getPostTime()));
      json.put("time", time.format(post.getPostTime()));
      Date deleteTime = post.getDeleteTime();
      if (deleteTime == null) {
        Date now = new Date(post.getPostTime().getTime() + 60 * 60 * 1000);
        json.put("del_date", date.format(now));
        json.put("del_time", time.format(now));
      } else {
        json.put("del_date", date.format(deleteTime));
        json.put("del_time", time.format(deleteTime));
      }
    }
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().print(json.toString());
  }
}
 public static List<PostingTask> loadingStatistics(List<PostingTask> postingTasks, int userId) {
   if (postingTasks.size() > 0) {
     List<Profile> profiles = factory.create(ProfileService.class).getByUserId(userId);
     Vkontakte vk = new Vkontakte(SocialNetworkCredentialsUtils.getDefaultVkAppsIdAsApps());
     for (Profile profile : profiles) {
       AccessToken accessToken = new AccessToken();
       accessToken.setAccessToken(profile.getAccessToken());
       accessToken.setUserId(profile.getVkId());
       accessToken.setExpirationMoment(profile.getExtTime());
       vk.setAccessToken(accessToken);
       try {
         return vk.execute().getPostStats(postingTasks);
       } catch (VKException e) {
       }
     }
   }
   return postingTasks;
 }
/** @author Boyarsky Vitaliy */
public class GetPostedPostCommand implements ActionCommand {

  private static final Logger LOG = Logger.getLogger(GetPostedPostCommand.class);

  private static ServiceFactory factory = ServiceFactory.getInstance();
  private static PostingTaskService service = factory.create(PostingTaskService.class);

  public static List<PostingTask> loadingStatistics(List<PostingTask> postingTasks, int userId) {
    if (postingTasks.size() > 0) {
      List<Profile> profiles = factory.create(ProfileService.class).getByUserId(userId);
      Vkontakte vk = new Vkontakte(SocialNetworkCredentialsUtils.getDefaultVkAppsIdAsApps());
      for (Profile profile : profiles) {
        AccessToken accessToken = new AccessToken();
        accessToken.setAccessToken(profile.getAccessToken());
        accessToken.setUserId(profile.getVkId());
        accessToken.setExpirationMoment(profile.getExtTime());
        vk.setAccessToken(accessToken);
        try {
          return vk.execute().getPostStats(postingTasks);
        } catch (VKException e) {
        }
      }
    }
    return postingTasks;
  }

  @Override
  public void execute(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    HttpSession session = request.getSession();
    User user = (User) session.getAttribute("user");
    int offset = Integer.parseInt(request.getParameter("iDisplayStart"));
    int limit = Integer.parseInt(request.getParameter("iDisplayLength"));

    Integer wallId = null;
    try {
      wallId = service.getById(Integer.parseInt(request.getParameter("postId"))).getWallId();
    } catch (NumberFormatException ignored) {
    }

    List<PostingTask> posts =
        service.getByUserIdWithParameters(
            user.getId(),
            offset,
            limit,
            "POSTED",
            request.getParameter("sSearch"),
            request.getParameter("sSortDir_0"),
            wallId);
    int postCount =
        service.getCountAllByUserIdWithParameters(
            user.getId(), "POSTED", request.getParameter("sSearch"), wallId);

    JSONObject result = new JSONObject();
    JSONArray array = new JSONArray();

    posts = loadingStatistics(posts, user.getId());
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm");
    for (PostingTask post : posts) {
      try {
        JSONArray row = new JSONArray();
        row.put(
            post.getPost().getMessage().length() > 45
                ? post.getPost().getMessage().substring(0, 42) + "..."
                : post.getPost().getMessage());
        row.put(post.getOwner().getName());
        row.put(post.getFullId());
        row.put(dateFormat.format(post.getPostTime()));
        PostingTaskImpl.Stats stats = post.getStats();
        String data = stats.getLikes() + "|" + stats.getRePosts() + "|" + stats.getComments();
        row.put(data);
        row.put(post.getPostId());
        row.put(post.getId());
        array.put(row);
      } catch (Exception e) {
        LOG.warn("Post id = " + post.getPostId() + " deleted.");
        service.delete(post.getId());
      }
    }
    result.put("iTotalDisplayRecords", postCount);
    result.put("aaData", array);
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().print(result.toString());
  }
}