package org.ovirt.engine.core.bll.memory;

import java.util.List;

import org.ovirt.engine.core.bll.tasks.TaskHandlerCommand;
import org.ovirt.engine.core.common.businessentities.Disk;
import org.ovirt.engine.core.common.businessentities.VM;
import org.ovirt.engine.core.common.vdscommands.DeleteImageGroupVDSCommandParameters;
import org.ovirt.engine.core.compat.Guid;

public class MemoryImageRemoverFromExportDomain extends MemoryImageRemover {

    private Guid storagePoolId;
    private Guid storageDomainId;
    protected Boolean cachedPostZero;
    private VM vm;

    public MemoryImageRemoverFromExportDomain(VM vm, TaskHandlerCommand<?> enclosingCommand,
            Guid storagePoolId, Guid storageDomainId) {
        super(enclosingCommand);
        this.vm = vm;
        this.storagePoolId = storagePoolId;
        this.storageDomainId = storageDomainId;
    }

    public void remove() {
        removeMemoryVolumes(MemoryUtils.getMemoryVolumesFromSnapshots(vm.getSnapshots()));
    }

    @Override
    protected boolean removeMemoryVolume(String memoryVolumes) {
        return super.removeMemoryVolume(
                MemoryUtils.changeStorageDomainAndPoolInMemoryState(
                        memoryVolumes, storageDomainId, storagePoolId));
    }

    @Override
    protected DeleteImageGroupVDSCommandParameters buildDeleteMemoryImageParams(List<Guid> guids) {
        return new DeleteImageGroupVDSCommandParameters(
                guids.get(1), guids.get(0), guids.get(2), isPostZero(), false);
    }

    @Override
    protected DeleteImageGroupVDSCommandParameters buildDeleteMemoryConfParams(List<Guid> guids) {
        return new DeleteImageGroupVDSCommandParameters(
                guids.get(1), guids.get(0), guids.get(4), isPostZero(), false);
    }

    /**
     * We set the post zero field on memory image deletion from export domain as we do
     * when it is deleted from data domain even though the export domain is NFS and NFS
     * storage do the wipe on its own, in order to be compliance with the rest of the
     * code that do the same, and to be prepared for supporting export domains which
     * are not NFS.
     */
    protected boolean isPostZero() {
        if (cachedPostZero == null) {
            // check if one of the disks is marked with wipe_after_delete
            cachedPostZero =
                    vm.getDiskMap().values().contains(new Object() {
                        @Override
                        public boolean equals(Object obj) {
                            return ((Disk) obj).isWipeAfterDelete();
                        }
                    });
        }
        return cachedPostZero;
    }
}

@@ -2251,77 +2251,67 @@
     
-// check if one of the disks is marked with wipe_a
+cachedPostZero = isVmContainsWipeA
 fter
-_d
+D
 elete
+Disk();
 %0A       
     
@@ -2298,37 +2298,50 @@
 eDisk();%0A       
+ %7D%0A
      
+   return 
 cachedPostZero =
@@ -2342,221 +2342,236 @@
 Zero
- =
+;
 %0A    
+%7D%0A%0A
     
+/**%0A
      
-       vm.getDiskMap().values().contains(new Object() %7B%0A                        @Override%0A                        public boolean equals(Object obj) %7B%0A                            return ((Disk) obj)
+* @return true IFF one of the disks is marked with wipe_after_delete%0A     */%0A    private boolean isVmContainsWipeAfterDeleteDisk() %7B%0A        for (Disk disk : vm.getDiskMap().values()) %7B%0A            if (disk
 .isW
@@ -2586,17 +2586,19 @@
 Delete()
-;
+) %7B
 %0A       
@@ -2606,25 +2606,28 @@
         
-        %7D
+return true;
 %0A       
@@ -2635,19 +2635,9 @@
     
-        %7D);
+%7D
 %0A   
@@ -2654,36 +2654,27 @@
      return 
-cachedPostZero
+false
 ;%0A    %7D%0A%7D%0A

fix findbugs errors