001/* 002 * Copyright 2017 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.odfweb.restrictions.rules; 017 018import java.util.Collection; 019import java.util.Collections; 020import java.util.Objects; 021 022import org.apache.commons.lang3.ArrayUtils; 023 024import org.ametys.odf.program.Program; 025import org.ametys.plugins.repository.query.expression.Expression; 026import org.ametys.plugins.repository.query.expression.OrExpression; 027 028/** 029 * Or restriction rule 030 */ 031public class OdfOrRestrictionRule implements OdfRestrictionRule 032{ 033 private final Collection<OdfRestrictionRule> _rules; 034 035 /** 036 * Or restriction rule constructor 037 * @param rules The list of rules on which a "or" operator must be applied 038 */ 039 public OdfOrRestrictionRule(Collection<OdfRestrictionRule> rules) 040 { 041 _rules = rules; 042 _rules.removeAll(Collections.<OdfRestrictionRule>singleton(null)); 043 } 044 045 @Override 046 public boolean contains(Program program) 047 { 048 return _rules.stream().anyMatch(r -> r.contains(program)); 049 } 050 051 @Override 052 public Expression getExpression() 053 { 054 Expression[] exprs = _rules.stream() 055 .map(OdfRestrictionRule::getExpression) 056 .filter(Objects::nonNull) 057 .toArray(Expression[]::new); 058 059 if (ArrayUtils.isEmpty(exprs)) 060 { 061 return null; 062 } 063 064 if (exprs.length == 1) 065 { 066 return exprs[0]; 067 } 068 069 return new OrExpression(exprs); 070 } 071 072 @Override 073 public boolean hasOrgunitRestrictions() 074 { 075 return _rules.stream().anyMatch(OdfRestrictionRule::hasOrgunitRestrictions); 076 } 077}