View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.extensions.validator.core.storage;
20  
21  import org.apache.myfaces.extensions.validator.util.GroupUtils;
22  import org.apache.myfaces.extensions.validator.internal.UsageInformation;
23  import static org.apache.myfaces.extensions.validator.internal.UsageCategory.INTERNAL;
24  
25  import java.util.List;
26  import java.util.Map;
27  import java.util.HashMap;
28  import java.util.ArrayList;
29  import java.util.logging.Logger;
30  
31  /**
32   * default storage implementation for groups
33   *
34   * @author Gerhard Petracek
35   * @since x.x.3
36   */
37  @UsageInformation(INTERNAL)
38  public class DefaultGroupStorage implements GroupStorage
39  {
40      protected final Logger logger = Logger.getLogger(getClass().getName());
41  
42      private Map<String, List<Class>> addedGroups = new HashMap<String, List<Class>>();
43  
44      private Map<String, List<Class>> restrictedGroups = new HashMap<String, List<Class>>();
45  
46      public void addGroup(Class groupClass, String viewId, String clientId)
47      {
48          addGroupToGroupStorage(groupClass, viewId, clientId, this.addedGroups);
49      }
50  
51      public void restrictGroup(Class groupClass, String viewId, String clientId)
52      {
53          addGroupToGroupStorage(groupClass, viewId, clientId, this.restrictedGroups);
54      }
55  
56      public Class[] getGroups(String viewId, String clientId)
57      {
58          if(this.addedGroups.size() < 1)
59          {
60              return null;
61          }
62  
63          //add found groups
64          String key = GroupUtils.getGroupKey(viewId, null);
65          List<Class> resultListForPage = buildGroupList(key, this.addedGroups);
66  
67          key = GroupUtils.getGroupKey(viewId, clientId);
68          List<Class> resultListForComponent = buildGroupList(key, this.addedGroups);
69  
70          //remove restricted groups
71          Class[] resultsForPage =
72                  filterGroupList(GroupUtils.getGroupKey(viewId, null), resultListForPage);
73          Class[] resultsForComponent =
74                  filterGroupList(GroupUtils.getGroupKey(viewId, clientId), resultListForComponent);
75  
76          if(resultsForPage.length == 0)
77          {
78              if(resultsForComponent.length == 0)
79              {
80                  this.logger.fine("no groups for group-validation available." +
81                          "maybe you restricted all groups or you aren't using groups." +
82                          "bean validation will use the default group for validation");
83              }
84              return resultsForComponent;
85          }
86          else if(resultsForComponent.length == 0)
87          {
88              return resultsForPage;
89          }
90  
91          return mergeResults(resultsForPage, resultsForComponent);
92      }
93  
94      private void addGroupToGroupStorage(Class groupClass, String viewId, String clientId,
95                                          Map<String, List<Class>> groupStorage)
96      {
97          List<Class> groupList = groupStorage.get(GroupUtils.getGroupKey(viewId, clientId));
98  
99          if(groupList == null)
100         {
101             groupList = new ArrayList<Class>();
102             groupStorage.put(GroupUtils.getGroupKey(viewId, clientId), groupList);
103         }
104 
105         if(!groupList.contains(groupClass))
106         {
107             groupList.add(groupClass);
108         }
109     }
110 
111     private List<Class> buildGroupList(String key, Map<String, List<Class>> groupStorage)
112     {
113         List<Class> list = groupStorage.get(key);
114         return (list != null) ? list : new ArrayList<Class>();
115     }
116 
117     private Class[] filterGroupList(String key, List<Class> addedGroups)
118     {
119         List<Class> restrictedGroups = buildGroupList(key, this.restrictedGroups);
120         List<Class> results = new ArrayList<Class>();
121 
122         for(Class currentGroup : addedGroups)
123         {
124             if(!restrictedGroups.contains(currentGroup))
125             {
126                 results.add(currentGroup);
127             }
128         }
129 
130         return results.toArray(new Class[results.size()]);
131     }
132 
133     private Class[] mergeResults(Class[] resultsForPage, Class[] resultsForComponent)
134     {
135         Class[] mergedResult = new Class[resultsForPage.length + resultsForComponent.length];
136 
137         System.arraycopy(resultsForPage, 0, mergedResult, 0, resultsForPage.length);
138         System.arraycopy(resultsForComponent, 0, mergedResult, resultsForPage.length, resultsForComponent.length);
139 
140         return mergedResult;
141     }
142 }