<commit_before>package org.axway.grapes.server.core;

import org.apache.commons.jcs.JCS;
import org.apache.commons.jcs.access.CacheAccess;
import org.apache.commons.jcs.access.exception.CacheException;
import org.axway.grapes.commons.datamodel.*;
import org.axway.grapes.server.core.cache.CacheName;
import org.axway.grapes.server.core.options.FiltersHolder;
import org.axway.grapes.server.core.options.filters.CorporateFilter;
import org.axway.grapes.server.core.options.filters.PromotedFilter;
import org.axway.grapes.server.db.DataUtils;
import org.axway.grapes.server.db.ModelMapper;
import org.axway.grapes.server.db.RepositoryHandler;
import org.axway.grapes.server.db.datamodel.*;
import org.axway.grapes.server.webapp.views.PromotionReportView;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Module Handler
 * <p>
 * <p>Manages all operation regarding Modules. It can, get/update Modules of the database.</p>
 *
 * @author jdcoffre
 */
public class ModuleHandler {

    private static final Logger LOG = LoggerFactory.getLogger(ModuleHandler.class);

    private final RepositoryHandler repositoryHandler;

    private CacheAccess<String, PromotionReportView> cache = null;


    public ModuleHandler(final RepositoryHandler repositoryHandler) {
        this.repositoryHandler = repositoryHandler;
        cache = CacheUtils.initCache(CacheName.PROMOTION_REPORTS, PromotionReportView.class);
    }

    /**
     * Add/update module in the database
     *
     * @param dbModule DbModule
     */
    public void store(final DbModule dbModule) {
        repositoryHandler.store(dbModule);
    }

    /**
     * Returns the available module names regarding the filters
     *
     * @param filters FiltersHolder
     * @return List<String>
     */
    public List<String> getModuleNames(final FiltersHolder filters) {
        return repositoryHandler.getModuleNames(filters);
    }

    /**
     * Returns the available module names regarding the filters
     *
     * @param name    String
     * @param filters FiltersHolder
     * @return List<String>
     */
    public List<String> getModuleVersions(final String name, final FiltersHolder filters) {
        final List<String> versions = repositoryHandler.getModuleVersions(name, filters);

        if (versions.isEmpty()) {
            throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)
                    .entity("Module " + name + " does not exist.").build());
        }

        return versions;
    }

    /**
     * Returns a module
     *
     * @param moduleId String
     * @return DbModule
     */
    public DbModule getModule(final String moduleId) {
        final DbModule dbModule = repositoryHandler.getModule(moduleId);

        if (dbModule == null) {
            throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)
                    .entity("Module " + moduleId + " does not exist.").build());
        }

        return dbModule;
    }

    /**
     * Delete a module
     *
     * @param moduleId String
     */
    public void deleteModule(final String moduleId) {
        final DbModule module = getModule(moduleId);
        repositoryHandler.deleteModule(module.getId());

        for (final String gavc : DataUtils.getAllArtifacts(module)) {
            repositoryHandler.deleteArtifact(gavc);
        }
    }

    /**
     * Return a licenses view of the targeted module
     *
     * @param moduleId String
     * @return List<DbLicense>
     */
    public List<DbLicense> getModuleLicenses(final String moduleId) {
        final DbModule module = getModule(moduleId);

        final List<DbLicense> licenses = new ArrayList<DbLicense>();
        final FiltersHolder filters = new FiltersHolder();
        final ArtifactHandler artifactHandler = new ArtifactHandler(repositoryHandler);

        for (final String gavc : DataUtils.getAllArtifacts(module)) {
            licenses.addAll(artifactHandler.getArtifactLicenses(gavc, filters));
        }

        return licenses;
    }

    /**
     * Perform the module promotion
     *
     * @param moduleId String
     */
    public void promoteModule(final String moduleId) {
        final DbModule module = getModule(moduleId);

        for (final String gavc : DataUtils.getAllArtifacts(module)) {
            final DbArtifact artifact = repositoryHandler.getArtifact(gavc);
            artifact.setPromoted(true);
            repositoryHandler.store(artifact);
        }

        repositoryHandler.promoteModule(module);
    }

    /**
     * Provide a report about the promotion feasibility
     *
     * @param moduleId String
     * @return PromotionReportView
     */
    public PromotionReportView getPromotionReport(final String moduleId) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format(":: Starting promo report %s", moduleId));
        }

        final PromotionReportView fromCache = cache.get(moduleId);
        if (null != fromCache) {
            return fromCache;
        }

        final DependencyHandler depHandler = new DependencyHandler(repositoryHandler);
        final ModelMapper modelMapper = new ModelMapper(repositoryHandler);
        final DbModule module = getModule(moduleId);
        final DbOrganization organization = getOrganization(module);


        final PromotionReportView report = new PromotionReportView();
        report.setRootModule(DataModelFactory.createModule(module.getName(), module.getVersion()));

        if (!report.isSnapshot()) {
            // filters initialization
            final FiltersHolder filters = new FiltersHolder();
            filters.addFilter(new PromotedFilter(false));
            filters.addFilter(new CorporateFilter(organization));

            // Checks if each dependency module has been promoted
            final List<Dependency> deps = depHandler.getModuleDependencies(moduleId, filters);
            removeDuplicates(deps);
            // printDeps(moduleId, deps);

            for (final Dependency dependency : deps) {
                final DbModule depModule = repositoryHandler.getRootModuleOf(dependency.getTarget().getGavc());
                if (depModule != null && !depModule.getId().equals(moduleId) && !depModule.isPromoted()) {
                    report.addUnPromotedDependency(depModule.getId());
                    report.addDependencyPromotionReport(depModule.getId(), getPromotionReport(depModule.getId()));
                }
            }

            // Checks if the module has dependencies that shouldn't be used
            final List<String> treatedArtifacts = new ArrayList<String>();
            for (final DbDependency dependency : DataUtils.getAllDbDependencies(module)) {
                final DbArtifact artifactDep = repositoryHandler.getArtifact(dependency.getTarget());

                if (artifactDep == null) {
                    // handle the case of a corporate artifact which is not available in the repository
                    continue;
                }
                if (artifactDep.getDoNotUse() && !treatedArtifacts.contains(artifactDep.getGavc())) {
                    report.addDoNotUseArtifact(modelMapper.getArtifact(artifactDep));
                    treatedArtifacts.add(artifactDep.getGavc());
                }

                // Checks if the module has third party dependency license missing
                // filter the corporate dependencies and check the third party
                List<String> artifactLicenses = artifactDep.getLicenses();
                if (!filters.getCorporateFilter().filter(dependency)) {
                    if (artifactLicenses.isEmpty()) {
                        report.addMissingThirdPartyDependencyLicenses(modelMapper.getArtifact(artifactDep));
                    } else {
                        // Check if the existing license name exists in the database
                        for (String licenseName : artifactLicenses) {
                            DbLicense currentLicense = repositoryHandler.getLicense(licenseName);
                            if (currentLicense == null) {
                                report.addMissingThirdPartyDependencyLicenses(modelMapper.getArtifact(artifactDep));
                            } else if (currentLicense.isApproved() != null && !currentLicense.isApproved()) { // Check if the third party license is approved. If approved == null it is still valid license<commit_msg>Eliminating the test dependencies from the license restrictions<commit_after>package org.axway.grapes.server.core;

import org.apache.commons.jcs.JCS;
import org.apache.commons.jcs.access.CacheAccess;
import org.apache.commons.jcs.access.exception.CacheException;
import org.axway.grapes.commons.datamodel.*;
import org.axway.grapes.server.core.cache.CacheName;
import org.axway.grapes.server.core.options.FiltersHolder;
import org.axway.grapes.server.core.options.filters.CorporateFilter;
import org.axway.grapes.server.core.options.filters.PromotedFilter;
import org.axway.grapes.server.db.DataUtils;
import org.axway.grapes.server.db.ModelMapper;
import org.axway.grapes.server.db.RepositoryHandler;
import org.axway.grapes.server.db.datamodel.*;
import org.axway.grapes.server.webapp.views.PromotionReportView;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Module Handler
 * <p>
 * <p>Manages all operation regarding Modules. It can, get/update Modules of the database.</p>
 *
 * @author jdcoffre
 */
public class ModuleHandler {

    private static final Logger LOG = LoggerFactory.getLogger(ModuleHandler.class);

    private final RepositoryHandler repositoryHandler;

    private CacheAccess<String, PromotionReportView> cache = null;


    public ModuleHandler(final RepositoryHandler repositoryHandler) {
        this.repositoryHandler = repositoryHandler;
        cache = CacheUtils.initCache(CacheName.PROMOTION_REPORTS, PromotionReportView.class);
    }

    /**
     * Add/update module in the database
     *
     * @param dbModule DbModule
     */
    public void store(final DbModule dbModule) {
        repositoryHandler.store(dbModule);
    }

    /**
     * Returns the available module names regarding the filters
     *
     * @param filters FiltersHolder
     * @return List<String>
     */
    public List<String> getModuleNames(final FiltersHolder filters) {
        return repositoryHandler.getModuleNames(filters);
    }

    /**
     * Returns the available module names regarding the filters
     *
     * @param name    String
     * @param filters FiltersHolder
     * @return List<String>
     */
    public List<String> getModuleVersions(final String name, final FiltersHolder filters) {
        final List<String> versions = repositoryHandler.getModuleVersions(name, filters);

        if (versions.isEmpty()) {
            throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)
                    .entity("Module " + name + " does not exist.").build());
        }

        return versions;
    }

    /**
     * Returns a module
     *
     * @param moduleId String
     * @return DbModule
     */
    public DbModule getModule(final String moduleId) {
        final DbModule dbModule = repositoryHandler.getModule(moduleId);

        if (dbModule == null) {
            throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)
                    .entity("Module " + moduleId + " does not exist.").build());
        }

        return dbModule;
    }

    /**
     * Delete a module
     *
     * @param moduleId String
     */
    public void deleteModule(final String moduleId) {
        final DbModule module = getModule(moduleId);
        repositoryHandler.deleteModule(module.getId());

        for (final String gavc : DataUtils.getAllArtifacts(module)) {
            repositoryHandler.deleteArtifact(gavc);
        }
    }

    /**
     * Return a licenses view of the targeted module
     *
     * @param moduleId String
     * @return List<DbLicense>
     */
    public List<DbLicense> getModuleLicenses(final String moduleId) {
        final DbModule module = getModule(moduleId);

        final List<DbLicense> licenses = new ArrayList<DbLicense>();
        final FiltersHolder filters = new FiltersHolder();
        final ArtifactHandler artifactHandler = new ArtifactHandler(repositoryHandler);

        for (final String gavc : DataUtils.getAllArtifacts(module)) {
            licenses.addAll(artifactHandler.getArtifactLicenses(gavc, filters));
        }

        return licenses;
    }

    /**
     * Perform the module promotion
     *
     * @param moduleId String
     */
    public void promoteModule(final String moduleId) {
        final DbModule module = getModule(moduleId);

        for (final String gavc : DataUtils.getAllArtifacts(module)) {
            final DbArtifact artifact = repositoryHandler.getArtifact(gavc);
            artifact.setPromoted(true);
            repositoryHandler.store(artifact);
        }

        repositoryHandler.promoteModule(module);
    }

    /**
     * Provide a report about the promotion feasibility
     *
     * @param moduleId String
     * @return PromotionReportView
     */
    public PromotionReportView getPromotionReport(final String moduleId) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format(":: Starting promo report [%s]", moduleId));
        }

        final PromotionReportView fromCache = cache.get(moduleId);
        if (null != fromCache) {
            return fromCache;
        }

        final DependencyHandler depHandler = new DependencyHandler(repositoryHandler);
        final ModelMapper modelMapper = new ModelMapper(repositoryHandler);
        final DbModule module = getModule(moduleId);
        final DbOrganization organization = getOrganization(module);


        final PromotionReportView report = new PromotionReportView();
        report.setRootModule(DataModelFactory.createModule(module.getName(), module.getVersion()));

        if (!report.isSnapshot()) {
            // filters initialization
            final FiltersHolder filters = new FiltersHolder();
            filters.addFilter(new PromotedFilter(false));
            filters.addFilter(new CorporateFilter(organization));

            // Checks if each dependency module has been promoted
            final List<Dependency> deps = depHandler.getModuleDependencies(moduleId, filters);
            removeDuplicates(deps);
            // printDeps(moduleId, deps);

            for (final Dependency dependency : deps) {
                final DbModule depModule = repositoryHandler.getRootModuleOf(dependency.getTarget().getGavc());
                if (depModule != null && !depModule.getId().equals(moduleId) && !depModule.isPromoted()) {
                    report.addUnPromotedDependency(depModule.getId());
                    report.addDependencyPromotionReport(depModule.getId(), getPromotionReport(depModule.getId()));
                }
            }

            // Checks if the module has dependencies that shouldn't be used
            final List<String> treatedArtifacts = new ArrayList<String>();
            for (final DbDependency dependency : DataUtils.getAllDbDependencies(module)) {
                final DbArtifact artifactDep = repositoryHandler.getArtifact(dependency.getTarget());

                if (artifactDep == null) {
                    // handle the case of a corporate artifact which is not available in the repository
                    continue;
                }

                if(dependency.getScope().equals(Scope.TEST)) {
                    continue;
                }

                if (artifactDep.getDoNotUse() && !treatedArtifacts.contains(artifactDep.getGavc())) {
                    report.addDoNotUseArtifact(modelMapper.getArtifact(artifactDep));
                    treatedArtifacts.add(artifactDep.getGavc());
                }

                // Checks if the module has third party dependency license missing
                // filter the corporate dependencies and check the third party
                List<String> artifactLicenses = artifactDep.getLicenses();
                if (!filters.getCorporateFilter().filter(dependency)) {
                    if (artifactLicenses.isEmpty()) {
                        report.addMissingThirdPartyDependencyLicenses(modelMapper.getArtifact(artifactDep));
                    } else {
                        // Check if the existing license name exists in the database
                        for (String licenseName : artifactLicenses) {
                            DbLicense currentLicense = repositoryHandler.getLicense(licenseName);
                            if (currentLicense == null) {
                                report.addMissingThirdPartyDependencyLicenses(modelMapper.getArtifact(artifactDep));
                            } else if (currentLicense.isApproved() != null && !currentLicense.isApproved()) { // Check if the third party license is approved. If approved == null it is still valid license
{'fraction_non_alphanumeric': 0.09041843189600139, 'fraction_numerical': 0.0002321397481283733, 'mean_word_length': 2.5654872749844815, 'pattern_counts': {'":': 2, '<': 37, '<?xml version=': 0, '>': 37, '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': '5218126', 'n_tokens_mistral': 4512, 'n_tokens_neox': 4295, 'n_words': 1018}