1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class ThemeCompanyLimit implements Serializable {
40  
41      public ThemeCompanyLimit() {
42          _includes = new ArrayList<ThemeCompanyId>();
43          _excludes = new ArrayList<ThemeCompanyId>();
44      }
45  
46      public List<ThemeCompanyId> getIncludes() {
47          return _includes;
48      }
49  
50      public void setIncludes(List<? extends ThemeCompanyId> includes) {
51          _includes = (List<ThemeCompanyId>)includes;
52      }
53  
54      public boolean isIncluded(long companyId) {
55          return _matches(_includes, companyId);
56      }
57  
58      public List<ThemeCompanyId> getExcludes() {
59          return _excludes;
60      }
61  
62      public void setExcludes(List<? extends ThemeCompanyId> excludes) {
63          _excludes = (List<ThemeCompanyId>)excludes;
64      }
65  
66      public boolean isExcluded(long companyId) {
67          return _matches(_excludes, companyId);
68      }
69  
70      private boolean _matches(
71          List<ThemeCompanyId> themeCompanyIds, long companyId) {
72  
73          for (int i = 0; i < themeCompanyIds.size(); i++) {
74              ThemeCompanyId themeCompanyId = themeCompanyIds.get(i);
75  
76              String themeCompanyIdValue = themeCompanyId.getValue();
77  
78              if (themeCompanyId.isPattern()) {
79                  Pattern pattern = Pattern.compile(themeCompanyIdValue);
80                  Matcher matcher = pattern.matcher(String.valueOf(companyId));
81  
82                  if (matcher.matches()) {
83                      return true;
84                  }
85              }
86              else {
87                  long themeCompanyIdValueLong = GetterUtil.getLong(
88                      themeCompanyIdValue);
89  
90                  if (themeCompanyIdValueLong == companyId) {
91                      return true;
92                  }
93              }
94          }
95  
96          return false;
97      }
98  
99      private List<ThemeCompanyId> _includes;
100     private List<ThemeCompanyId> _excludes;
101 
102 }