All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nosi.core.webapp.FlashMessage Maven / Gradle / Ivy

Go to download

IGRP Framework is a powerful and highly customizable platform developed by the Operational Nucleus for the Information Society (NOSi) to create web applications, it provides out of box, several modules to make easy to create stand-alone, production-grade web applications: authentication and access-control, business processes automation, reporting, page builder with automatic code generation and incorporation of the Once-Only-Principle, written in Java. IGRP Framework WAR - Contains some keys resources that give UI to IGRP Framework and others supports files.

There is a newer version: 2.0.0.240912-RCM
Show newest version
package nosi.core.webapp;

import java.util.Map;

import nosi.core.webapp.Igrp;

import java.util.HashMap;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;


/**
 * @author Marcel Iekiny
 * Apr 19, 2017
 */
public class FlashMessage implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Message msgs;
	
	public static final String SUCCESS = "success";
	public static final String INFO_LINK = "info_link";
	public static final String ERROR = "error";
	public static final String WARNING = "warning";
	public static final String DEBUG = "debug";
	public static final String INFO = "info";
	public static final String MSG_ERROR = "Operação falhada!";
	public static final String MSG_SUCCESS = "Operação efetuada com sucesso!";
    public static final String WARNING_EXPORT_APP = "Esta aplicação não possui conteúdo suficiente para ser exportado";
    
	public static final String MESSAGE_SUCCESS = "Operação efetuada com sucesso!";
	public static final String MESSAGE_ERROR = "Falha ao tentar efetuar esta operação!";

	public static final String WARNING_EXPORT_PAGE = "Esta página não possui conteúdo suficiente para ser exportado.";
    public static final String WARNING_PAGE_INVALID = "Nome da página é inválida.";

	public static final String ERROR_IMPORT = "Ocorreu um erro ao importa o ficheiro";

	public static final String ERROR_COMPILED = "Erro de compilação";

	public static final String MESSAGE_ERROR_VALID_PAGE = "Nome da página é inválida";

	public static final String MSG_CONFIRM = "Deseja realmente realizar esta operação?";

	public static final String CONFIRM = "confirm";

	protected FlashMessage(){ // Make sure that this will be only invocate by the Igrp class 
		// "_flash" is the reserved name for messages in session
		if(Igrp.getInstance().getRequest().getSession() != null) {
			if(Igrp.getInstance().getRequest().getSession().getAttribute("_flash") == null){
				this.msgs = new Message();
				Igrp.getInstance().getRequest().getSession().setAttribute("_flash", this.msgs);
			}else
				this.msgs = (Message) Igrp.getInstance().getRequest().getSession().getAttribute("_flash");
		}
	}
	
	
	public FlashMessage addMessage(String name, String msg){
		this.msgs.addMessage(name, msg);
		// atualizar session  /* Sorry we dont need it */
		//Igrp.getInstance().getRequest().getSession().setAttribute("flash", this.msgs);
		return this;
	}
	
	public void setMessage(String name, String msg){
		this.msgs.setMessage(name, msg);
		// atualizar session /* Sorry we dont need it */
		//Igrp.getInstance().getRequest().getSession().setAttribute("flash", this.msgs);
	}
	
	public boolean hasMessage(String name){
		return this.msgs.hasMessage(name);
	}
	
	public String getMessagesAsString(String name){
		return this.msgs.getMessagesAsString(name);
	}
	
	public ArrayList getMessages(String name){
		return this.msgs.getMessages(name);
	}

	// Please dont uncomment this method below ... (because it is only for test purpose)
	/*public Message getMessage(){
		return this.msgs;
	}*/
	
	private class Message implements Serializable{ // inner/internal class for all message
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private Map> msg;
		
		public Message(){
			this.msg = new HashMap>();
			this.msg.put(FlashMessage.ERROR, new ArrayList());
			this.msg.put(FlashMessage.SUCCESS, new ArrayList());
			this.msg.put(FlashMessage.CONFIRM, new ArrayList());
			this.msg.put(FlashMessage.INFO, new ArrayList());
			this.msg.put(FlashMessage.WARNING, new ArrayList());
			this.msg.put(FlashMessage.DEBUG, new ArrayList());
			this.msg.put(FlashMessage.INFO_LINK, new ArrayList());
		}
		
		public void addMessage(String name, String msg){
			if(this.msg!=null && this.msg.containsKey(name))
				this.msg.get(name).add(msg);
			else
				this.setMessage(name, msg);
		}
		
		public void setMessage(String name, String msg){
			ArrayList aux = new ArrayList();
			aux.add(msg);
			this.msg.put(name, aux);
		}
		
		public boolean hasMessage(String name){
			return this.msg.containsKey(name) && this.msg.get(name).size() > 0;
		}
		
		public String getMessagesAsString(String name){ // return all specific message as a String
			String result = "";
			if(this.msg.containsKey(name)){
				Iterator i = this.msg.get(name).iterator();
				while(i.hasNext())
					result += i.next() + " ";
			}
			this.msg.get(name).clear();
			return result;
		}
		
		public ArrayList getMessages(String name){
			ArrayList result = new ArrayList(); // empty ArrayList for NullPointerException when return it ...
			if(this.msg.containsKey(name)){
				result = new ArrayList(this.msg.get(name)); // to make clone of collection
				this.msg.get(name).clear();
			}
			return result;
		}
		

		public void removeMsg(String name) {
			if(this.msg!=null && this.msg.containsKey(name))
				this.msg.remove(name);
		}
	}

	public void removeMsg(String name) {
		this.msgs.removeMsg(name);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy