1 13 20 package org.jahia.engines.shared; 21 22 import java.util.HashMap ; 23 24 import org.apache.log4j.Logger; 25 import org.jahia.data.ConnectionTypes; 26 import org.jahia.data.FormDataManager; 27 import org.jahia.data.containers.JahiaContainer; 28 import org.jahia.data.fields.JahiaField; 29 import org.jahia.engines.EngineMessages; 30 import org.jahia.engines.EngineParams; 31 import org.jahia.engines.JahiaEngine; 32 import org.jahia.engines.JahiaEngineTools; 33 import org.jahia.exceptions.JahiaException; 34 import org.jahia.params.ParamBean; 35 import org.jahia.registries.EnginesRegistry; 36 import org.jahia.registries.ServicesRegistry; 37 import org.jahia.services.acl.JahiaBaseACL; 38 import org.jahia.utils.JahiaTools; 39 40 public class Integer_Field 41 { 42 43 private static Logger logger = Logger.getLogger(Integer_Field.class); 44 45 private static Integer_Field theObject = null; 46 private static final String JSP_FILE = "/jsp/jahia/engines/shared/integer_field.jsp"; 47 public static final String READONLY_JSP = "/jsp/jahia/engines/shared/readonly_smalltext_field.jsp"; 48 49 50 55 public static synchronized Integer_Field getInstance() 56 { 57 if (theObject == null) { 58 theObject = new Integer_Field(); 59 } 60 return theObject; 61 } 63 64 65 74 public boolean handleField( ParamBean jParams, Integer modeInt, HashMap engineMap ) 75 throws JahiaException 76 { 77 int mode = modeInt.intValue(); 78 JahiaField theField = (JahiaField) engineMap.get( "theField" ); 79 switch (mode) 80 { 81 case (JahiaEngine.LOAD_MODE) : return composeEngineMap( jParams, engineMap, theField ); 82 case (JahiaEngine.UPDATE_MODE) : return getFormData( jParams, engineMap, theField ); 83 case (JahiaEngine.SAVE_MODE) : return saveData( jParams, engineMap, theField ); 84 } 85 return false; 86 } 88 89 90 99 private boolean getFormData( ParamBean jParams, HashMap engineMap, JahiaField theField ) 100 throws JahiaException 101 { 102 boolean out = true; 103 104 String fieldValue = jParams.getRequest().getParameter( "_" + new Integer (theField.getID()).toString() ); 105 String engineParams = jParams.getRequest().getParameter( "engine_params" ); 106 fieldValue = JahiaTools.replacePattern(fieldValue,"|","|"); 107 108 109 EngineParams eParams = new EngineParams( engineParams ); 110 String localSwitch = eParams.getParameter( "localswitch" ); 111 String dataSourceUrl = eParams.getParameter( "dsurl" ); 112 113 if (dataSourceUrl != null) { 114 theField.setValue( dataSourceUrl ); 115 theField.setConnectType( ConnectionTypes.DATASOURCE ); 116 } else if (localSwitch != null) { 117 theField.setValue( theField.getDefinition().getDefaultValue( jParams.getPage().getPageTemplateID() ) ); 118 theField.setConnectType( ConnectionTypes.LOCAL ); 119 } else if (fieldValue != null) { 120 EngineMessages resultMessages = new EngineMessages(); 121 if (FormDataManager.getInstance().checkIntegrity(fieldValue, resultMessages)) { 122 theField.setValue( fieldValue ); 123 } else { 124 theField.setValue( fieldValue ); 125 out = false; 126 } 127 } 128 129 return out; 130 } 132 133 134 143 private boolean saveData( ParamBean jParams, HashMap engineMap, JahiaField theField ) 144 throws JahiaException 145 { 146 logger.debug("Saving Field " + theField.getDefinition().getName() + " : " + theField.getValue() ); 147 return theField.save(jParams); 148 } 150 151 152 161 private boolean composeEngineMap( ParamBean jParams, HashMap engineMap, JahiaField theField ) 162 throws JahiaException 163 { 164 165 boolean editable = false; 166 JahiaContainer theContainer = (JahiaContainer)engineMap.get("theContainer"); 167 168 if ( theContainer == null ){ 169 editable = true; 171 } else { 172 HashMap ctnListFieldAcls = (HashMap ) engineMap 173 .get("ctnListFieldAcls"); 174 175 if (theContainer.getListID() != 0 && ctnListFieldAcls != null 176 && ctnListFieldAcls.size() > 0) { 177 JahiaBaseACL acl = JahiaEngineTools.getCtnListFieldACL( 178 ctnListFieldAcls, theField.getID()); 179 if (acl != null) { 180 editable = acl.getPermission(jParams.getUser(), 181 JahiaBaseACL.WRITE_RIGHTS, true); 182 } 183 } else { 184 editable = true; 185 } 186 } 187 188 String output = ""; 189 190 if ( editable ) { 191 192 String localSwitchUrl = "ReloadEngine('localswitch" + EngineParams.VALUE_TOKEN + "yes')"; 193 engineMap.put( "localSwitchUrl", localSwitchUrl ); 194 195 String dataSourceConnectUrl = ""; 196 JahiaEngine dsEngine = (JahiaEngine) EnginesRegistry.getInstance().getEngine( "selectdatasource" ); 197 dataSourceConnectUrl = dsEngine.renderLink( jParams, theField ); 198 engineMap.put( "dataSourceConnectUrl", dataSourceConnectUrl ); 199 200 String dataSourceIDUrl = ""; 201 dsEngine = (JahiaEngine) EnginesRegistry.getInstance().getEngine( "viewdatasourceid" ); 202 dataSourceIDUrl = dsEngine.renderLink( jParams, theField ); 203 engineMap.put( "dataSourceIDUrl", dataSourceIDUrl ); 204 205 boolean isIE = false; 206 String userAgent = jParams.getRequest().getHeader( "user-agent" ); 207 if (userAgent != null) { 208 isIE = (userAgent.indexOf( "IE" ) != -1); 209 } 210 jParams.getRequest().setAttribute("isIE" , new Boolean (isIE)); 211 212 theField.setValue( FormDataManager.getInstance().formEncode(theField.getValue()) ); 213 output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, JSP_FILE ); 214 } else { 215 output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, READONLY_JSP ); 216 } 217 218 engineMap.put( "fieldForm", output ); 219 220 return true; 221 } 223 224 } | Popular Tags |