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

import java.util.Arrays;

import org.ovirt.engine.core.bll.Backend;
import org.ovirt.engine.core.bll.InternalCommandAttribute;
import org.ovirt.engine.core.common.action.StorageServerConnectionParametersBase;
import org.ovirt.engine.core.common.businessentities.StorageServerConnections;
import org.ovirt.engine.core.common.errors.VdcFault;
import org.ovirt.engine.core.common.utils.Pair;
import org.ovirt.engine.core.common.vdscommands.StorageServerConnectionManagementVDSParameters;
import org.ovirt.engine.core.common.vdscommands.VDSCommandType;
import org.ovirt.engine.core.compat.Guid;

@InternalCommandAttribute
public class ConnectStorageToVdsCommand<T extends StorageServerConnectionParametersBase> extends
        StorageServerConnectionCommandBase<T> {
    public ConnectStorageToVdsCommand(T parameters) {
        super(parameters);
    }

    @Override
    protected void executeCommand() {
        Pair<Boolean, Integer> result = connect(getVds().getId());
        setSucceeded(result.getFirst());
        if (!result.getFirst()) {
            setErrorMessageAtReturn(result);
        }
    }

    private void setErrorMessageAtReturn(Pair<Boolean, Integer> result) {
        VdcFault fault = new VdcFault();
        fault.setError(result.getSecond());
        if (fault.getError() != null) {
            fault.setMessage(
                    Backend.getInstance()
                            .getVdsErrorsTranslator()
                            .TranslateErrorTextSingle(fault.getError().toString()));
        }
        getReturnValue().setFault(fault);
    }

    protected Pair<Boolean, Integer> connectHostToStorage() {
            Guid vdsId = getVds().getId();
            Pair<Boolean, Integer> result = connect(vdsId);
            return result;
    }

    protected Pair<Boolean, Integer> connect(Guid vdsId) {
        java.util.HashMap<String, String> result = (java.util.HashMap<String, String>) runVdsCommand(
                        VDSCommandType.ConnectStorageServer,
                        new StorageServerConnectionManagementVDSParameters(vdsId, Guid.Empty,
                                getParameters().getStorageServerConnection().getstorage_type(),
                                new java.util.ArrayList<StorageServerConnections>(java.util.Arrays
                                        .asList(new StorageServerConnections[] { getConnection() }))))
                .getReturnValue();
        return new Pair<Boolean, Integer>(StorageHelperDirector.getInstance()
                .getItem(getParameters().getStorageServerConnection().getstorage_type())
                .isConnectSucceeded(result, Arrays.asList(getConnection())),
                Integer.parseInt(result.values().iterator().next()));
    }
}

@@ -61,16 +61,87 @@
 .Arrays;
+%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;
 %0A%0Aimport
@@ -1062,25 +1062,22 @@
 nect
-(getVds().getId()
+HostToStorage(
 );%0A 
@@ -1755,218 +1755,94 @@
     
-    Guid vdsId = getVds().getId();%0A            Pair%3CBoolean, Integer%3E result = connect(vdsId);%0A            return result;%0A    %7D%0A%0A    protected Pair%3CBoolean, Integer%3E connect(Guid vdsId) %7B%0A        java.util.Hash
+List%3CStorageServerConnections%3E connections = Arrays.asList(getConnection());%0A%0A        
 Map%3C
@@ -1867,26 +1867,16 @@
 sult = (
-java.util.
 HashMap%3C
@@ -1923,24 +1923,16 @@
         
-        
 VDSComma
@@ -1972,32 +1972,24 @@
             
-      
-  
 new StorageS
@@ -2031,13 +2031,24 @@
 ers(
-vdsId
+getVds().getId()
 , Gu
@@ -2085,298 +2085,56 @@
     
-  
-      getParameters().getStorageServerConnection().getstorage_type(),%0A                                new java.util.ArrayList%3CStorageServerConnections%3E(java.util.Arrays%0A                                        .asList(new StorageServerConnections%5B%5D %7B getConnection() %7D))))%0A                
+getConnection().getstorage_type(), connections))
 .get
@@ -2144,24 +2144,25 @@
 urnValue();%0A
+%0A
         retu
@@ -2173,32 +2173,16 @@
 ew Pair%3C
-Boolean, Integer
 %3E(Storag
@@ -2243,37 +2243,8 @@
 (get
-Parameters().getStorageServer
 Conn
@@ -2315,34 +2315,17 @@
 result, 
-Arrays.asList(getC
+c
 onnectio
@@ -2325,19 +2325,17 @@
 nnection
-())
+s
 ),%0A     

Simplify ConnectStorageToVdsCommand