/*
 * Copyright 2017 ThoughtWorks, Inc.
 *
 * Licensed under the Apache 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.apache.org/licenses/LICENSE-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 com.thoughtworks.go.config;

import java.util.List;

import com.thoughtworks.go.domain.ConfigErrors;
import com.thoughtworks.go.domain.config.Admin;

@ConfigTag("role")
public class AdminRole implements Admin {
    @ConfigValue private CaseInsensitiveString name;
    private ConfigErrors configErrors = new ConfigErrors();
    public static final String NAME = "name";

    public AdminRole() {
    }

    public AdminRole(final CaseInsensitiveString name) {
        this.name = name;
    }

    public AdminRole(final Role role){
        this(role.getName());
    }

    public boolean isSameAs(Admin admin, List<Role> memberRoles) {
        if (this.name == null || memberRoles == null) {
            return false;
        }
        for (Role memberRole : memberRoles) {
            if (name.equals(memberRole.getName())) {
                return true;
            }
        }
        return false;
    }

    public CaseInsensitiveString getName() {
        return name;
    }

    public String describe() {
        return "Role";
    }

    public boolean belongsTo(Role role) {
        return role.getName().equals(name);
    }

    public void addError(String message) {
        errors().add(NAME, message);
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        AdminRole adminRole = (AdminRole) o;

        return name.equals(adminRole.name);
    }

    public int hashCode() {
        return name.hashCode();
    }

    public String toString() {
        return CaseInsensitiveString.str(name);
    }

    public void validate(ValidationContext validationContext) {
        if (validationContext.shouldNotCheckRole()) {
            return;
        }
        SecurityConfig securityConfig = validationContext.getServerSecurityConfig();
        if (!securityConfig.isRoleExist(this.name)) {
            if(validationContext.isWithinPipelines()) {
                configErrors.add(NAME, String.format("Role \"%s\" defined for `%s` does not exist.", this.name, validationContext.getPipeline().getName()));
            }
            else {
                configErrors.add(NAME, String.format("Role \"%s\" does not exist.", this.name));
            }
        }
    }

    public ConfigErrors errors() {
        return configErrors;
    }

    public void addError(String fieldName, String message) {
        configErrors.add(fieldName, message);
    }

}

@@ -2557,258 +2557,8 @@
 ) %7B%0A
-            if(validationContext.isWithinPipelines()) %7B%0A                configErrors.add(NAME, String.format(%22Role %5C%22%25s%5C%22 defined for %60%25s%60 does not exist.%22, this.name, validationContext.getPipeline().getName()));%0A            %7D%0A            else %7B%0A    
     
@@ -2646,30 +2646,16 @@
 name));%0A
-            %7D%0A
         

Revert the change around validation error message based on the validation context as it breaks the pipeline group permissions view.