package org.jboss.aerogear.unifiedpush.service.impl;

import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.enterprise.inject.Alternative;
import javax.inject.Inject;

import org.jboss.aerogear.unifiedpush.api.Installation;
import org.jboss.aerogear.unifiedpush.api.Variant;
import org.jboss.aerogear.unifiedpush.service.ClientInstallationService;
import org.jboss.aerogear.unifiedpush.service.VerificationService;

/**
 * An alternative class which disables the installation prior to adding it. Once the installation is added,
 * this implementation fires a verification request to the newly added installation.
 */
@Stateless
@Alternative
public class VerifyingClientInstallationServiceImpl extends ClientInstallationServiceImpl implements ClientInstallationService {
	
	@Inject
	private VerificationService verificationService;
	
	// TODO: this logic might be worth moving to ClientInstallationServiceImpl itself (and then removing this class).
	// The decision whether to verify installations or not could be made part of the Application.
	@Override
    @Asynchronous
    public void addInstallation(Variant variant, Installation entity) {
		entity.setEnabled(false);
		super.addInstallation(variant, entity);
		verificationService.initiateDeviceVerification(entity);
	}
}

@@ -359,16 +359,77 @@
 ervice;%0A
+import org.jboss.aerogear.unifiedpush.service.Configuration;%0A
 import o
@@ -487,16 +487,16 @@
 ervice;%0A
-
 %0A/**%0A * 
@@ -844,16 +844,161 @@
 ice %7B%0A%09%0A
+%09private static final String ENABLE_VERIFICATION = %22aerogear.config.enable_sms_verification%22;%0A%09%0A%09@Inject%0A%09private Configuration configuration;%0A%09%0A
 %09@Inject
@@ -1363,16 +1363,134 @@
 ty) %7B%0A%09%09
+%0A%09%09boolean shouldVerifiy = configuration.getBooleanProperty(ENABLE_VERIFICATION, false);%0A%09%09%0A%09%09if (shouldVerifiy) %7B%0A%09%09%09
 entity.s
@@ -1509,16 +1509,17 @@
 lse);%0A%09%09
+%09
 super.ad
@@ -1550,16 +1550,17 @@
 ntity);%0A
+%09
 %09%09verifi
@@ -1609,13 +1609,71 @@
 ntity);%0A
+%09%09%7D else %7B%0A%09%09%09super.addInstallation(variant, entity);%0A%09%09%7D%0A
 %09%7D%0A%7D%0A

Check if verification is enabled