package org.ovirt.engine.core.bll;

import java.util.ArrayList;
import java.util.Arrays;

import org.ovirt.engine.core.common.VdcObjectType;
import org.ovirt.engine.core.common.action.CreateCloneOfTemplateParameters;
import org.ovirt.engine.core.common.action.VdcActionType;
import org.ovirt.engine.core.common.asynctasks.AsyncTaskType;
import org.ovirt.engine.core.common.businessentities.CopyVolumeType;
import org.ovirt.engine.core.common.businessentities.DiskImage;
import org.ovirt.engine.core.common.errors.VdcBLLException;
import org.ovirt.engine.core.common.errors.VdcBllErrors;
import org.ovirt.engine.core.common.vdscommands.CopyImageVDSCommandParameters;
import org.ovirt.engine.core.common.vdscommands.VDSCommandType;
import org.ovirt.engine.core.common.vdscommands.VDSReturnValue;
import org.ovirt.engine.core.compat.Guid;

/**
 * This command responsible to creating a copy of template image. Usually it
 * will be called during Create Vm From Template.
 */
@InternalCommandAttribute
public class CreateCloneOfTemplateCommand<T extends CreateCloneOfTemplateParameters> extends
        CreateSnapshotFromTemplateCommand<T> {
    public CreateCloneOfTemplateCommand(T parameters) {
        super(parameters);
    }

    @Override
    protected DiskImage cloneDiskImage(Guid newImageGuid) {
        DiskImage returnValue = super.cloneDiskImage(newImageGuid);
        returnValue.setStorageIds(new ArrayList<Guid>(Arrays.asList(getDestinationStorageDomainId())));
        returnValue.setQuotaId(getParameters().getQuotaId());
        // override to have no template
        returnValue.setParentId(VmTemplateHandler.BLANK_VM_TEMPLATE_ID);
        returnValue.setImageTemplateId(VmTemplateHandler.BLANK_VM_TEMPLATE_ID);
        if (getParameters().getDiskImageBase() != null) {
            returnValue.setVolumeType(getParameters().getDiskImageBase().getVolumeType());
            returnValue.setvolumeFormat(getParameters().getDiskImageBase().getVolumeFormat());
        }
        return returnValue;
    }

    @Override
    protected VDSReturnValue performImageVdsmOperation() {
        setDestinationImageId(Guid.newGuid());
        mNewCreatedDiskImage = cloneDiskImage(getDestinationImageId());
        mNewCreatedDiskImage.setId(Guid.newGuid());
        Guid storagePoolID = mNewCreatedDiskImage.getStoragePoolId() != null ? mNewCreatedDiskImage
                .getStoragePoolId() : Guid.Empty;

        VDSReturnValue vdsReturnValue = null;
        try {
            Guid taskId = persistAsyncTaskPlaceHolder(VdcActionType.AddVmFromTemplate);

            vdsReturnValue = runVdsCommand(VDSCommandType.CopyImage,
                    new CopyImageVDSCommandParameters(storagePoolID, getDiskImage().getStorageIds().get(0),
                            getVmTemplateId(), getDiskImage().getId(), getImage().getImageId(),
                            mNewCreatedDiskImage.getId(), getDestinationImageId(),
                            "", getDestinationStorageDomainId(), CopyVolumeType.LeafVol,
                            mNewCreatedDiskImage.getVolumeFormat(), mNewCreatedDiskImage.getVolumeType(),
                            getDiskImage().isWipeAfterDelete(), false));

            if (vdsReturnValue.getSucceeded()) {
                getReturnValue().getInternalVdsmTaskIdList().add(
                        createTask(taskId,
                                vdsReturnValue.getCreationInfo(),
                                VdcActionType.AddVmFromTemplate,
                                VdcObjectType.Storage,
                                getParameters().getStorageDomainId(),
                                getDestinationStorageDomainId()));
            }
        } catch (Exception e) {
            log.errorFormat("Failed creating snapshot from image id -'{0}'", getImage().getImageId());
            throw new VdcBLLException(VdcBllErrors.VolumeCreationError);
        }
        return vdsReturnValue;
    }

    @Override
    protected AsyncTaskType getTaskType() {
        return AsyncTaskType.copyImage;
    }
}

@@ -2456,26 +2456,8 @@
 ll;%0A
-        try %7B%0A    
     
@@ -2536,16 +2536,29 @@
 plate);%0A
+        try %7B
 %0A       
@@ -3175,24 +3175,245 @@
 lse));%0A%0A    
+    %7D catch (VdcBLLException e) %7B%0A            log.errorFormat(%22Failed creating snapshot from image id -'%7B0%7D'%22, getImage().getImageId());%0A            throw new VdcBLLException(VdcBllErrors.VolumeCreationError);%0A        %7D%0A%0A
         if (
@@ -3457,20 +3457,16 @@
         
-    
 getRetur
@@ -3527,20 +3527,16 @@
         
-   
- 
 createTa
@@ -3546,20 +3546,16 @@
 taskId,%0A
-    
         
@@ -3636,20 +3636,16 @@
         
-  
-  
 VdcActio
@@ -3697,20 +3697,16 @@
         
-    
 VdcObjec
@@ -3748,20 +3748,16 @@
         
-  
-  
 getParam
@@ -3810,28 +3810,24 @@
             
-    
 getDestinati
@@ -3861,231 +3861,10 @@
     
- 
-   %7D%0A        %7D catch (Exception e) %7B%0A            log.errorFormat(%22Failed creating snapshot from image id -'%7B0%7D'%22, getImage().getImageId());%0A            throw new VdcBLLException(VdcBllErrors.VolumeCreationError);%0A        
 %7D
+%0A
 %0A   

Use shorter scope for try catch block