001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.theme;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    
019    import java.io.Serializable;
020    
021    import java.util.ArrayList;
022    import java.util.List;
023    import java.util.regex.Matcher;
024    import java.util.regex.Pattern;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class ThemeCompanyLimit implements Serializable {
030    
031            public ThemeCompanyLimit() {
032                    _includes = new ArrayList<ThemeCompanyId>();
033                    _excludes = new ArrayList<ThemeCompanyId>();
034            }
035    
036            public List<ThemeCompanyId> getExcludes() {
037                    return _excludes;
038            }
039    
040            public List<ThemeCompanyId> getIncludes() {
041                    return _includes;
042            }
043    
044            public boolean isExcluded(long companyId) {
045                    return _matches(_excludes, companyId);
046            }
047    
048            public boolean isIncluded(long companyId) {
049                    return _matches(_includes, companyId);
050            }
051    
052            public void setExcludes(List<? extends ThemeCompanyId> excludes) {
053                    _excludes = (List<ThemeCompanyId>)excludes;
054            }
055    
056            public void setIncludes(List<? extends ThemeCompanyId> includes) {
057                    _includes = (List<ThemeCompanyId>)includes;
058            }
059    
060            private boolean _matches(
061                    List<ThemeCompanyId> themeCompanyIds, long companyId) {
062    
063                    for (int i = 0; i < themeCompanyIds.size(); i++) {
064                            ThemeCompanyId themeCompanyId = themeCompanyIds.get(i);
065    
066                            String themeCompanyIdValue = themeCompanyId.getValue();
067    
068                            if (themeCompanyId.isPattern()) {
069                                    Pattern pattern = Pattern.compile(themeCompanyIdValue);
070                                    Matcher matcher = pattern.matcher(String.valueOf(companyId));
071    
072                                    if (matcher.matches()) {
073                                            return true;
074                                    }
075                            }
076                            else {
077                                    long themeCompanyIdValueLong = GetterUtil.getLong(
078                                            themeCompanyIdValue);
079    
080                                    if (themeCompanyIdValueLong == companyId) {
081                                            return true;
082                                    }
083                            }
084                    }
085    
086                    return false;
087            }
088    
089            private List<ThemeCompanyId> _excludes;
090            private List<ThemeCompanyId> _includes;
091    
092    }