/*
 *  Copyright (C) 2016  Datamatica (dev@datamatica.pl)
 * 
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published
 *   by the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *  
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Affero General Public License for more details.
 *  
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package pl.datamatica.traccar.api.fcm;

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import pl.datamatica.traccar.api.providers.MailSender;
import pl.datamatica.traccar.model.User;

/**
 *
 * @author Jan Usarek
 */
public class RegistrationConfirmResender extends Thread {

    private Thread t;
    private final String threadName;
    private final String emailTitle;
    private final String emailMsg;
    private final Logger logger;
    private final String className;
    private final MailSender sender;
    private final User user;
    private final static int NUM_OF_TRIES = 3;
    private final static int SLEEP_TIME_SECONDS = 30;
    
    public RegistrationConfirmResender(MailSender sender, User user, String title, String msgContent) {
        this.sender = sender;
        this.user = user;
        emailTitle = title;
        emailMsg = msgContent;
        className = RegistrationConfirmResender.class.getName();
        threadName = className + "-" + user.getId();
        logger = Logger.getLogger(className);
    }

    public void run() {
        try {
            for (int i = 0; i < NUM_OF_TRIES; i++) {
                Thread.sleep(TimeUnit.SECONDS.toMillis(SLEEP_TIME_SECONDS));
                logger.log(Level.INFO,
                        String.format("%s: is trying to resend confirmation. Try %d of %d", threadName, i + 1, NUM_OF_TRIES));
                Boolean sendResult = sender.sendMessage(user.getEmail(), emailTitle, emailMsg);
                if (sendResult) {
                    logger.log(Level.INFO, "Confirmation successfully sent by " + threadName);
                    break;
                }
            }
        } catch (InterruptedException ie) {
            logger.log(Level.SEVERE, String.format("%s: resending confirmation email interrupted: ", className), ie);
        }
        logger.log(Level.INFO, "Confirmation resender " + threadName + " exiting.");
    }

    public void start() {
        logger.log(Level.INFO, "Starting resending confirmation: " + threadName);
        if (t == null) {
            t = new Thread(this, threadName);
            t.start();
        }
    }

}

@@ -21,9 +21,9 @@
  201
-6
+8
   Da
@@ -1444,17 +1444,17 @@
 CONDS = 
-3
+6
 0;%0A    %0A
@@ -1910,14 +1910,15 @@
 i = 
-0
+1
 ; i %3C
+=
  NUM
@@ -2126,16 +2126,36 @@
 irmation
+ for user with id %25d
 . Try %25d
@@ -2164,28 +2164,71 @@
 f %25d
+.
 %22,
- threadName, i + 1
+%0A                                threadName, user.getId(), i
 , NU
@@ -2417,87 +2417,417 @@
 NFO,
- %22Confirmation successfully sent by %22 + threadName);%0A                    break;
+%0A                            String.format(%22Confirmation successfully sent by %25s to user with id %25d.%22, threadName, user.getId()));%0A                    break;%0A                %7D else %7B%0A                    if (i == NUM_OF_TRIES) %7B%0A                        logger.log(Level.SEVERE,%0A                                String.format(%22Resending confirmation for user id %25d failed.%22, user.getId()));%0A                    %7D
 %0A   
@@ -3149,90 +3149,8 @@
 ) %7B%0A
-        logger.log(Level.INFO, %22Starting resending confirmation: %22 + threadName);%0A
     

Correct logging informations in RegistrationConfirmResender.