1 42 43 44 package org.jahia.data.fields; 45 46 import java.util.ArrayList ; 47 import java.util.HashMap ; 48 import java.util.Properties ; 49 import java.io.Serializable ; 50 51 import org.jahia.exceptions.JahiaException; 52 import org.jahia.params.ParamBean; 53 import org.jahia.registries.JahiaFieldDefinitionsRegistry; 54 import org.jahia.registries.ServicesRegistry; 55 import org.jahia.services.acl.ACLNotFoundException; 56 import org.jahia.services.acl.ACLResourceInterface; 57 import org.jahia.services.acl.JahiaBaseACL; 58 import org.jahia.services.fields.ContentField; 59 import org.jahia.services.usermanager.JahiaUser; 60 import org.jahia.services.version.EntryLoadRequest; 61 import org.jahia.utils.textdiff.HunkTextDiffVisitor; 62 import org.jahia.content.PropertiesInterface; 63 import org.jahia.utils.JahiaTools; 64 65 public abstract class JahiaField implements Cloneable , Serializable , ACLResourceInterface, PropertiesInterface { 66 67 public static final String MULTIPLE_VALUES_SEP = "$$$"; 68 69 protected int ID; 70 protected int jahiaID; 71 protected int pageID; 72 protected int ctnid; 73 protected int fieldDefID; 74 protected int fieldType; 75 protected int connectType; 76 protected String fieldValue = ""; 77 protected String fieldRawValue = ""; 78 protected int rank; 79 protected int aclID; 80 protected int versionID; 81 protected int workflowState; 82 protected String languageCode; 83 protected Object objectItem; protected Properties properties; 85 private boolean propertiesLoaded = false; 86 protected boolean hasChanged = false; 87 88 protected JahiaField( Integer ID, 89 Integer jahiaID, 90 Integer pageID, 91 Integer ctnid, 92 Integer fieldDefID, 93 Integer fieldType, 94 Integer connectType, 95 String fieldValue, 96 Integer rank, 97 Integer aclID, 98 Integer versionID, 99 Integer workflowState, 100 String languageCode) 101 { 102 setID( ID.intValue() ); 103 this.jahiaID = jahiaID.intValue(); 104 this.pageID = pageID.intValue(); 105 this.fieldDefID = fieldDefID.intValue(); 106 this.ctnid = ctnid.intValue(); 107 this.fieldType = fieldType.intValue(); 108 this.connectType = connectType.intValue(); 109 this.fieldValue = fieldValue; 110 this.rank = rank.intValue(); 111 this.aclID = aclID.intValue(); 112 this.versionID = versionID.intValue(); 113 this.workflowState = workflowState.intValue(); 114 this.languageCode = languageCode; 115 this.properties = null; 116 } 117 118 123 public int getID() { return ID; } 124 public int getJahiaID() { return jahiaID; } 125 public int getSiteID() { return jahiaID; } public int getPageID() { return pageID; } 127 public int getFieldDefID() { return fieldDefID; } 128 public int getctnid() { return ctnid; } 129 public int getType() { return fieldType; } 130 public int getConnectType() { return connectType; } 131 public int getRank() { return rank; } 132 public final int getAclID() { return aclID; } 133 public int getVersionID() { return versionID; } 134 public int getWorkflowState() { return workflowState; } 135 public boolean hasChanged() { return hasChanged; } 136 137 public String getLanguageCode() { 138 return languageCode; 139 } 140 141 public final JahiaBaseACL getACL() { 142 JahiaBaseACL acl = null; 143 try { 144 acl = new JahiaBaseACL(getAclID()); 145 } catch ( Throwable t ) { 146 t.printStackTrace(); 147 } 148 return acl; 149 } 150 151 public Object getObject() { return objectItem; } 152 153 public void setID( int ID ) { this.ID = ID; } 154 public void setAclID( int aclID ) { this.aclID = aclID; } 155 public void setType( int fieldType ) { this.fieldType = fieldType; } 156 157 162 public void setValue( String fieldValue ) { registerChange(); this.fieldValue = fieldValue; } 163 public void setctnid( int ctnid ) { registerChange(); this.ctnid = ctnid; } 164 public void setFieldDefID(int fieldDefID) { registerChange(); this.fieldDefID = fieldDefID; } 165 166 public void setConnectType( int connectType ) { registerChange(); this.connectType = connectType; } 167 public void setObject( Object objectItem ) { registerChange(); this.objectItem = objectItem; } 168 170 public String getValue() { 171 return fieldValue; 172 } 173 174 179 public String [] getValues() { 180 return JahiaTools.getTokens(getValue(),JahiaField.MULTIPLE_VALUES_SEP); 181 } 182 183 188 public String getRawValue() { 189 return fieldRawValue; 190 } 191 192 public void setRawValue(String fieldRawValue) { 193 registerChange(); 194 this.fieldRawValue = fieldRawValue; 195 } 196 197 198 public Properties getProperties () { 199 if (propertiesLoaded) { 200 return properties; 201 } else { 202 return null; 203 } 204 } 205 206 public void setProperties (Properties properties) { 207 this.properties = properties; 208 propertiesLoaded = true; 209 } 211 216 222 public abstract void load(int loadFlag, ParamBean jParams) 223 throws JahiaException; 224 225 231 public abstract boolean save(ParamBean jParams) 232 throws JahiaException; 233 234 240 public abstract void delete(ParamBean jParams) 241 throws JahiaException; 242 243 248 public abstract String getEngineName(); 249 250 256 public abstract String getFieldContent4Ranking() 257 throws JahiaException; 258 259 263 public abstract String getIconNameOff(); 264 265 269 public abstract String getIconNameOn(); 270 271 272 277 public abstract JahiaField cloneField(int newctnid, int newPageID, int clonedAclID, boolean childrenCloned) 278 throws JahiaException; 279 280 281 284 public abstract Object clone(); 285 290 292 293 297 public JahiaFieldDefinition getDefinition() 298 throws JahiaException 299 { 300 JahiaFieldDefinition theDef = JahiaFieldDefinitionsRegistry.getInstance().getDefinition( fieldDefID ); 301 if (theDef != null) { 302 return theDef; 303 } else { 304 String msg = "JahiaField definition " + fieldDefID + " not found in definitions registry !"; 305 throw new JahiaException( "Synchronisation error in database", 306 msg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY ); 307 } 308 } 310 311 312 322 public final boolean checkReadAccess (JahiaUser user) 323 { 324 return checkAccess (user, JahiaBaseACL.READ_RIGHTS); 325 } 326 327 337 public final boolean checkWriteAccess (JahiaUser user) 338 { 339 return checkAccess (user, JahiaBaseACL.WRITE_RIGHTS); 340 } 341 342 343 353 public final boolean checkAdminAccess (JahiaUser user) 354 { 355 return checkAccess (user, JahiaBaseACL.ADMIN_RIGHTS); 356 } 357 358 359 361 private boolean checkAccess (JahiaUser user, int permission) 363 { 364 if (user == null) { 365 return false; 366 } 367 368 371 boolean result = false; 372 try 373 { 374 JahiaBaseACL fieldACL = new JahiaBaseACL (aclID); 376 377 result = fieldACL.getPermission (user, permission); 379 380 fieldACL = null; 382 } 383 catch (ACLNotFoundException ex) { 384 } 387 catch (JahiaException ex) { 388 logger.error("JahiaException caught in checkAccess.", ex); 389 } 390 391 return result; 397 } 398 399 400 401 402 407 public String getAnchor(){ 408 409 StringBuffer anchor = new StringBuffer ("<A NAME="); 410 anchor.append(this.getID()); 411 anchor.append("></A>"); 412 413 return anchor.toString(); 414 } 415 416 419 public abstract boolean isShared (); 420 421 private void registerChange(){ 422 this.hasChanged = true; 423 } 424 425 431 public abstract void copyValueInAnotherLanguage (JahiaField aField,ParamBean jParams) 432 throws JahiaException; 433 434 442 public void copyValueToAnotherField (JahiaField aField,ParamBean jParams) 443 throws JahiaException{ 444 445 aField.setValue(this.getValue()); 446 aField.setRawValue(this.getValue()); 447 aField.setObject(this.getObject()); 448 } 449 450 455 public HashMap getValuesForSearch () throws JahiaException { 456 457 HashMap values = new HashMap (); 458 if ( this.isShared() ){ 459 values.put(ContentField.SHARED_LANGUAGE,this.getValues()); 460 } else { 461 String [] vals = this.getValues(); 462 if ( vals == null ){ 463 vals = new String [0]; 464 ArrayList aList = new ArrayList (); 465 aList.add(""); 466 vals = (String [])aList.toArray(vals); 467 } 468 values.put(this.getLanguageCode(),vals); 469 } 470 return values; 471 } 472 473 public void setVersionID(int versionID){ 474 this.versionID = versionID; 475 } 476 477 public void setWorkflowState(int workflowState){ 478 this.workflowState = workflowState; 479 } 480 481 public void setlanguageCode(String languageCode){ 482 this.languageCode = languageCode; 483 } 484 485 490 public ContentField getContentField() { 491 ContentField contentField = null; 492 try { 493 contentField = ContentField.getField(ID); 494 } catch (JahiaException je) { 495 logger.debug(je); 496 } 497 return contentField; 498 } 499 500 501 507 public String getHighLightDiffValue(ParamBean jParams) { 508 return getHighLightDiffValue(jParams.getDiffVersionID(),jParams); 509 } 510 511 517 public String getHighLightDiffValue(int diffVersionID, ParamBean jParams) { 518 519 if ( diffVersionID == 0 ){ 520 return this.getValue(); 521 } 522 523 String oldValue = this.getValue(); 524 String newValue = ""; 525 String mergedValue = ""; 526 527 try { 528 EntryLoadRequest loadVersion = 529 EntryLoadRequest.getEntryLoadRequest(diffVersionID, 530 this.languageCode); 531 532 JahiaField jahiaField = ServicesRegistry.getInstance() 533 .getJahiaFieldService() 534 .loadField(this.getID(),LoadFlags.ALL, 535 jParams,loadVersion); 536 537 int newValueWorkflowState = this.getWorkflowState(); 538 if ( jahiaField != null ){ 539 newValue = jahiaField.getValue(); 540 newValueWorkflowState = jahiaField.getWorkflowState(); 541 } 542 543 554 555 HunkTextDiffVisitor hunkTextDiffV = null; 557 if ( this.getVersionID() == -1 && newValueWorkflowState==1){ 558 return HunkTextDiffVisitor.getDeletedText(oldValue); 560 } else if ( this.getWorkflowState() < newValueWorkflowState ){ 561 hunkTextDiffV = new HunkTextDiffVisitor(oldValue,newValue); 562 } else { 563 hunkTextDiffV = new HunkTextDiffVisitor(newValue,oldValue); 564 } 565 hunkTextDiffV.highLightDiff(); 566 mergedValue = hunkTextDiffV.getMergedDiffText(); 567 568 575 } catch ( Throwable t ){ 576 t.printStackTrace(); 577 } 578 return mergedValue; 579 } 580 581 582 private static org.apache.log4j.Logger logger = 583 org.apache.log4j.Logger.getLogger(JahiaField.class); 584 585 586 587 } | Popular Tags |