001 // Copyright 2011 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry5.alerts; 016 017 import org.apache.tapestry5.BaseOptimizedSessionPersistedObject; 018 import org.apache.tapestry5.ioc.internal.util.CollectionFactory; 019 020 import java.io.Serializable; 021 import java.util.Iterator; 022 import java.util.List; 023 024 /** 025 * A stateless session object used to store Alerts between requests. 026 * 027 * @since 5.3 028 */ 029 public class AlertStorage extends BaseOptimizedSessionPersistedObject implements Serializable 030 { 031 private final List<Alert> alerts = CollectionFactory.newList(); 032 033 public synchronized void add(Alert alert) 034 { 035 assert alert != null; 036 037 alerts.add(alert); 038 039 markDirty(); 040 } 041 042 /** 043 * Dismisses all Alerts. 044 */ 045 public synchronized void dismissAll() 046 { 047 if (!alerts.isEmpty()) 048 { 049 alerts.clear(); 050 markDirty(); 051 } 052 } 053 054 /** 055 * Dismisses non-persistent Alerts; this is useful after rendering the {@link org.apache.tapestry5.corelib.components.Alerts} 056 * component. 057 */ 058 public synchronized void dismissNonPersistent() 059 { 060 boolean dirty = false; 061 062 Iterator<Alert> i = alerts.iterator(); 063 064 while (i.hasNext()) 065 { 066 if (!i.next().duration.persistent) 067 { 068 dirty = true; 069 i.remove(); 070 } 071 } 072 073 if (dirty) 074 { 075 markDirty(); 076 } 077 } 078 079 080 /** 081 * Dismisses a single Alert, if present. 082 */ 083 public synchronized void dismiss(long alertId) 084 { 085 Iterator<Alert> i = alerts.iterator(); 086 087 while (i.hasNext()) 088 { 089 if (i.next().id == alertId) 090 { 091 i.remove(); 092 markDirty(); 093 return; 094 } 095 } 096 } 097 098 099 /** 100 * Returns all stored alerts. 101 * 102 * @return list of alerts (possibly empty) 103 */ 104 public List<Alert> getAlerts() 105 { 106 return alerts; 107 } 108 }