|
|
@@ -0,0 +1,250 @@
|
|
|
+package cn.reghao.autodop.dmaster.app.service;
|
|
|
+
|
|
|
+import cn.reghao.autodop.common.utils.JsonUtil;
|
|
|
+import cn.reghao.autodop.dmaster.app.constant.ConfigType;
|
|
|
+import cn.reghao.autodop.dmaster.app.constant.NotifierType;
|
|
|
+import cn.reghao.autodop.dmaster.app.entity.build.AppCompile;
|
|
|
+import cn.reghao.autodop.dmaster.app.entity.build.AppPack;
|
|
|
+import cn.reghao.autodop.dmaster.app.entity.build.AppUpdate;
|
|
|
+import cn.reghao.autodop.dmaster.app.entity.LocalFsDir;
|
|
|
+import cn.reghao.autodop.dmaster.app.entity.Notification;
|
|
|
+import cn.reghao.autodop.dmaster.app.repository.build.AppCompileRepository;
|
|
|
+import cn.reghao.autodop.dmaster.app.repository.build.AppPackRepository;
|
|
|
+import cn.reghao.autodop.dmaster.app.repository.build.AppUpdateRepository;
|
|
|
+import cn.reghao.autodop.dmaster.app.repository.dao.ConfigDAO;
|
|
|
+import cn.reghao.autodop.dmaster.app.repository.orchestration.LocalFsDirRepository;
|
|
|
+import cn.reghao.autodop.dmaster.app.repository.orchestration.NotificationRepository;
|
|
|
+import cn.reghao.autodop.dmaster.app.vo.PageList;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.PageRequest;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author reghao
|
|
|
+ * @date 2020-02-28 17:10:37
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class ConfigService {
|
|
|
+ private LocalFsDirRepository configRepository;
|
|
|
+ private NotificationRepository notifyRepository;
|
|
|
+ private AppUpdateRepository updateRepository;
|
|
|
+ private AppCompileRepository compileRepository;
|
|
|
+ private AppPackRepository packRepository;
|
|
|
+ private ConfigDAO configDAO;
|
|
|
+
|
|
|
+ public ConfigService(LocalFsDirRepository configRepository,
|
|
|
+ NotificationRepository notifyRepository,
|
|
|
+ AppUpdateRepository updateRepository,
|
|
|
+ AppCompileRepository compileRepository,
|
|
|
+ AppPackRepository packRepository,
|
|
|
+ ConfigDAO configDAO) {
|
|
|
+ this.configRepository = configRepository;
|
|
|
+ this.notifyRepository = notifyRepository;
|
|
|
+ this.updateRepository = updateRepository;
|
|
|
+ this.compileRepository = compileRepository;
|
|
|
+ this.packRepository = packRepository;
|
|
|
+ this.configDAO = configDAO;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void add(String configType, String json) throws Exception {
|
|
|
+ switch (ConfigType.valueOf(configType)) {
|
|
|
+ // 本地文件系统目录
|
|
|
+ case localDir:
|
|
|
+ LocalFsDir localFsDir = (LocalFsDir) JsonUtil.jsonToObject(json, LocalFsDir.class);
|
|
|
+ configRepository.save(localFsDir);
|
|
|
+
|
|
|
+ /*AppEnv configEntity = configRepository.findByEnv(appEnv.getEnv());
|
|
|
+ if (configEntity == null) {
|
|
|
+ configRepository.save(appEnv);
|
|
|
+ }*/
|
|
|
+ break;
|
|
|
+ // 通知配置
|
|
|
+ case notifier:
|
|
|
+ Notification notification = (Notification) JsonUtil.jsonToObject(json, Notification.class);
|
|
|
+ // TODO 考虑 isDelete 为 true 的情况
|
|
|
+ Notification notifyEntity = notifyRepository.findByNotifierName(notification.getNotifierName());
|
|
|
+ if (notifyEntity == null) {
|
|
|
+ notifyRepository.save(notification);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ // 仓库配置
|
|
|
+ case repo:
|
|
|
+ AppUpdate appUpdate = (AppUpdate) JsonUtil.jsonToObject(json, AppUpdate.class);
|
|
|
+ AppUpdate updateEntity = updateRepository.findByRepoName(appUpdate.getRepoName());
|
|
|
+ if (updateEntity == null) {
|
|
|
+ updateRepository.save(appUpdate);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ // 编译器配置
|
|
|
+ case compiler:
|
|
|
+ AppCompile appCompile = (AppCompile) JsonUtil.jsonToObject(json, AppCompile.class);
|
|
|
+ AppCompile compileEntity = compileRepository.findByCompilerName(appCompile.getCompilerName());
|
|
|
+ if (compileEntity == null) {
|
|
|
+ compileRepository.save(appCompile);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ // 打包工具配置
|
|
|
+ case packer:
|
|
|
+ AppPack appPack = (AppPack) JsonUtil.jsonToObject(json, AppPack.class);
|
|
|
+ AppPack packEntity = packRepository.findByPackerName(appPack.getPackerName());
|
|
|
+ if (packEntity == null) {
|
|
|
+ packRepository.save(appPack);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ throw new Exception(configType + " 类型不存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public PageList getByPage(String configType, PageRequest pageRequest) throws Exception {
|
|
|
+ switch (ConfigType.valueOf(configType)) {
|
|
|
+ case localDir:
|
|
|
+ Page<LocalFsDir> configs = configRepository.findAll(pageRequest);
|
|
|
+ PageList<LocalFsDir> pageList = new PageList<>();
|
|
|
+ pageList.setTotalSize(configs.getTotalElements());
|
|
|
+ pageList.setTotalPages(configs.getTotalPages());
|
|
|
+ pageList.setList(configs.getContent().stream().map(LocalFsDir::vo).collect(Collectors.toList()));
|
|
|
+ return pageList;
|
|
|
+ case notifier:
|
|
|
+ Page<Notification> notifiers = notifyRepository.findAll(pageRequest);
|
|
|
+ PageList<Notification> pageList1 = new PageList<>();
|
|
|
+ pageList1.setTotalSize(notifiers.getTotalElements());
|
|
|
+ pageList1.setTotalPages(notifiers.getTotalPages());
|
|
|
+ pageList1.setList(notifiers.getContent().stream().map(Notification::vo).collect(Collectors.toList()));
|
|
|
+ return pageList1;
|
|
|
+ case repo:
|
|
|
+ Page<AppUpdate> appUpdates = updateRepository.findAll(pageRequest);
|
|
|
+ PageList<AppUpdate> pageList2 = new PageList<>();
|
|
|
+ pageList2.setTotalSize(appUpdates.getTotalElements());
|
|
|
+ pageList2.setTotalPages(appUpdates.getTotalPages());
|
|
|
+ pageList2.setList(appUpdates.getContent().stream().map(AppUpdate::vo).collect(Collectors.toList()));
|
|
|
+ return pageList2;
|
|
|
+ case compiler:
|
|
|
+ Page<AppCompile> appCompiles = compileRepository.findAll(pageRequest);
|
|
|
+ PageList<AppCompile> pageList3 = new PageList<>();
|
|
|
+ pageList3.setTotalSize(appCompiles.getTotalElements());
|
|
|
+ pageList3.setTotalPages(appCompiles.getTotalPages());
|
|
|
+ pageList3.setList(appCompiles.getContent().stream().map(AppCompile::vo).collect(Collectors.toList()));
|
|
|
+ return pageList3;
|
|
|
+ case packer:
|
|
|
+ Page<AppPack> appPacks = packRepository.findAll(pageRequest);
|
|
|
+ PageList<AppPack> pageList4 = new PageList<>();
|
|
|
+ pageList4.setTotalSize(appPacks.getTotalElements());
|
|
|
+ pageList4.setTotalPages(appPacks.getTotalPages());
|
|
|
+ pageList4.setList(appPacks.getContent().stream().map(AppPack::vo).collect(Collectors.toList()));
|
|
|
+ return pageList4;
|
|
|
+ default:
|
|
|
+ throw new Exception(configType + " 类型不存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void update(String configType, String json) throws Exception {
|
|
|
+ switch (ConfigType.valueOf(configType)) {
|
|
|
+ // 本地文件系统目录
|
|
|
+ case localDir:
|
|
|
+ LocalFsDir localFsDir = (LocalFsDir) JsonUtil.jsonToObject(json, LocalFsDir.class);
|
|
|
+ configRepository.save(localFsDir);
|
|
|
+ /*AppEnv configEntity = configRepository.findByEnv(appEnv.getEnv());
|
|
|
+ if (configEntity != null && !configEntity.equals(appEnv)) {
|
|
|
+ appEnv.setId(configEntity.getId());
|
|
|
+ appEnv.setIsDelete(configEntity.getIsDelete());
|
|
|
+ appEnv.setCreateTime(configEntity.getCreateTime());
|
|
|
+ appEnv.setUpdateTime(LocalDateTime.now());
|
|
|
+ configRepository.save(appEnv);
|
|
|
+ } else if (configEntity == null) {
|
|
|
+ configRepository.save(appEnv);
|
|
|
+ }*/
|
|
|
+ break;
|
|
|
+ // 通知配置
|
|
|
+ case notifier:
|
|
|
+ Notification notification = (Notification) JsonUtil.jsonToObject(json, Notification.class);
|
|
|
+ // TODO 考虑 isDelete 为 true 的情况
|
|
|
+ Notification notifyEntity = notifyRepository.findByNotifierName(notification.getNotifierName());
|
|
|
+ if (notifyEntity != null && !notifyEntity.equals(notification)) {
|
|
|
+ notification.setId(notifyEntity.getId());
|
|
|
+ notification.setIsDelete(notifyEntity.getIsDelete());
|
|
|
+ notification.setCreateTime(notifyEntity.getCreateTime());
|
|
|
+ notification.setUpdateTime(LocalDateTime.now());
|
|
|
+ notifyRepository.save(notification);
|
|
|
+ } else if (notifyEntity == null) {
|
|
|
+ notifyRepository.save(notification);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ // 仓库配置
|
|
|
+ case repo:
|
|
|
+ AppUpdate appUpdate = (AppUpdate) JsonUtil.jsonToObject(json, AppUpdate.class);
|
|
|
+ AppUpdate updateEntity = updateRepository.findByRepoName(appUpdate.getRepoName());
|
|
|
+ if (updateEntity != null && !updateEntity.equals(appUpdate)) {
|
|
|
+ appUpdate.setId(updateEntity.getId());
|
|
|
+ appUpdate.setIsDelete(false);
|
|
|
+ appUpdate.setCreateTime(updateEntity.getCreateTime());
|
|
|
+ appUpdate.setUpdateTime(LocalDateTime.now());
|
|
|
+ updateRepository.save(appUpdate);
|
|
|
+ } else if (updateEntity == null) {
|
|
|
+ updateRepository.save(appUpdate);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ // 编译器配置
|
|
|
+ case compiler:
|
|
|
+ AppCompile appCompile = (AppCompile) JsonUtil.jsonToObject(json, AppCompile.class);
|
|
|
+ AppCompile compileEntity = compileRepository.findByCompilerName(appCompile.getCompilerName());
|
|
|
+ if (compileEntity != null && !compileEntity.equals(appCompile)) {
|
|
|
+ appCompile.setId(compileEntity.getId());
|
|
|
+ appCompile.setIsDelete(false);
|
|
|
+ appCompile.setCreateTime(compileEntity.getCreateTime());
|
|
|
+ appCompile.setUpdateTime(LocalDateTime.now());
|
|
|
+ compileRepository.save(appCompile);
|
|
|
+ } else if (compileEntity == null) {
|
|
|
+ compileRepository.save(appCompile);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ // 打包工具配置
|
|
|
+ case packer:
|
|
|
+ AppPack appPack = (AppPack) JsonUtil.jsonToObject(json, AppPack.class);
|
|
|
+ AppPack packEntity = packRepository.findByPackerName(appPack.getPackerName());
|
|
|
+ if (packEntity != null && !packEntity.equals(appPack)) {
|
|
|
+ appPack.setId(packEntity.getId());
|
|
|
+ appPack.setIsDelete(false);
|
|
|
+ appPack.setCreateTime(packEntity.getCreateTime());
|
|
|
+ appPack.setUpdateTime(LocalDateTime.now());
|
|
|
+ packRepository.save(appPack);
|
|
|
+ } else if (packEntity == null) {
|
|
|
+ packRepository.save(appPack);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ throw new Exception(configType + " 类型不存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void delete(String configType, String name) throws Exception {
|
|
|
+ switch (ConfigType.valueOf(configType)) {
|
|
|
+ case localDir:
|
|
|
+ break;
|
|
|
+ case notifier:
|
|
|
+ notifyRepository.deleteByNotifierName(name);
|
|
|
+ break;
|
|
|
+ case repo:
|
|
|
+ updateRepository.deleteByRepoName(name);
|
|
|
+ break;
|
|
|
+ case compiler:
|
|
|
+ compileRepository.deleteByCompilerName(name);
|
|
|
+ break;
|
|
|
+ case packer:
|
|
|
+ packRepository.deleteByPackerName(name);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ throw new Exception(configType + " 类型不存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String, Map<String, String[]>> buildConfigs() {
|
|
|
+ Map<String, Map<String, String[]>> map = configDAO.buildConfigs();
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+}
|