001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008 Sun Microsystems, Inc.
015 * Portions Copyright 2013-2015 ForgeRock AS.
016 */
017package org.opends.server.authorization.dseecompat;
018
019import static org.opends.messages.AccessControlMessages.*;
020
021import java.util.Calendar;
022import java.util.GregorianCalendar;
023import java.util.LinkedList;
024import java.util.List;
025
026import org.forgerock.i18n.LocalizableMessage;
027
028/**
029 * This class implements the dayofweek bind rule keyword.
030 */
031public class DayOfWeek  implements KeywordBindRule {
032
033    /** List containing the enumeration of the day of the week. */
034    private List<EnumDayOfWeek> days;
035
036    /** Enumeration representing the bind rule operation type. */
037    private EnumBindRuleType type;
038
039    /**
040     * Create a class representing a dayofweek bind rule keyword.
041     * @param days  A list of day of the week enumerations.
042     * @param type An enumeration representing the bind rule type.
043     */
044    private DayOfWeek(List<EnumDayOfWeek> days, EnumBindRuleType type) {
045        this.days=days;
046        this.type=type;
047    }
048
049    /**
050     * Decode an string representing a dayofweek bind rule.
051     * @param expr A string representation of the bind rule.
052     * @param type  An enumeration representing the bind rule type.
053     * @return  A keyword bind rule class that can be used to evaluate
054     * this bind rule.
055     * @throws AciException  If the expression string is invalid.
056     */
057    public static KeywordBindRule decode(String expr, EnumBindRuleType type)
058    throws AciException
059    {
060        List<EnumDayOfWeek> days = new LinkedList<>();
061        String[] dayArray=expr.split(",", -1);
062        for (String element : dayArray)
063        {
064          EnumDayOfWeek day=EnumDayOfWeek.createDayOfWeek(element);
065          if (day == null)
066          {
067              LocalizableMessage message = WARN_ACI_SYNTAX_INVALID_DAYOFWEEK.get(expr);
068              throw new AciException(message);
069          }
070          days.add(day);
071        }
072        return new DayOfWeek(days, type);
073    }
074
075    /**
076     * Performs evaluation of a dayofweek bind rule using the provided
077     * evaluation context.
078     * @param evalCtx  An evaluation context to use in the evaluation.
079     * @return An enumeration evaluation result.
080     */
081    @Override
082    public EnumEvalResult evaluate(AciEvalContext evalCtx) {
083        EnumEvalResult matched=EnumEvalResult.FALSE;
084        GregorianCalendar calendar = new GregorianCalendar();
085        EnumDayOfWeek dayofweek
086            = EnumDayOfWeek.getDayOfWeek(calendar.get(Calendar.DAY_OF_WEEK));
087        if(days.contains(dayofweek))
088        {
089          matched=EnumEvalResult.TRUE;
090        }
091        return matched.getRet(type, false);
092    }
093
094    /** {@inheritDoc} */
095    @Override
096    public String toString()
097    {
098      final StringBuilder sb = new StringBuilder();
099      toString(sb);
100      return sb.toString();
101    }
102
103    /** {@inheritDoc} */
104    @Override
105    public final void toString(StringBuilder buffer)
106    {
107      buffer.append(super.toString());
108    }
109
110}