1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.theme;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  
27  import java.io.Serializable;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  import java.util.regex.Matcher;
32  import java.util.regex.Pattern;
33  
34  /**
35   * <a href="ThemeCompanyLimit.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class ThemeCompanyLimit implements Serializable {
41  
42      public ThemeCompanyLimit() {
43          _includes = new ArrayList<ThemeCompanyId>();
44          _excludes = new ArrayList<ThemeCompanyId>();
45      }
46  
47      public List<ThemeCompanyId> getIncludes() {
48          return _includes;
49      }
50  
51      public void setIncludes(List<? extends ThemeCompanyId> includes) {
52          _includes = (List<ThemeCompanyId>)includes;
53      }
54  
55      public boolean isIncluded(long companyId) {
56          return _matches(_includes, companyId);
57      }
58  
59      public List<ThemeCompanyId> getExcludes() {
60          return _excludes;
61      }
62  
63      public void setExcludes(List<? extends ThemeCompanyId> excludes) {
64          _excludes = (List<ThemeCompanyId>)excludes;
65      }
66  
67      public boolean isExcluded(long companyId) {
68          return _matches(_excludes, companyId);
69      }
70  
71      private boolean _matches(
72          List<ThemeCompanyId> themeCompanyIds, long companyId) {
73  
74          for (int i = 0; i < themeCompanyIds.size(); i++) {
75              ThemeCompanyId themeCompanyId = themeCompanyIds.get(i);
76  
77              String themeCompanyIdValue = themeCompanyId.getValue();
78  
79              if (themeCompanyId.isPattern()) {
80                  Pattern pattern = Pattern.compile(themeCompanyIdValue);
81                  Matcher matcher = pattern.matcher(String.valueOf(companyId));
82  
83                  if (matcher.matches()) {
84                      return true;
85                  }
86              }
87              else {
88                  long themeCompanyIdValueLong = GetterUtil.getLong(
89                      themeCompanyIdValue);
90  
91                  if (themeCompanyIdValueLong == companyId) {
92                      return true;
93                  }
94              }
95          }
96  
97          return false;
98      }
99  
100     private List<ThemeCompanyId> _includes;
101     private List<ThemeCompanyId> _excludes;
102 
103 }