package org.ovirt.engine.core.bll;

import java.util.List;

import org.ovirt.engine.core.bll.job.ExecutionHandler;
import org.ovirt.engine.core.common.action.RestoreAllSnapshotsParameters;
import org.ovirt.engine.core.common.action.UpdateVmVersionParameters;
import org.ovirt.engine.core.common.action.VdcActionType;
import org.ovirt.engine.core.common.action.VdcReturnValueBase;
import org.ovirt.engine.core.common.action.VmOperationParameterBase;
import org.ovirt.engine.core.common.businessentities.DiskImage;
import org.ovirt.engine.core.common.businessentities.SnapshotActionEnum;
import org.ovirt.engine.core.common.businessentities.Snapshot.SnapshotType;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;

@InternalCommandAttribute
public class RestoreStatelessVmCommand<T extends VmOperationParameterBase> extends VmCommand<T> {

    /**
     * Constructor for command creation when compensation is applied on startup
     *
     * @param commandId
     */
    protected RestoreStatelessVmCommand(Guid commandId) {
        super(commandId);
    }

    public RestoreStatelessVmCommand(T parameters) {
        super(parameters);
    }

    @Override
    protected void executeCommand() {
        VdcReturnValueBase result =
                getBackend().runInternalAction(VdcActionType.UpdateVmVersion,
                        new UpdateVmVersionParameters(getVmId()),
                        ExecutionHandler.createDefaultContexForTasks(getExecutionContext(), getLock()));

        // if it fail because of canDoAction, its safe to restore the snapshot
        // and the vm will still be usable with previous version
        if (!result.getSucceeded() && !result.getCanDoAction()) {
            log.warnFormat("Couldn't update VM {0} ({1}) version from it's template, continue with restoring stateless snapshot.",
                    getVm().getName(),
                    getVmId());

            boolean returnVal = true;
            Guid snapshotId = DbFacade.getInstance().getSnapshotDao().getId(getVmId(), SnapshotType.STATELESS);
            List<DiskImage> imagesList = null;

            if (snapshotId != null) {
                imagesList = DbFacade.getInstance().getDiskImageDao().getAllSnapshotsForVmSnapshot(snapshotId);
            }

            if (imagesList != null && imagesList.size() > 0) {
                /**
                 * restore all snapshots
                 */
                RestoreAllSnapshotsParameters restoreParameters = new RestoreAllSnapshotsParameters(getVm().getId(), SnapshotActionEnum.RESTORE_STATELESS);
                restoreParameters.setShouldBeLogged(false);
                restoreParameters.setImages(imagesList);
                VdcReturnValueBase vdcReturn =
                        Backend.getInstance().runInternalAction(VdcActionType.RestoreAllSnapshots,
                                restoreParameters,
                                ExecutionHandler.createDefaultContexForTasks(getExecutionContext(), getLock()));
                returnVal = vdcReturn.getSucceeded();
            }
            setSucceeded(returnVal);
        }
    }
}

@@ -567,26 +567,29 @@
 Snapshot
-ActionEnum
+.SnapshotType
 ;%0Aimport
@@ -639,37 +639,34 @@
 ies.Snapshot
-.SnapshotType
+ActionEnum
 ;%0Aimport org
@@ -748,16 +748,62 @@
 bFacade;
+%0Aimport org.ovirt.engine.core.dao.SnapshotDao;
 %0A%0A@Inter
@@ -1993,33 +1993,105 @@
     
-boolean returnVal = true;
+setSucceeded(restoreInitialState());%0A        %7D%0A    %7D%0A%0A    private boolean restoreInitialState() %7B
 %0A   
@@ -2095,20 +2095,16 @@
         
-    
 Guid sna
@@ -2113,39 +2113,16 @@
 hotId = 
-DbFacade.getInstance().
 getSnaps
@@ -2184,47 +2184,33 @@
     
-    List%3CDiskImage%3E imagesList 
+if (snapshotId =
 = null
-;%0A
+) %7B
 %0A   
@@ -2222,33 +2222,20 @@
     
-if (snapshotId != null) %7B
+return true;
 %0A   
@@ -2239,24 +2239,27 @@
         
+%7D%0A%0A
         
 imagesLi
@@ -2254,44 +2254,37 @@
     
-imagesList = DbFacade.getInstance().
+List%3CDiskImage%3E imagesList = 
 getD
@@ -2343,27 +2343,8 @@
 d);%0A
-            %7D%0A%0A    
     
@@ -2366,18 +2366,18 @@
 ist 
-!
+=
 = null 
-&&
+%7C%7C
  ima
@@ -2388,18 +2388,17 @@
 ist.
-size() %3E 0
+isEmpty()
 ) %7B%0A
@@ -2413,72 +2413,29 @@
     
-   
- /**%0A                 * restore all snapshots%0A       
+return true;%0A
         
   */
@@ -2430,20 +2430,18 @@
         
-  */
+%7D%0A
 %0A       
@@ -2445,281 +2445,33 @@
     
-        RestoreAllSnapshotsParameters restoreParameters = new RestoreAllSnapshotsParameters(getVm().getId(), SnapshotActionEnum.RESTORE_STATELESS);%0A                restoreParameters.setShouldBeLogged(false);%0A                restoreParameters.setImages(imagesList);%0A        
+// restore all snapshots%0A
     
@@ -2505,24 +2505,16 @@
 eturn =%0A
-        
         
@@ -2624,43 +2624,56 @@
     
-        restoreParameters,%0A        
+buildRestoreAllSnapshotsParameters(imagesList),%0A
     
@@ -2773,24 +2773,16 @@
 ck()));%0A
-        
         
@@ -2791,13 +2791,8 @@
 turn
-Val =
  vdc
@@ -2822,64 +2822,506 @@
     
+%7D%0A%0A
     
-    %7D%0A            setSucceeded(returnVal);%0A        %7D
+private RestoreAllSnapshotsParameters buildRestoreAllSnapshotsParameters(List%3CDiskImage%3E imagesList) %7B%0A        RestoreAllSnapshotsParameters restoreParameters = new RestoreAllSnapshotsParameters(getVm().getId(), SnapshotActionEnum.RESTORE_STATELESS);%0A        restoreParameters.setShouldBeLogged(false);%0A        restoreParameters.setImages(imagesList);%0A        return restoreParameters;%0A    %7D%0A%0A    private SnapshotDao getSnapshotDao() %7B%0A        return DbFacade.getInstance().getSnapshotDao();
 %0A   

refactor RestoreStatelessVmCommand