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.JahiaFieldDefinitionProperties; 29 import org.jahia.data.fields.JahiaField; 30 import org.jahia.engines.EngineMessages; 31 import org.jahia.engines.EngineParams; 32 import org.jahia.engines.JahiaEngine; 33 import org.jahia.engines.JahiaEngineTools; 34 import org.jahia.exceptions.JahiaException; 35 import org.jahia.params.ParamBean; 36 import org.jahia.registries.EnginesRegistry; 37 import org.jahia.registries.ServicesRegistry; 38 import org.jahia.services.acl.JahiaBaseACL; 39 import org.jahia.utils.JahiaTools; 40 41 public class Date_Field 42 { 43 44 private static Logger logger = Logger.getLogger(Date_Field.class); 45 46 private static Date_Field theObject = null; 47 public static final String JSP_FILE = "/jsp/jahia/engines/shared/date_field.jsp"; 48 public static final String READONLY_JSP = "/jsp/jahia/engines/shared/readonly_date_field.jsp"; 49 50 51 56 public static synchronized Date_Field getInstance() 57 { 58 if (theObject == null) { 59 theObject = new Date_Field(); 60 } 61 return theObject; 62 } 64 65 66 75 public boolean handleField( ParamBean jParams, Integer modeInt, HashMap engineMap ) 76 throws JahiaException 77 { 78 int mode = modeInt.intValue(); 79 JahiaField theField = (JahiaField) engineMap.get( "theField" ); 80 switch (mode) 81 { 82 case (JahiaEngine.LOAD_MODE) : return composeEngineMap( jParams, engineMap, theField ); 83 case (JahiaEngine.UPDATE_MODE) : return getFormData( jParams, engineMap, theField ); 84 case (JahiaEngine.SAVE_MODE) : return saveData( jParams, engineMap, theField ); 85 } 86 return false; 87 } 89 90 91 92 101 private boolean getFormData( ParamBean jParams, HashMap engineMap, JahiaField theField ) 102 throws JahiaException 103 { 104 boolean out = true; 105 106 String fieldValue = jParams.getRequest().getParameter( "_" + new Integer (theField.getID()).toString() ); 107 String fieldValueStr = jParams.getRequest().getParameter( "_Str" + new Integer (theField.getID()).toString() ); 108 String engineParams = jParams.getRequest().getParameter( "engine_params" ); 109 fieldValue = JahiaTools.replacePattern(fieldValue,"|","|"); 110 111 112 113 EngineParams eParams = new EngineParams( engineParams ); 114 String localSwitch = eParams.getParameter( "localswitch" ); 115 String dataSourceUrl = eParams.getParameter( "dsurl" ); 116 117 if (dataSourceUrl != null) { 118 theField.setValue( dataSourceUrl ); 119 theField.setConnectType( ConnectionTypes.DATASOURCE ); 120 } else if (localSwitch != null) { 121 theField.setValue( theField.getDefinition().getDefaultValue( jParams.getPage().getPageTemplateID() ) ); 122 theField.setConnectType( ConnectionTypes.LOCAL ); 123 } else if (fieldValue != null) { 124 EngineMessages resultMessages = new EngineMessages(); 125 if (FormDataManager.getInstance().checkIntegrity(fieldValue, resultMessages)) { 126 theField.setObject( fieldValue ); 127 theField.setValue( fieldValueStr ); 128 } else { 129 theField.setObject( fieldValue ); 130 theField.setValue( fieldValueStr ); 131 out = false; 132 } 133 } 136 137 return out; 138 } 140 141 142 151 private boolean saveData( ParamBean jParams, HashMap engineMap, JahiaField theField ) 152 throws JahiaException 153 { 154 logger.debug("Saving Field " + theField.getDefinition().getName() + " : " + theField.getValue() ); 155 return theField.save(jParams); 156 } 158 159 160 169 private boolean composeEngineMap( ParamBean jParams, HashMap engineMap, JahiaField theField ) 170 throws JahiaException 171 { 172 boolean editable = false; 173 JahiaContainer theContainer = (JahiaContainer)engineMap.get("theContainer"); 174 if ( theContainer == null ){ 175 editable = true; 177 } else { 178 HashMap ctnListFieldAcls = (HashMap ) engineMap 179 .get("ctnListFieldAcls"); 180 181 if (theContainer.getListID() != 0 && ctnListFieldAcls != null 182 && ctnListFieldAcls.size() > 0) { 183 JahiaBaseACL acl = JahiaEngineTools.getCtnListFieldACL( 184 ctnListFieldAcls, theField.getID()); 185 if (acl != null) { 186 editable = acl.getPermission(jParams.getUser(), 187 JahiaBaseACL.WRITE_RIGHTS, true); 188 } 189 } else { 190 editable = true; 191 } 192 } 193 194 String output = ""; 195 196 String forward = theField.getDefinition() 197 .getProperty(JahiaFieldDefinitionProperties.FIELD_UPDATE_JSP_FILE_PROP); 198 if ( forward == null ){ 199 forward = Date_Field.JSP_FILE; 200 if ( !editable ){ 201 forward = Date_Field.READONLY_JSP; 202 } 203 } 204 205 if ( editable ){ 206 207 String localSwitchUrl = "ReloadEngine('localswitch" + EngineParams.VALUE_TOKEN + "yes')"; 208 engineMap.put( "localSwitchUrl", localSwitchUrl ); 209 210 String dataSourceConnectUrl = ""; 211 JahiaEngine dsEngine = (JahiaEngine) EnginesRegistry.getInstance().getEngine( "selectdatasource" ); 212 dataSourceConnectUrl = dsEngine.renderLink( jParams, theField ); 213 engineMap.put( "dataSourceConnectUrl", dataSourceConnectUrl ); 214 215 String dataSourceIDUrl = ""; 216 dsEngine = (JahiaEngine) EnginesRegistry.getInstance().getEngine( "viewdatasourceid" ); 217 dataSourceIDUrl = dsEngine.renderLink( jParams, theField ); 218 engineMap.put( "dataSourceIDUrl", dataSourceIDUrl ); 219 220 theField.setValue( FormDataManager.getInstance().formEncode(theField.getValue()) ); 221 output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, forward ); 222 } else { 224 output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, forward ); 225 } 226 227 engineMap.put( "fieldForm", output ); 228 229 return true; 230 } 232 233 } | Popular Tags |