<commit_before>package net.sourceforge.javydreamercsw.client.ui.nodes.actions;

import com.validation.manager.core.api.entity.manager.VMEntityManager;
import com.validation.manager.core.db.Requirement;
import com.validation.manager.core.db.Step;
import com.validation.manager.core.tool.message.MessageHandler;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.openide.util.Cancellable;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.RequestProcessor;
import org.openide.util.TaskListener;

/**
 *
 * @author <NAME> <<EMAIL>>
 */
public class ExportRequirementMappingAction extends AbstractAction {

    private static final Logger LOG
            = Logger.getLogger(ExportRequirementMappingAction.class.getSimpleName());
    private final RequestProcessor RP
            = new RequestProcessor("Requirement Mapping Exporter", 1, true);
    private RequestProcessor.Task theTask = null;
    private ProgressHandle ph;
    private boolean valid = false;

    public ExportRequirementMappingAction() {
        super("Export Requirement Mapping",
                new ImageIcon("com/validation/manager/resources/icons/Electronics/USB.png"));
    }

    /**
     * @return the valid
     */
    public boolean isValid() {
        return valid;
    }

    /**
     * @param valid the valid to set
     */
    public void setValid(boolean valid) {
        this.valid = valid;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                ph = ProgressHandleFactory.createHandle("Requirement Mapping Exporter",
                        new Cancellable() {

                            @Override
                            public boolean cancel() {
                                return handleCancel();
                            }
                        });
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        List<Requirement> mapping = new ArrayList<>();
                        VMEntityManager manager = null;
                        for (VMEntityManager m : Lookup.getDefault().lookupAll(VMEntityManager.class)) {
                            if (m.supportEntity(Requirement.class)) {
                                manager = m;
                                break;
                            }
                        }
                        if (manager != null) {
                            for (Object o : manager.getEntities()) {
                                Requirement r = (Requirement) o;
                                //Only export approved requirements.
                                if (r.getRequirementStatusId().getStatus().equals("general.approved")) {
                                    mapping.add(r);
                                }
                            }
                        }
                        final ExportOptionsDialog dialog = new ExportOptionsDialog(new javax.swing.JFrame(), true);
                        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                            @Override
                            public void windowClosing(java.awt.event.WindowEvent e) {
                                dialog.dispose();
                            }
                        });
                        dialog.setIconImage(new ImageIcon("com/validation/manager/resources/icons/VMSmall.png").getImage());
                        dialog.setLocationRelativeTo(null);
                        dialog.setVisible(true);
                        while (dialog.isVisible()) {
                            //Do nothing
                        }
                        //Pick where to save the file
                        JFileChooser fc = new JFileChooser();
                        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                        int returnVal = fc.showOpenDialog(new JFrame());
                        if (returnVal == JFileChooser.APPROVE_OPTION) {
                            try {
                                File file = fc.getSelectedFile();
                                String FS = System.getProperty("file.separator");
                                File export = new File(file.getAbsolutePath() + FS
                                        + "Export.xls");
                                if (export.exists()) {
                                    export.delete();
                                }
                                export.createNewFile();
                                //Now export
                                HSSFWorkbook workbook = new HSSFWorkbook();
                                HSSFSheet sheet = workbook.createSheet("Report Sheet");

                                Map<Integer, Object[]> data = new TreeMap<>();
                                int count = 0;
                                data.put(++count,
                                        new Object[]{"Requirement", "Children", "Test"});
                                for (Requirement r : mapping) {
                                    StringBuilder childSb = new StringBuilder();
                                    StringBuilder testSb = new StringBuilder();
                                    //Add test directly testing the requirement itself.
                                    for (Step step : r.getStepList()) {
                                        if (!testSb.toString().trim().isEmpty()) {
                                            testSb.append(";");
                                        }
                                        if (dialog.isUsePlan()) {
                                            testSb.append("Test: ")
                                                    .append(step.getTestCase().getTest().getName());
                                        }
                                        if (dialog.isUseTest()) {
                                            testSb.append(", Test Case: ")
                                                    .append(step.getTestCase().getName());
                                        }
                                        if (dialog.isUseStep()) {
                                            testSb.append(", step ")
                                                    .append(step.getStepSequence());
                                        }
                                        dialog.dispose();
                                    }
                                    //Now process the children
                                    for (Requirement child : r.getRequirementList1()) {
                                        if (!childSb.toString().trim().isEmpty()) {
                                            childSb.append(",");<commit_msg>Remove duplicates, specially useful if details are ignored.<commit_after>package net.sourceforge.javydreamercsw.client.ui.nodes.actions;

import com.validation.manager.core.api.entity.manager.VMEntityManager;
import com.validation.manager.core.db.Requirement;
import com.validation.manager.core.db.Step;
import com.validation.manager.core.tool.message.MessageHandler;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.openide.util.Cancellable;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.RequestProcessor;
import org.openide.util.TaskListener;

/**
 *
 * @author <NAME> <<EMAIL>>
 */
public class ExportRequirementMappingAction extends AbstractAction {

    private static final Logger LOG
            = Logger.getLogger(ExportRequirementMappingAction.class.getSimpleName());
    private final RequestProcessor RP
            = new RequestProcessor("Requirement Mapping Exporter", 1, true);
    private RequestProcessor.Task theTask = null;
    private ProgressHandle ph;
    private boolean valid = false;

    public ExportRequirementMappingAction() {
        super("Export Requirement Mapping",
                new ImageIcon("com/validation/manager/resources/icons/Electronics/USB.png"));
    }

    /**
     * @return the valid
     */
    public boolean isValid() {
        return valid;
    }

    /**
     * @param valid the valid to set
     */
    public void setValid(boolean valid) {
        this.valid = valid;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                ph = ProgressHandleFactory.createHandle("Requirement Mapping Exporter",
                        new Cancellable() {

                            @Override
                            public boolean cancel() {
                                return handleCancel();
                            }
                        });
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        List<Requirement> mapping = new ArrayList<>();
                        VMEntityManager manager = null;
                        for (VMEntityManager m : Lookup.getDefault().lookupAll(VMEntityManager.class)) {
                            if (m.supportEntity(Requirement.class)) {
                                manager = m;
                                break;
                            }
                        }
                        if (manager != null) {
                            for (Object o : manager.getEntities()) {
                                Requirement r = (Requirement) o;
                                //Only export approved requirements.
                                if (r.getRequirementStatusId().getStatus().equals("general.approved")) {
                                    mapping.add(r);
                                }
                            }
                        }
                        final ExportOptionsDialog dialog = new ExportOptionsDialog(new javax.swing.JFrame(), true);
                        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                            @Override
                            public void windowClosing(java.awt.event.WindowEvent e) {
                                dialog.dispose();
                            }
                        });
                        dialog.setIconImage(new ImageIcon("com/validation/manager/resources/icons/VMSmall.png").getImage());
                        dialog.setLocationRelativeTo(null);
                        dialog.setVisible(true);
                        while (dialog.isVisible()) {
                            //Do nothing
                        }
                        //Pick where to save the file
                        JFileChooser fc = new JFileChooser();
                        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                        int returnVal = fc.showOpenDialog(new JFrame());
                        if (returnVal == JFileChooser.APPROVE_OPTION) {
                            try {
                                File file = fc.getSelectedFile();
                                String FS = System.getProperty("file.separator");
                                File export = new File(file.getAbsolutePath() + FS
                                        + "Export.xls");
                                if (export.exists()) {
                                    export.delete();
                                }
                                export.createNewFile();
                                //Now export
                                HSSFWorkbook workbook = new HSSFWorkbook();
                                HSSFSheet sheet = workbook.createSheet("Report Sheet");

                                Map<Integer, Object[]> data = new TreeMap<>();
                                int count = 0;
                                data.put(++count,
                                        new Object[]{"Requirement", "Children", "Test"});
                                for (Requirement r : mapping) {
                                    StringBuilder childSb = new StringBuilder();
                                    StringBuilder testSb = new StringBuilder();
                                    //Add test directly testing the requirement itself.
                                    for (Step step : r.getStepList()) {
                                        String temp;
                                        if (!testSb.toString().trim().isEmpty()) {
                                            testSb.append(";");
                                        }
                                        if (dialog.isUsePlan()) {
                                            temp = "Test: "
                                                    + step.getTestCase().getTest().getName();
                                            if (!testSb.toString().contains(temp)) {
                                                testSb.append(temp);
                                            }
                                        }
                                        if (dialog.isUseTest()) {
                                            temp = ", Test Case: "
                                                    + step.getTestCase().getName();
                                            if (!testSb.toString().contains(temp)) {
                                                testSb.append(temp);
                                            }
                                        }
                                        if (dialog.isUseStep()) {
                                            temp = ", step "
                                                    + step.getStepSequence();
                                            if (!testSb.toString().contains(temp)) {
                                                testSb.append(temp);
                                            }
                                        }
                                        dialog.dispose();
                                    }
                                    //Now process the children
                                    for (Requirement child : r.getRequirementList1()) {
                                        if (!childSb.toString().trim().isEmpty()) {
                                            childSb.append(",");
{'fraction_non_alphanumeric': 0.08619173262972735, 'fraction_numerical': 0.0003769317753486619, 'mean_word_length': 1.0722468107263734, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 117, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3409455', 'n_tokens_mistral': 3428, 'n_tokens_neox': 3233, 'n_words': 674}