<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.harvard.iq.dataverse.mydata;

import edu.harvard.iq.dataverse.DvObject;
import edu.harvard.iq.dataverse.DvObjectServiceBean;
import edu.harvard.iq.dataverse.RoleAssigneeServiceBean;
import edu.harvard.iq.dataverse.authorization.DataverseRolePermissionHelper;
import edu.harvard.iq.dataverse.search.SearchFields;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import org.apache.commons.lang.StringUtils;

/**
 * Given a user and a set of filters (dvobject type, roles, publication status):
 *  - Use postgres to identify DvObject types
 *  - Format a solr query string
 * 
 * @author rmp553
 */
//@Stateless
public class MyDataFinder {
        
    private static final Logger logger = Logger.getLogger(MyDataFinder.class.getCanonicalName());

    private String userIdentifier;
    MyDataFilterParams filterParams;
    
    //private String searchTerm = "*";
    
    // --------------------
    private DataverseRolePermissionHelper rolePermissionHelper;
    private RoleAssigneeServiceBean roleAssigneeService;
    private DvObjectServiceBean dvObjectServiceBean;
    //private RoleAssigneeServiceBean roleService = new RoleAssigneeServiceBean();
    //private MyDataQueryHelperServiceBean myDataQueryHelperService;
    // --------------------
    public boolean errorFound = false;
    public String errorMessage = null;
    // --------------------

    // Populated in initial query.  DvObject ids -- regardless of Dtype,
    // are sorted into respective buckets in regard to permissions.
    // The same id may appear in multiple lists--and more than once
    //
    // ----------------------------
    // POPULATED IN STEP 1 (1st query)
    // ----------------------------
    public Map<Long, Long> childToParentIds = new HashMap();
    public Map<Long, Boolean> idsWithDataversePermissions = new HashMap<>();  // { role id : true }
    public Map<Long, Boolean> idsWithDatasetPermissions = new HashMap<>();  // { role id : true }
    public Map<Long, Boolean> idsWithFilePermissions = new HashMap<>();  // { role id : true }

    private List<Long> directDvObjectIds = new ArrayList<Long>();

    // Lists later used to format Solr Queries
    // 
    // ----------------------------
    // POPULATED IN STEP 2 (2nd query)
    // ----------------------------
    private List<Long> directDataverseIds = new ArrayList<>();
    private List<Long> directDatasetIds = new ArrayList<>();
    private List<Long> directFileIds = new ArrayList<>();
    
    private List<Long> datasetParentIds = new ArrayList<>(); // dataverse has dataset permissions

    private List<Long> fileParentIds = new ArrayList<>();   // dataset has file permissions      
    private List<Long> fileGrandparentFileIds = new ArrayList<>();  // dataverse has file permissions

    
    public MyDataFinder(DataverseRolePermissionHelper rolePermissionHelper, RoleAssigneeServiceBean roleAssigneeService, DvObjectServiceBean dvObjectServiceBean) {
        this.msgt("MyDataFinder, constructor");
        this.rolePermissionHelper = rolePermissionHelper;
        this.roleAssigneeService = roleAssigneeService;
        this.dvObjectServiceBean = dvObjectServiceBean;
    }

   public void initFields(){
        // ----------------------------
        // POPULATED IN STEP 1 (1st query)
        // ----------------------------
        this.childToParentIds = new HashMap();
        this.idsWithDataversePermissions = new HashMap<>();  // { role id : true }
        this.idsWithDatasetPermissions = new HashMap<>();  // { role id : true }
        this.idsWithFilePermissions = new HashMap<>();  // { role id : true }

        this.directDvObjectIds = new ArrayList<Long>();

        // Lists later used to format Solr Queries
        // 
        // ----------------------------
        // POPULATED IN STEP 2 (2nd query)
        // ----------------------------
        this.directDataverseIds = new ArrayList<>();
        this.directDatasetIds = new ArrayList<>();
        this.directFileIds = new ArrayList<>();

        this.datasetParentIds = new ArrayList<>(); // dataverse has dataset permissions

        this.fileParentIds = new ArrayList<>();   // dataset has file permissions      
        this.fileGrandparentFileIds = new ArrayList<>();  // dataverse has file permissions
    
    }
   
    /*
    private ArrayList<Long> dataverseIds;
    private ArrayList<Long> primaryDatasetIds;
    private ArrayList<Long> primaryFileIds;
    private ArrayList<Long> parentIds;
    */
    
    /*public void runFindDataSteps(String userIdentifier){<commit_msg>Allow access to DataverseRolePermissionHelper.<commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.harvard.iq.dataverse.mydata;

import edu.harvard.iq.dataverse.DvObject;
import edu.harvard.iq.dataverse.DvObjectServiceBean;
import edu.harvard.iq.dataverse.RoleAssigneeServiceBean;
import edu.harvard.iq.dataverse.authorization.DataverseRolePermissionHelper;
import edu.harvard.iq.dataverse.search.SearchFields;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import org.apache.commons.lang.StringUtils;

/**
 * Given a user and a set of filters (dvobject type, roles, publication status):
 *  - Use postgres to identify DvObject types
 *  - Format a solr query string
 * 
 * @author rmp553
 */
//@Stateless
public class MyDataFinder {
        
    private static final Logger logger = Logger.getLogger(MyDataFinder.class.getCanonicalName());

    private String userIdentifier;
    MyDataFilterParams filterParams;
    
    //private String searchTerm = "*";
    
    // --------------------
    private DataverseRolePermissionHelper rolePermissionHelper;
    private RoleAssigneeServiceBean roleAssigneeService;
    private DvObjectServiceBean dvObjectServiceBean;
    //private RoleAssigneeServiceBean roleService = new RoleAssigneeServiceBean();
    //private MyDataQueryHelperServiceBean myDataQueryHelperService;
    // --------------------
    public boolean errorFound = false;
    public String errorMessage = null;
    // --------------------

    // Populated in initial query.  DvObject ids -- regardless of Dtype,
    // are sorted into respective buckets in regard to permissions.
    // The same id may appear in multiple lists--and more than once
    //
    // ----------------------------
    // POPULATED IN STEP 1 (1st query)
    // ----------------------------
    public Map<Long, Long> childToParentIds = new HashMap();
    public Map<Long, Boolean> idsWithDataversePermissions = new HashMap<>();  // { role id : true }
    public Map<Long, Boolean> idsWithDatasetPermissions = new HashMap<>();  // { role id : true }
    public Map<Long, Boolean> idsWithFilePermissions = new HashMap<>();  // { role id : true }

    private List<Long> directDvObjectIds = new ArrayList<Long>();

    // Lists later used to format Solr Queries
    // 
    // ----------------------------
    // POPULATED IN STEP 2 (2nd query)
    // ----------------------------
    private List<Long> directDataverseIds = new ArrayList<>();
    private List<Long> directDatasetIds = new ArrayList<>();
    private List<Long> directFileIds = new ArrayList<>();
    
    private List<Long> datasetParentIds = new ArrayList<>(); // dataverse has dataset permissions

    private List<Long> fileParentIds = new ArrayList<>();   // dataset has file permissions      
    private List<Long> fileGrandparentFileIds = new ArrayList<>();  // dataverse has file permissions

    
    public MyDataFinder(DataverseRolePermissionHelper rolePermissionHelper, RoleAssigneeServiceBean roleAssigneeService, DvObjectServiceBean dvObjectServiceBean) {
        this.msgt("MyDataFinder, constructor");
        this.rolePermissionHelper = rolePermissionHelper;
        this.roleAssigneeService = roleAssigneeService;
        this.dvObjectServiceBean = dvObjectServiceBean;
    }

   public void initFields(){
        // ----------------------------
        // POPULATED IN STEP 1 (1st query)
        // ----------------------------
        this.childToParentIds = new HashMap();
        this.idsWithDataversePermissions = new HashMap<>();  // { role id : true }
        this.idsWithDatasetPermissions = new HashMap<>();  // { role id : true }
        this.idsWithFilePermissions = new HashMap<>();  // { role id : true }

        this.directDvObjectIds = new ArrayList<Long>();

        // Lists later used to format Solr Queries
        // 
        // ----------------------------
        // POPULATED IN STEP 2 (2nd query)
        // ----------------------------
        this.directDataverseIds = new ArrayList<>();
        this.directDatasetIds = new ArrayList<>();
        this.directFileIds = new ArrayList<>();

        this.datasetParentIds = new ArrayList<>(); // dataverse has dataset permissions

        this.fileParentIds = new ArrayList<>();   // dataset has file permissions      
        this.fileGrandparentFileIds = new ArrayList<>();  // dataverse has file permissions
    
    }
   
    public DataverseRolePermissionHelper getRolePermissionHelper(){
        return this.rolePermissionHelper;
    }
    /*
    private ArrayList<Long> dataverseIds;
    private ArrayList<Long> primaryDatasetIds;
    private ArrayList<Long> primaryFileIds;
    private ArrayList<Long> parentIds;
    */
    
    /*public void runFindDataSteps(String userIdentifier){
{'fraction_non_alphanumeric': 0.14687530949787064, 'fraction_numerical': 0.0021788650094087354, 'mean_word_length': 2.573248407643312, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 73, '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': '4043582', 'n_tokens_mistral': 2581, 'n_tokens_neox': 2540, 'n_words': 737}