1 13 package org.jahia.services.fields; 14 15 import org.jahia.data.ConnectionTypes; 16 import org.jahia.data.files.JahiaFileField; 17 import org.jahia.exceptions.JahiaException; 18 import org.jahia.params.ParamBean; 19 import org.jahia.registries.ServicesRegistry; 20 import org.jahia.services.filemanager.Filemanager; 21 import org.jahia.services.filemanager.JahiaFileFieldsManager; 22 import org.jahia.services.filemanager.JahiaFilemanagerService; 23 import org.jahia.services.version.*; 24 import org.jahia.services.webdav.DAVFileAccess; 25 import org.jahia.services.webdav.JahiaWebdavBaseService; 26 import org.jahia.utils.Base64; 27 import org.jahia.utils.xml.XMLSerializationOptions; 28 import org.jahia.utils.xml.XmlWriter; 29 30 import java.io.*; 31 import java.util.Hashtable ; 32 import java.util.List ; 33 import java.util.Set ; 34 import java.util.Vector ; 35 36 37 public class ContentFileField extends ContentField { 38 private static org.apache.log4j.Logger logger = 39 org.apache.log4j.Logger.getLogger (ContentFileField.class); 40 41 protected ContentFileField (Integer ID, 42 Integer jahiaID, 43 Integer pageID, 44 Integer ctnid, 45 Integer fieldDefID, 46 Integer fieldType, 47 Integer connectType, 48 Integer aclID, 49 Vector activeAndStagingEntryStates, 50 Hashtable activeAndStagedDBValues) { 51 super (ID.intValue (), jahiaID.intValue (), pageID.intValue (), 52 ctnid.intValue (), fieldDefID.intValue (), fieldType.intValue (), 53 connectType.intValue (), aclID.intValue (), activeAndStagingEntryStates, 54 activeAndStagedDBValues); 55 } 56 57 58 public static synchronized ContentFileField createFile (int siteID, 59 int pageID, int containerID, 60 int fieldDefID, int parentAclID, 61 int aclID, 62 JahiaFileField fField, 63 ParamBean jParams) 64 throws JahiaException { 65 ContentFileField result = 66 (ContentFileField) ContentField.createField (siteID, pageID, 67 containerID, fieldDefID, 68 ContentFieldTypes.FILE, 69 ConnectionTypes.LOCAL, 70 parentAclID, aclID); 71 72 EntrySaveRequest saveRequest = 73 new EntrySaveRequest (jParams.getUser (), 74 jParams.getLocale ().toString ()); 75 76 result.setFile (fField, saveRequest); 77 return result; 78 } 79 80 88 public String getValue (ParamBean jParams, 89 ContentObjectEntryState entryState) 90 throws JahiaException { 91 return getDBValue (entryState); 92 } 93 94 98 public void setFile (JahiaFileField fField, EntrySaveRequest saveRequest) 99 throws JahiaException { 100 if (fField == null) { 101 return; 102 } 103 preSet (fField.getRealName (), saveRequest); 105 } 106 107 116 public String getValueForSearch (ParamBean jParams, 117 ContentObjectEntryState entryState) 118 throws JahiaException { 119 return getDBValue (entryState); 120 } 121 122 139 public ActivationTestResults changeEntryState (ContentObjectEntryState fromEntryState, 140 ContentObjectEntryState toEntryState, 141 ParamBean jParams, 142 StateModificationContext stateModifContext) 143 throws JahiaException { 144 DAVFileAccess file = JahiaWebdavBaseService.getInstance ().getDAVFileAccess (jParams, 145 this.getValue (fromEntryState)); 146 147 if (file.isValid () && file.hasRevisions ()) { 148 List usages = JahiaWebdavBaseService.getInstance ().findUsages (file.getPath (), 150 jParams, true); 151 boolean wasLocked = fromEntryState.getWorkflowState () == EntryLoadRequest.ACTIVE_WORKFLOW_STATE || fromEntryState.getWorkflowState () == EntryLoadRequest.WAITING_WORKFLOW_STATE; 153 boolean wasUnlocked = fromEntryState.getWorkflowState () != EntryLoadRequest.ACTIVE_WORKFLOW_STATE && fromEntryState.getWorkflowState () != EntryLoadRequest.WAITING_WORKFLOW_STATE; 154 boolean shouldBeLocked = toEntryState.getWorkflowState () == EntryLoadRequest.ACTIVE_WORKFLOW_STATE || toEntryState.getWorkflowState () == EntryLoadRequest.WAITING_WORKFLOW_STATE; 155 boolean shouldBeUnlocked = toEntryState.getWorkflowState () != EntryLoadRequest.ACTIVE_WORKFLOW_STATE && toEntryState.getWorkflowState () != EntryLoadRequest.WAITING_WORKFLOW_STATE; 156 if (wasLocked && shouldBeUnlocked) { 157 if (usages.size () == 1) { 167 file.unlockFile (true); 168 } 169 } else if (wasUnlocked && shouldBeLocked && jParams.settings().isFileLockOnPublication()) { 172 if (usages.size () == 0) { 177 file.unlockFile (true); 178 file.lockFile (true); 179 } 180 } 185 } 186 187 188 return new ActivationTestResults (); 189 } 190 191 protected ActivationTestResults isContentValidForActivation ( 193 Set languageCodes, 194 ParamBean jParams, 195 StateModificationContext stateModifContext) 196 throws JahiaException { 197 198 return new ActivationTestResults (); 199 } 200 201 210 public void copyEntry (EntryStateable fromEntryState, 211 EntryStateable toEntryState) 212 throws JahiaException { 213 214 super.copyEntry (fromEntryState, toEntryState); 215 216 if ((toEntryState.getWorkflowState () 217 == EntryLoadRequest.STAGING_WORKFLOW_STATE) 218 && (fromEntryState.getWorkflowState () 219 != EntryLoadRequest.STAGING_WORKFLOW_STATE)) { 220 221 String fromValue = this.getDBValue (fromEntryState); 222 try { 223 JahiaFileField fField = JahiaFileFieldsManager.getInstance () 224 .getJahiaFileField (Integer.parseInt (fromValue)); 225 fField.setID (-1); 227 JahiaFileFieldsManager.getInstance ().insertJahiaFileField (fField); 228 EntrySaveRequest saveRequest = 229 new EntrySaveRequest (fromEntryState.getLanguageCode ()); 230 this.setFile (fField, saveRequest); 231 } catch (java.lang.NumberFormatException nfe) { 232 logger.debug ( 233 "File Field number format exception when parsing from field value[" + this.getID () + "], fieldvalue=" + fromValue); 234 } catch (Throwable t) { 235 logger.debug ("copyEntry for field[" + this.getID () + "] exception", t); 236 } 237 } 238 } 239 240 250 public void deleteEntry (EntryStateable deleteEntryState) 251 throws JahiaException { 252 253 super.deleteEntry (deleteEntryState); 254 } 255 256 261 public boolean isShared () { 262 return false; 263 } 264 265 286 protected void serializeContentToXML (XmlWriter xmlWriter, 287 XMLSerializationOptions xmlSerializationOptions, 288 ContentObjectEntryState entryState, 289 ParamBean paramBean) throws java.io.IOException { 290 291 try { 292 int fileID = -1; 293 try { 294 fileID = Integer.parseInt (getDBValue (entryState)); 295 } catch (Throwable t) { 296 } 299 300 JahiaFilemanagerService fMgr = ServicesRegistry.getInstance () 301 .getJahiaFilemanagerService (); 302 JahiaFileField fileField = JahiaWebdavBaseService.getInstance ().getJahiaFileField ( 303 paramBean, getDBValue (entryState)); 304 Filemanager fmng = fMgr.getFilemanager (fileField.getFilemanagerID ()); 306 307 if (xmlSerializationOptions.isEmbeddingBinary ()) { 308 309 312 xmlWriter.writeEntity ("file"). 313 writeAttribute ("name", fileField.getRealName ()). 314 writeAttribute ("contentType", fileField.getType ()). 315 writeAttribute ("encoding", "base64"); 316 317 String path = fMgr.getFileRepositoryRootPath () + File.separator + 318 fmng.getStoragePath () + File.separator + 319 fileField.getStorageName (); 320 321 FileInputStream fileStream = new FileInputStream (path); 322 ByteArrayOutputStream byteArray = new ByteArrayOutputStream (); 323 copyStream (fileStream, byteArray, 65536); 324 char[] encodedStream = Base64.encode (byteArray.toByteArray ()); 325 StringBuffer result = new StringBuffer (encodedStream.length); 326 result.append ("\n"); 327 for (int i = 0; i < encodedStream.length; i++) { 328 result.append (encodedStream[i]); 329 if (i % 76 == 75) { 330 result.append ("\n"); 331 } 332 } 333 result.append ("\n"); 334 xmlWriter.writeText (result.toString ()); 335 xmlWriter.endEntity (); 336 } else { 337 340 JahiaFileField jahiaFileField = JahiaWebdavBaseService.getInstance () 341 .getJahiaFileField (paramBean, getDBValue (entryState)); 342 if (jahiaFileField == null) { 343 xmlWriter.writeEntity ("file"). 345 writeAttribute ("url", getDBValue (entryState)); 346 xmlWriter.endEntity (); 347 } 348 349 xmlWriter.writeEntity ("file"). 350 writeAttribute ("url", jahiaFileField.getDownloadUrl ()); 351 xmlWriter.endEntity (); 352 } 353 354 } catch (JahiaException je) { 355 logger.debug ("Error while serializing file field to XML :", je); 356 } 357 } 358 359 369 private void copyStream (InputStream ins, 370 OutputStream outs, int bufferSize) 371 throws IOException { 372 byte[] m_WriteBuffer = new byte[bufferSize]; 373 BufferedInputStream bis = 374 new BufferedInputStream (ins, bufferSize); 375 BufferedOutputStream bos = 376 new BufferedOutputStream (outs, bufferSize); 377 int bufread; 378 while ((bufread = bis.read (m_WriteBuffer)) != -1) { 379 bos.write (m_WriteBuffer, 0, bufread); 380 } 381 bos.flush (); 382 bos.close (); 383 bis.close (); 384 } 386 387 protected void purgeContent () 388 throws JahiaException { 389 390 } 391 392 393 } 394 | Popular Tags |