/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.extension.xar.internal.security;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xwiki.security.GroupSecurityReference;
import org.xwiki.security.UserSecurityReference;
import org.xwiki.security.authorization.Right;
import org.xwiki.security.authorization.RuleState;
import org.xwiki.security.authorization.SecurityRule;

/**
 * Xar document oriented implementation of {@link XarSecurityRule}.
 *
 * @version $Id$
 * @since 10.5RC1
 */
public class XarSecurityRule implements SecurityRule
{
    protected static final Logger LOGGER = LoggerFactory.getLogger(XarSecurityRule.class);

    private final Right right;

    private final boolean simple;

    private XarSecurityTool securityTool;

    /**
     * @param right the affected right
     * @param simple true if the rule only apply to simple users
     * @param securityTool is used to get information about the user
     */
    public XarSecurityRule(Right right, boolean simple, XarSecurityTool securityTool)
    {
        this.right = right;
        this.simple = simple;
        this.securityTool = securityTool;
    }

    @Override
    public boolean match(Right right)
    {
        return right == this.right;
    }

    @Override
    public boolean match(GroupSecurityReference group)
    {
        return false;
    }

    @Override
    public boolean match(UserSecurityReference user)
    {
        return !this.simple || this.securityTool.isSimpleUser(user.getOriginalDocumentReference());
    }

    @Override
    public RuleState getState()
    {
        return RuleState.DENY;
    }

    @Override
    public int hashCode()
    {
        return new HashCodeBuilder().append(right).append(simple).toHashCode();
    }

    @Override
    public boolean equals(Object object)
    {
        if (object == this) {
            return true;
        }
        if (!(object instanceof XarSecurityRule)) {
            return false;
        }

        XarSecurityRule rhs = (XarSecurityRule) object;
        return new EqualsBuilder()
                .append(this.right, rhs.right)
                .append(this.simple, rhs.simple)
                .isEquals();
    }
}

@@ -1043,32 +1043,89 @@
 ashCodeBuilder;%0A
+import org.apache.commons.lang3.builder.ToStringBuilder;%0A
 import org.slf4j
@@ -1415,16 +1415,60 @@
 ityRule;
+%0Aimport org.xwiki.text.XWikiToStringBuilder;
 %0A%0A/**%0A *
@@ -3099,24 +3099,25 @@
 object;%0A
+%0A
         
 return n
@@ -3104,30 +3104,47 @@
 t;%0A%0A        
-return
+EqualsBuilder builder =
  new EqualsB
@@ -3151,30 +3151,26 @@
 uilder()
+;
 %0A
-     
         
    .appe
@@ -3153,35 +3153,39 @@
 lder();%0A        
-   
+builder
 .append(this.rig
@@ -3194,24 +3194,25 @@
 , rhs.right)
+;
 %0A           
@@ -3208,24 +3208,23 @@
         
-        
+builder
 .append(
@@ -3247,16 +3247,18 @@
 .simple)
+;%0A
 %0A       
@@ -3262,25 +3262,281 @@
     
-        .isEquals
+return builder.isEquals();%0A    %7D%0A%0A    @Override%0A    public String toString()%0A    %7B%0A        ToStringBuilder builder = new XWikiToStringBuilder(this);%0A        builder.append(%22right%22, this.right);%0A        builder.append(%22simple%22, this.simple);%0A%0A        return builder.toString
 ();%0A

Improve codestyle and add debug information