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