1 13 24 package org.jahia.engines.selectdatasource; 25 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import java.net.URLEncoder ; 29 30 import org.jahia.data.JahiaData; 31 import org.jahia.data.fields.JahiaField; 32 import org.jahia.engines.EngineParams; 33 import org.jahia.engines.EngineRenderer; 34 import org.jahia.engines.JahiaEngine; 35 import org.jahia.exceptions.JahiaException; 36 import org.jahia.params.ParamBean; 37 38 39 public class SelectDataSource_Engine implements JahiaEngine { 40 41 42 public static final String ENGINE_NAME = "selectdatasource"; 43 44 private static SelectDataSource_Engine instance = null; 45 46 47 private static final org.apache.log4j.Logger logger = 48 org.apache.log4j.Logger.getLogger (SelectDataSource_Engine.class); 49 50 51 54 private SelectDataSource_Engine () { 55 logger.debug ("***** Starting " + SelectDataSource_Engine.class.getName () + 56 " engine *****"); 57 } 58 59 60 63 public static synchronized SelectDataSource_Engine getInstance () { 64 if (instance == null) { 65 instance = new SelectDataSource_Engine (); 66 } 67 return instance; 68 } 69 70 71 74 public boolean authoriseRender (ParamBean jParams) { 75 return (jParams.getOperationMode () == ParamBean.EDIT); 76 } 77 78 79 82 public String renderLink (ParamBean jParams, Object theObj) 83 throws JahiaException { 84 JahiaField theField = (JahiaField) theObj; 85 String params = "?mode=displaywindow&fid=" + theField.getID (); 86 return jParams.composeEngineUrl (ENGINE_NAME, params); 87 } 88 89 90 93 public boolean needsJahiaData (ParamBean jParams) { 94 return false; 95 } 96 97 98 101 public void handleActions (ParamBean jParams, JahiaData jData) 102 throws JahiaException { 103 String mode = jParams.getRequest ().getParameter ("mode"); 104 String ipAddr = jParams.getRequest ().getRemoteAddr (); 105 if (mode != null) { 106 if (mode.equals ("displaywindow")) { 107 logger.debug (ipAddr + " is displaying SelectDataSource Window"); 108 displayFormWindow (jParams); 109 110 } else if (mode.equals ("processform")) { 111 logger.debug (ipAddr + " is processing SelectDataSource Form"); 112 processForm (jParams); 113 } 114 } 115 } 116 117 122 public final String getName () { 123 return ENGINE_NAME; 124 } 125 126 127 130 public void displayFormWindow (ParamBean jParams) throws JahiaException { 131 String urlParams = EMPTY_STRING; 132 urlParams += "&mode=processform"; 133 String theUrl = jParams.composeEngineUrl (ENGINE_NAME, urlParams); 134 135 StringBuffer html = new StringBuffer ( 136 "<center><table border=\"0\" width=\"90%\"><tr>\n"); 137 html.append ("<td width=\"100%\" colspan=\"2\" bgcolor=\"#333333\">\n"); 138 html.append ("<font face=\"arial\" size=\"2\">Please enter datasource id :</font>\n"); 139 html.append ("</td></tr>\n"); 140 html.append ("<tr><td width\"20%\" valign=\"top\"> </td>\n"); 141 html.append ("<td width=\"80%\">"); 142 html.append ("<form method=\"POST\" action=\"" + theUrl + "\">\n"); 143 html.append ( 144 "<input type=\"text\" name=\"datasource_url\" size=\"20\" maxlenght=\"250\" value=\"http://\">"); 145 html.append ("</form>\n"); 146 html.append ("</td></tr></table>\n"); 147 148 EngineRenderer.getInstance ().render (jParams, ENGINE_NAME, html.toString ()); 149 } 150 151 152 155 public void processForm (ParamBean jParams) throws JahiaException { 156 String dsUrl; 157 try { 158 dsUrl = jParams.getRequest ().getParameter ("datasource_url"); 159 160 PrintWriter out = jParams.getResponse ().getWriter (); 161 jParams.getResponse ().setContentType ("text/html"); 162 out.println ("<center> Computing values... </center>"); 163 164 String encodedUrl = URLEncoder.encode (dsUrl); 165 out.println ( 166 "<script language=\"javascript\" SRC=\"" + 167 jParams.settings ().getJsHttpPath () + 168 "\">"); 169 out.println ("</script>"); 170 out.println ("<script language=\"javascript\">"); 171 if (!dsUrl.equals (EMPTY_STRING)) { 172 out.println ( 173 " CloseJahiaWindow('&engine_params=dsurl" + EngineParams.VALUE_TOKEN + 174 encodedUrl + 175 "');"); 176 } else { 177 out.println (" CloseJahiaWindow();"); 178 } 179 out.println ("</script>"); 180 181 } catch (IOException ie) { 182 String errorMsg = "Error while handling the SelectDataSource Window : " + 183 ie.getMessage () + 184 " -> BAILING OUT"; 185 logger.error (errorMsg); 186 throw new JahiaException ("Error while handling a Jahia window's content", 187 errorMsg, JahiaException.DATA_ERROR, JahiaException.CRITICAL_SEVERITY); 188 } 189 } 190 191 } 192 | Popular Tags |