1 13 16 package org.jahia.data.fields; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.HashMap ; 21 import java.util.HashSet ; 22 import java.util.Hashtable ; 23 import java.util.Iterator ; 24 import java.util.Locale ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 import java.util.Set ; 28 29 import org.jahia.data.JahiaData; 30 import org.jahia.exceptions.JahiaException; 31 import org.jahia.registries.JahiaFieldDefinitionsRegistry; 32 import org.jahia.registries.ServicesRegistry; 33 import org.jahia.services.fields.ContentField; 34 import org.jahia.services.pages.JahiaPage; 35 import org.jahia.services.version.EntryLoadRequest; 36 37 public class JahiaFieldSet implements Map { 38 39 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(JahiaFieldSet.class); 40 41 private JahiaData jData; 42 43 46 private Map fields; 47 48 51 private Set declaredFields; 52 53 56 private Hashtable declaredFieldDefProps; 57 58 64 private Set absoluteFieldAccesses = new HashSet (); 65 66 67 72 public JahiaFieldSet( JahiaData jData ) 73 throws JahiaException 74 { 75 this.jData = jData; 76 this.fields = new HashMap (); 77 this.declaredFields = new HashSet (); 78 this.declaredFieldDefProps = new Hashtable (); 79 80 } 82 83 87 public void addField( JahiaField theField ) 88 throws JahiaException 89 { 90 fields.put( theField.getDefinition().getName(), theField ); 91 } 93 94 98 public void removeField( JahiaField theField ) 99 throws JahiaException 100 { 101 fields.remove( theField.getDefinition().getName() ); 102 } 104 105 109 public void declareField( String fieldName, String fieldTitle, int fieldType, String defaultValue ) 110 throws JahiaException 111 { 112 113 synchronized ( JahiaFieldSet.class ) { 114 115 logger.debug( "fieldName="+fieldName+", defValue=" + defaultValue ); 116 117 if (checkDeclared(fieldName) || 120 jData.containers().checkDeclared(fieldName) || 121 fieldName.length() == 0 || fieldTitle.length() == 0) { 122 String errorMsg = "Field already declared or has a null name - title : " + fieldName; 124 logger.debug( ".declareField," + errorMsg + " -> BAILING OUT" ); 125 throw new JahiaException( errorMsg, errorMsg, JahiaException.TEMPLATE_ERROR, 126 JahiaException.CRITICAL_SEVERITY ); 127 } else { 128 JahiaFieldDefinitionsRegistry fldDefRegistry = JahiaFieldDefinitionsRegistry.getInstance(); 131 JahiaFieldDefinition aDef = fldDefRegistry.getDefinition( jData.params().getSiteID(),fieldName ); 132 int pageDefID = jData.params().getPage().getPageTemplateID(); 133 if (aDef != null) { 134 135 Properties props = aDef.getProperties(); 136 Properties declaredProps = (Properties )this.declaredFieldDefProps.get(fieldName); 137 138 boolean propsHasChanged = ( 139 (props == null && (declaredProps != null && declaredProps.size()>0)) || 140 ( (props != null && props.size()>0) && declaredProps == null ) 141 || ( props != null && declaredProps != null && !props.equals(declaredProps) ) ); 142 143 if ( aDef.getType(pageDefID) == -1 ){ 146 logger.debug(".declareField, Create new field sub def for pageDef=" + pageDefID + " and fieldName=" + fieldName + ""); 149 aDef.setType( fieldType, pageDefID ); 150 aDef.setTitle( fieldTitle, pageDefID ); 151 aDef.setDefaultValue( defaultValue, pageDefID ); 152 aDef.setProperties((Properties )this.declaredFieldDefProps.get(fieldName)); 153 fldDefRegistry.setDefinition( aDef ); 154 155 } else if (!((aDef.getTitle(pageDefID).equals(fieldTitle)) && 156 (aDef.getType(pageDefID) == fieldType) && 157 (aDef.getDefaultValue(pageDefID).equals(defaultValue)) && 158 !propsHasChanged)) { 159 160 logger.debug(".declareField, Field Data for field " + fieldName + 161 " is not synchronized between template and database !" ); 162 163 if ( !aDef.getTitle(pageDefID).equals(fieldTitle) ){ 164 logger.debug(".declareField, Title: " + 165 aDef.getTitle(pageDefID) + 166 " is not equal to " + fieldTitle + "" ); 167 } 168 169 if ( aDef.getType(pageDefID) != fieldType ){ 170 logger.debug(".declareField, Type: " + 171 aDef.getType(pageDefID) + 172 " is not equal to " + fieldType + ""); 173 } 174 175 if ( !aDef.getDefaultValue(pageDefID).equals(defaultValue) ){ 176 if (defaultValue.length() > 250) { 178 logger.debug(".declareField, Default value is too big to fit in database."); 179 } else { 180 logger.debug(".declareField, Default Value: " + 181 aDef.getDefaultValue(pageDefID) + 182 "\n is not equal to \n" + defaultValue + ""); 183 } 184 } 185 if ((aDef.getType(pageDefID) != fieldType) && (aDef.getType(pageDefID) != -1)) { 190 String errorMsg = "Cannot change field type because field data already exists : " + fieldName; 194 logger.debug( ".declareField, " + errorMsg + " -> BAILING OUT" ); 195 throw new JahiaException( errorMsg, errorMsg, JahiaException.TEMPLATE_ERROR, 196 JahiaException.CRITICAL_SEVERITY ); 197 198 } else { 199 aDef.setType( fieldType, pageDefID ); 206 aDef.setTitle( fieldTitle, pageDefID ); 207 aDef.setDefaultValue( defaultValue, pageDefID ); 208 aDef.setProperties((Properties )this.declaredFieldDefProps.get(fieldName)); 209 fldDefRegistry.setDefinition( aDef ); 210 } 211 } 212 } else { 213 Hashtable subDefs = new Hashtable (); 222 subDefs.put( new Integer (pageDefID), new JahiaFieldSubDefinition( 0, 0, pageDefID, fieldTitle, fieldType, defaultValue ) ); 223 aDef = new JahiaFieldDefinition( 0, jData.params().getPage().getJahiaID(), 224 fieldName, subDefs ); 225 226 aDef.setProperties((Properties )this.declaredFieldDefProps.get(fieldName)); 227 228 fldDefRegistry.setDefinition( aDef ); 229 230 } 231 232 JahiaField theField = (JahiaField) fields.get( fieldName ); 234 if (theField == null) { 239 240 EntryLoadRequest currentEntryLoadRequest = jData.params().getEntryLoadRequest(); 242 243 int fieldID = ServicesRegistry.getInstance().getJahiaFieldService() 244 .getFieldID( fieldName, jData.params().getPageID() ); 245 246 Locale currentLocale = jData.params().getEntryLoadRequest().getFirstLocale(true); 247 248 boolean fieldCurrentlyExists = false; 249 if ((fieldID != -1)) { 250 ContentField curField = ContentField.getField(fieldID); 252 if ((curField.hasActiveEntries()) || 253 (curField.hasStagingEntries())) { 254 fieldCurrentlyExists = true; 255 } else { 256 261 logger.debug("Field " + fieldID + 262 " found in versioned or deleted state but not active or staged, creating new staged..."); 263 ContentField.removeFromCache(fieldID); 264 } 265 } 266 267 if ( !fieldCurrentlyExists ) 268 { 269 String fieldDefValue = ""; 272 if ( defaultValue.indexOf("jahia_multivalue") == -1 ){ 273 fieldDefValue = defaultValue; 274 } 275 276 int useFieldID = 0; 277 if (fieldID != -1) { 278 useFieldID = fieldID; 279 } 280 281 JahiaField newField = ServicesRegistry.getInstance(). 282 getJahiaFieldService().createJahiaField( 283 useFieldID, jData.params().getPage().getJahiaID(), 284 jData.params().getPage().getID(),0, 285 aDef.getID(), fieldType, 0, 286 fieldDefValue, 0, 0, 0, 287 EntryLoadRequest.STAGING_WORKFLOW_STATE, 288 currentLocale.toString()); 289 290 if (fieldID != -1) { 293 newField.setObject("createFieldWithCurrentID"); 294 } 295 296 ServicesRegistry.getInstance().getJahiaFieldService() 297 .saveField( newField, jData.params().getPage().getAclID(), jData.params() ); 298 299 newField = ServicesRegistry.getInstance().getJahiaFieldService().loadField( newField.getID(), LoadFlags.ALL, jData.params(), currentEntryLoadRequest ); 302 303 if ( newField != null && (currentEntryLoadRequest.getWorkflowState () == newField.getWorkflowState ()) ) { 305 addField( newField ); 306 } 307 logger.debug(".declareField, created new field with name=" + aDef.getName()); 308 } else { 309 JahiaField newField = ServicesRegistry.getInstance() 311 .getJahiaFieldService().loadField( fieldID, LoadFlags.ALL, jData.params(), currentEntryLoadRequest ); 312 if ( newField == null ){ 313 ArrayList siteLocales = jData.params().getSite().getLanguageSettingsAsLocales(true); 315 JahiaContentFieldFacade fieldFacade = 316 new JahiaContentFieldFacade(fieldID,LoadFlags.ALL,jData.params(),siteLocales,true); 317 newField = fieldFacade.getField(jData.params().getEntryLoadRequest(),true); 318 } 319 320 if ( newField != null ) { 322 addField( newField ); 323 } 324 } 325 } 326 327 declaredFields.add( fieldName ); 329 } 330 } 331 332 } 334 341 public void declareFieldDefProp(String fieldName, 342 String propName, 343 String propValue){ 344 if ( fieldName != null && propName != null && propValue != null ){ 345 Properties props = (Properties )this.declaredFieldDefProps.get(fieldName); 346 if ( props == null ){ 347 props = new Properties (); 348 } 349 props.setProperty(propName, propValue); 350 this.declaredFieldDefProps.put(fieldName,props); 351 } 352 } 353 354 355 360 public boolean checkDeclared( String fieldName ) 361 { 362 return declaredFields.contains(fieldName); 363 } 365 366 367 373 public JahiaField getField( String fieldName ) 374 throws JahiaException 375 { 376 if (checkDeclared( fieldName )) 377 { 378 JahiaField theField = (JahiaField) fields.get( fieldName ); 379 return theField; 380 } 381 return null; 382 } 384 385 386 392 public JahiaField getField( int fieldID ) 393 throws JahiaException 394 { 395 Iterator fieldIter = fields.values().iterator(); 396 while (fieldIter.hasNext()) { 397 JahiaField aField = (JahiaField) fieldIter.next(); 398 if (aField.getID() == fieldID) { 399 return aField; 400 } 401 } 402 return null; 403 } 405 406 407 418 public JahiaField getAbsoluteField( String fieldName, int pageID ) 419 throws JahiaException 420 { 421 logger.debug("for field " + fieldName ); 422 int fieldID = ServicesRegistry.getInstance().getJahiaFieldService( 423 ).getFieldID( fieldName, pageID ); 424 JahiaField theField = null; 425 if (fieldID != -1) 426 { 427 absoluteFieldAccesses.add(new Integer (fieldID)); 429 430 theField = getField(fieldID); 432 if ( theField == null ) 433 { 434 logger.debug("Field not in cache " + fieldID ); 435 436 theField = ServicesRegistry.getInstance() 437 .getJahiaFieldService() 438 .loadField( fieldID, LoadFlags.ALL, jData.params() ); 439 if ( theField == null ){ 440 ArrayList siteLocales = jData.params().getSite().getLanguageSettingsAsLocales(true); 442 JahiaContentFieldFacade fieldFacade = 443 new JahiaContentFieldFacade(fieldID,LoadFlags.ALL,jData.params(),siteLocales,true); 444 theField = fieldFacade.getField(jData.params().getEntryLoadRequest(),true); 445 } 446 } 447 } 448 if ( theField != null ){ 449 logger.debug("Returned field " + fieldID + " value " + theField.getValue()); 450 451 return theField; 452 } else { 453 JahiaFieldDefinition fieldDef = JahiaFieldDefinitionsRegistry.getInstance(). 455 getDefinition( jData.params().getSiteID(), fieldName ); 456 JahiaPage sourcePage = ServicesRegistry.getInstance().getJahiaPageService(). 457 lookupPage( pageID, jData.params() ); 458 if (sourcePage != null) { 459 int pageDefID = sourcePage.getPageTemplateID(); 461 if (fieldDef != null) { 462 int fieldType = fieldDef.getType(pageDefID); 464 if (fieldType != -1) { 465 JahiaField fakeField = ServicesRegistry.getInstance(). 471 getJahiaFieldService().createJahiaField( 472 0, fieldDef.getJahiaID(), pageID, 0, 473 fieldDef.getID(), fieldType, 0, 474 fieldDef.getDefaultValue(fieldDef.getID()), 475 0, 0); 476 addField( fakeField ); 477 478 absoluteFieldAccesses.add(new Integer (fakeField.getID())); 479 return fakeField; 480 } 481 } 482 } 483 return null; 484 } 485 } 487 495 public Set getAbsoluteFieldAccesses() { 496 return absoluteFieldAccesses; 497 } 498 499 502 503 public boolean equals(Object o) { 504 return fields.equals(o); 505 } 506 507 public int hashCode() { 508 return fields.hashCode(); 509 } 510 511 public Set entrySet () { 512 return fields.entrySet(); 513 } 514 515 public Collection values () { 516 return fields.values(); 517 } 518 519 public Set keySet () { 520 return fields.keySet(); 521 } 522 523 public void clear () 524 throws UnsupportedOperationException { 525 throw new UnsupportedOperationException ( 526 "Jahia field collection is read-only!"); 527 } 528 529 public void putAll (Map t) 530 throws UnsupportedOperationException { 531 throw new UnsupportedOperationException ( 532 "Jahia field collection is read-only!"); 533 } 534 535 public Object remove (Object key) 536 throws UnsupportedOperationException { 537 throw new UnsupportedOperationException ( 538 "Jahia field collection is read-only!"); 539 } 540 541 public Object put (Object key, 542 Object value) 543 throws UnsupportedOperationException { 544 throw new UnsupportedOperationException ( 545 "Jahia field collection is read-only!"); 546 } 547 548 public Object get (Object key) { 549 return fields.get(key); 550 } 551 552 public boolean containsValue (Object value) { 553 return fields.containsValue(value); 554 } 555 556 public boolean containsKey(Object key) { 557 return fields.containsKey(key); 558 } 559 560 public boolean isEmpty() { 561 return fields.isEmpty(); 562 } 563 564 public int size() { 565 return fields.size(); 566 } 567 568 569 } | Popular Tags |