added convenience methods for multiple ui messages to be displayed

This commit is contained in:
2019-01-30 00:56:18 +01:00
parent a3e7f6c527
commit 5c7abf4c47
4 changed files with 209 additions and 11 deletions

View File

@ -0,0 +1,103 @@
/*
* Copyright 2019 joern.muehlencord.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.jeeutil;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
/**
*
* @author joern@muehlencord.de
*/
public class DefaultUIMessage implements UIMessage {
private static final long serialVersionUID = 3417186732917401077L;
public static DefaultUIMessage create(String message) {
return new DefaultUIMessage(UIMessageType.INFO, message, null);
}
public static DefaultUIMessage createWarning(String message) {
return new DefaultUIMessage(UIMessageType.WARNING, message, null);
}
public static DefaultUIMessage create(UIMessageType messageType, String message, String detail) {
return new DefaultUIMessage(messageType, message, detail);
}
private UIMessageType messageType;
private String message;
private String detail;
public DefaultUIMessage(UIMessageType messageType, String message) {
this.messageType = messageType;
this.message = message;
}
private DefaultUIMessage(UIMessageType messageType, String message, String detail) {
this.messageType = messageType;
this.message = message;
this.detail = detail;
}
@Override
public UIMessageType getMessageType() {
return messageType;
}
@Override
public String getMessage() {
return message;
}
@Override
public String getDetail() {
return (detail == null) ? "" : detail;
}
@Override
public FacesMessage getFacesMessage() {
return getFacesMessage(null);
}
@Override
public FacesMessage getFacesMessage(String prefix) {
Severity severity;
switch (getMessageType()) {
case INFO:
severity = FacesMessage.SEVERITY_INFO;
break;
case WARNING:
severity = FacesMessage.SEVERITY_WARN;
break;
case ERROR:
severity = FacesMessage.SEVERITY_WARN;
break;
default:
severity = FacesMessage.SEVERITY_ERROR;
break;
}
String summary;
if ((prefix == null) || (prefix.equals(""))) {
summary = getMessage();
} else {
summary = prefix + ": " + getMessage();
}
return new FacesMessage(severity, summary, getDetail());
}
}

View File

@ -1,5 +1,21 @@
/*
* Copyright 2019 joern.muehlencord.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.jeeutil;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
@ -182,4 +198,19 @@ public abstract class FacesUtil {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_FATAL, summary, detail);
FacesContext.getCurrentInstance().addMessage(clientId, message);
}
public static void addMessages(List<UIMessage> messages) {
messages.stream().map(msg -> msg.getFacesMessage()).forEach(msg -> {
if (msg.getSeverity() == FacesMessage.SEVERITY_ERROR) {
FacesUtil.addGlobalErrorMessage(msg.getSummary(), msg.getDetail());
} else if (msg.getSeverity() == FacesMessage.SEVERITY_WARN) {
FacesUtil.addGlobalWarningMessage(msg.getSummary(), msg.getDetail());
} else if (msg.getSeverity() == FacesMessage.SEVERITY_FATAL) {
FacesUtil.addGlobalFatalMessage(msg.getSummary(), msg.getDetail());
} else {
FacesUtil.addGlobalInfoMessage(msg.getSummary(), msg.getDetail());
}
});
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2019 joern.muehlencord.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.jeeutil;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
/**
*
* @author joern.muehlencord
*/
public interface UIMessage extends Serializable {
String getDetail();
String getMessage();
UIMessageType getMessageType();
FacesMessage getFacesMessage();
FacesMessage getFacesMessage(String prefix);
}

View File

@ -0,0 +1,27 @@
/*
* Copyright 2019 joern.muehlencord.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.jeeutil;
/**
*
* @author joern.muehlencord
*/
public enum UIMessageType {
INFO,
WARNING,
ERROR;
}