<commit_before>/*
 * Copyright 2015 Aroma Tech.
 *
 * 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 tech.aroma.banana.service.operations;

import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import javax.inject.Inject;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sir.wellington.alchemy.collections.sets.Sets;
import tech.aroma.banana.data.ApplicationRepository;
import tech.aroma.banana.data.UserRepository;
import tech.aroma.banana.thrift.Application;
import tech.aroma.banana.thrift.LengthOfTime;
import tech.aroma.banana.thrift.TimeUnit;
import tech.aroma.banana.thrift.User;
import tech.aroma.banana.thrift.authentication.ApplicationToken;
import tech.aroma.banana.thrift.authentication.AuthenticationToken;
import tech.aroma.banana.thrift.authentication.TokenType;
import tech.aroma.banana.thrift.authentication.UserToken;
import tech.aroma.banana.thrift.authentication.service.AuthenticationService;
import tech.aroma.banana.thrift.authentication.service.CreateTokenRequest;
import tech.aroma.banana.thrift.authentication.service.CreateTokenResponse;
import tech.aroma.banana.thrift.authentication.service.GetTokenInfoRequest;
import tech.aroma.banana.thrift.authentication.service.GetTokenInfoResponse;
import tech.aroma.banana.thrift.exceptions.InvalidArgumentException;
import tech.aroma.banana.thrift.exceptions.InvalidTokenException;
import tech.aroma.banana.thrift.exceptions.OperationFailedException;
import tech.aroma.banana.thrift.service.ProvisionApplicationRequest;
import tech.aroma.banana.thrift.service.ProvisionApplicationResponse;
import tech.sirwellington.alchemy.annotations.access.Internal;
import tech.sirwellington.alchemy.arguments.AlchemyAssertion;
import tech.sirwellington.alchemy.thrift.operations.ThriftOperation;

import static java.time.Instant.now;
import static tech.aroma.banana.data.assertions.AuthenticationAssertions.completeToken;
import static tech.sirwellington.alchemy.arguments.Arguments.checkThat;
import static tech.sirwellington.alchemy.arguments.assertions.Assertions.notNull;

/**
 *
 * @author SirWellington
 */
@Internal
final class ProvisionApplicationOperation implements ThriftOperation<ProvisionApplicationRequest, ProvisionApplicationResponse>
{
    
    private final static Logger LOG = LoggerFactory.getLogger(ProvisionApplicationOperation.class);
    
    private final ApplicationRepository appRepo;
    private final UserRepository userRepo;
    private final AuthenticationService.Iface authenticationService;
    private final Function<AuthenticationToken, ApplicationToken> appTokenMapper;

    @Inject
    ProvisionApplicationOperation(ApplicationRepository appRepo,
                                  UserRepository userRepo,
                                  AuthenticationService.Iface authenticationService,
                                  Function<AuthenticationToken, ApplicationToken> appTokenMapper)
    {
        checkThat(appRepo, userRepo, authenticationService, appTokenMapper)
            .are(notNull());
        
        this.appRepo = appRepo;
        this.userRepo = userRepo;
        this.authenticationService = authenticationService;
        this.appTokenMapper = appTokenMapper;
    }
    
    @Override
    public ProvisionApplicationResponse process(ProvisionApplicationRequest request) throws TException
    {
        LOG.info("Received request to provision an Application", request);
        
        checkThat(request)
            .throwing(ex -> new InvalidArgumentException(ex.getMessage()))
            .is(good());
        
        AuthenticationToken authTokenForUser = getUserTokenFrom(request.token);
        User user = userRepo.getUser(authTokenForUser.ownerId);
        <commit_msg>ProvisionApplicationOperation: Log statement updates<commit_after>/*
 * Copyright 2015 Aroma Tech.
 *
 * 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 tech.aroma.banana.service.operations;

import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import javax.inject.Inject;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sir.wellington.alchemy.collections.sets.Sets;
import tech.aroma.banana.data.ApplicationRepository;
import tech.aroma.banana.data.UserRepository;
import tech.aroma.banana.thrift.Application;
import tech.aroma.banana.thrift.LengthOfTime;
import tech.aroma.banana.thrift.TimeUnit;
import tech.aroma.banana.thrift.User;
import tech.aroma.banana.thrift.authentication.ApplicationToken;
import tech.aroma.banana.thrift.authentication.AuthenticationToken;
import tech.aroma.banana.thrift.authentication.TokenType;
import tech.aroma.banana.thrift.authentication.UserToken;
import tech.aroma.banana.thrift.authentication.service.AuthenticationService;
import tech.aroma.banana.thrift.authentication.service.CreateTokenRequest;
import tech.aroma.banana.thrift.authentication.service.CreateTokenResponse;
import tech.aroma.banana.thrift.authentication.service.GetTokenInfoRequest;
import tech.aroma.banana.thrift.authentication.service.GetTokenInfoResponse;
import tech.aroma.banana.thrift.exceptions.InvalidArgumentException;
import tech.aroma.banana.thrift.exceptions.InvalidTokenException;
import tech.aroma.banana.thrift.exceptions.OperationFailedException;
import tech.aroma.banana.thrift.service.ProvisionApplicationRequest;
import tech.aroma.banana.thrift.service.ProvisionApplicationResponse;
import tech.sirwellington.alchemy.annotations.access.Internal;
import tech.sirwellington.alchemy.arguments.AlchemyAssertion;
import tech.sirwellington.alchemy.thrift.operations.ThriftOperation;

import static java.time.Instant.now;
import static tech.aroma.banana.data.assertions.AuthenticationAssertions.completeToken;
import static tech.sirwellington.alchemy.arguments.Arguments.checkThat;
import static tech.sirwellington.alchemy.arguments.assertions.Assertions.notNull;

/**
 *
 * @author SirWellington
 */
@Internal
final class ProvisionApplicationOperation implements ThriftOperation<ProvisionApplicationRequest, ProvisionApplicationResponse>
{
    
    private final static Logger LOG = LoggerFactory.getLogger(ProvisionApplicationOperation.class);
    
    private final ApplicationRepository appRepo;
    private final UserRepository userRepo;
    private final AuthenticationService.Iface authenticationService;
    private final Function<AuthenticationToken, ApplicationToken> appTokenMapper;

    @Inject
    ProvisionApplicationOperation(ApplicationRepository appRepo,
                                  UserRepository userRepo,
                                  AuthenticationService.Iface authenticationService,
                                  Function<AuthenticationToken, ApplicationToken> appTokenMapper)
    {
        checkThat(appRepo, userRepo, authenticationService, appTokenMapper)
            .are(notNull());
        
        this.appRepo = appRepo;
        this.userRepo = userRepo;
        this.authenticationService = authenticationService;
        this.appTokenMapper = appTokenMapper;
    }
    
    @Override
    public ProvisionApplicationResponse process(ProvisionApplicationRequest request) throws TException
    {
        LOG.info("Received request to provision an Application", request);
        
        checkThat(request)
            .throwing(ex -> new InvalidArgumentException(ex.getMessage()))
            .is(good());
        
        AuthenticationToken authTokenForUser = getUserTokenFrom(request.token);
        User user = userRepo.getUser(authTokenForUser.ownerId);
        
        LOG.debug("Owner ID {} Maps to user {}", authTokenForUser.ownerId, user);
        
{'fraction_non_alphanumeric': 0.07985314364387333, 'fraction_numerical': 0.002294630564479119, 'mean_word_length': 4.3676108374384235, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5016245', 'n_tokens_mistral': 2298, 'n_tokens_neox': 2311, 'n_words': 488}