<commit_before>/*
 * Copyright (c) 2015, salesforce.com, inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided
 * that the following conditions are met:
 *
 *    Redistributions of source code must retain the above copyright notice, this list of conditions and the
 *    following disclaimer.
 *
 *    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
 *    the following disclaimer in the documentation and/or other materials provided with the distribution.
 *
 *    Neither the name of salesforce.com, inc. nor the names of its contributors may be used to endorse or
 *    promote products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.salesforce.dataloader.ui.extraction;

import java.util.*;
import java.util.List;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import org.eclipse.jface.viewers.*;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

import com.salesforce.dataloader.config.Config;
import com.salesforce.dataloader.controller.Controller;
import com.salesforce.dataloader.ui.Labels;
import com.salesforce.dataloader.ui.UIUtils;
import com.sforce.soap.partner.*;

/**
 * Creates the soql
 * 
 * @author <NAME>
 * @since 6.0
 */
public class ExtractionSOQLPage extends ExtractionPage {

    private final Controller controller;
    private final Logger logger = LogManager.getLogger(ExtractionSOQLPage.class);
    private Text soqlText;
    private Field[] fields;
    private CheckboxTableViewer fieldViewer;
    private final String[] operationsDisplayNormal = { "equals", "not equals", "less than", "greater than", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            "less than or equals", "greater than or equals" }; //$NON-NLS-1$ //$NON-NLS-2$
    private final String[] operationsDisplayString = {
            "equals", "not equals", "like", "starts with", "ends with", "contains", "less than", "greater than", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
            "less than or equals", "greater than or equals" }; //$NON-NLS-1$ //$NON-NLS-2$
    private final String[] operationsDisplayMulti = { "equals", "not equals", "includes", "excludes" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    private HashMap<String, String> operationMap;
    private CCombo fieldCombo;
    private Composite whereComp;
    private Composite builderComp;
    private boolean isPickListField;
    private boolean isFocusDialogWanted = true;
    private int lastFieldType;
    private Text valueText;
    private PicklistEntry[] picklistValues;
    private static final String SPACE = " "; //$NON-NLS-1$
    private static final String BEGIN_SINGLEQUOTE = " '"; //$NON-NLS-1$
    private static final String END_SINGLEQUOTE = "' "; //$NON-NLS-1$
    private static final String WILD_CARD = "%"; //$NON-NLS-1$
    private static final String OPEN_BRACKET = "(";//$NON-NLS-1$
    private static final String CLOSE_BRACKET = ")";//$NON-NLS-1$

    // fieldType constants
    private static final int FIELD_STRING = 0;
    private static final int FIELD_MULTI = 1;
    private static final int FIELD_NORMAL = 2;

    // SOQL building variables
    private StringBuffer fromPart;
    private final String SELECT = "Select "; //$NON-NLS-1$
    private StringBuffer fieldPart = new StringBuffer();
    private StringBuffer wherePart = new StringBuffer();
    private Combo operCombo;

    public ExtractionSOQLPage(Controller controller) {
        super(
                Labels.getString("ExtractionSOQLPage.title"), Labels.getString("ExtractionSOQLPage.titleMessage"), UIUtils.getImageRegistry().getDescriptor("splashscreens")); //$NON-NLS-1$ //$NON-NLS-2$

        this.controller = controller;

        // Set the description
        setDescription(Labels.getString("ExtractionSOQLPage.description")); //$NON-NLS-1$
        initOperMap();

        setPageComplete(false);
        lastFieldType = FIELD_NORMAL;
        isPickListField = false;

    }

    private void initOperMap() {
        operationMap = new HashMap<String, String>();
        operationMap.put("equals", "=");
        operationMap.put("not equals", "!=");
        operationMap.put("like", "like");
        operationMap.put("less than", "<");
        operationMap.put("greater than", ">");
        operationMap.put("less than or equals", "<=");
        operationMap.put("greater than or equals", ">=");
        operationMap.put("includes", "includes");
        operationMap.put("excludes", "excludes");
        operationMap.put("starts with", "like");
        operationMap.put("ends with", "like");
        operationMap.put("contains", "like");

    }

    private void setLastFieldType(int type) {
        lastFieldType = type;
    }

    @Override
    public void createControl(Composite parent) {
        Composite comp = new Composite(parent, SWT.NONE);
        GridData data;

        GridLayout gridLayout = new GridLayout(1, false);
        gridLayout.horizontalSpacing = 10;
        gridLayout.marginHeight = 20;
        comp.setLayout(gridLayout);

        builderComp = new Composite(comp, SWT.NONE);
        data = new GridData(SWT.FILL, SWT.FILL, true, true);
        builderComp.setLayoutData(data);
        gridLayout = new GridLayout(2, false);
        gridLayout.horizontalSpacing = 25;
        builderComp.setLayout(gridLayout);

        Label fieldLable = new Label(builderComp, SWT.LEFT);
        fieldLable.setText(Labels.getString("ExtractionSOQLPage.chooseFields")); //$NON-NLS-1$

        Label fieldWhere = new Label(builderComp, SWT.LEFT);
        fieldWhere.setText(Labels.getString("ExtractionSOQLPage.createClauses")); //$NON-NLS-1$

        Composite fieldComp = new Composite(builderComp, SWT.NONE);
        gridLayout = new GridLayout(1, false);
        gridLayout.horizontalSpacing = 25;
        fieldComp.setLayout(gridLayout);

        Text search = new Text(fieldComp, SWT.SEARCH | SWT.ICON_CANCEL | SWT.ICON_SEARCH);
        data = new GridData(GridData.FILL_HORIZONTAL);
        search.setLayoutData(data);
        fieldComp.setLayoutData(data);

        fieldViewer = CheckboxTableViewer.newCheckList(fieldComp, SWT.BORDER);
        ColumnViewerToolTipSupport.enableFor(fieldViewer);
        fieldViewer.setLabelProvider(new ExtrFieldLabelProvider());
        fieldViewer.setContentProvider(new ExtrFieldContentProvider());
        data = new GridData(GridData.FILL_HORIZONTAL);
        data.heightHint = 100;
        fieldViewer.getTable().setLayoutData(data);

        FieldFilter filter = new FieldFilter(search);
        fieldViewer.addFilter(filter);
        fieldViewer.addCheckStateListener(new ICheckStateListener() {
            @Override
            public void checkStateChanged(CheckStateChangedEvent event) {
                generateFieldPart();
                generateSOQLText();
            }
        });
        
        search.addSelectionListener(new SelectionAdapter() {
            public void widgetDefaultSelected(SelectionEvent e) {
                fieldViewer.refresh();
            }
        });
        
        search.addListener(SWT.KeyUp, new Listener() {
            public void handleEvent(Event e) {
                fieldViewer.refresh();
            }
        });

        whereComp = new Composite(builderComp, SWT.NONE);
        data = new GridData(GridData.FILL_BOTH);<commit_msg>set initial width of field selection table in edit SoQL step<commit_after>/*
 * Copyright (c) 2015, salesforce.com, inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided
 * that the following conditions are met:
 *
 *    Redistributions of source code must retain the above copyright notice, this list of conditions and the
 *    following disclaimer.
 *
 *    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
 *    the following disclaimer in the documentation and/or other materials provided with the distribution.
 *
 *    Neither the name of salesforce.com, inc. nor the names of its contributors may be used to endorse or
 *    promote products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.salesforce.dataloader.ui.extraction;

import java.util.*;
import java.util.List;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import org.eclipse.jface.viewers.*;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

import com.salesforce.dataloader.config.Config;
import com.salesforce.dataloader.controller.Controller;
import com.salesforce.dataloader.ui.Labels;
import com.salesforce.dataloader.ui.UIUtils;
import com.sforce.soap.partner.*;

/**
 * Creates the soql
 * 
 * @author <NAME>
 * @since 6.0
 */
public class ExtractionSOQLPage extends ExtractionPage {

    private final Controller controller;
    private final Logger logger = LogManager.getLogger(ExtractionSOQLPage.class);
    private Text soqlText;
    private Field[] fields;
    private CheckboxTableViewer fieldViewer;
    private final String[] operationsDisplayNormal = { "equals", "not equals", "less than", "greater than", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            "less than or equals", "greater than or equals" }; //$NON-NLS-1$ //$NON-NLS-2$
    private final String[] operationsDisplayString = {
            "equals", "not equals", "like", "starts with", "ends with", "contains", "less than", "greater than", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
            "less than or equals", "greater than or equals" }; //$NON-NLS-1$ //$NON-NLS-2$
    private final String[] operationsDisplayMulti = { "equals", "not equals", "includes", "excludes" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    private HashMap<String, String> operationMap;
    private CCombo fieldCombo;
    private Composite whereComp;
    private Composite builderComp;
    private boolean isPickListField;
    private boolean isFocusDialogWanted = true;
    private int lastFieldType;
    private Text valueText;
    private PicklistEntry[] picklistValues;
    private static final String SPACE = " "; //$NON-NLS-1$
    private static final String BEGIN_SINGLEQUOTE = " '"; //$NON-NLS-1$
    private static final String END_SINGLEQUOTE = "' "; //$NON-NLS-1$
    private static final String WILD_CARD = "%"; //$NON-NLS-1$
    private static final String OPEN_BRACKET = "(";//$NON-NLS-1$
    private static final String CLOSE_BRACKET = ")";//$NON-NLS-1$

    // fieldType constants
    private static final int FIELD_STRING = 0;
    private static final int FIELD_MULTI = 1;
    private static final int FIELD_NORMAL = 2;

    // SOQL building variables
    private StringBuffer fromPart;
    private final String SELECT = "Select "; //$NON-NLS-1$
    private StringBuffer fieldPart = new StringBuffer();
    private StringBuffer wherePart = new StringBuffer();
    private Combo operCombo;

    public ExtractionSOQLPage(Controller controller) {
        super(
                Labels.getString("ExtractionSOQLPage.title"), Labels.getString("ExtractionSOQLPage.titleMessage"), UIUtils.getImageRegistry().getDescriptor("splashscreens")); //$NON-NLS-1$ //$NON-NLS-2$

        this.controller = controller;

        // Set the description
        setDescription(Labels.getString("ExtractionSOQLPage.description")); //$NON-NLS-1$
        initOperMap();

        setPageComplete(false);
        lastFieldType = FIELD_NORMAL;
        isPickListField = false;

    }

    private void initOperMap() {
        operationMap = new HashMap<String, String>();
        operationMap.put("equals", "=");
        operationMap.put("not equals", "!=");
        operationMap.put("like", "like");
        operationMap.put("less than", "<");
        operationMap.put("greater than", ">");
        operationMap.put("less than or equals", "<=");
        operationMap.put("greater than or equals", ">=");
        operationMap.put("includes", "includes");
        operationMap.put("excludes", "excludes");
        operationMap.put("starts with", "like");
        operationMap.put("ends with", "like");
        operationMap.put("contains", "like");

    }

    private void setLastFieldType(int type) {
        lastFieldType = type;
    }

    @Override
    public void createControl(Composite parent) {
        Composite comp = new Composite(parent, SWT.NONE);
        GridData data;

        GridLayout gridLayout = new GridLayout(1, false);
        gridLayout.horizontalSpacing = 10;
        gridLayout.marginHeight = 20;
        comp.setLayout(gridLayout);

        builderComp = new Composite(comp, SWT.NONE);
        data = new GridData(SWT.FILL, SWT.FILL, true, true);
        builderComp.setLayoutData(data);
        gridLayout = new GridLayout(2, false);
        gridLayout.horizontalSpacing = 25;
        builderComp.setLayout(gridLayout);

        Label fieldLable = new Label(builderComp, SWT.LEFT);
        fieldLable.setText(Labels.getString("ExtractionSOQLPage.chooseFields")); //$NON-NLS-1$

        Label fieldWhere = new Label(builderComp, SWT.LEFT);
        fieldWhere.setText(Labels.getString("ExtractionSOQLPage.createClauses")); //$NON-NLS-1$

        Composite fieldComp = new Composite(builderComp, SWT.NONE);
        gridLayout = new GridLayout(1, false);
        gridLayout.horizontalSpacing = 25;
        fieldComp.setLayout(gridLayout);

        Text search = new Text(fieldComp, SWT.SEARCH | SWT.ICON_CANCEL | SWT.ICON_SEARCH);
        data = new GridData(GridData.FILL_HORIZONTAL);
        search.setLayoutData(data);
        fieldComp.setLayoutData(data);

        fieldViewer = CheckboxTableViewer.newCheckList(fieldComp, SWT.BORDER);
        ColumnViewerToolTipSupport.enableFor(fieldViewer);
        fieldViewer.setLabelProvider(new ExtrFieldLabelProvider());
        fieldViewer.setContentProvider(new ExtrFieldContentProvider());
        data = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
        data.heightHint = 150;
        data.widthHint = 135;
        fieldViewer.getTable().setLayoutData(data);

        FieldFilter filter = new FieldFilter(search);
        fieldViewer.addFilter(filter);
        fieldViewer.addCheckStateListener(new ICheckStateListener() {
            @Override
            public void checkStateChanged(CheckStateChangedEvent event) {
                generateFieldPart();
                generateSOQLText();
            }
        });
        
        search.addSelectionListener(new SelectionAdapter() {
            public void widgetDefaultSelected(SelectionEvent e) {
                fieldViewer.refresh();
            }
        });
        
        search.addListener(SWT.KeyUp, new Listener() {
            public void handleEvent(Event e) {
                fieldViewer.refresh();
            }
        });

        whereComp = new Composite(builderComp, SWT.NONE);
        data = new GridData(GridData.FILL_BOTH);
{'fraction_non_alphanumeric': 0.10991379310344827, 'fraction_numerical': 0.00646551724137931, 'mean_word_length': 3.2309019221291275, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3753714', 'n_tokens_mistral': 5077, 'n_tokens_neox': 4839, 'n_words': 1334}