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  
20  package org.apache.myfaces.tobago.internal.component;
21  
22  import org.apache.commons.collections.iterators.SingletonIterator;
23  import org.apache.myfaces.tobago.component.Visual;
24  import org.apache.myfaces.tobago.layout.OrderBy;
25  
26  import javax.faces.application.FacesMessage;
27  import javax.faces.context.FacesContext;
28  import java.util.ArrayList;
29  import java.util.Collections;
30  import java.util.Comparator;
31  import java.util.Iterator;
32  import java.util.List;
33  
34  public abstract class AbstractUIMessages extends javax.faces.component.UIMessages
35      implements Visual {
36  
37    public List<Item> createMessageList(final FacesContext facesContext) {
38  
39      final Iterator clientIds;
40      if (isGlobalOnly()) {
41        clientIds = new SingletonIterator(null);
42      } else if (getFor() != null) {
43        clientIds = new SingletonIterator(getFor());
44      } else {
45        clientIds = facesContext.getClientIdsWithMessages();
46      }
47  
48      final List<Item> messages = collectMessageList(facesContext, clientIds);
49  
50      // todo
51      if (OrderBy.severity == getOrderBy()) {
52        // sort
53        Collections.sort(messages, new ItemComparator());
54      }
55      return messages;
56    }
57  
58    private List<Item> collectMessageList(final FacesContext facesContext, final Iterator clientIds) {
59      final List<Item> messages = new ArrayList<Item>();
60      while (clientIds.hasNext()) {
61        final String clientId = (String) clientIds.next();
62        final Iterator<FacesMessage> i = facesContext.getMessages(clientId);
63        while (i.hasNext()) {
64          final FacesMessage facesMessage = i.next();
65          if (getMaxNumber() != null && messages.size() >= getMaxNumber()) {
66            return messages;
67          }
68          if (facesMessage.getSeverity().getOrdinal() < getMinSeverity().getOrdinal()) {
69            continue;
70          }
71          if (facesMessage.getSeverity().getOrdinal() > getMaxSeverity().getOrdinal()) {
72            continue;
73          }
74          messages.add(new Item(clientId, facesMessage));
75        }
76      }
77      return messages;
78    }
79  
80    public static class Item {
81  
82      private String clientId;
83      private FacesMessage facesMessage;
84  
85      public Item(final String clientId, final FacesMessage facesMessage) {
86        this.clientId = clientId;
87        this.facesMessage = facesMessage;
88      }
89  
90      public String getClientId() {
91        return clientId;
92      }
93  
94      public void setClientId(final String clientId) {
95        this.clientId = clientId;
96      }
97  
98      public FacesMessage getFacesMessage() {
99        return facesMessage;
100     }
101 
102     public void setFacesMessage(final FacesMessage facesMessage) {
103       this.facesMessage = facesMessage;
104     }
105   }
106 
107   public static class ItemComparator implements Comparator<Item> {
108     @Override
109     public int compare(final Item item1, final Item item2) {
110       return item2.getFacesMessage().getSeverity().getOrdinal() - item1.getFacesMessage().getSeverity().getOrdinal();
111     }
112   }
113 
114   public abstract FacesMessage.Severity getMinSeverity();
115 
116   public abstract FacesMessage.Severity getMaxSeverity();
117 
118   public abstract Integer getMaxNumber();
119 
120   public abstract OrderBy getOrderBy();
121 
122 /* TBD: if we support JSF 1.2 whe have to do something here.
123   public abstract String getFor();
124 */
125 
126 }