001: /*
002: * SystemwideAlerts.java
003: *
004: * Version: $Revision: 1.3 $
005: *
006: * Date: $Date: 2006/07/13 23:20:54 $
007: *
008: * Copyright (c) 2002, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040: package org.dspace.app.xmlui.aspect.administrative;
041:
042: import java.io.Serializable;
043:
044: import org.apache.cocoon.caching.CacheableProcessingComponent;
045: import org.apache.excalibur.source.SourceValidity;
046: import org.apache.excalibur.source.impl.validity.NOPValidity;
047: import org.dspace.app.xmlui.cocoon.AbstractDSpaceTransformer;
048: import org.dspace.app.xmlui.wing.Message;
049: import org.dspace.app.xmlui.wing.WingException;
050: import org.dspace.app.xmlui.wing.element.Metadata;
051: import org.dspace.app.xmlui.wing.element.PageMeta;
052:
053: /**
054: * This class maintains any system-wide alerts. If any alerts are activated then
055: * they are added to the page's metadata for display by the theme.
056: *
057: * This class also creates an interface for the alert system to be maintained.
058: *
059: * @author Scott Phillips
060: */
061: public class SystemwideAlerts extends AbstractDSpaceTransformer
062: implements CacheableProcessingComponent {
063: /** Language Strings */
064: private static final Message T_COUNTDOWN = message("xmlui.administrative.SystemwideAlerts.countdown");
065:
066: // Is an alert activated?
067: private static boolean active;
068:
069: // The alert's message
070: private static String message;
071:
072: // If a count down time is present, what time are we counting down too?
073: private static long countDownToo;
074:
075: /**
076: * Generate the unique caching key.
077: */
078: public Serializable getKey() {
079: if (active)
080: // Don't cache any alert messages
081: return null;
082: else
083: return "1";
084: }
085:
086: /**
087: * Generate the cache validity object.
088: */
089: public SourceValidity getValidity() {
090: if (active)
091: return null;
092: else
093: return NOPValidity.SHARED_INSTANCE;
094: }
095:
096: /**
097: * If an alert is activated then add a count down message.
098: */
099: public void addPageMeta(PageMeta pageMeta) throws WingException {
100:
101: if (active) {
102: Metadata alert = pageMeta.addMetadata("alert", "message");
103:
104: long time = countDownToo - System.currentTimeMillis();
105: if (time > 0) {
106: // from milliseconds to minutes
107: time = time / (60 * 1000);
108:
109: alert.addContent(T_COUNTDOWN.parameterize(time));
110: }
111:
112: alert.addContent(message);
113: }
114: }
115:
116: /**
117: * Check whether an alert is active.
118: */
119: public static boolean isAlertActive() {
120: return SystemwideAlerts.active;
121: }
122:
123: /**
124: * Activate the current alert.
125: */
126: public static void activateAlert() {
127: SystemwideAlerts.active = true;
128: }
129:
130: /**
131: * Deactivate the current alert.
132: */
133: public static void deactivateAlert() {
134: SystemwideAlerts.active = false;
135: }
136:
137: /**
138: * Set the current alert's message.
139: * @param message The new message
140: */
141: public static void setMessage(String message) {
142: SystemwideAlerts.message = message;
143: }
144:
145: /**
146: * @return the current alert's message
147: */
148: public static String getMessage() {
149: return SystemwideAlerts.message;
150: }
151:
152: /**
153: * Get the time, in millieseconds, when the countdown timer is scheduled to end.
154: */
155: public static long getCountDownToo() {
156: return SystemwideAlerts.countDownToo;
157: }
158:
159: /**
160: * Set the time, in millieseconds, to which the countdown timer should end.
161: *
162: * Note, that once the countdown has expried, the alert is
163: * still active. However the countdown will disappear.
164: */
165: public static void setCountDownToo(long countDownTo) {
166: SystemwideAlerts.countDownToo = countDownTo;
167: }
168:
169: }
|