package org.radarcns.management.service;


import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Optional;

import org.radarcns.management.config.ManagementPortalProperties;
import org.radarcns.management.domain.MetaToken;
import org.radarcns.management.repository.MetaTokenRepository;
import org.radarcns.management.service.dto.TokenDTO;
import org.radarcns.management.web.rest.errors.CustomNotFoundException;
import org.radarcns.management.web.rest.errors.ErrorConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by nivethika.
 *
 * <p>Service to delegate MetaToken handling.</p>
 *
 */
@Service
@Transactional
public class MetaTokenService {

    private final Logger log = LoggerFactory.getLogger(MetaTokenService.class);

    @Autowired
    private MetaTokenRepository metaTokenRepository;

    @Autowired
    private ManagementPortalProperties managementPortalProperties;

    /**
     * Save a metaToken.
     *
     * @param metaToken the entity to save
     * @return the persisted entity
     */
    public MetaToken save(MetaToken metaToken) {
        log.debug("Request to save MetaToken : {}", metaToken);
        return metaTokenRepository.save(metaToken);
    }

    /**
     * Get one project by id.
     *
     * @param tokenName the id of the entity
     * @return the entity
     * @throws CustomNotFoundException if there is no project with the given id
     */
    @Transactional()
    public TokenDTO fetchToken(String tokenName) throws CustomNotFoundException,
            MalformedURLException {
        log.debug("Request to get Token : {}", tokenName);
        Optional<MetaToken> fetchedToken = metaTokenRepository.findOneByTokenName(tokenName);

        if (fetchedToken.isPresent()) {
            // get token from database
            MetaToken metaToken = fetchedToken.get();
            // create response
            TokenDTO result = new TokenDTO(metaToken.getToken(),
                    new URL(managementPortalProperties.getMail().getBaseUrl()));
            // change fetched status to true. This will be cleared later on.
            metaTokenRepository.save(metaToken.fetched(true));
            // return result
            return result;
        } else {
            throw new CustomNotFoundException(
                ErrorConstants.ERR_TOKEN_NOT_FOUND,
                Collections.singletonMap("tokenName", tokenName));
        }

    }


}

@@ -453,24 +453,101 @@
 dException;%0A
+import org.radarcns.management.web.rest.errors.CustomParameterizedException;%0A
 import org.r
@@ -1716,29 +1716,8 @@
  */%0A
-    @Transactional()%0A
     
@@ -1900,17 +1900,8 @@
     
-Optional%3C
 Meta
@@ -1905,17 +1905,16 @@
 etaToken
-%3E
  fetched
@@ -1925,46 +1925,16 @@
 n = 
-m
+g
 et
-a
 Token
-Repository.findOneByTokenName
 (tok
@@ -1955,16 +1955,17 @@
     if (
+!
 fetchedT
@@ -1975,113 +1975,20 @@
 n.is
-Present()) %7B%0A            // get token from database%0A            MetaToken metaToken = fetchedToken.get();
+Fetched()) %7B
 %0A   
@@ -2058,20 +2058,23 @@
 okenDTO(
-meta
+fetched
 Token.ge
@@ -2099,20 +2099,16 @@
         
-    
 new URL(
@@ -2156,24 +2156,25 @@
 aseUrl()));%0A
+%0A
             
@@ -2177,144 +2177,456 @@
     
-// change fetched status to true. This will be cleared later on.%0A            metaTokenRepository.save(metaToken.fetched(true)
+fetchedToken.fetched(true);%0A            save(fetchedToken);%0A            return result;%0A        %7D else %7B%0A            throw new CustomParameterizedException(%22invalidRequest%22, %22token is already fetched. %22%0A                + %22Invalid request%22);%0A        %7D%0A%0A%0A    %7D%0A%0A    @Transactional(readOnly = true)%0A    public MetaToken getToken(String tokenName) %7B%0A        Optional%3CMetaToken%3E fetchedToken = metaTokenRepository.findOneByTokenName(tokenName
 );%0A
+%0A
         
     
@@ -2625,28 +2625,39 @@
     
-    // return result
+if (fetchedToken.isPresent()) %7B
 %0A   
@@ -2664,38 +2664,51 @@
          return 
-result
+ fetchedToken.get()
 ;%0A        %7D else
@@ -2720,32 +2720,33 @@
           throw 
+ 
 new CustomNotFou

make the read method separate