package org.ovirt.engine.core.bll.storage.disk.cinder;

import java.util.List;

import org.ovirt.engine.core.bll.storage.disk.image.BaseImagesCommand;
import org.ovirt.engine.core.bll.storage.disk.image.ImagesHandler;
import org.ovirt.engine.core.bll.tasks.CommandCoordinatorUtil;
import org.ovirt.engine.core.common.action.ImagesContainterParametersBase;
import org.ovirt.engine.core.common.businessentities.storage.CinderDisk;
import org.ovirt.engine.core.common.businessentities.storage.DiskImage;
import org.ovirt.engine.core.common.businessentities.storage.ImageStatus;
import org.ovirt.engine.core.compat.CommandStatus;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;
import org.ovirt.engine.core.dao.DiskDao;

public class CloneSingleCinderDiskCommandCallback<T extends BaseImagesCommand<? extends ImagesContainterParametersBase>> extends AbstractCinderDiskCommandCallback<T> {

    @Override
    public void doPolling(Guid cmdId, List<Guid> childCmdIds) {
        super.doPolling(cmdId, childCmdIds);

        ImageStatus imageStatus = getCinderBroker().getDiskStatus(getDiskId());
        DiskImage disk = getDisk();
        if (imageStatus != null && imageStatus != disk.getImageStatus()) {
            switch (imageStatus) {
            case OK:
                getCommand().setCommandStatus(CommandStatus.SUCCEEDED);
                break;
            case ILLEGAL:
                getCommand().setCommandStatus(CommandStatus.FAILED);
                break;
            }
        }
    }

    @Override
    public void onFailed(Guid cmdId, List<Guid> childCmdIds) {
        super.onFailed(cmdId, childCmdIds);
        ImagesHandler.updateImageStatus(getDiskId(), ImageStatus.ILLEGAL);
        getCommand().getParameters().setTaskGroupSuccess(false);
        onFinish(cmdId);
    }

    @Override
    public void onSucceeded(Guid cmdId, List<Guid> childCmdIds) {
        super.onSucceeded(cmdId, childCmdIds);
        ImagesHandler.updateImageStatus(getDiskId(), ImageStatus.OK);
        onFinish(cmdId);
    }

    private void onFinish(Guid cmdId) {
        ImagesHandler.updateImageStatus(getCommand().getParameters().getImageId(), ImageStatus.OK);
        getCommand().endAction();
        CommandCoordinatorUtil.removeAllCommandsInHierarchy(cmdId);
    }

    @Override
    protected Guid getDiskId() {
        return getCommand().getParameters().getDestinationImageId();
    }

    @Override
    protected CinderDisk getDisk() {
        if (disk == null) {
            disk = (CinderDisk) getDiskDao().get(getCommand().getParameters().getContainerId());
        }
        return disk;
    }

    protected DiskDao getDiskDao() {
        return DbFacade.getInstance().getDiskDao();
    }

    @Override
    protected CinderBroker getCinderBroker() {
        return getCommand().getCinderBroker();
    }
}

@@ -215,71 +215,8 @@
 er;%0A
-import org.ovirt.engine.core.bll.tasks.CommandCoordinatorUtil;%0A
 impo
@@ -1742,38 +1742,147 @@
 e);%0A        
-onFinish(cmdId
+ImagesHandler.updateImageStatus(getCommand().getParameters().getImageId(), ImageStatus.OK);%0A        getCommand().endAction(
 );%0A    %7D%0A%0A  
@@ -2080,80 +2080,8 @@
 K);%0A
-        onFinish(cmdId);%0A    %7D%0A%0A    private void onFinish(Guid cmdId) %7B%0A
     
@@ -2168,32 +2168,32 @@
 mageStatus.OK);%0A
+
         getComma
@@ -2214,76 +2214,8 @@
 ();%0A
-        CommandCoordinatorUtil.removeAllCommandsInHierarchy(cmdId);%0A
     

Refactor call back of CloneSingleCinderDisk