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

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import org.ovirt.engine.core.bll.Backend;
import org.ovirt.engine.core.common.action.StorageDomainManagementParameter;
import org.ovirt.engine.core.common.action.StorageServerConnectionParametersBase;
import org.ovirt.engine.core.common.action.VdcActionType;
import org.ovirt.engine.core.common.businessentities.SANState;
import org.ovirt.engine.core.common.businessentities.StorageDomainStatic;
import org.ovirt.engine.core.common.businessentities.StorageDomainType;
import org.ovirt.engine.core.common.errors.VdcBllMessages;
import org.ovirt.engine.core.common.utils.Pair;
import org.ovirt.engine.core.common.vdscommands.HSMGetStorageDomainInfoVDSCommandParameters;
import org.ovirt.engine.core.common.vdscommands.HSMGetStorageDomainsListVDSCommandParameters;
import org.ovirt.engine.core.common.vdscommands.VDSCommandType;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;

public class AddExistingFileStorageDomainCommand<T extends StorageDomainManagementParameter> extends
        AddNFSStorageDomainCommand<T> {

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

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

    @Override
    protected boolean canAddDomain() {
        return checkExistingStorageDomain();
    }

    @Override
    protected void executeCommand() {
        if (StringUtils.isEmpty(getStorageDomain().getStorage())) {
            getStorageDomain().setStorage(
                    (String) Backend
                            .getInstance()
                            .runInternalAction(
                                    VdcActionType.AddStorageServerConnection,
                                    new StorageServerConnectionParametersBase(getStorageDomain().getStorageStaticData()
                                            .getConnection(), getVds().getId())).getActionReturnValue());
        }
        addStorageDomainInDb();
        updateStorageDomainDynamicFromIrs();
        setSucceeded(true);
    }

    protected boolean checkExistingStorageDomain() {
        // prevent importing DATA domain
        if (getParameters().getStorageDomain().getStorageDomainType() == StorageDomainType.Data) {
            return failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_IMPORT_DATA_DOMAIN_PROHIBITED);
        }

        if (DbFacade.getInstance().getStorageDomainStaticDao().get(getStorageDomain().getId()) != null) {
            return  failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_STORAGE_DOMAIN_NOT_EXIST);
        }

        List<Guid> storageIds = (ArrayList<Guid>) runVdsCommand(VDSCommandType.HSMGetStorageDomainsList,
                new HSMGetStorageDomainsListVDSCommandParameters(getVdsId(), Guid.Empty, getStorageDomain()
                        .getStorageType(), getStorageDomain().getStorageDomainType(), "")
        ).getReturnValue();

        if (!storageIds.contains(getStorageDomain().getId())) {
            return failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_STORAGE_DOMAIN_ALREADY_EXIST);
        }

        Pair<StorageDomainStatic, SANState> domainFromIrs = (Pair<StorageDomainStatic, SANState>) runVdsCommand(
                VDSCommandType.HSMGetStorageDomainInfo,
                new HSMGetStorageDomainInfoVDSCommandParameters(getVdsId(), getStorageDomain().getId())
        ).getReturnValue();

        if (domainFromIrs != null
                && domainFromIrs.getFirst().getStorageDomainType() != getStorageDomain().getStorageDomainType()) {
            return failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_CANNOT_CHANGE_STORAGE_DOMAIN_TYPE);
        }

        return concreteCheckExistingStorageDomain(domainFromIrs);
    }

    protected boolean concreteCheckExistingStorageDomain(Pair<StorageDomainStatic, SANState> domain) {
        boolean returnValue = false;

        if (domain != null) {
            StorageDomainStatic domainFromIrs = domain.getFirst();
            if (StringUtils.isEmpty(getStorageDomain().getStorageStaticData().getStorage())
                    && StringUtils.isEmpty(domainFromIrs.getStorage()) && domainFromIrs.getConnection() != null
                    && getStorageDomain().getStorageStaticData().getConnection() != null) {
                returnValue = (StringUtils.equals(domainFromIrs.getConnection().getconnection(), getStorageDomain()
                        .getStorageStaticData().getConnection().getconnection()));
            } else if (!StringUtils.isEmpty(getStorageDomain().getStorageStaticData().getStorage())
                    && !StringUtils.isEmpty(domainFromIrs.getStorage())) {
                returnValue = (StringUtils.equals(domainFromIrs.getStorage(), getStorageDomain().getStorageStaticData()
                        .getStorage()));
            }
        }
        if (!returnValue) {
            addCanDoActionMessage(VdcBllMessages.ERROR_CANNOT_ADD_EXISTING_STORAGE_DOMAIN_CONNECTION_DATA_ILLEGAL);
        }
        return returnValue;
    }
}

@@ -3701,49 +3701,8 @@
 mIrs
- != null%0A                && domainFromIrs
 .get
@@ -3776,24 +3776,24 @@
 inType()) %7B%0A
+
             
@@ -4113,43 +4113,8 @@
 se;%0A
-%0A        if (domain != null) %7B%0A    
     
@@ -4168,28 +4168,24 @@
 getFirst();%0A
-    
         if (
@@ -4252,36 +4252,32 @@
 ).getStorage())%0A
-    
                 
@@ -4384,20 +4384,16 @@
         
-    
 && getSt
@@ -4448,36 +4448,32 @@
 on() != null) %7B%0A
-    
             retu
@@ -4580,36 +4580,32 @@
                 
-   
- 
 .getStorageStati
@@ -4643,28 +4643,24 @@
 ection()));%0A
-    
         %7D el
@@ -4759,20 +4759,16 @@
         
-    
 && !Stri
@@ -4818,36 +4818,32 @@
 ) %7B%0A            
- 
-   
 returnValue = (S
@@ -4942,36 +4942,32 @@
                 
-    
 .getStorage()));
@@ -4971,22 +4971,8 @@
 ));%0A
-            %7D%0A
     

Fix FB on AddExistingFileStorageDomain