Alarm.java
/*
** Module : Alarm.java
** Abstract : Alarm service.
**
** Copyright (c) 2017, Golden Code Development Corporation.
**
** -#- -I- --Date-- --------------------------------Description-----------------------------------
** 001 HC 20170612 Initial version.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.admin.client.application;
import com.goldencode.p2j.admin.client.*;
import com.goldencode.p2j.admin.client.widget.dialog.*;
import com.goldencode.p2j.admin.shared.*;
import com.google.gwt.safehtml.shared.*;
import com.google.inject.*;
import java.util.*;
import java.util.function.*;
/**
* Admin alarm UI service.
*/
public class Alarm
{
/** Admin Service reference */
private AdminServiceAsync adm;
/** Modal dialogs */
private ModalDialogs dialogs;
/**
* Ctor.
*
* @param adm
* Admin Service reference.
* @param dialogs
* Modal dialogs.
*/
@Inject
public Alarm(AdminServiceAsync adm, ModalDialogs dialogs)
{
this.adm = adm;
this.dialogs = dialogs;
}
/**
* Peeks the last server message and displays it in a modal message dialog.
*/
public void ring()
{
adm.peekMessage(new AdminCallback<String>()
{
@Override
public void onDone(String result)
{
dialogs.showError(result);
}
});
}
/**
* Displays the supplied message in a modal message dialog.
*
* @param message
* The message to be displayed.
*/
public void ring(String message)
{
ring(Collections.singletonList(message), null);
}
/**
* Displays the supplied messages in a modal message dialog.
*
* @param messages
* The messages to be displayed.
*/
public void ring(List<String> messages)
{
ring(messages, null);
}
/**
* Displays the supplied message in a modal message dialog.
*
* @param message
* The message to be displayed.
* @param closeCallback
* The callback called when the message dialog is dismissed.
*/
public void ring(String message, Runnable closeCallback)
{
ring(Collections.singletonList(message), closeCallback);
}
/**
* Displays the supplied messages in a modal message dialog.
*
* @param messages
* The messages to be displayed.
* @param closeCallback
* The callback called when the message dialog is dismissed.
*/
public void ring(List<String> messages, Runnable closeCallback)
{
List<String> actual = messages;
if (messages.size() > 5)
{
actual = new ArrayList<>();
actual.add("Showing only the last five server messages. To see all please go to " +
"Help->Show Messages.");
int msgSize = messages.size();
actual.addAll(messages.subList(msgSize - 5, msgSize));
}
StringBuilder sb = new StringBuilder();
for (String message : actual)
{
if (sb.length() > 0)
{
sb.append("<br>");
}
sb.append(message);
}
dialogs.showError(sb.toString(), closeCallback);
}
/**
* Fetches all the messages from the admin service. The result messages will be HTML-escaped.
*
* @param resultConsumer
* A callback called with the resulting list of messages.
*/
public void getAllMessages(Consumer<List<String>> resultConsumer)
{
getAllMessages(resultConsumer, true);
}
/**
* Fetches all the messages from the admin service.
*
* @param resultConsumer
* A callback called with the resulting list of messages.
* @param escape
* When <code>true</code> the messages returned will be HTML-escaped.
*/
public void getAllMessages(Consumer<List<String>> resultConsumer, boolean escape)
{
adm.getMessages(true, new AdminCallback<String[][]>()
{
@Override
public void onDone(String[][] result)
{
if (result != null) {
List<String> resultMsg = new ArrayList<>(100);
for (int i = 0; i < result.length; i++) {
if (result[i] == null) {
continue;
}
for (int j = 0; j < result[i].length; j++) {
String res = escape ? SafeHtmlUtils.htmlEscape(result[i][j]) :
result[i][j];
resultMsg.add(res);
}
}
resultConsumer.accept(resultMsg);
}
}
});
}
}