<commit_before>/* $This file is distributed under the terms of the license in /doc/license.txt$ */

package edu.cornell.mannlib.vitro.webapp.controller.freemarker;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.cornell.mannlib.vedit.beans.LoginStatusBean;
import edu.cornell.mannlib.vedit.util.FormUtils;
import edu.cornell.mannlib.vitro.webapp.auth.policy.PolicyHelper;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.UseAdvancedDataToolsPages;
import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMap;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.dao.jena.pellet.PelletListener;

public class SiteAdminController extends FreemarkerHttpServlet {
	
    private static final long serialVersionUID = 1L;
    private static final Log log = LogFactory.getLog(SiteAdminController.class);
    private static final String TEMPLATE_DEFAULT = "siteAdmin-main.ftl";
    private static final int REQUIRED_LOGIN_LEVEL = LoginStatusBean.EDITOR;
    
    @Override
	public String getTitle(String siteName, VitroRequest vreq) {
        return siteName + " Site Administration";
	}

    /* requiredLoginLevel() must be an instance method, else, due to the way sublcass
     * hiding works, when called from FreemarkerHttpServlet we will get its own method,
     * rather than the subclass method. To figure out whether to display links at the
     * page level, we need another, static method.
     */
    public static int staticRequiredLoginLevel() {
        return REQUIRED_LOGIN_LEVEL;
    }

    @Override
    protected int requiredLoginLevel() {
        return staticRequiredLoginLevel();
    }
    
    @Override
    protected ResponseValues processRequest(VitroRequest vreq) {
        // Note that we don't get here unless logged in at least at editor level, due
        // to requiresLoginLevel().
    	LoginStatusBean loginBean = LoginStatusBean.getBean(vreq);
    	
        Map<String, Object> body = new HashMap<String, Object>();        

        UrlBuilder urlBuilder = new UrlBuilder(vreq.getPortal());
        
        body.put("dataInput", getDataInputData(vreq));

        // rjy7 There is a risk that the login levels required to show the links will get out
        // of step with the levels required by the pages themselves. We should implement a 
        // mechanism similar to what's used on the front end to display links to Site Admin
        // and Revision Info iff the user has access to those pages.
        if (loginBean.isLoggedInAtLeast(LoginStatusBean.CURATOR)) {
            body.put("siteConfig", getSiteConfigurationData(vreq, urlBuilder));
            body.put("ontologyEditor", getOntologyEditorData(vreq, urlBuilder));
            
			if (PolicyHelper.isAuthorized(vreq,	UseAdvancedDataToolsPages.class)) {
                body.put("dataTools", getDataToolsData(vreq, urlBuilder));
                
                // Only for DataStar. Should handle without needing a DataStar-specific version of this controller.
                //body.put("customReports", getCustomReportsData(vreq));
            }
        }
        
        return new TemplateResponseValues(TEMPLATE_DEFAULT, body);
 
    }
    
    private Map<String, Object> getDataInputData(VitroRequest vreq) {
    
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("formAction", UrlBuilder.getUrl("/edit/editRequestDispatch.jsp"));
        
        WebappDaoFactory wadf = vreq.getFullWebappDaoFactory();
        
        // Create map for data input entry form options list
        List classGroups = wadf.getVClassGroupDao().getPublicGroupsWithVClasses(true,true,false); // order by displayRank, include uninstantiated classes, don't get the counts of individuals
        
        //boolean classGroupDisplayAssumptionsMet = checkClassGroupDisplayAssumptions(classGroups);   
        
        Set<String> seenGroupNames = new HashSet<String>();
        
        Iterator classGroupIt = classGroups.iterator();
        LinkedHashMap<String, List> orderedClassGroups = new LinkedHashMap<String, List>(classGroups.size());
        while (classGroupIt.hasNext()) {<commit_msg>NIHVIVO-2492 A better implementation to allow RequiresAuthorizationFor this or that.<commit_after>/* $This file is distributed under the terms of the license in /doc/license.txt$ */

package edu.cornell.mannlib.vitro.webapp.controller.freemarker;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.cornell.mannlib.vedit.beans.LoginStatusBean;
import edu.cornell.mannlib.vedit.util.FormUtils;
import edu.cornell.mannlib.vitro.webapp.auth.policy.PolicyHelper;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.UseAdvancedDataToolsPages;
import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMap;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.dao.jena.pellet.PelletListener;

public class SiteAdminController extends FreemarkerHttpServlet {
	
    private static final long serialVersionUID = 1L;
    private static final Log log = LogFactory.getLog(SiteAdminController.class);
    private static final String TEMPLATE_DEFAULT = "siteAdmin-main.ftl";
    private static final int REQUIRED_LOGIN_LEVEL = LoginStatusBean.EDITOR;
    
    @Override
	public String getTitle(String siteName, VitroRequest vreq) {
        return siteName + " Site Administration";
	}

    /* requiredLoginLevel() must be an instance method, else, due to the way sublcass
     * hiding works, when called from FreemarkerHttpServlet we will get its own method,
     * rather than the subclass method. To figure out whether to display links at the
     * page level, we need another, static method.
     */
    public static int staticRequiredLoginLevel() {
        return REQUIRED_LOGIN_LEVEL;
    }

    @Override
    protected int requiredLoginLevel() {
        return staticRequiredLoginLevel();
    }
    
    @Override
    protected ResponseValues processRequest(VitroRequest vreq) {
        // Note that we don't get here unless logged in at least at editor level, due
        // to requiresLoginLevel().
    	LoginStatusBean loginBean = LoginStatusBean.getBean(vreq);
    	
        Map<String, Object> body = new HashMap<String, Object>();        

        UrlBuilder urlBuilder = new UrlBuilder(vreq.getPortal());
        
        body.put("dataInput", getDataInputData(vreq));

        // rjy7 There is a risk that the login levels required to show the links will get out
        // of step with the levels required by the pages themselves. We should implement a 
        // mechanism similar to what's used on the front end to display links to Site Admin
        // and Revision Info iff the user has access to those pages.
        // jeb228 This could be done with
        //           PolicyHelper.areRequiredAuthorizationsSatisfied(req, SomeServlet.class);
        if (loginBean.isLoggedInAtLeast(LoginStatusBean.CURATOR)) {
            body.put("siteConfig", getSiteConfigurationData(vreq, urlBuilder));
        }
        if (loginBean.isLoggedInAtLeast(LoginStatusBean.CURATOR)) {
        	body.put("ontologyEditor", getOntologyEditorData(vreq, urlBuilder));
        }
		if (PolicyHelper.isActionAuthorized(vreq,	UseAdvancedDataToolsPages.class)) {
            body.put("dataTools", getDataToolsData(vreq, urlBuilder));
            
            // Only for DataStar. Should handle without needing a DataStar-specific version of this controller.
            //body.put("customReports", getCustomReportsData(vreq));
        }
        
        return new TemplateResponseValues(TEMPLATE_DEFAULT, body);
 
    }
    
    private Map<String, Object> getDataInputData(VitroRequest vreq) {
    
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("formAction", UrlBuilder.getUrl("/edit/editRequestDispatch.jsp"));
        
        WebappDaoFactory wadf = vreq.getFullWebappDaoFactory();
        
        // Create map for data input entry form options list
        List classGroups = wadf.getVClassGroupDao().getPublicGroupsWithVClasses(true,true,false); // order by displayRank, include uninstantiated classes, don't get the counts of individuals
        
        //boolean classGroupDisplayAssumptionsMet = checkClassGroupDisplayAssumptions(classGroups);   
        
        Set<String> seenGroupNames = new HashSet<String>();
        
        Iterator classGroupIt = classGroups.iterator();
        LinkedHashMap<String, List> orderedClassGroups = new LinkedHashMap<String, List>(classGroups.size());
        while (classGroupIt.hasNext()) {
{'fraction_non_alphanumeric': 0.08584925035941672, 'fraction_numerical': 0.0011295953994660095, 'mean_word_length': 3.4818223653934655, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '1570555', 'n_tokens_mistral': 2797, 'n_tokens_neox': 2713, 'n_words': 693}