1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.theme;
21  
22  import com.liferay.portal.kernel.util.GetterUtil;
23  
24  import java.io.Serializable;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.regex.Matcher;
29  import java.util.regex.Pattern;
30  
31  /**
32   * <a href="ThemeCompanyLimit.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   *
36   */
37  public class ThemeCompanyLimit implements Serializable {
38  
39      public ThemeCompanyLimit() {
40          _includes = new ArrayList<ThemeCompanyId>();
41          _excludes = new ArrayList<ThemeCompanyId>();
42      }
43  
44      public List<ThemeCompanyId> getIncludes() {
45          return _includes;
46      }
47  
48      public void setIncludes(List<? extends ThemeCompanyId> includes) {
49          _includes = (List<ThemeCompanyId>)includes;
50      }
51  
52      public boolean isIncluded(long companyId) {
53          return _matches(_includes, companyId);
54      }
55  
56      public List<ThemeCompanyId> getExcludes() {
57          return _excludes;
58      }
59  
60      public void setExcludes(List<? extends ThemeCompanyId> excludes) {
61          _excludes = (List<ThemeCompanyId>)excludes;
62      }
63  
64      public boolean isExcluded(long companyId) {
65          return _matches(_excludes, companyId);
66      }
67  
68      private boolean _matches(
69          List<ThemeCompanyId> themeCompanyIds, long companyId) {
70  
71          for (int i = 0; i < themeCompanyIds.size(); i++) {
72              ThemeCompanyId themeCompanyId = themeCompanyIds.get(i);
73  
74              String themeCompanyIdValue = themeCompanyId.getValue();
75  
76              if (themeCompanyId.isPattern()) {
77                  Pattern pattern = Pattern.compile(themeCompanyIdValue);
78                  Matcher matcher = pattern.matcher(String.valueOf(companyId));
79  
80                  if (matcher.matches()) {
81                      return true;
82                  }
83              }
84              else {
85                  long themeCompanyIdValueLong = GetterUtil.getLong(
86                      themeCompanyIdValue);
87  
88                  if (themeCompanyIdValueLong == companyId) {
89                      return true;
90                  }
91              }
92          }
93  
94          return false;
95      }
96  
97      private List<ThemeCompanyId> _includes;
98      private List<ThemeCompanyId> _excludes;
99  
100 }