1 13 15 package org.jahia.engines.shared; 16 17 import java.util.Enumeration ; 18 import java.util.HashMap ; 19 import java.util.Hashtable ; 20 import java.util.Vector ; 21 22 import org.apache.log4j.Logger; 23 import org.jahia.data.FormDataManager; 24 import org.jahia.data.containers.JahiaContainer; 25 import org.jahia.data.fields.FieldTypes; 26 import org.jahia.data.fields.JahiaField; 27 import org.jahia.data.fields.LoadFlags; 28 import org.jahia.engines.JahiaEngine; 29 import org.jahia.engines.JahiaEngineTools; 30 import org.jahia.exceptions.JahiaException; 31 import org.jahia.params.ParamBean; 32 import org.jahia.registries.ServicesRegistry; 33 import org.jahia.services.acl.JahiaBaseACL; 34 import org.jahia.services.pages.JahiaPage; 35 36 public class Undefined_Field { 37 38 private static Logger logger = Logger.getLogger(Undefined_Field.class); 39 40 private static Undefined_Field theObject = null; 41 private static final String JSP_FILE = 42 "/jsp/jahia/engines/shared/undefined_field.jsp"; 43 44 49 public static synchronized Undefined_Field getInstance () { 50 if (theObject == null) { 51 theObject = new Undefined_Field(); 52 } 53 return theObject; 54 } 56 65 public boolean handleField (ParamBean jParams, Integer modeInt, 66 HashMap engineMap) 67 throws JahiaException { 68 int mode = modeInt.intValue(); 69 JahiaField theField = (JahiaField) engineMap.get("theField"); 70 switch (mode) { 71 case (JahiaEngine.LOAD_MODE): 72 return composeEngineMap(jParams, engineMap, theField); 73 case (JahiaEngine.UPDATE_MODE): 74 return getFormData(jParams, engineMap, theField); 75 case (JahiaEngine.SAVE_MODE): 76 return saveData(jParams, engineMap, theField); 77 } 78 return false; 79 } 81 90 private boolean getFormData (ParamBean jParams, HashMap engineMap, 91 JahiaField theField) 92 throws JahiaException { 93 boolean out = true; 94 String choosedType = ""; 95 96 String fieldTypeStr = jParams.getRequest().getParameter("_" + 97 new Integer (theField.getID()).toString()); 98 choosedType += jParams.getRequest().getParameter("choosedType"); 99 100 logger.debug("fieldType: " + fieldTypeStr); 101 if (choosedType.equals("yes") && (fieldTypeStr != null)) { 102 try { 103 int fieldType = Integer.parseInt(fieldTypeStr); 104 JahiaField f = ServicesRegistry.getInstance(). 105 getJahiaFieldService(). 106 createJahiaField(theField.getID(), 107 theField.getJahiaID(), 108 theField.getPageID(), 109 theField.getctnid(), 110 theField.getFieldDefID(), 111 fieldType, 112 theField.getConnectType(), 113 theField.getValue(), 114 theField.getRank(), 115 theField.getAclID(), 116 theField.getVersionID(), 117 theField.getWorkflowState(), 118 theField.getLanguageCode()); 119 120 String flag = (String ) engineMap.get("updateField"); 121 if (flag == null) { 122 JahiaContainer theContainer = (JahiaContainer) engineMap. 123 get("theContainer"); 124 theContainer.setField(f); 125 engineMap.put("theContainer", theContainer); 126 } 127 128 135 theField = f; 136 engineMap.put("theField", f); 138 } catch (NumberFormatException nfe) { 139 theField.setType(0); 140 out = false; 141 } 142 } 143 if (!choosedType.equals("yes")) { 144 theField.setType(0); 145 } 146 return out; 147 } 149 158 private boolean saveData (ParamBean jParams, HashMap engineMap, 159 JahiaField theField) 160 throws JahiaException { 161 return theField.save(jParams); 162 } 164 173 private boolean composeEngineMap (ParamBean jParams, HashMap engineMap, 174 JahiaField theField) 175 throws JahiaException { 176 177 boolean editable = false; 178 JahiaContainer theContainer = (JahiaContainer) engineMap.get( 179 "theContainer"); 180 if (theContainer == null) { 181 editable = true; 183 } else { 184 HashMap ctnListFieldAcls = (HashMap ) engineMap 185 .get("ctnListFieldAcls"); 186 187 if (theContainer.getListID() != 0 && ctnListFieldAcls != null 188 && ctnListFieldAcls.size() > 0) { 189 JahiaBaseACL acl = JahiaEngineTools.getCtnListFieldACL( 190 ctnListFieldAcls, theField.getID()); 191 if (acl != null) { 192 editable = acl.getPermission(jParams.getUser(), 193 JahiaBaseACL.WRITE_RIGHTS, true); 194 } 195 } else { 196 editable = true; 197 } 198 } 199 200 String output = ""; 201 202 if (editable) { 203 204 Hashtable typesList = FieldTypes.getInstance().getFieldTypes(); 206 Hashtable typesList2 = new Hashtable (); 207 int pageID = theField.getPageID(); 209 JahiaPage thePage = ServicesRegistry.getInstance(). 210 getJahiaPageService().lookupPage(pageID, 211 jParams); 212 String defValue = theField.getDefinition().getDefaultValue(thePage. 213 getPageTemplateID()); 214 215 try { 216 if (defValue.toUpperCase().indexOf("JAHIA_MASKTYPE") != -1) { 217 defValue = defValue.substring(defValue.indexOf("[") + 1, 218 defValue.indexOf("]")); 219 } 221 int defValueInt = Integer.parseInt(defValue); 222 if (defValueInt == -1) { 223 typesList2 = typesList; 224 } else { 225 Enumeration keys = typesList.keys(); 226 Hashtable loadFlags = LoadFlags.getInstance().getLoadFlags(); 227 int loadFlag = 0; 228 int i = -1; 229 while (keys.hasMoreElements()) { 230 i++; 231 String theKey = (String ) keys.nextElement(); 232 loadFlag = ( (Integer ) loadFlags.get( typesList.get(theKey)) != null) 233 ? 234 ( (Integer ) loadFlags.get( typesList. 235 get(theKey))).intValue() : 0; 236 boolean flag = ( (defValueInt & loadFlag) == loadFlag) ? true : false; 237 if (flag) { 239 typesList2.put(theKey, 240 typesList.get(theKey)); 241 } 242 } 243 } 244 245 } catch (NumberFormatException e) { 246 typesList2.put("SmallText", typesList.get("SmallText")); 247 typesList2.put("BigText", typesList.get("BigText")); 248 typesList2.put("Application", 249 typesList.get("Application")); 250 } 251 252 engineMap.put("typesList", typesList2); 253 254 Vector categoryList = new Vector (); 257 engineMap.put("categoryList", categoryList); 258 259 theField.setValue(FormDataManager.getInstance().formEncode(theField. 260 getValue())); 261 output = ServicesRegistry.getInstance().getJahiaFetcherService(). 262 fetchServlet(jParams, JSP_FILE); 263 } else { 264 output = ServicesRegistry.getInstance().getJahiaFetcherService(). 265 fetchServlet(jParams, JahiaEngine.NOT_EDITABLE_FIELD); 266 } 267 268 engineMap.put("fieldForm", output); 269 return true; 270 271 } 273 } | Popular Tags |