1 13 24 package org.jahia.engines.viewdatasourceid; 25 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 29 import org.jahia.data.JahiaData; 30 import org.jahia.data.fields.JahiaField; 31 import org.jahia.engines.JahiaEngine; 32 import org.jahia.exceptions.JahiaException; 33 import org.jahia.params.ParamBean; 34 35 public class ViewDataSourceID_Engine implements JahiaEngine { 36 37 private static ViewDataSourceID_Engine instance = null; 38 public static final String ENGINE_NAME = "viewdatasourceid"; 39 40 41 private static final org.apache.log4j.Logger logger = 42 org.apache.log4j.Logger.getLogger (ViewDataSourceID_Engine.class); 43 44 45 48 private ViewDataSourceID_Engine () { 49 logger.debug ("***** Starting " + ViewDataSourceID_Engine.class.getName () + 50 " engine *****"); 51 } 52 53 54 57 public static synchronized ViewDataSourceID_Engine getInstance () { 58 if (instance == null) { 59 instance = new ViewDataSourceID_Engine (); 60 } 61 return instance; 62 } 63 64 65 68 public boolean authoriseRender (ParamBean jParams) { 69 return (jParams.getOperationMode () == ParamBean.EDIT); 70 } 71 72 73 76 public String renderLink (ParamBean jParams, Object theObj) 77 throws JahiaException { 78 JahiaField theField = (JahiaField) theObj; 79 String params = "?mode=displayid&fid=" + theField.getID (); 80 return jParams.composeEngineUrl (ENGINE_NAME, params); 81 } 82 83 84 87 public boolean needsJahiaData (ParamBean jParams) { 88 return false; 89 } 90 91 92 95 public void handleActions (ParamBean jParams, JahiaData jData) 96 throws JahiaException { 97 String mode = jParams.getRequest ().getParameter ("mode"); 98 String ipAddr = jParams.getRequest ().getRemoteAddr (); 99 int fid = jParams.getFieldID (); 100 if (mode != null) { 101 if (mode.equals ("displayid")) { 102 logger.debug (ipAddr + " is accessing ViewDataSourceID " + fid); 103 displayID (jParams, fid); 104 } 105 } 106 } 107 108 109 114 public final String getName () { 115 return ENGINE_NAME; 116 } 117 118 119 122 public void displayID (ParamBean jParams, int fieldID) 123 throws JahiaException { 124 try { 125 PrintWriter out = jParams.getResponse ().getWriter (); 126 jParams.getResponse ().setContentType ("text/html"); 127 String dsUrl = jParams.composeEngineUrl ("datasourcefield", 128 "&mode=displaydata&fid=" + fieldID); 129 StringBuffer html = 130 new StringBuffer ("<html><head><title>Data Source ID</title></head>\n"); 131 html.append ( 132 "<body bgcolor=\"black\" onLoad=\"document.ds.dsid.focus();document.ds.dsid.select()\">\n"); 133 html.append ("<font color=\"white\" size=\"2\"><b>Data Source ID :</b><br>\n"); 134 html.append ( 135 "<form name=\"ds\"><input type=\"text\" name=\"dsid\" size=\"60\" value=\""); 136 html.append (dsUrl); 137 html.append ("\"><br>\n"); 138 html.append ( 139 "<input type=\"button\" value=\"Close\" onClick=\"window.close()\">\n"); 140 html.append ("</form></body></html>"); 141 out.println (html.toString ()); 142 143 } catch (IOException ie) { 144 String errorMsg = "Error while returning a datasource field : " + ie.getMessage () + 145 " -> BAILING OUT"; 146 logger.error (errorMsg); 147 throw new JahiaException ("Cannot retrieve remote data", 148 errorMsg, JahiaException.DATA_ERROR, JahiaException.CRITICAL_SEVERITY); 149 } 150 151 } 152 153 } 154 | Popular Tags |