AppService.java 7.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2021 Apollo Authors
 *
 * 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.
 *
 */
L
lepdou 已提交
17 18
package com.ctrip.framework.apollo.portal.service;

J
Jason Song 已提交
19
import com.ctrip.framework.apollo.common.dto.AppDTO;
20
import com.ctrip.framework.apollo.common.dto.PageDTO;
L
lepdou 已提交
21
import com.ctrip.framework.apollo.common.entity.App;
J
Jason Song 已提交
22
import com.ctrip.framework.apollo.common.exception.BadRequestException;
L
lepdou 已提交
23
import com.ctrip.framework.apollo.common.utils.BeanUtils;
24
import com.ctrip.framework.apollo.portal.environment.Env;
L
lepdou 已提交
25
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
P
powerYao 已提交
26
import com.ctrip.framework.apollo.portal.constant.TracerEventType;
27
import com.ctrip.framework.apollo.portal.entity.bo.UserInfo;
L
lepdou 已提交
28 29
import com.ctrip.framework.apollo.portal.entity.vo.EnvClusterInfo;
import com.ctrip.framework.apollo.portal.repository.AppRepository;
30
import com.ctrip.framework.apollo.portal.spi.UserInfoHolder;
31
import com.ctrip.framework.apollo.portal.spi.UserService;
32
import com.ctrip.framework.apollo.tracer.Tracer;
33
import com.google.common.collect.Lists;
34
import org.springframework.data.domain.Page;
35
import org.springframework.data.domain.Pageable;
J
Jason Song 已提交
36 37 38 39
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
40
import java.util.Set;
J
Jason Song 已提交
41

L
lepdou 已提交
42 43 44
@Service
public class AppService {

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  private final UserInfoHolder userInfoHolder;
  private final AdminServiceAPI.AppAPI appAPI;
  private final AppRepository appRepository;
  private final ClusterService clusterService;
  private final AppNamespaceService appNamespaceService;
  private final RoleInitializationService roleInitializationService;
  private final RolePermissionService rolePermissionService;
  private final FavoriteService favoriteService;
  private final UserService userService;

  public AppService(
      final UserInfoHolder userInfoHolder,
      final AdminServiceAPI.AppAPI appAPI,
      final AppRepository appRepository,
      final ClusterService clusterService,
      final AppNamespaceService appNamespaceService,
      final RoleInitializationService roleInitializationService,
      final RolePermissionService rolePermissionService,
      final FavoriteService favoriteService,
      final UserService userService) {
    this.userInfoHolder = userInfoHolder;
    this.appAPI = appAPI;
    this.appRepository = appRepository;
    this.clusterService = clusterService;
    this.appNamespaceService = appNamespaceService;
    this.roleInitializationService = roleInitializationService;
    this.rolePermissionService = rolePermissionService;
    this.favoriteService = favoriteService;
    this.userService = userService;
  }
L
lepdou 已提交
75 76 77 78


  public List<App> findAll() {
    Iterable<App> apps = appRepository.findAll();
79 80

    return Lists.newArrayList(apps);
L
lepdou 已提交
81 82
  }

83 84 85 86 87 88 89 90 91 92 93 94
  public PageDTO<App> findAll(Pageable pageable) {
    Page<App> apps = appRepository.findAll(pageable);

    return new PageDTO<>(apps.getContent(), pageable, apps.getTotalElements());
  }

  public PageDTO<App> searchByAppIdOrAppName(String query, Pageable pageable) {
    Page<App> apps = appRepository.findByAppIdContainingOrNameContaining(query, query, pageable);

    return new PageDTO<>(apps.getContent(), pageable, apps.getTotalElements());
  }

95
  public List<App> findByAppIds(Set<String> appIds) {
96 97 98
    return appRepository.findByAppIdIn(appIds);
  }

99 100 101 102
  public List<App> findByAppIds(Set<String> appIds, Pageable pageable) {
    return appRepository.findByAppIdIn(appIds, pageable);
  }

103
  public List<App> findByOwnerName(String ownerName, Pageable page) {
104 105 106
    return appRepository.findByOwnerName(ownerName, page);
  }

L
lepdou 已提交
107
  public App load(String appId) {
L
lepdou 已提交
108
    return appRepository.findByAppId(appId);
L
lepdou 已提交
109 110 111 112 113 114
  }

  public AppDTO load(Env env, String appId) {
    return appAPI.loadApp(env, appId);
  }

L
lepdou 已提交
115
  public void createAppInRemote(Env env, App app) {
L
lepdou 已提交
116 117 118 119
    String username = userInfoHolder.getUser().getUserId();
    app.setDataChangeCreatedBy(username);
    app.setDataChangeLastModifiedBy(username);

K
kezhenxu94 已提交
120
    AppDTO appDTO = BeanUtils.transform(AppDTO.class, app);
L
lepdou 已提交
121 122
    appAPI.createApp(env, appDTO);
  }
L
update  
lepdou 已提交
123

L
lepdou 已提交
124
  @Transactional
L
lepdou 已提交
125
  public App createAppInLocal(App app) {
L
lepdou 已提交
126 127
    String appId = app.getAppId();
    App managedApp = appRepository.findByAppId(appId);
L
update  
lepdou 已提交
128

L
lepdou 已提交
129
    if (managedApp != null) {
130 131 132 133 134 135
      throw new BadRequestException(String.format("App already exists. AppId = %s", appId));
    }

    UserInfo owner = userService.findByUserId(app.getOwnerName());
    if (owner == null) {
      throw new BadRequestException("Application's owner not exist.");
L
lepdou 已提交
136
    }
137 138 139 140 141 142 143 144 145 146 147
    app.setOwnerEmail(owner.getEmail());

    String operator = userInfoHolder.getUser().getUserId();
    app.setDataChangeCreatedBy(operator);
    app.setDataChangeLastModifiedBy(operator);

    App createdApp = appRepository.save(app);

    appNamespaceService.createDefaultAppNamespace(appId);
    roleInitializationService.initAppRoles(createdApp);

P
powerYao 已提交
148
    Tracer.logEvent(TracerEventType.CREATE_APP, appId);
149 150

    return createdApp;
L
lepdou 已提交
151 152
  }

L
lepdou 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  @Transactional
  public App updateAppInLocal(App app) {
    String appId = app.getAppId();

    App managedApp = appRepository.findByAppId(appId);
    if (managedApp == null) {
      throw new BadRequestException(String.format("App not exists. AppId = %s", appId));
    }

    managedApp.setName(app.getName());
    managedApp.setOrgId(app.getOrgId());
    managedApp.setOrgName(app.getOrgName());

    String ownerName = app.getOwnerName();
    UserInfo owner = userService.findByUserId(ownerName);
    if (owner == null) {
      throw new BadRequestException(String.format("App's owner not exists. owner = %s", ownerName));
    }
    managedApp.setOwnerName(owner.getUserId());
    managedApp.setOwnerEmail(owner.getEmail());

    String operator = userInfoHolder.getUser().getUserId();
    managedApp.setDataChangeLastModifiedBy(operator);

    return appRepository.save(managedApp);
  }

L
lepdou 已提交
180 181 182 183 184 185
  public EnvClusterInfo createEnvNavNode(Env env, String appId) {
    EnvClusterInfo node = new EnvClusterInfo(env);
    node.setClusters(clusterService.findClusters(env, appId));
    return node;
  }

186
  @Transactional
N
nobodyiam 已提交
187
  public App deleteAppInLocal(String appId) {
188 189 190 191 192
    App managedApp = appRepository.findByAppId(appId);
    if (managedApp == null) {
      throw new BadRequestException(String.format("App not exists. AppId = %s", appId));
    }
    String operator = userInfoHolder.getUser().getUserId();
N
nobodyiam 已提交
193

N
nobodyiam 已提交
194
    //this operator is passed to com.ctrip.framework.apollo.portal.listener.DeletionListener.onAppDeletionEvent
N
nobodyiam 已提交
195 196
    managedApp.setDataChangeLastModifiedBy(operator);

197 198 199 200
    //删除portal数据库中的app
    appRepository.deleteApp(appId, operator);

    //删除portal数据库中的appNamespace
N
nobodyiam 已提交
201
    appNamespaceService.batchDeleteByAppId(appId, operator);
202 203

    //删除portal数据库中的收藏表
N
nobodyiam 已提交
204
    favoriteService.batchDeleteByAppId(appId, operator);
205

N
nobodyiam 已提交
206
    //删除portal数据库中Permission、Role相关数据
207
    rolePermissionService.deleteRolePermissionsByAppId(appId, operator);
N
nobodyiam 已提交
208 209

    return managedApp;
210
  }
L
lepdou 已提交
211
}