package org.zstack.identity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.zstack.core.cascade.AbstractAsyncCascadeExtension;
import org.zstack.core.cascade.CascadeAction;
import org.zstack.core.cascade.CascadeConstant;
import org.zstack.core.cloudbus.CloudBus;
import org.zstack.core.cloudbus.CloudBusListCallBack;
import org.zstack.core.db.DatabaseFacade;
import org.zstack.header.core.Completion;
import org.zstack.header.identity.AccountConstant;
import org.zstack.header.identity.AccountDeletionMsg;
import org.zstack.header.identity.AccountInventory;
import org.zstack.header.identity.AccountVO;
import org.zstack.header.message.MessageReply;
import org.zstack.utils.CollectionUtils;
import org.zstack.utils.function.Function;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.List;
/**
* Created by frank on 7/15/2015.
*/
public class AccountCascadeExtension extends AbstractAsyncCascadeExtension {
@Autowired
private CloudBus bus;
@Autowired
private DatabaseFacade dbf;
@Override
public void asyncCascade(CascadeAction action, Completion completion) {
if (action.isActionCode(CascadeConstant.DELETION_CHECK_CODE)) {
handleDeletionCheck(action, completion);
} else if (action.isActionCode(CascadeConstant.DELETION_DELETE_CODE, CascadeConstant.DELETION_FORCE_DELETE_CODE)) {
handleDeletion(action, completion);
} else if (action.isActionCode(CascadeConstant.DELETION_CLEANUP_CODE)) {
handleDeletionCleanup(action, completion);
} else {
completion.success();
}
}
private void handleDeletionCleanup(CascadeAction action, Completion completion) {
completion.success();
}
private void handleDeletion(final CascadeAction action, final Completion completion) {
final List<AccountInventory> ainvs = action.getParentIssuerContext();
List<AccountDeletionMsg> msgs = CollectionUtils.transformToList(ainvs, new Function<AccountDeletionMsg, AccountInventory>() {
@Override
public AccountDeletionMsg call(AccountInventory arg) {
AccountDeletionMsg msg = new AccountDeletionMsg();
msg.setUuid(arg.getUuid());
bus.makeTargetServiceIdByResourceUuid(msg, AccountConstant.SERVICE_ID, arg.getUuid());
return msg;
}
});
bus.send(msgs, 10, new CloudBusListCallBack(completion) {
@Override
public void run(List<MessageReply> replies) {
if (!action.isActionCode(CascadeConstant.DELETION_FORCE_DELETE_CODE)) {
for (MessageReply r : replies) {
if (!r.isSuccess()) {
completion.fail(r.getError());
return;
}
}
}
List<String> uuids = CollectionUtils.transformToList(ainvs, new Function<String, AccountInventory>() {
@Override
public String call(AccountInventory arg) {
return arg.getUuid();
}
});
dbf.removeByPrimaryKeys(uuids, AccountVO.class);
completion.success();
}
});
}
private void handleDeletionCheck(CascadeAction action, Completion completion) {
completion.success();
}
@Override
public List<String> getEdgeNames() {
return Arrays.asList();
}
@Override
public String getCascadeResourceName() {
return AccountVO.class.getSimpleName();
}
@Override
public CascadeAction createActionForChildResource(CascadeAction action) {
if (CascadeConstant.DELETION_CODES.contains(action.getActionCode())) {
return action;
}
return null;
}
}