1 17 18 package org.pentaho.ui; 19 20 import java.io.IOException ; 21 import java.io.OutputStream ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import org.apache.commons.logging.Log; 25 import org.pentaho.messages.Messages; 26 import org.pentaho.core.services.IActionRequestHandler; 27 import org.pentaho.core.session.IPentahoSession; 28 import org.pentaho.core.solution.IParameterProvider; 29 import org.pentaho.core.system.PentahoMessenger; 30 import org.pentaho.core.system.PentahoSystem; 31 import org.pentaho.core.ui.IPentahoUrlFactory; 32 import org.pentaho.messages.util.LocaleHelper; 33 34 40 public abstract class BaseUIComponent extends PentahoMessenger { 41 42 public static final boolean debug = PentahoSystem.debug; 43 44 protected HashMap xslProperties; 45 46 protected HashMap contentTypes; 47 48 private IActionRequestHandler requestHandler; 49 50 private IPentahoSession userSession; 51 52 public abstract Log getLogger(); 53 54 private HashMap parameterProviders; 55 56 protected IPentahoUrlFactory urlFactory; 57 58 private String sourcePath; 59 60 public void handleRequest(OutputStream outputStream, IActionRequestHandler actionRequestHandler, String contentType, HashMap requestParameterProviders) throws IOException { 61 62 this.parameterProviders = requestParameterProviders; 63 this.requestHandler = actionRequestHandler; 64 String content = getContent(contentType); 65 if (content != null) { 66 outputStream.write(content.getBytes(LocaleHelper.getSystemEncoding())); 67 } else { 68 error(Messages.getString("BaseUI.ERROR_0001_NO_CONTENT")); } 70 } 71 72 protected void setSourcePath(String sourcePath) { 73 this.sourcePath = sourcePath; 74 } 75 76 protected String getSourcePath() { 77 return sourcePath; 78 } 79 80 public void setUrlFactory(IPentahoUrlFactory urlFactory) { 81 this.urlFactory = urlFactory; 82 } 83 84 public void setRequestHandler(IActionRequestHandler actionRequestHandler) { 85 this.requestHandler = actionRequestHandler; 86 } 87 88 public BaseUIComponent(IPentahoUrlFactory urlFactory, List messages, String sourcePath) { 89 super(); 90 this.urlFactory = urlFactory; 91 xslProperties = new HashMap (); 92 contentTypes = new HashMap (); 93 setMessages(messages); 94 parameterProviders = new HashMap (); 95 this.sourcePath = sourcePath; 96 } 97 98 public void setParameterProvider(String name, IParameterProvider parameterProvider) { 99 if (parameterProviders == null) { 100 parameterProviders = new HashMap (); 101 } 102 parameterProviders.put(name, parameterProvider); 103 } 104 105 public void setParameterProviders(HashMap parameterProviders) { 106 this.parameterProviders = parameterProviders; 107 } 108 109 protected IPentahoUrlFactory getUrlFactory() { 110 return urlFactory; 111 } 112 113 protected IActionRequestHandler getRequestHandler() { 114 return requestHandler; 115 } 116 117 public HashMap getParameterProviders() { 118 return parameterProviders; 119 } 120 121 public String getParameter(String name, String defaultValue) { 122 IParameterProvider parameterProvider = (IParameterProvider) parameterProviders.get("options"); String value = null; 124 if (parameterProvider != null) { 125 value = parameterProvider.getStringParameter(name, null); 126 if (value != null) { 127 return value; 128 } 129 } 130 parameterProvider = (IParameterProvider) parameterProviders.get("request"); if (parameterProvider != null) { 132 value = parameterProvider.getStringParameter(name, null); 133 if (value != null) { 134 return value; 135 } 136 } 137 parameterProvider = (IParameterProvider) parameterProviders.get("session"); if (parameterProvider != null) { 139 value = parameterProvider.getStringParameter(name, null); 140 if (value != null) { 141 return value; 142 } 143 } 144 return defaultValue; 145 } 146 147 public String [] getParameterAsArray(String name) { 148 IParameterProvider parameterProvider = (IParameterProvider) parameterProviders.get("options"); Object value; 150 if (parameterProvider != null) { 151 value = parameterProvider.getParameter(name); 152 if (value != null) { 153 return toStringArray( value ); 154 } 155 } 156 parameterProvider = (IParameterProvider) parameterProviders.get("request"); if (parameterProvider != null) { 158 value = parameterProvider.getParameter(name); 159 if (value != null) { 160 return toStringArray( value ); 161 } 162 } 163 parameterProvider = (IParameterProvider) parameterProviders.get("session"); if (parameterProvider != null) { 165 value = parameterProvider.getParameter(name); 166 if (value != null) { 167 return toStringArray( value ); 168 } 169 } 170 return( new String [] {} ); 171 } 172 173 private String [] toStringArray( Object value ) { 174 if ( value == null ) { 175 return( new String [] {} ); 176 } 177 178 if ( value instanceof String [] ) { 179 return( (String [])value ); 180 } 181 182 return( new String [] { value.toString() } ); 183 } 184 185 protected IPentahoSession getSession() { 186 return userSession; 187 } 188 189 public void setXsl(String mimeType, String xslName) { 190 contentTypes.put(mimeType, xslName); 191 } 192 193 public String getXsl(String mimeType) { 194 return (String ) contentTypes.get(mimeType); 195 } 196 197 public abstract boolean validate(); 198 199 public boolean validate(IPentahoSession session, IActionRequestHandler actionRequestHandler) { 200 this.userSession = session; 201 this.genLogIdFromSession(session); 202 this.requestHandler = actionRequestHandler; 203 return validate(); 204 } 205 206 public void setXslProperty(String name, String value) { 207 xslProperties.put(name, value); 208 } 209 210 public HashMap getXslProperties() { 211 return xslProperties; 212 } 213 214 public abstract String getContent(String mimeType); 215 216 public void done() { 217 218 } 219 220 } 221 | Popular Tags |