package org.esupportail.smsuapi.services.sms.impl.olm;

import org.esupportail.commons.services.logging.Logger;
import org.esupportail.commons.services.logging.LoggerImpl;
import org.esupportail.smsuapi.domain.beans.sms.SMSBroker;
import org.esupportail.smsuapi.services.sms.ISMSSender;

import fr.cvf.util.mgs.message.NotificationLevel;
import fr.cvf.util.mgs.mode.sgs.impl.message.request.RequestFactory;
import fr.cvf.util.mgs.mode.sgs.message.request.SMText;

/**
 * Olm implementation of the SMS sender.
 * @author PRQD8824
 *
 */
public class SMSSenderOlmImpl implements ISMSSender {

	/**
	 * Log4j logger.
	 */
	private final Logger logger = new LoggerImpl(getClass());
	
	/**
	 * SMS notification level.
	 */
	private static final int NOTIFICATION_LEVEL = NotificationLevel.FINAL;
	
	/**
	 * Olm connector used to send message.
	 */
	private OlmConnector olmConnector;
	
	/**
	 * use to simulate sending.
	 */
	private boolean simulateMessageSending;
	

	/* (non-Javadoc)
	 * @see org.esupportail.smsuapi.services.sms.ISMSSender
	 * #sendMessage(org.esupportail.smsuapi.domain.beans.sms.SMSMessage)
	 */
	public void sendMessage(final SMSBroker sms) {
		
		final int smsId = sms.getId();
		final String smsRecipient = sms.getRecipient();
		final String smsMessge = sms.getMessage();
		
		if (logger.isDebugEnabled()) {
			logger.debug("Entering into send message with parameter : ");
			logger.debug("   - message id : " + smsId);
			logger.debug("   - message recipient : " + smsRecipient);
			logger.debug("   - message : " + smsMessge);
		}
		
		try {
			final String messageISOLatin = new String(smsMessge.getBytes(),"ISO-8859-1");
			if (logger.isDebugEnabled()) {
				logger.debug("sending encoded message : " + messageISOLatin);
			}

			final SMText smText = RequestFactory.createSMText(smsId, smsRecipient, messageISOLatin);
			smText.setNotificationLevel(NOTIFICATION_LEVEL);
			
			// only send the message if required
			if (!simulateMessageSending) {
				olmConnector.submit(smText);
				if (logger.isDebugEnabled()) {
					logger.debug("message with : " + 
						  " - id : " + smsId + "successfully sent");
				}
			} else {
				logger.warn("Message with id : " + smsId + " not sent because simlation mode is enable");
			}

			
		} catch (Throwable t) {
			logger.error("An error occurs sending SMS : " + 
				     " - id : " + smsId + 
				     " - recipient : " + smsRecipient + 
				     " - message : " + smsMessge);
		}		

	}
	
	
	/*******************
	 * Mutator.
	 *******************/

	/**
	 * Standard setter used by Spring.
	 */
	public void setOlmConnector(final OlmConnector olmConnector) {
		this.olmConnector = olmConnector;
	}
	
	/**
	 * Standard setter used by spring.
	 * @param simulateMessageSending
	 */
	public void setSimulateMessageSending(final boolean simulateMessageSending) {
		this.simulateMessageSending = simulateMessageSending;
	}
}

@@ -1309,16 +1309,17 @@
  smsMess
+a
 ge = sms
@@ -1583,32 +1583,33 @@
 ge : %22 + smsMess
+a
 ge);%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09
@@ -1668,16 +1668,17 @@
 (smsMess
+a
 ge.getBy
@@ -2518,16 +2518,17 @@
  smsMess
+a
 ge);%0D%0A%09%09

code cleanup: fix variable name