use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.
the class AppService method createAppInLocal.
@Transactional
public App createAppInLocal(App app) {
String appId = app.getAppId();
App managedApp = appRepository.findByAppId(appId);
if (managedApp != null) {
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.");
}
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);
Tracer.logEvent(TracerEventType.CREATE_APP, appId);
return createdApp;
}
use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.
the class FavoriteService method addFavorite.
public Favorite addFavorite(Favorite favorite) {
UserInfo user = userService.findByUserId(favorite.getUserId());
if (user == null) {
throw new BadRequestException("user not exist");
}
UserInfo loginUser = userInfoHolder.getUser();
// user can only add himself favorite app
if (!loginUser.equals(user)) {
throw new BadRequestException("add favorite fail. " + "because favorite's user is not current login user.");
}
Favorite checkedFavorite = favoriteRepository.findByUserIdAndAppId(loginUser.getUserId(), favorite.getAppId());
if (checkedFavorite != null) {
return checkedFavorite;
}
favorite.setPosition(POSITION_DEFAULT);
favorite.setDataChangeCreatedBy(user.getUserId());
favorite.setDataChangeLastModifiedBy(user.getUserId());
return favoriteRepository.save(favorite);
}
use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.
the class NamespaceBranchService method calculateBranchChangeSet.
private ItemChangeSets calculateBranchChangeSet(String appId, Env env, String clusterName, String namespaceName, String branchName) {
NamespaceBO parentNamespace = namespaceService.loadNamespaceBO(appId, env, clusterName, namespaceName);
if (parentNamespace == null) {
throw new BadRequestException("base namespace not existed");
}
if (parentNamespace.getItemModifiedCnt() > 0) {
throw new BadRequestException("Merge operation failed. Because master has modified items");
}
List<ItemDTO> masterItems = itemService.findItems(appId, env, clusterName, namespaceName);
List<ItemDTO> branchItems = itemService.findItems(appId, env, branchName, namespaceName);
ItemChangeSets changeSets = itemsComparator.compareIgnoreBlankAndCommentItem(parentNamespace.getBaseInfo().getId(), masterItems, branchItems);
changeSets.setDeleteItems(Collections.emptyList());
changeSets.setDataChangeLastModifiedBy(userInfoHolder.getUser().getUserId());
return changeSets;
}
use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.
the class NamespaceService method deleteNamespace.
@Transactional
public void deleteNamespace(String appId, Env env, String clusterName, String namespaceName) {
// 1. check private namespace
AppNamespace appNamespace = appNamespaceService.findByAppIdAndName(appId, namespaceName);
if (appNamespace != null && !appNamespace.isPublic()) {
throw new BadRequestException("Private namespace can not be deleted");
}
// 2. check parent namespace has not instances
if (namespaceHasInstances(appId, env, clusterName, namespaceName)) {
throw new BadRequestException("Can not delete namespace because namespace has active instances");
}
// 3. check child namespace has not instances
NamespaceDTO childNamespace = branchService.findBranchBaseInfo(appId, env, clusterName, namespaceName);
if (childNamespace != null && namespaceHasInstances(appId, env, childNamespace.getClusterName(), namespaceName)) {
throw new BadRequestException("Can not delete namespace because namespace's branch has active instances");
}
// 4. check public namespace has not associated namespace
if (appNamespace != null && publicAppNamespaceHasAssociatedNamespace(namespaceName, env)) {
throw new BadRequestException("Can not delete public namespace which has associated namespaces");
}
String operator = userInfoHolder.getUser().getUserId();
namespaceAPI.deleteNamespace(env, appId, clusterName, namespaceName, operator);
}
use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.
the class NamespaceAcquireLockAspect method requireLockAdvice.
// delete item
@Before("@annotation(PreAcquireNamespaceLock) && args(itemId, operator, ..)")
public void requireLockAdvice(long itemId, String operator) {
Item item = itemService.findOne(itemId);
if (item == null) {
throw new BadRequestException("item not exist.");
}
acquireLock(item.getNamespaceId(), operator);
}
Aggregations