001    /*
002     * 
003     * $Revision$ $Date$
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.access.mcrimpl;
025    
026    import org.jdom.Attribute;
027    import org.jdom.Element;
028    import org.mycore.parsers.bool.MCRCondition;
029    import org.mycore.parsers.bool.MCRConditionVisitor;
030    import org.mycore.user.MCRGroup;
031    import org.mycore.user.MCRUserFacade;
032    
033    /**
034     * Implementation of a (group xy) clause
035     * 
036     * @author Matthias Kramm
037     * @author Mathias Fricke
038     */
039    class MCRGroupClause implements MCRCondition {
040        private MCRGroup group;
041    
042        private String groupname;
043        private boolean not;
044    
045        MCRGroupClause(String group, boolean not) {
046            this.groupname = group;
047            this.group = new MCRGroup(group);
048            this.not = not;
049        }
050    
051        public boolean evaluate(Object o) {
052            MCRAccessData data = (MCRAccessData) o;
053            if (data.getUser() != null) {
054                return data.getUser().isMemberOf(group) ^ this.not;
055            }
056            return MCRUserFacade.isUserInGroup(group.getID()) ^ this.not;
057        }
058    
059        public String toString() {
060            return "group"+ (this.not?" != ":" = ") + groupname + " ";
061        }
062    
063        public Element toXML() {
064            Element cond = new Element("condition");
065            cond.setAttribute("field", "group");
066            cond.setAttribute("operator", (this.not? "!=": "="));
067            cond.setAttribute("value", groupname);
068            return cond;
069        }
070    
071        public Element info() {
072            Element el = new Element("info");
073            el.setAttribute(new Attribute("type", "GROUP"));
074            return el;
075        }
076    
077        public void accept(MCRConditionVisitor visitor) { 
078            visitor.visitType(info());
079        }
080    };