package org.ovirt.engine.core.bll;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.core.bll.context.CommandContext;
import org.ovirt.engine.core.common.VdcObjectType;
import org.ovirt.engine.core.common.action.ImagesContainterParametersBase;
import org.ovirt.engine.core.common.businessentities.Snapshot;
import org.ovirt.engine.core.common.businessentities.storage.DiskImage;
import org.ovirt.engine.core.common.vdscommands.GetImageInfoVDSCommandParameters;
import org.ovirt.engine.core.common.vdscommands.VDSCommandType;
import org.ovirt.engine.core.common.vdscommands.VDSReturnValue;
import org.ovirt.engine.core.compat.Guid;

public abstract class RemoveSnapshotSingleDiskCommandBase<T extends ImagesContainterParametersBase> extends BaseImagesCommand<T> {
    protected RemoveSnapshotSingleDiskCommandBase(T parameters) {
        this(parameters, null);
    }


    protected RemoveSnapshotSingleDiskCommandBase(T parameters, CommandContext cmdContext) {
        super(parameters, cmdContext);
    }

    @Override
    public Map<String, String> getJobMessageProperties() {
        if (jobProperties == null) {
            jobProperties = super.getJobMessageProperties();
            jobProperties.put(VdcObjectType.Disk.name().toLowerCase(), getDiskImage().getDiskAlias());
            jobProperties.put("sourcesnapshot",
                    getSnapshotDescriptionById(getDiskImage().getVmSnapshotId()));
            jobProperties.put("destinationsnapshot",
                    getSnapshotDescriptionById(getDestinationDiskImage().getVmSnapshotId()));
        }
        return jobProperties;
    }

    protected DiskImage getImageInfoFromVdsm(final DiskImage targetImage) {
        VDSReturnValue ret = runVdsCommand(
                VDSCommandType.GetImageInfo,
                new GetImageInfoVDSCommandParameters(targetImage.getStoragePoolId(),
                        targetImage.getStorageIds().get(0),
                        targetImage.getId(),
                        targetImage.getImageId()));

        return (DiskImage) ret.getReturnValue();
    }

    protected void updateDiskImageDynamic(final DiskImage imageFromVdsm, final DiskImage targetImage) {
        // Update image's actual size in DB
        if (imageFromVdsm != null) {
            completeImageData(imageFromVdsm);
        } else {
            log.warn("Could not update DiskImage's size with ID '{}'",
                    targetImage.getImageId());
        }
    }

    @Override
    protected void endWithFailure() {
        // TODO: FILL! We should determine what to do in case of
        // failure (is everything rolled-backed? rolled-forward?
        // some and some?).
        setSucceeded(true);
    }

    private String getSnapshotDescriptionById(Guid snapshotId) {
        Snapshot snapshot = getSnapshotDao().get(snapshotId);
        return snapshot != null ? snapshot.getDescription() : StringUtils.EMPTY;
    }
}

@@ -414,16 +414,76 @@
 kImage;%0A
+import org.ovirt.engine.core.common.errors.EngineException;%0A
 import o
@@ -1770,32 +1770,51 @@
  targetImage) %7B%0A
+%0A        try %7B%0A    
         VDSRetur
@@ -1857,16 +1857,20 @@
         
+  
+  
 VDSComma
@@ -1890,16 +1890,20 @@
 geInfo,%0A
+    
         
@@ -1995,32 +1995,36 @@
                 
+ 
+   
 targetImage.getS
@@ -2043,16 +2043,20 @@
 get(0),%0A
+    
         
@@ -2108,32 +2108,36 @@
                 
+    
 targetImage.getI
@@ -2153,24 +2153,28 @@
 );%0A%0A        
+   
+ 
 return (Disk
@@ -2198,24 +2198,205 @@
 urnValue();%0A
+        %7D catch (EngineException e) %7B%0A            log.warn(%22Failed to get info of volume '%7B0%7D' using GetImageInfo%22, targetImage.getImageId(), e);%0A            return null;%0A        %7D%0A
     %7D%0A%0A    p

Handle exception if calling VDSM fails