Skip to content

Commit

Permalink
add status
Browse files Browse the repository at this point in the history
  • Loading branch information
lhpqaq committed Oct 15, 2024
1 parent cafc1f4 commit 2da15a0
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public class AuthPlatformPO extends BasePO implements Serializable {
@Column(name = "platform_id", nullable = false)
private Long platformId;

@Column(name = "is_deleted")
private Boolean isDeleted;
@Column(name = "status")
private Integer status;

@Column(name = "name")
private String name;

@Column(name = "model")
private String model;

@Column(name = "notes")
private String notes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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 org.apache.bigtop.manager.server.enums;

import lombok.Getter;

@Getter
public enum AuthPlatformStatus {
DELETED(-1),
NORMAL(0),
ACTIVE(1),
INACTIVE(2),
;

private final Integer code;

AuthPlatformStatus(Integer code) {
this.code = code;
}

public static AuthPlatformStatus fromCode(Integer code) {
for (AuthPlatformStatus status : AuthPlatformStatus.values()) {
if (status.code.equals(code)) {
return status;
}
}
return NORMAL;
}

public static boolean isNormal(int code) {
return NORMAL.code.equals(code);
}

public static boolean isActive(int code) {
return ACTIVE.code.equals(code);
}

public static boolean isAvailable(int code) {
return isNormal(code) || isActive(code);
}

public static boolean isDeleted(int code) {
return DELETED.code.equals(code);
}

public static boolean needSwitch(int oldStatus, int newStatus) {
return isNormal(oldStatus) && isActive(newStatus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.bigtop.manager.dao.repository.ChatThreadDao;
import org.apache.bigtop.manager.dao.repository.PlatformDao;
import org.apache.bigtop.manager.server.enums.ApiExceptionEnum;
import org.apache.bigtop.manager.server.enums.AuthPlatformStatus;
import org.apache.bigtop.manager.server.exception.ApiException;
import org.apache.bigtop.manager.server.holder.SessionUserHolder;
import org.apache.bigtop.manager.server.model.converter.AuthPlatformConverter;
Expand Down Expand Up @@ -110,7 +111,7 @@ private AIAssistant buildAIAssistant(
@Override
public ChatThreadVO createChatThreads(Long authId, String model) {
AuthPlatformPO authPlatformPO = authPlatformDao.findById(authId);
if (authPlatformPO == null || authPlatformPO.getIsDeleted()) {
if (authPlatformPO == null || AuthPlatformStatus.isDeleted(authPlatformPO.getStatus())) {
throw new ApiException(ApiExceptionEnum.PLATFORM_NOT_AUTHORIZED);
}
AuthPlatformDTO authPlatformDTO = AuthPlatformConverter.INSTANCE.fromPO2DTO(authPlatformPO);
Expand Down Expand Up @@ -175,7 +176,7 @@ public SseEmitter talk(Long authId, Long threadId, String message) {
throw new ApiException(ApiExceptionEnum.CHAT_THREAD_NOT_FOUND);
}
AuthPlatformPO authPlatformPO = authPlatformDao.findById(authId);
if (authPlatformPO == null || authPlatformPO.getIsDeleted()) {
if (authPlatformPO == null || AuthPlatformStatus.isDeleted(authPlatformPO.getStatus())) {
throw new ApiException(ApiExceptionEnum.PLATFORM_NOT_AUTHORIZED);
}
AuthPlatformDTO authPlatformDTO = AuthPlatformConverter.INSTANCE.fromPO2DTO(authPlatformPO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.bigtop.manager.dao.repository.ChatThreadDao;
import org.apache.bigtop.manager.dao.repository.PlatformDao;
import org.apache.bigtop.manager.server.enums.ApiExceptionEnum;
import org.apache.bigtop.manager.server.enums.AuthPlatformStatus;
import org.apache.bigtop.manager.server.exception.ApiException;
import org.apache.bigtop.manager.server.model.converter.AuthPlatformConverter;
import org.apache.bigtop.manager.server.model.converter.PlatformConverter;
Expand Down Expand Up @@ -131,7 +132,7 @@ public List<AuthPlatformVO> authorizedPlatforms() {
List<AuthPlatformVO> authorizedPlatforms = new ArrayList<>();
List<AuthPlatformPO> authPlatformPOList = authPlatformDao.findAll();
for (AuthPlatformPO authPlatformPO : authPlatformPOList) {
if (authPlatformPO.getIsDeleted()) {
if (AuthPlatformStatus.isDeleted(authPlatformPO.getStatus())) {
continue;
}

Expand All @@ -154,7 +155,11 @@ public AuthPlatformVO addAuthorizedPlatform(AuthPlatformDTO authPlatformDTO) {

authPlatformDTO.setAuthCredentials(credentialSet);
AuthPlatformPO authPlatformPO = AuthPlatformConverter.INSTANCE.fromDTO2PO(authPlatformDTO);
// TODO: set status
if (authPlatformDTO.isTest()) {
authPlatformPO.setStatus(AuthPlatformStatus.NORMAL.getCode());
} else {
authPlatformPO.setStatus(AuthPlatformStatus.INACTIVE.getCode());
}
authPlatformDao.save(authPlatformPO);
return AuthPlatformConverter.INSTANCE.fromPO2VO(authPlatformPO, platformPO);
}
Expand All @@ -178,11 +183,11 @@ public AuthPlatformVO addAuthorizedPlatform(AuthPlatformDTO authPlatformDTO) {
@Override
public boolean deleteAuthorizedPlatform(Long authId) {
AuthPlatformPO authPlatformPO = authPlatformDao.findById(authId);
if (authPlatformPO == null || authPlatformPO.getIsDeleted()) {
if (authPlatformPO == null || AuthPlatformStatus.isDeleted(authPlatformPO.getStatus())) {
throw new ApiException(ApiExceptionEnum.PLATFORM_NOT_AUTHORIZED);
}

authPlatformPO.setIsDeleted(true);
authPlatformPO.setStatus(AuthPlatformStatus.DELETED.getCode());
authPlatformDao.partialUpdateById(authPlatformPO);

List<ChatThreadPO> chatThreadPOS = chatThreadDao.findAllByAuthId(authPlatformPO.getId());
Expand Down Expand Up @@ -213,7 +218,7 @@ public boolean testAuthorizedPlatform(AuthPlatformDTO authPlatformDTO) {

if (authPlatformDTO.getId() != null) {
AuthPlatformPO authPlatformPO = authPlatformDao.findById(authPlatformDTO.getId());
if (authPlatformPO == null || authPlatformPO.getIsDeleted()) {
if (authPlatformPO == null || AuthPlatformStatus.isDeleted(authPlatformPO.getStatus())) {
throw new ApiException(ApiExceptionEnum.PLATFORM_NOT_AUTHORIZED);
}
AuthPlatformDTO existAuthPlatformDTO = AuthPlatformConverter.INSTANCE.fromPO2DTO(authPlatformPO);
Expand All @@ -228,8 +233,8 @@ public boolean testAuthorizedPlatform(AuthPlatformDTO authPlatformDTO) {
}

if (authPlatformDTO.getId() != null) {
// TODO
AuthPlatformPO authPlatformPO = AuthPlatformConverter.INSTANCE.fromDTO2PO(authPlatformDTO);
authPlatformPO.setStatus(AuthPlatformStatus.NORMAL.getCode());
authPlatformDao.partialUpdateById(authPlatformPO);
}

Expand All @@ -238,7 +243,36 @@ public boolean testAuthorizedPlatform(AuthPlatformDTO authPlatformDTO) {

@Override
public AuthPlatformVO updateAuthorizedPlatform(AuthPlatformDTO authPlatformDTO) {
// TODO
return null;
AuthPlatformPO authPlatformPO = authPlatformDao.findById(authPlatformDTO.getId());
if (authPlatformPO == null || AuthPlatformStatus.isDeleted(authPlatformPO.getStatus())) {
throw new ApiException(ApiExceptionEnum.PLATFORM_NOT_AUTHORIZED);
}

AuthPlatformPO newAuthPlatformPO = AuthPlatformConverter.INSTANCE.fromDTO2PO(authPlatformDTO);

if (AuthPlatformStatus.needSwitch(authPlatformPO.getStatus(), newAuthPlatformPO.getStatus())) {
switchPlatform(newAuthPlatformPO.getId());
newAuthPlatformPO.setStatus(AuthPlatformStatus.ACTIVE.getCode());
}

authPlatformDao.partialUpdateById(newAuthPlatformPO);

return AuthPlatformConverter.INSTANCE.fromPO2VO(
newAuthPlatformPO, platformDao.findById(newAuthPlatformPO.getPlatformId()));
}

private void switchPlatform(Long id) {
List<AuthPlatformPO> authPlatformPOS = authPlatformDao.findAll();
for (AuthPlatformPO authPlatformPO : authPlatformPOS) {
if (!AuthPlatformStatus.isAvailable(authPlatformPO.getStatus())) {
continue;
}
if (authPlatformPO.getId().equals(id)) {
authPlatformPO.setStatus(AuthPlatformStatus.ACTIVE.getCode());
} else {
authPlatformPO.setStatus(AuthPlatformStatus.NORMAL.getCode());
}
}
authPlatformDao.partialUpdateByIds(authPlatformPOS);
}
}

0 comments on commit 2da15a0

Please sign in to comment.