<commit_before>/**
 * JBoss, Home of Professional Open Source
 * Copyright Red Hat, Inc., and individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.aerogear.unifiedpush.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.ejb.Asynchronous;
import javax.ejb.DependsOn;
import javax.ejb.Stateless;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;

import org.jboss.aerogear.unifiedpush.api.Alias;
import org.jboss.aerogear.unifiedpush.api.Category;
import org.jboss.aerogear.unifiedpush.api.Installation;
import org.jboss.aerogear.unifiedpush.api.PushApplication;
import org.jboss.aerogear.unifiedpush.api.Variant;
import org.jboss.aerogear.unifiedpush.api.VariantType;
import org.jboss.aerogear.unifiedpush.dao.AliasDao;
import org.jboss.aerogear.unifiedpush.dao.CategoryDao;
import org.jboss.aerogear.unifiedpush.dao.InstallationDao;
import org.jboss.aerogear.unifiedpush.dao.PushApplicationDao;
import org.jboss.aerogear.unifiedpush.dao.ResultsStream;
import org.jboss.aerogear.unifiedpush.service.ClientInstallationService;
import org.jboss.aerogear.unifiedpush.service.Configuration;
import org.jboss.aerogear.unifiedpush.service.VerificationService;
import org.jboss.aerogear.unifiedpush.service.annotations.LoggedIn;
import org.jboss.aerogear.unifiedpush.utils.AeroGearLogger;

/**
 * (Default) implementation of the {@code ClientInstallationService} interface.
 * Delegates work to an injected DAO object.
 */
@Stateless
@DependsOn(value={"Configuration", "VerificationServiceImpl"})
public class ClientInstallationServiceImpl implements ClientInstallationService {
	private static final String ENABLE_VERIFICATION = "aerogear.config.sms.enable_verification";

    private final AeroGearLogger logger = AeroGearLogger.getInstance(ClientInstallationServiceImpl.class);

    @Inject
    private InstallationDao installationDao;

    @Inject
    private CategoryDao categoryDao;
    
    @Inject
    private AliasDao aliasDao;
    
    @Inject
    private PushApplicationDao pushApplicationDao;
    
    @Inject
    @LoggedIn
    private Instance<String> developer;

	@Inject
	private VerificationService verificationService;
	
    @Inject
	private Configuration configuration;
    
	@Override
	public Installation associateInstallation(Installation installation, VariantType installationVariantType) {
		
		if (installation.getAlias() == null) {
			return installation;
		}
		
		Alias alias = aliasDao.findByName(installation.getAlias());
		
		if (alias == null) {
			return installation;
		}
		
		PushApplication application = pushApplicationDao.findByPushApplicationID(alias.getPushApplicationID());
		List<Variant> variants = application.getVariants();
		for (Variant variant : variants) {
			// Match variant type according to previous variant.
			if(variant.getType().equals(installationVariantType)){
				installation.setVariant(variant);
				break;
			}
		}
		
		updateInstallation(installation);
		return installation;
	}

	@Override
	public void addInstallationSynchronously(Variant variant, Installation entity) {
		this.addInstallation(variant, entity);
	}
	
    @Override
    @Asynchronous
    public void addInstallation(Variant variant, Installation entity) {
    	boolean shouldVerifiy = configuration.getProperty(ENABLE_VERIFICATION, false);
    	
        // does it already exist ?
        Installation installation = this.findInstallationForVariantByDeviceToken(variant.getVariantID(), entity.getDeviceToken());

        // Needed for the Admin UI Only. Help for setting up Routes
        entity.setPlatform(variant.getType().getTypeName());

        // new device/client ?
        if (installation == null) {
            logger.finest("Performing new device/client registration");
            
            if (shouldVerifiy)
            	entity.setEnabled(false);
			
            // store the installation:
            storeInstallationAndSetReferences(variant, entity);
            
            // A better implementation would initiate a new REST call when registration is done.
            if (shouldVerifiy)
            	verificationService.initiateDeviceVerification(entity, variant);
        } else {
            // We only update the metadata, if the device is enabled:
            if (installation.isEnabled()) {
                logger.finest("Updating received metadata for an 'enabled' installation");
                // update the entity:
                this.updateInstallation(installation, entity);
            }
        }
    }
<commit_msg>Send Verification code on every /registry/device (CAPP-2190)<commit_after>/**
 * JBoss, Home of Professional Open Source
 * Copyright Red Hat, Inc., and individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.aerogear.unifiedpush.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.ejb.Asynchronous;
import javax.ejb.DependsOn;
import javax.ejb.Stateless;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;

import org.jboss.aerogear.unifiedpush.api.Alias;
import org.jboss.aerogear.unifiedpush.api.Category;
import org.jboss.aerogear.unifiedpush.api.Installation;
import org.jboss.aerogear.unifiedpush.api.PushApplication;
import org.jboss.aerogear.unifiedpush.api.Variant;
import org.jboss.aerogear.unifiedpush.api.VariantType;
import org.jboss.aerogear.unifiedpush.dao.AliasDao;
import org.jboss.aerogear.unifiedpush.dao.CategoryDao;
import org.jboss.aerogear.unifiedpush.dao.InstallationDao;
import org.jboss.aerogear.unifiedpush.dao.PushApplicationDao;
import org.jboss.aerogear.unifiedpush.dao.ResultsStream;
import org.jboss.aerogear.unifiedpush.service.ClientInstallationService;
import org.jboss.aerogear.unifiedpush.service.Configuration;
import org.jboss.aerogear.unifiedpush.service.VerificationService;
import org.jboss.aerogear.unifiedpush.service.annotations.LoggedIn;
import org.jboss.aerogear.unifiedpush.utils.AeroGearLogger;

/**
 * (Default) implementation of the {@code ClientInstallationService} interface.
 * Delegates work to an injected DAO object.
 */
@Stateless
@DependsOn(value={"Configuration", "VerificationServiceImpl"})
public class ClientInstallationServiceImpl implements ClientInstallationService {
	private static final String ENABLE_VERIFICATION = "aerogear.config.sms.enable_verification";

    private final AeroGearLogger logger = AeroGearLogger.getInstance(ClientInstallationServiceImpl.class);

    @Inject
    private InstallationDao installationDao;

    @Inject
    private CategoryDao categoryDao;
    
    @Inject
    private AliasDao aliasDao;
    
    @Inject
    private PushApplicationDao pushApplicationDao;
    
    @Inject
    @LoggedIn
    private Instance<String> developer;

	@Inject
	private VerificationService verificationService;
	
    @Inject
	private Configuration configuration;
    
	@Override
	public Installation associateInstallation(Installation installation, VariantType installationVariantType) {
		
		if (installation.getAlias() == null) {
			return installation;
		}
		
		Alias alias = aliasDao.findByName(installation.getAlias());
		
		if (alias == null) {
			return installation;
		}
		
		PushApplication application = pushApplicationDao.findByPushApplicationID(alias.getPushApplicationID());
		List<Variant> variants = application.getVariants();
		for (Variant variant : variants) {
			// Match variant type according to previous variant.
			if(variant.getType().equals(installationVariantType)){
				installation.setVariant(variant);
				break;
			}
		}
		
		updateInstallation(installation);
		return installation;
	}

	@Override
	public void addInstallationSynchronously(Variant variant, Installation entity) {
		this.addInstallation(variant, entity);
	}
	
    @Override
    @Asynchronous
    public void addInstallation(Variant variant, Installation entity) {
    	boolean shouldVerifiy = configuration.getProperty(ENABLE_VERIFICATION, false);
    	
        // does it already exist ?
        Installation installation = this.findInstallationForVariantByDeviceToken(variant.getVariantID(), entity.getDeviceToken());

        // Needed for the Admin UI Only. Help for setting up Routes
        entity.setPlatform(variant.getType().getTypeName());

        // new device/client ?
        if (installation == null) {
            logger.finest("Performing new device/client registration");
            
            if (shouldVerifiy)
            	entity.setEnabled(false);
			
            // store the installation:
            storeInstallationAndSetReferences(variant, entity);
        } else {
            // We only update the metadata, if the device is enabled:
            if (installation.isEnabled()) {
                logger.finest("Updating received metadata for an 'enabled' installation");
                // update the entity:
                this.updateInstallation(installation, entity);
            }
        }
        
        // A better implementation would initiate a new REST call when registration is done.
        if (shouldVerifiy)
        	verificationService.initiateDeviceVerification(entity, variant);
    }

{'fraction_non_alphanumeric': 0.08462968398395461, 'fraction_numerical': 0.001174053419430584, 'mean_word_length': 3.835383159886471, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3878439', 'n_tokens_mistral': 2953, 'n_tokens_neox': 2845, 'n_words': 700}