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