<commit_before>package org.ovirt.engine.ui.uicommonweb.models.gluster;

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

import org.ovirt.engine.core.common.action.VdcActionType;
import org.ovirt.engine.core.common.action.VdcReturnValueBase;
import org.ovirt.engine.core.common.action.VdsActionParameters;
import org.ovirt.engine.core.common.action.gluster.CreateBrickParameters;
import org.ovirt.engine.core.common.businessentities.VDS;
import org.ovirt.engine.core.common.businessentities.VDSStatus;
import org.ovirt.engine.core.common.businessentities.gluster.StorageDevice;
import org.ovirt.engine.core.common.mode.ApplicationMode;
import org.ovirt.engine.core.common.queries.ConfigurationValues;
import org.ovirt.engine.core.common.queries.GetConfigurationValueParameters;
import org.ovirt.engine.ui.frontend.AsyncQuery;
import org.ovirt.engine.ui.frontend.Frontend;
import org.ovirt.engine.ui.frontend.INewAsyncCallback;
import org.ovirt.engine.ui.uicommonweb.Linq;
import org.ovirt.engine.ui.uicommonweb.UICommand;
import org.ovirt.engine.ui.uicommonweb.dataprovider.AsyncDataProvider;
import org.ovirt.engine.ui.uicommonweb.help.HelpTag;
import org.ovirt.engine.ui.uicommonweb.models.SearchableListModel;
import org.ovirt.engine.ui.uicompat.ConstantsManager;
import org.ovirt.engine.ui.uicompat.FrontendActionAsyncResult;
import org.ovirt.engine.ui.uicompat.IFrontendActionAsyncCallback;
import org.ovirt.engine.ui.uicompat.PropertyChangedEventArgs;

public class HostGlusterStorageDevicesListModel extends SearchableListModel<VDS, StorageDevice> {

    public HostGlusterStorageDevicesListModel() {
        setTitle(ConstantsManager.getInstance().getConstants().storageDevices());
        setHelpTag(HelpTag.gluster_storage_devices);
        setHashName("gluster_storage_devices"); //$NON-NLS-1$
        setSyncStorageDevicesCommand(new UICommand("sync", this)); //$NON-NLS-1$
        setCreateBrickCommand(new UICommand("createBrick", this)); //$NON-NLS-1$
        setAvailableInModes(ApplicationMode.GlusterOnly);
    }

    @Override
    public VDS getEntity() {
        return super.getEntity();
    }

    public void setEntity(VDS value)
    {
        super.setEntity(value);
        updateActionAvailability();

    }

    private UICommand createBrickCommand;

    public UICommand getCreateBrickCommand() {
        return createBrickCommand;
    }

    public void setCreateBrickCommand(UICommand createBrickCommand) {
        this.createBrickCommand = createBrickCommand;
    }

    private UICommand syncStorageDevicesCommand;

    public UICommand getSyncStorageDevicesCommand() {
        return syncStorageDevicesCommand;
    }

    public void setSyncStorageDevicesCommand(UICommand syncStorageDevicesCommand) {
        this.syncStorageDevicesCommand = syncStorageDevicesCommand;
    }

    @Override
    protected void onEntityChanged() {
        super.onEntityChanged();
        getSearchCommand().execute();
    }

    @Override
    protected void entityPropertyChanged(Object sender, PropertyChangedEventArgs e)
    {
        super.entityPropertyChanged(sender, e);
        getSearchCommand().execute();
    }

    @Override
    public void search()
    {
        if (getEntity() != null)
        {
            super.search();
        }
    }

    @Override
    protected void syncSearch()
    {
        if (getEntity() == null) {
            return;
        }

        AsyncDataProvider.getInstance().getStorageDevices(new AsyncQuery(this, new INewAsyncCallback() {

            @Override
            public void onSuccess(Object model, Object returnValue) {
                List<StorageDevice> devices = (List<StorageDevice>) returnValue;
                Collections.sort(devices, new Linq.StorageDeviceComparer());
                setItems(devices);
            }
        }), getEntity().getId());

    }

    @Override
    protected String getListName() {
        return "HostGlusterStorageDevicesListModel"; //$NON-NLS-1$
    }

    private void createBrick() {
        if (getWindow() != null) {
            return;
        }

        VDS host = getEntity();
        if (host == null) {
            return;
        }

        final CreateBrickModel lvModel = new CreateBrickModel();
        lvModel.setTitle(ConstantsManager.getInstance().getConstants().createBrick());
        lvModel.setHelpTag(HelpTag.create_brick);
        lvModel.setHashName("create_brick"); //$NON-NLS-1$
        lvModel.startProgress(ConstantsManager.getInstance().getConstants().fetchingDataMessage());
        setWindow(lvModel);

        AsyncQuery asyncQueryForDeviceList = new AsyncQuery();
        asyncQueryForDeviceList.setModel(lvModel);
        asyncQueryForDeviceList.asyncCallback = new INewAsyncCallback() {

            @Override
            public void onSuccess(Object model, Object returnValue) {
                lvModel.stopProgress();
                CreateBrickModel lvModel = (CreateBrickModel) model;
                List<StorageDevice> devices = (List<StorageDevice>) returnValue;
                final List<StorageDevice> eligibleDevices = new ArrayList<>();

                for (StorageDevice device : devices) {
                    if (device.getCanCreateBrick()) {
                        eligibleDevices.add(device);
                    }
                }
                lvModel.getStorageDevices().setItems(eligibleDevices);
                if (getSelectedItems() != null) {
                    lvModel.setSelectedDevices(getSelectedItems());
                }
            }
        };
        AsyncDataProvider.getInstance().getStorageDevices(asyncQueryForDeviceList, host.getId());

        AsyncQuery asyncQueryForDefaultMountPoint = new AsyncQuery();
        asyncQueryForDefaultMountPoint.setModel(lvModel);
        asyncQueryForDefaultMountPoint.asyncCallback = new INewAsyncCallback() {

            @Override
            public void onSuccess(Object model, Object returnValue) {
                lvModel.stopProgress();
                CreateBrickModel lvModel = (CreateBrickModel) model;
                String defaultMountPoint = (String) returnValue;
                lvModel.getDefaultMountFolder().setEntity(defaultMountPoint);
            }
        };
        AsyncDataProvider.getInstance()
                .getConfigFromCache(new GetConfigurationValueParameters(ConfigurationValues.DefaultGlusterBrickMountPoint,
                        AsyncDataProvider.getInstance().getDefaultConfigurationVersion()),
                        asyncQueryForDefaultMountPoint);

        UICommand okCommand = UICommand.createDefaultOkUiCommand("onCreateBrick", this); //$NON-NLS-1$
        lvModel.getCommands().add(okCommand);

        UICommand cancelCommand = UICommand.createCancelUiCommand("closeWindow", this); //$NON-NLS-1$
        lvModel.getCommands().add(cancelCommand);
    }

    private void syncStorageDevices() {
        Frontend.getInstance()
                .runAction(VdcActionType.SyncStorageDevices, new VdsActionParameters(getEntity().getId()));
    }

    private void updateActionAvailability() {
        VDS vds = getEntity();
        if (vds != null) {
            getSyncStorageDevicesCommand().setIsExecutionAllowed(vds.getStatus() == VDSStatus.Up);
            getCreateBrickCommand().setIsExecutionAllowed(vds.getStatus() == VDSStatus.Up);
        }
    }

    private void onCreateBrick() {
        CreateBrickModel lvModel = (CreateBrickModel) getWindow();
        if (lvModel == null) {
            return;
        }
        if (!lvModel.validate()) {
            return;
        }

        VDS host = getEntity();
        if (host == null) {
            return;
        }

        lvModel.startProgress(null);

        List<StorageDevice> selectedDevices = new ArrayList<StorageDevice>();
        for (StorageDevice device : lvModel.getStorageDevices().getSelectedItems()) {
            selectedDevices.add(device);
        }

        CreateBrickParameters parameters =
                new CreateBrickParameters(host.getId(),
                        lvModel.getLvName().getEntity(),
                        lvModel.getMountPoint().getEntity(),
                        lvModel.getRaidTypeList().getSelectedItem(),
                        lvModel.getNoOfPhysicalDisksInRaidVolume().getEntity(),
                        lvModel.getStripeSize().getEntity(),
                        selectedDevices);

        Frontend.getInstance().runAction(VdcActionType.CreateBrick, parameters, new IFrontendActionAsyncCallback() {
            @Override
            public void executed(FrontendActionAsyncResult result) {
                postCreateBrick(result.getReturnValue());
            }
        }, this);

    }

    private void postCreateBrick(VdcReturnValueBase returnValue) {
        CreateBrickModel model = (CreateBrickModel) getWindow();
        model.stopProgress();
        if (returnValue != null && returnValue.getSucceeded()) {
            setWindow(null);
        }
    }

    @Override
    public void executeCommand(UICommand command) {
        super.executeCommand(command);
        if (command.equals(getCreateBrickCommand())) {
            createBrick();
        } else if (command.getName().equals("onCreateBrick")) { //$NON-NLS-1$
            onCreateBrick();
        } else if (command.getName().equals("closeWindow")) { //$NON-NLS-1$
            setWindow(null);
        } else if (command.equals(getSyncStorageDevicesCommand())) {
            syncStorageDevices();
        }
    }

}
<commit_msg>webadmin: fix action availability for create brick<commit_after>package org.ovirt.engine.ui.uicommonweb.models.gluster;

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

import org.ovirt.engine.core.common.action.VdcActionType;
import org.ovirt.engine.core.common.action.VdcReturnValueBase;
import org.ovirt.engine.core.common.action.VdsActionParameters;
import org.ovirt.engine.core.common.action.gluster.CreateBrickParameters;
import org.ovirt.engine.core.common.businessentities.VDS;
import org.ovirt.engine.core.common.businessentities.VDSStatus;
import org.ovirt.engine.core.common.businessentities.gluster.StorageDevice;
import org.ovirt.engine.core.common.mode.ApplicationMode;
import org.ovirt.engine.core.common.queries.ConfigurationValues;
import org.ovirt.engine.core.common.queries.GetConfigurationValueParameters;
import org.ovirt.engine.ui.frontend.AsyncQuery;
import org.ovirt.engine.ui.frontend.Frontend;
import org.ovirt.engine.ui.frontend.INewAsyncCallback;
import org.ovirt.engine.ui.uicommonweb.Linq;
import org.ovirt.engine.ui.uicommonweb.UICommand;
import org.ovirt.engine.ui.uicommonweb.dataprovider.AsyncDataProvider;
import org.ovirt.engine.ui.uicommonweb.help.HelpTag;
import org.ovirt.engine.ui.uicommonweb.models.SearchableListModel;
import org.ovirt.engine.ui.uicompat.ConstantsManager;
import org.ovirt.engine.ui.uicompat.FrontendActionAsyncResult;
import org.ovirt.engine.ui.uicompat.IFrontendActionAsyncCallback;
import org.ovirt.engine.ui.uicompat.PropertyChangedEventArgs;

public class HostGlusterStorageDevicesListModel extends SearchableListModel<VDS, StorageDevice> {

    public HostGlusterStorageDevicesListModel() {
        setTitle(ConstantsManager.getInstance().getConstants().storageDevices());
        setHelpTag(HelpTag.gluster_storage_devices);
        setHashName("gluster_storage_devices"); //$NON-NLS-1$
        setSyncStorageDevicesCommand(new UICommand("sync", this)); //$NON-NLS-1$
        setCreateBrickCommand(new UICommand("createBrick", this)); //$NON-NLS-1$
        setAvailableInModes(ApplicationMode.GlusterOnly);
    }

    @Override
    public VDS getEntity() {
        return super.getEntity();
    }

    public void setEntity(VDS value)
    {
        super.setEntity(value);
        updateActionAvailability();

    }

    private UICommand createBrickCommand;

    public UICommand getCreateBrickCommand() {
        return createBrickCommand;
    }

    public void setCreateBrickCommand(UICommand createBrickCommand) {
        this.createBrickCommand = createBrickCommand;
    }

    private UICommand syncStorageDevicesCommand;

    public UICommand getSyncStorageDevicesCommand() {
        return syncStorageDevicesCommand;
    }

    public void setSyncStorageDevicesCommand(UICommand syncStorageDevicesCommand) {
        this.syncStorageDevicesCommand = syncStorageDevicesCommand;
    }

    @Override
    protected void onEntityChanged() {
        super.onEntityChanged();
        getSearchCommand().execute();
    }

    @Override
    protected void entityPropertyChanged(Object sender, PropertyChangedEventArgs e)
    {
        super.entityPropertyChanged(sender, e);
        getSearchCommand().execute();
    }

    @Override
    public void search()
    {
        if (getEntity() != null)
        {
            super.search();
        }
    }

    @Override
    protected void syncSearch()
    {
        if (getEntity() == null) {
            return;
        }

        AsyncDataProvider.getInstance().getStorageDevices(new AsyncQuery(this, new INewAsyncCallback() {

            @Override
            public void onSuccess(Object model, Object returnValue) {
                List<StorageDevice> devices = (List<StorageDevice>) returnValue;
                Collections.sort(devices, new Linq.StorageDeviceComparer());
                setItems(devices);
            }
        }), getEntity().getId());

    }

    @Override
    protected String getListName() {
        return "HostGlusterStorageDevicesListModel"; //$NON-NLS-1$
    }

    private void createBrick() {
        if (getWindow() != null) {
            return;
        }

        VDS host = getEntity();
        if (host == null) {
            return;
        }

        final CreateBrickModel lvModel = new CreateBrickModel();
        lvModel.setTitle(ConstantsManager.getInstance().getConstants().createBrick());
        lvModel.setHelpTag(HelpTag.create_brick);
        lvModel.setHashName("create_brick"); //$NON-NLS-1$
        lvModel.startProgress(ConstantsManager.getInstance().getConstants().fetchingDataMessage());
        setWindow(lvModel);
        List<StorageDevice> selectedDevices = getSelectedItems();
        lvModel.getStorageDevices().setItems(selectedDevices);
        lvModel.setSelectedDevices(selectedDevices);

        AsyncQuery asyncQueryForDefaultMountPoint = new AsyncQuery();
        asyncQueryForDefaultMountPoint.setModel(lvModel);
        asyncQueryForDefaultMountPoint.asyncCallback = new INewAsyncCallback() {

            @Override
            public void onSuccess(Object model, Object returnValue) {
                lvModel.stopProgress();
                CreateBrickModel lvModel = (CreateBrickModel) model;
                String defaultMountPoint = (String) returnValue;
                lvModel.getDefaultMountFolder().setEntity(defaultMountPoint);
            }
        };
        AsyncDataProvider.getInstance()
                .getConfigFromCache(new GetConfigurationValueParameters(ConfigurationValues.DefaultGlusterBrickMountPoint,
                        AsyncDataProvider.getInstance().getDefaultConfigurationVersion()),
                        asyncQueryForDefaultMountPoint);

        UICommand okCommand = UICommand.createDefaultOkUiCommand("onCreateBrick", this); //$NON-NLS-1$
        lvModel.getCommands().add(okCommand);

        UICommand cancelCommand = UICommand.createCancelUiCommand("closeWindow", this); //$NON-NLS-1$
        lvModel.getCommands().add(cancelCommand);
    }

    private void syncStorageDevices() {
        Frontend.getInstance()
                .runAction(VdcActionType.SyncStorageDevices, new VdsActionParameters(getEntity().getId()));
    }

    @Override
    protected void onSelectedItemChanged() {
        super.onSelectedItemChanged();
        updateActionAvailability();
    }

    @Override
    protected void selectedItemsChanged()
    {
        super.selectedItemsChanged();
        updateActionAvailability();
    }

    private void updateActionAvailability() {
        VDS vds = getEntity();
        if (vds != null && vds.getStatus() == VDSStatus.Up) {
            getSyncStorageDevicesCommand().setIsExecutionAllowed(true);
            getCreateBrickCommand().setIsExecutionAllowed(canCreateBrick());
        } else {
            getSyncStorageDevicesCommand().setIsExecutionAllowed(false);
            getCreateBrickCommand().setIsExecutionAllowed(false);
        }
    }

    private boolean canCreateBrick(){
        boolean canCreateBrick = false;
        List<StorageDevice> selectedDevices = getSelectedItems();
        if (selectedDevices != null) {
            for (StorageDevice device : selectedDevices) {
                if (device.getCanCreateBrick()) {
                    canCreateBrick = true;
                } else {
                    canCreateBrick = false;
                    break;
                }
            }
        }

        return canCreateBrick;

    }
    private void onCreateBrick() {
        CreateBrickModel lvModel = (CreateBrickModel) getWindow();
        if (lvModel == null) {
            return;
        }
        if (!lvModel.validate()) {
            return;
        }

        VDS host = getEntity();
        if (host == null) {
            return;
        }

        lvModel.startProgress(null);

        List<StorageDevice> selectedDevices = new ArrayList<StorageDevice>();
        for (StorageDevice device : lvModel.getStorageDevices().getSelectedItems()) {
            selectedDevices.add(device);
        }

        CreateBrickParameters parameters =
                new CreateBrickParameters(host.getId(),
                        lvModel.getLvName().getEntity(),
                        lvModel.getMountPoint().getEntity(),
                        lvModel.getRaidTypeList().getSelectedItem(),
                        lvModel.getNoOfPhysicalDisksInRaidVolume().getEntity(),
                        lvModel.getStripeSize().getEntity(),
                        selectedDevices);

        Frontend.getInstance().runAction(VdcActionType.CreateBrick, parameters, new IFrontendActionAsyncCallback() {
            @Override
            public void executed(FrontendActionAsyncResult result) {
                postCreateBrick(result.getReturnValue());
            }
        }, this);

    }

    private void postCreateBrick(VdcReturnValueBase returnValue) {
        CreateBrickModel model = (CreateBrickModel) getWindow();
        model.stopProgress();
        if (returnValue != null && returnValue.getSucceeded()) {
            setWindow(null);
        }
    }

    @Override
    public void executeCommand(UICommand command) {
        super.executeCommand(command);
        if (command.equals(getCreateBrickCommand())) {
            createBrick();
        } else if (command.getName().equals("onCreateBrick")) { //$NON-NLS-1$
            onCreateBrick();
        } else if (command.getName().equals("closeWindow")) { //$NON-NLS-1$
            setWindow(null);
        } else if (command.equals(getSyncStorageDevicesCommand())) {
            syncStorageDevices();
        }
    }

}

{'fraction_non_alphanumeric': 0.1007642378559464, 'fraction_numerical': 0.0009422110552763819, 'mean_word_length': 2.707549000582185, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4160659', 'n_tokens_mistral': 5212, 'n_tokens_neox': 5102, 'n_words': 791}