1 package org.jahia.engines; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 import java.util.Set ; 9 10 import javax.servlet.ServletRequest ; 11 import javax.servlet.jsp.PageContext ; 12 13 22 23 public class EngineMessages { 24 25 public static final String GLOBAL_MESSAGE = "org.jahia.engines.global_message"; 26 public static final String CONTEXT_KEY = "engineMessages"; 27 28 Map messages = new HashMap (); 29 30 public EngineMessages () { 31 } 32 33 public void add (EngineMessage message) { 34 add (GLOBAL_MESSAGE, message); 35 } 36 37 public void add (String property, 38 EngineMessage message) { 39 ArrayList propertyList = (ArrayList ) messages.get (property); 40 if (propertyList == null) { 41 propertyList = new ArrayList (); 42 } 43 propertyList.add (message); 44 messages.put (property, propertyList); 45 } 46 47 public Set getProperties () { 48 return messages.keySet (); 49 } 50 51 public Set getEntrySet () { 52 return messages.entrySet (); 53 } 54 55 public int getSize () { 56 Iterator propertyIter = getProperties ().iterator (); 57 int size = 0; 58 while (propertyIter.hasNext ()) { 59 String curPropertyName = (String ) propertyIter.next (); 60 ArrayList curPropertyList = (ArrayList ) messages.get (curPropertyName); 61 size += curPropertyList.size (); 62 } 63 return size; 64 } 65 66 public List getMessages () { 67 ArrayList fullList = new ArrayList (); 68 Iterator propertyIter = getProperties ().iterator (); 69 while (propertyIter.hasNext ()) { 70 String curPropertyName = (String ) propertyIter.next (); 71 ArrayList curPropertyList = (ArrayList ) messages.get (curPropertyName); 72 fullList.addAll (curPropertyList); 73 } 74 return fullList; 75 } 76 77 public List getMessages (String property) { 78 ArrayList propertyList = (ArrayList ) messages.get (property); 79 return propertyList; 80 } 81 82 public int getSize (String property) { 83 ArrayList propertyList = (ArrayList ) messages.get (property); 84 if (propertyList != null) { 85 return propertyList.size (); 86 } else { 87 return 0; 88 } 89 } 90 91 public boolean isEmpty () { 92 return messages.isEmpty (); 93 } 94 95 101 public void saveMessages (String contextPrefix, ServletRequest request) { 102 request.setAttribute (contextPrefix + CONTEXT_KEY , this); 103 } 104 105 public void saveMessages (ServletRequest request) { 106 request.setAttribute (CONTEXT_KEY, this); 107 } 108 109 public void saveMessages (PageContext pageContext) { 110 pageContext.setAttribute (CONTEXT_KEY, this); 111 } 112 113 public void saveMessages (PageContext pageContext, int scope) { 114 pageContext.setAttribute (CONTEXT_KEY, this, scope); 115 } 116 117 } 118 | Popular Tags |