Coverage Report - com.ctrip.framework.apollo.portal.service.AppService
 
Classes in this File Line Coverage Branch Coverage Complexity
AppService
5%
2/35
0%
0/6
2.143
 
 1  
 package com.ctrip.framework.apollo.portal.service;
 2  
 
 3  
 import com.google.common.collect.Lists;
 4  
 
 5  
 import java.util.Collections;
 6  
 import java.util.List;
 7  
 
 8  
 import org.slf4j.Logger;
 9  
 import org.slf4j.LoggerFactory;
 10  
 import org.springframework.beans.factory.annotation.Autowired;
 11  
 import org.springframework.context.ApplicationEventPublisher;
 12  
 import org.springframework.stereotype.Service;
 13  
 import org.springframework.transaction.annotation.Transactional;
 14  
 import org.springframework.web.client.HttpStatusCodeException;
 15  
 
 16  
 import com.ctrip.framework.apollo.common.entity.App;
 17  
 import com.ctrip.framework.apollo.common.utils.BeanUtils;
 18  
 import com.ctrip.framework.apollo.common.utils.ExceptionUtils;
 19  
 import com.ctrip.framework.apollo.core.dto.AppDTO;
 20  
 import com.ctrip.framework.apollo.core.enums.Env;
 21  
 import com.ctrip.framework.apollo.core.exception.BadRequestException;
 22  
 import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
 23  
 import com.ctrip.framework.apollo.portal.auth.UserInfoHolder;
 24  
 import com.ctrip.framework.apollo.portal.entity.vo.EnvClusterInfo;
 25  
 import com.ctrip.framework.apollo.portal.listener.AppCreationEvent;
 26  
 import com.ctrip.framework.apollo.portal.repository.AppRepository;
 27  
 
 28  
 @Service
 29  1
 public class AppService {
 30  
 
 31  1
   private Logger logger = LoggerFactory.getLogger(AppService.class);
 32  
 
 33  
   @Autowired
 34  
   private UserInfoHolder userInfoHolder;
 35  
   @Autowired
 36  
   private ClusterService clusterService;
 37  
   @Autowired
 38  
   private NamespaceService namespaceService;
 39  
   @Autowired
 40  
   private RoleInitializationService roleInitializationService;
 41  
 
 42  
   @Autowired
 43  
   private AdminServiceAPI.AppAPI appAPI;
 44  
 
 45  
   @Autowired
 46  
   private AppRepository appRepository;
 47  
 
 48  
   @Autowired
 49  
   private ApplicationEventPublisher publisher;
 50  
 
 51  
   public List<App> findAll() {
 52  0
     Iterable<App> apps = appRepository.findAll();
 53  0
     if (apps == null) {
 54  0
       return Collections.EMPTY_LIST;
 55  
     }
 56  0
     return Lists.newArrayList((apps));
 57  
   }
 58  
 
 59  
   public App load(String appId) {
 60  0
     App app = appRepository.findByAppId(appId);
 61  0
     if (app == null){
 62  0
       throw new BadRequestException(String.format("app %s cant found.", appId));
 63  
     }
 64  0
     return app;
 65  
   }
 66  
 
 67  
   public AppDTO load(Env env, String appId) {
 68  0
     return appAPI.loadApp(env, appId);
 69  
   }
 70  
 
 71  
   public void createApp(Env env, App app) {
 72  0
     enrichUserInfo(app);
 73  
     try {
 74  0
       AppDTO appDTO = BeanUtils.transfrom(AppDTO.class, app);
 75  0
       appAPI.createApp(env, appDTO);
 76  0
     } catch (HttpStatusCodeException e) {
 77  0
       logger.error(ExceptionUtils.toString(e));
 78  0
       throw e;
 79  0
     }
 80  0
   }
 81  
 
 82  
   public void enrichUserInfo(App app) {
 83  0
     String username = userInfoHolder.getUser().getUserId();
 84  0
     app.setDataChangeCreatedBy(username);
 85  0
     app.setDataChangeLastModifiedBy(username);
 86  0
   }
 87  
 
 88  
 
 89  
   @Transactional
 90  
   public App createOrUpdateAppInLocal(App app) {
 91  0
     String appId = app.getAppId();
 92  0
     App managedApp = appRepository.findByAppId(appId);
 93  
 
 94  0
     if (managedApp != null) {
 95  0
       BeanUtils.copyEntityProperties(app, managedApp);
 96  0
       return appRepository.save(managedApp);
 97  
     } else {
 98  0
       App createdApp = appRepository.save(app);
 99  0
       namespaceService.createDefaultAppNamespace(appId);
 100  
       //role
 101  0
       roleInitializationService.initAppRoles(createdApp);
 102  0
       return createdApp;
 103  
     }
 104  
   }
 105  
 
 106  
   public EnvClusterInfo createEnvNavNode(Env env, String appId) {
 107  0
     EnvClusterInfo node = new EnvClusterInfo(env);
 108  0
     node.setClusters(clusterService.findClusters(env, appId));
 109  0
     return node;
 110  
   }
 111  
 
 112  
 }