<commit_before>/**
 * $Id: SiteEntityProvider.java 52667 2008-09-25 16:00:21Z <EMAIL> $
 * $URL: https://source.sakaiproject.org/svn/entitybroker/trunk/impl/src/java/org/sakaiproject/entitybroker/providers/SiteEntityProvider.java $
 * SiteEntityProvider.java - entity-broker - Jun 29, 2008 8:35:55 AM - azeckoski
 **************************************************************************
 * Copyright (c) 2008 Sakai Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.osedu.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.sakaiproject.entitybroker.providers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.azeckoski.reflectutils.ReflectUtils;
import org.sakaiproject.authz.api.RoleAlreadyDefinedException;
import org.sakaiproject.entity.api.ResourcePropertiesEdit;
import org.sakaiproject.entitybroker.EntityReference;
import org.sakaiproject.entitybroker.EntityView;
import org.sakaiproject.entitybroker.EntityView.Method;
import org.sakaiproject.entitybroker.entityprovider.CoreEntityProvider;
import org.sakaiproject.entitybroker.entityprovider.annotations.EntityCustomAction;
import org.sakaiproject.entitybroker.entityprovider.annotations.EntityURLRedirect;
import org.sakaiproject.entitybroker.entityprovider.capabilities.ActionsExecutable;
import org.sakaiproject.entitybroker.entityprovider.capabilities.RESTful;
import org.sakaiproject.entitybroker.entityprovider.capabilities.Redirectable;
import org.sakaiproject.entitybroker.entityprovider.extension.Formats;
import org.sakaiproject.entitybroker.entityprovider.search.Restriction;
import org.sakaiproject.entitybroker.entityprovider.search.Search;
import org.sakaiproject.entitybroker.providers.model.EntitySite;
import org.sakaiproject.entitybroker.util.AbstractEntityProvider;
import org.sakaiproject.entitybroker.util.TemplateParseUtil;
import org.sakaiproject.exception.IdInvalidException;
import org.sakaiproject.exception.IdUnusedException;
import org.sakaiproject.exception.IdUsedException;
import org.sakaiproject.exception.PermissionException;
import org.sakaiproject.javax.PagingPosition;
import org.sakaiproject.site.api.Site;
import org.sakaiproject.site.api.SiteService;
import org.sakaiproject.site.api.SiteService.SelectionType;
import org.sakaiproject.site.api.SiteService.SortType;

/**
 * Creates a provider for dealing with sites
 * 
 * @author <NAME> (<EMAIL>)
 */
public class SiteEntityProvider extends AbstractEntityProvider implements CoreEntityProvider, 
        RESTful, ActionsExecutable, Redirectable {

    private SiteService siteService;
    public void setSiteService(SiteService siteService) {
        this.siteService = siteService;
    }

    private UserEntityProvider userEntityProvider;
    public void setUserEntityProvider(UserEntityProvider userEntityProvider) {
        this.userEntityProvider = userEntityProvider;
    }

    public static String PREFIX = "site";
    public String getEntityPrefix() {
        return PREFIX;
    }

    @EntityURLRedirect("/{prefix}/{id}/memberships")
    public String redirectMemberships(Map<String,String> vars) {
        return MembershipEntityProvider.PREFIX + "/site/" + vars.get("id") + vars.get(TemplateParseUtil.DOT_EXTENSION);
    }

    @EntityCustomAction(action="exists", viewKey=EntityView.VIEW_SHOW)
    public boolean checkSiteExists(EntityView view) {
        String siteId = view.getEntityReference().getId();
        boolean exists = entityExists(siteId);
        return exists;
    }
    
    @EntityCustomAction(action="role", viewKey="")
    public void handleRoles (EntityView view){
    	String siteId = view.getEntityReference().getId();
    	String roleId = view.getPathSegment(3);
    	if (roleId == null){
    		throw new IllegalArgumentException("No role id specified");
    	}
    	Site site = getSiteById(siteId);
    	if (view.getMethod().equals(Method.POST)){
	    	try {
				site.addRole(roleId);
			} catch (RoleAlreadyDefinedException e) {
				// Ignore
			} 
    	} else if (view.getMethod().equals(Method.DELETE)) {
    		site.removeRole(roleId);
    	} else {
    		throw new IllegalArgumentException("Method " + view.getMethod() + " not supported");
    	}
    	try {
			siteService.save(site);
		} catch (IdUnusedException e) {
			// Ignore
		} catch (PermissionException e) {
			throw new SecurityException("User not allowed to update role " + roleId + " in site " + siteId);
		}
    }

    /**
     * @param site the site to check perms in
     * @return true if the current user can view this site
     * @throws SecurityException if not allowed
     */
    protected boolean isAllowedAccessMembers(Site site) {<commit_msg>http://bugs.sakaiproject.org/jira/browse/SAK-13898 Static code review fixes, correctness<commit_after>/**
 * $Id: SiteEntityProvider.java 52667 2008-09-25 16:00:21Z <EMAIL> $
 * $URL: https://source.sakaiproject.org/svn/entitybroker/trunk/impl/src/java/org/sakaiproject/entitybroker/providers/SiteEntityProvider.java $
 * SiteEntityProvider.java - entity-broker - Jun 29, 2008 8:35:55 AM - azeckoski
 **************************************************************************
 * Copyright (c) 2008 Sakai Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.osedu.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.sakaiproject.entitybroker.providers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.azeckoski.reflectutils.ReflectUtils;
import org.sakaiproject.authz.api.RoleAlreadyDefinedException;
import org.sakaiproject.entity.api.ResourcePropertiesEdit;
import org.sakaiproject.entitybroker.EntityReference;
import org.sakaiproject.entitybroker.EntityView;
import org.sakaiproject.entitybroker.EntityView.Method;
import org.sakaiproject.entitybroker.entityprovider.CoreEntityProvider;
import org.sakaiproject.entitybroker.entityprovider.annotations.EntityCustomAction;
import org.sakaiproject.entitybroker.entityprovider.annotations.EntityURLRedirect;
import org.sakaiproject.entitybroker.entityprovider.capabilities.ActionsExecutable;
import org.sakaiproject.entitybroker.entityprovider.capabilities.RESTful;
import org.sakaiproject.entitybroker.entityprovider.capabilities.Redirectable;
import org.sakaiproject.entitybroker.entityprovider.extension.Formats;
import org.sakaiproject.entitybroker.entityprovider.search.Restriction;
import org.sakaiproject.entitybroker.entityprovider.search.Search;
import org.sakaiproject.entitybroker.providers.model.EntitySite;
import org.sakaiproject.entitybroker.util.AbstractEntityProvider;
import org.sakaiproject.entitybroker.util.TemplateParseUtil;
import org.sakaiproject.exception.IdInvalidException;
import org.sakaiproject.exception.IdUnusedException;
import org.sakaiproject.exception.IdUsedException;
import org.sakaiproject.exception.PermissionException;
import org.sakaiproject.javax.PagingPosition;
import org.sakaiproject.site.api.Site;
import org.sakaiproject.site.api.SiteService;
import org.sakaiproject.site.api.SiteService.SelectionType;
import org.sakaiproject.site.api.SiteService.SortType;

/**
 * Creates a provider for dealing with sites
 * 
 * @author <NAME> (<EMAIL>)
 */
public class SiteEntityProvider extends AbstractEntityProvider implements CoreEntityProvider, 
        RESTful, ActionsExecutable, Redirectable {

    private SiteService siteService;
    public void setSiteService(SiteService siteService) {
        this.siteService = siteService;
    }

    private UserEntityProvider userEntityProvider;
    public void setUserEntityProvider(UserEntityProvider userEntityProvider) {
        this.userEntityProvider = userEntityProvider;
    }

    public static String PREFIX = "site";
    public String getEntityPrefix() {
        return PREFIX;
    }

    @EntityURLRedirect("/{prefix}/{id}/memberships")
    public String redirectMemberships(Map<String,String> vars) {
        return MembershipEntityProvider.PREFIX + "/site/" + vars.get("id") + vars.get(TemplateParseUtil.DOT_EXTENSION);
    }

    @EntityCustomAction(action="exists", viewKey=EntityView.VIEW_SHOW)
    public boolean checkSiteExists(EntityView view) {
        String siteId = view.getEntityReference().getId();
        boolean exists = entityExists(siteId);
        return exists;
    }
    
    @EntityCustomAction(action="role", viewKey="")
    public void handleRoles (EntityView view){
    	String siteId = view.getEntityReference().getId();
    	String roleId = view.getPathSegment(3);
    	if (roleId == null){
    		throw new IllegalArgumentException("No role id specified");
    	}
    	Site site = getSiteById(siteId);
    	if (view.getMethod().equals(Method.POST.name())){
	    	try {
				site.addRole(roleId);
			} catch (RoleAlreadyDefinedException e) {
				// Ignore
			} 
    	} else if (view.getMethod().equals(Method.DELETE.name())) {
    		site.removeRole(roleId);
    	} else {
    		throw new IllegalArgumentException("Method " + view.getMethod() + " not supported");
    	}
    	try {
			siteService.save(site);
		} catch (IdUnusedException e) {
			// Ignore
		} catch (PermissionException e) {
			throw new SecurityException("User not allowed to update role " + roleId + " in site " + siteId);
		}
    }

    /**
     * @param site the site to check perms in
     * @return true if the current user can view this site
     * @throws SecurityException if not allowed
     */
    protected boolean isAllowedAccessMembers(Site site) {
{'fraction_non_alphanumeric': 0.10846280034738975, 'fraction_numerical': 0.008009263726720062, 'mean_word_length': 4.596112311015119, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '5017573', 'n_tokens_mistral': 3154, 'n_tokens_neox': 2958, 'n_words': 659}