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