1 4 package org.jahia.data.fields; 5 6 import java.util.ArrayList ; 7 import java.util.HashMap ; 8 import java.util.Properties ; 9 10 import org.jahia.data.FormDataManager; 11 import org.jahia.data.ConnectionTypes; 12 import org.jahia.data.files.JahiaFileField; 13 import org.jahia.data.files.JahiaFile; 14 import org.jahia.exceptions.JahiaException; 15 import org.jahia.params.ParamBean; 16 import org.jahia.registries.ServicesRegistry; 17 import org.jahia.services.fields.ContentField; 18 import org.jahia.services.fields.ContentFileField; 19 import org.jahia.services.filemanager.JahiaFileFieldsManager; 20 import org.jahia.services.version.EntryLoadRequest; 21 import org.jahia.services.version.EntrySaveRequest; 22 import org.jahia.services.webdav.JahiaWebdavBaseService; 23 import org.jahia.services.webdav.DAVFileAccess; 24 import org.jahia.sharing.JahiaDataSourceManager; 25 26 public class JahiaFileFieldWrapper extends JahiaField implements JahiaAllowApplyChangeToAllLangField 27 { 28 29 private static org.apache.log4j.Logger logger = 30 org.apache.log4j.Logger.getLogger(JahiaFileFieldWrapper.class); 31 32 37 public JahiaFileFieldWrapper(Integer ID, 38 Integer jahiaID, 39 Integer pageID, 40 Integer ctnid, 41 Integer fieldDefID, 42 Integer fieldType, 43 Integer connectType, 44 String fieldValue, 45 Integer rank, 46 Integer aclID, 47 Integer versionID, 48 Integer versionStatus, 49 String languageCode ) 50 { 51 super(ID, jahiaID, pageID, ctnid, fieldDefID, fieldType, connectType, 52 fieldValue, rank, aclID, versionID, versionStatus, languageCode); 53 54 if ( isShared() ){ 55 this.languageCode = ContentField.SHARED_LANGUAGE; 56 } 57 58 59 JahiaFileField fField; 60 JahiaFile file = new JahiaFile(-1, -1, "", "", "", 0, 0, "", "", "", String.valueOf(ServicesRegistry.getInstance() 71 .getJahiaVersionService().getCurrentVersionID()), JahiaFile.STATE_ACTIVE ); 73 fField = new JahiaFileField(file,new Properties ()); 74 fField.setID(-1); 75 fField.setDownloadUrl("#"); 76 setObject(fField); 77 } 79 80 public void load(int loadFlag, ParamBean jParams) 81 throws JahiaException { 82 ContentFileField contentFileField = 83 (ContentFileField)ContentFileField.getField(getID()); 84 String val = contentFileField.getValue(jParams); 85 this.setValue(FormDataManager.getInstance().htmlEncode(val)); 86 87 switch (this.getConnectType()) 88 { 89 case (ConnectionTypes.LOCAL) : 90 this.setRawValue(this.getValue()); 93 this.setValue(FormDataManager.getInstance().htmlEncode(this.getValue())); 94 break; 96 case (ConnectionTypes.DATASOURCE) : 97 if ((loadFlag & LoadFlags.DATASOURCE) != 0) { 98 this.setValue( JahiaDataSourceManager.getInstance(). 99 fields().getInstance().getRemoteFieldValue( 100 this.getValue() )); 101 } 102 } 103 104 105 JahiaFileField fField = null; 106 if (jParams != null) { 107 if (jParams.getSiteID() == this.getJahiaID()) { 108 fField = JahiaWebdavBaseService.getInstance().getJahiaFileField(jParams, val); 109 } else { 110 fField = JahiaWebdavBaseService.getInstance().getJahiaFileField(jParams, ServicesRegistry.getInstance().getJahiaSitesService().getSite(getSiteID()), jParams.getUser(), val); 111 } 112 } else { 113 DAVFileAccess davAccess = JahiaWebdavBaseService.getInstance().getDAVFileAccess(ServicesRegistry.getInstance().getJahiaSitesService().getSite(getSiteID()), val); 114 if ( davAccess != null ){ 115 fField = davAccess.getJahiaFileField(); 116 } 117 } 118 this.setObject(fField); 119 120 } 121 122 161 162 163 169 public boolean save(ParamBean jParams) 170 throws JahiaException { 171 172 String pagePrefix = getID()+";"; 173 ContentFileField contentFileField = 174 (ContentFileField)ContentFileField.getField(getID()); 175 176 String val = contentFileField.getValue(jParams); 177 if (val == null) val =""; 178 193 JahiaFileField fField = (JahiaFileField)this.getObject(); 194 195 if ( this.getWorkflowState() == EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){ 196 fField.setID(-1); 199 } 200 EntrySaveRequest saveRequest = 202 new EntrySaveRequest(jParams.getUser(), this.getLanguageCode()); 203 contentFileField.setFile(fField, saveRequest); 204 ServicesRegistry.getInstance().getJahiaSearchService().indexField(getID(),true, jParams, true); 205 206 217 return true; 218 } 219 220 244 245 251 public void delete(ParamBean jParams) 252 throws JahiaException { 253 254 String pagePrefix = getID()+";"; 255 JahiaFileField fField = (JahiaFileField)this.getObject(); 256 DAVFileAccess objectDav = JahiaWebdavBaseService.getInstance().getDAVFileAccess(jParams, fField.getRealName()); 257 if (objectDav.hasRevisions()) { 258 String list = (String ) objectDav.getProperty("http://www.jahia.org/", "fieldsinuse"); 259 if (list == null) { 260 list = ""; 261 } 262 if (!list.startsWith(pagePrefix) && list.indexOf(";"+pagePrefix)==-1) { 263 objectDav.setProperty("http://www.jahia.org/", "fieldsinuse", list + pagePrefix, true); 264 } 265 } 266 } 267 268 281 public String getEngineName() 282 { 283 return "org.jahia.engines.shared.DAVFile_Field"; 284 } 286 287 public String getFieldContent4Ranking() 288 { 289 String fieldInfo = ""; 290 JahiaFileField fle = (JahiaFileField)this.getObject(); 291 if (fle != null) 292 { 293 fieldInfo = fle.getFileFieldTitle(); 294 } 295 else 296 { 297 fieldInfo = this.getValue().substring(getValue().lastIndexOf('/')+1); 298 } 299 return fieldInfo; 300 } 301 302 public String getIconNameOff() 303 { 304 return "file"; 305 } 306 307 public String getIconNameOn() 308 { 309 return "file_on"; 310 } 311 312 public JahiaField cloneField(int newctnid, int newPageID, int clonedAclID, boolean childrenCloned) 313 throws JahiaException 314 { 315 JahiaField clonedField = ServicesRegistry.getInstance().getJahiaFieldService(). 316 createJahiaField(0, this.getJahiaID(), 317 newPageID, newctnid, 318 this.getFieldDefID(), this.getType(), 319 this.getConnectType(), 320 this.getValue(), this.getRank(), 321 clonedAclID, this.getVersionID(), this.getWorkflowState(), 322 this.getLanguageCode()); 323 324 if (clonedField == null) 326 { 327 throw new JahiaException ("Could not clone field.", 328 "Could not instanciate a new JahiaField object.", 329 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY); 330 } 331 332 clonedField.setRawValue(this.getRawValue()); 333 clonedField.setProperties( (Properties )(this.getProperties()).clone() ); 335 336 int theFieldID = ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement( "jahia_fields_data" ); 337 clonedField.setID(theFieldID); 338 clonedField.setObject(getObject()); 339 340 boolean flag = JahiaFileFieldsManager.getInstance().copyFileField(this.getID(), clonedField.getID(), clonedField.getPageID()); 341 342 return clonedField; 343 } 344 345 public Object clone() 346 { 347 Object objItem = this.getObject(); 348 JahiaFileFieldWrapper filef = new JahiaFileFieldWrapper (new Integer (ID), new Integer (jahiaID), 349 new Integer (pageID), 350 new Integer (ctnid), 351 new Integer (fieldDefID), 352 new Integer (fieldType), 353 new Integer (connectType), 354 fieldValue, new Integer (rank), 355 new Integer (aclID), 356 new Integer (versionID), 357 new Integer (workflowState), 358 new String (getLanguageCode()) ); 359 filef.setObject(objItem); 360 return filef; 361 } 362 363 366 public boolean isShared (){ 367 return false; 368 } 369 370 376 public void copyValueInAnotherLanguage (JahiaField aField,ParamBean jParams) 377 throws JahiaException { 378 if ( aField == null ){ 379 return; 380 } 381 if ( this.getLanguageCode() != aField.getLanguageCode() ){ 383 aField.setValue(this.getValue()); aField.setRawValue(this.getRawValue()); 385 JahiaFileField fField = 386 (JahiaFileField)this.getObject(); 387 aField.setObject(fField); 388 } 389 } 390 391 399 public void copyValueToAnotherField (JahiaField aField,ParamBean jParams) 400 throws JahiaException{ 401 402 if ( aField == null ){ 403 return; 404 } 405 aField.setValue(this.getValue()); aField.setRawValue(this.getRawValue()); 408 JahiaFileField fField = 409 (JahiaFileField)this.getObject(); 410 aField.setObject(fField); 411 } 412 413 418 public HashMap getValuesForSearch () throws JahiaException { 419 420 HashMap values = new HashMap (); 421 ArrayList arrayList = new ArrayList (); 422 JahiaFileField fField = (JahiaFileField)this.getObject(); 423 if ( fField != null ){ 424 arrayList.add(fField); 425 values.put(this.getLanguageCode(),arrayList.toArray()); 426 } else { 427 String [] vals = new String [0]; 428 arrayList.add(""); 429 vals = (String [])arrayList.toArray(vals); 430 values.put(this.getLanguageCode(),vals); 431 } 432 return values; 433 } 434 435 436 } 437 | Popular Tags |