All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.clarussecure.dataoperations.encryption.testing.Cloud Maven / Gradle / Ivy

The newest version!
package eu.clarussecure.dataoperations.encryption.testing;

import eu.clarussecure.dataoperations.Criteria;
import eu.clarussecure.dataoperations.encryption.operators.Select;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Cloud {
    // Dommy implementation for a cloud
    // The implementation is a table, saving columns and rows.

    private List data;
    private final String[] columns; // attributes

    public Cloud(String[] columns) {
        this.columns = columns;
        this.data = new ArrayList<>();
    }

    public void addRow(String[] row) {
        // It is assumed that the given arrays contains the columns in order!
        this.data.add(row);
    }

    public void addRows(String[][] rows) {
        // Each array will be added to the data
        this.data.addAll(Arrays.asList(rows));
    }

    public String[][] getRows(String[] protectedAttribNames, Criteria[] criteria) {
        // Select the columns regarding the required attribute names
        List results = new ArrayList<>();
        List filteredResults = new ArrayList<>();

        // First, parse the selection criteria and prepare the Select instances
        Map> selectorsSet = new HashMap<>();

        if (criteria == null) {
            // There is no criteria, use the Identity Function
            List selectors = selectorsSet.get(crit.getAttributeName());
                // Create the list of it does not exist
                if (selectors == null) {
                    selectors = new ArrayList<>();
                    selectorsSet.put(crit.getAttributeName(), selectors);
                }
                // Add the current selector to the list
                selectors.add(Select.getInstance(crit.getOperator(), crit.getValue()));
            }
        }

        // TODO - Process the criteria to filter the data
        // IDEA - This could be done in this part of the code.
        for (String[] row : data) { // Select each row of the loaded data
            int p = 0; // column position in the results table
            String[] selectedRow = new String[protectedAttribNames.length]; // new result row
            for (int i = 0; i < columns.length; i++) { // for each stored column name

                for (String protectedAttribName : protectedAttribNames) { //for each requested column
                    if (columns[i].equals(protectedAttribName)) { // check if this is a requested column

                        // Copy the value on position i (data stored in the requested column) in the found row
                        // to the row i, column p on the results
                        selectedRow[p] = row[i];
                        p++; // move to the left on the results table
                        break;
                    }
                }
            }
            results.add(selectedRow);
        }

        // Apply the filters of the rows
        results.forEach((rowResult) -> {
            boolean selected = true; // to decide if this row should be included in the result or not
            for (int i = 0; i < rowResult.length; i++) {
                // We assume the attribute names are in the same order of the content
                // Get the selectors of this attribute
                List