3030import org .compiere .model .MTable ;
3131import org .compiere .util .Env ;
3232import org .compiere .util .Util ;
33+ import org .spin .eca62 .support .IS3 ;
3334import org .spin .eca62 .support .ResourceMetadata ;
3435import org .spin .model .MADAppRegistration ;
3536import org .spin .model .MADAttachmentReference ;
@@ -67,6 +68,17 @@ public class AttachmentUtil {
6768 private int imageId ;
6869 private int archiveId ;
6970 private String transactionName ;
71+ private String baseFolder ;
72+ private String imageFolder ;
73+ private String attachmentFolder ;
74+ private String archiveFolder ;
75+ private String tmpFolder ;
76+
77+ private final String BASE_FOLDER_PARAMETER = "BaseFolder" ;
78+ private final String IMAGE_FOLDER_PARAMETER = "ImageFolder" ;
79+ private final String ATTACHMENT_FOLDER_PARAMETER = "AttachmentFolder" ;
80+ private final String ARCHIVE_FOLDER_PARAMETER = "ArchiveFolder" ;
81+ private final String TMP_FOLDER_PARAMETER = "TmpFolder" ;
7082
7183 /**
7284 * Private constructor
@@ -332,7 +344,7 @@ public byte[] getAttachment() throws Exception {
332344 description = attachmentReference .getDescription ();
333345 note = attachmentReference .getTextMsg ();
334346 // Get data
335- InputStream inputStream = handler .getResource (getCompleteFileName (attachmentReference ));
347+ InputStream inputStream = handler .getResource (getCompleteFileName (attachmentReference , handler ));
336348 if (inputStream == null ) {
337349 throw new AdempiereException ("@FileName@ @NotFound@" );
338350 }
@@ -434,7 +446,7 @@ public void saveAttachment() throws Exception {
434446 // Save
435447 attachmentReference .saveEx ();
436448 // Save
437- handler .putResource (getCompleteFileName (attachmentReference ), data );
449+ handler .putResource (getCompleteFileName (attachmentReference , handler ), data );
438450 } catch (Exception e ) {
439451 if (attachmentReference .getAD_AttachmentReference_ID () > 0 ) {
440452 attachmentReference .deleteEx (true );
@@ -456,7 +468,7 @@ public void deleteAttachment() throws Exception {
456468 }
457469 try {
458470 // Save
459- handler .deleteResource (getCompleteFileName (attachmentReference ));
471+ handler .deleteResource (getCompleteFileName (attachmentReference , handler ));
460472 // Remove from cache
461473 MADAttachmentReference .resetAttachmentReferenceCache (fileHandlerId , attachmentReference );
462474 // Delete reference
@@ -471,42 +483,102 @@ public void deleteAttachment() throws Exception {
471483 * @param attachmentReference
472484 * @return
473485 */
474- private String getCompleteFileName (MADAttachmentReference attachmentReference ) {
486+ private String getCompleteFileName (MADAttachmentReference attachmentReference , IWebDav handler ) {
487+ if (IS3 .class .isAssignableFrom (handler .getClass ())) {
488+ if (attachmentReference .getAD_Attachment_ID () > 0 ) {
489+ MAttachment attachment = new MAttachment (context , attachmentReference .getAD_Attachment_ID (), attachmentReference .get_TrxName ());
490+ String tableName = MTable .getTableName (context , attachment .getAD_Table_ID ());
491+ return ResourceMetadata .newInstance ()
492+ .withClientId (attachmentReference .getAD_Client_ID ())
493+ .withContainerType (ResourceMetadata .ContainerType .ATTACHMENT )
494+ .withTableName (tableName )
495+ .withRecordId (attachment .getRecord_ID ())
496+ .withName (attachmentReference .getFileName ())
497+ .getResourceFileName ()
498+ ;
499+ } else if (attachmentReference .getAD_Image_ID () > 0 ) {
500+ return ResourceMetadata .newInstance ()
501+ .withClientId (attachmentReference .getAD_Client_ID ())
502+ .withContainerType (ResourceMetadata .ContainerType .RESOURCE )
503+ .withContainerId ("image" )
504+ .withTableName (I_AD_Image .Table_Name )
505+ .withRecordId (attachmentReference .getAD_Image_ID ())
506+ .withName (attachmentReference .getFileName ())
507+ .getResourceFileName ()
508+ ;
509+ } else if (attachmentReference .getAD_Archive_ID () > 0 ) {
510+ MArchive archive = new MArchive (context , attachmentReference .getAD_Archive_ID (), attachmentReference .get_TrxName ());
511+ String tableName = MTable .getTableName (context , archive .getAD_Table_ID ());
512+ return ResourceMetadata .newInstance ()
513+ .withClientId (attachmentReference .getAD_Client_ID ())
514+ .withContainerType (ResourceMetadata .ContainerType .RESOURCE )
515+ .withContainerId ("archive" )
516+ .withTableName (tableName )
517+ .withRecordId (archive .getRecord_ID ())
518+ .withName (attachmentReference .getFileName ())
519+ .getResourceFileName ()
520+ ;
521+ }
522+ } else {
523+ return getCompleteFileNameOld (attachmentReference );
524+ }
525+ return null ;
526+ }
527+
528+ /**
529+ * Get complete path from attachment reference
530+ * @param attachmentReference
531+ * @return
532+ */
533+ private String getCompleteFileNameOld (MADAttachmentReference attachmentReference ) {
534+ String fileName = attachmentReference .getValidFileName ();
535+ String validForlder = getValidFolder (attachmentReference );
536+ String completePath = fileName ;
537+ if (!Util .isEmpty (validForlder )) {
538+ completePath = addSubFolder (validForlder , fileName );
539+ }
540+ return completePath ;
541+ }
542+
543+ /**
544+ * Get valid folder for all
545+ * @param attachmentReference
546+ * @return
547+ */
548+ private String getValidFolder (MADAttachmentReference attachmentReference ) {
549+ String validForlder = "" ;
550+ // For attachment
475551 if (attachmentReference .getAD_Attachment_ID () > 0 ) {
476- MAttachment attachment = new MAttachment (context , attachmentReference .getAD_Attachment_ID (), attachmentReference .get_TrxName ());
477- String tableName = MTable .getTableName (context , attachment .getAD_Table_ID ());
478- return ResourceMetadata .newInstance ()
479- .withClientId (attachmentReference .getAD_Client_ID ())
480- .withContainerType (ResourceMetadata .ContainerType .ATTACHMENT )
481- .withTableName (tableName )
482- .withRecordId (attachment .getRecord_ID ())
483- .withName (attachmentReference .getFileName ())
484- .getResourceFileName ()
485- ;
552+ validForlder = addSubFolder (baseFolder , attachmentFolder );
486553 } else if (attachmentReference .getAD_Image_ID () > 0 ) {
487- return ResourceMetadata .newInstance ()
488- .withClientId (attachmentReference .getAD_Client_ID ())
489- .withContainerType (ResourceMetadata .ContainerType .RESOURCE )
490- .withContainerId ("image" )
491- .withTableName (I_AD_Image .Table_Name )
492- .withRecordId (attachmentReference .getAD_Image_ID ())
493- .withName (attachmentReference .getFileName ())
494- .getResourceFileName ()
495- ;
554+ validForlder = addSubFolder (baseFolder , imageFolder );
496555 } else if (attachmentReference .getAD_Archive_ID () > 0 ) {
497- MArchive archive = new MArchive (context , attachmentReference .getAD_Archive_ID (), attachmentReference .get_TrxName ());
498- String tableName = MTable .getTableName (context , archive .getAD_Table_ID ());
499- return ResourceMetadata .newInstance ()
500- .withClientId (attachmentReference .getAD_Client_ID ())
501- .withContainerType (ResourceMetadata .ContainerType .RESOURCE )
502- .withContainerId ("archive" )
503- .withTableName (tableName )
504- .withRecordId (archive .getRecord_ID ())
505- .withName (attachmentReference .getFileName ())
506- .getResourceFileName ()
507- ;
556+ validForlder = addSubFolder (baseFolder , archiveFolder );
508557 }
509- return null ;
558+ if (Util .isEmpty (validForlder )) {
559+ validForlder = "" ;
560+ }
561+ return validForlder ;
562+ }
563+
564+ /**
565+ * Get and Add sub-folder
566+ * @param baseFolder
567+ * @param subFolder
568+ * @return
569+ */
570+ private String addSubFolder (String baseFolder , String subFolder ) {
571+ String completePath = "" ;
572+ if (!Util .isEmpty (baseFolder )) {
573+ completePath = baseFolder ;
574+ }
575+ // Sub-Folder
576+ if (!Util .isEmpty (completePath )) {
577+ completePath = completePath + "/" + subFolder ;
578+ } else {
579+ completePath = subFolder ;
580+ }
581+ return completePath ;
510582 }
511583
512584 /**
@@ -539,6 +611,8 @@ private IWebDav getFileHandler() throws Exception {
539611 if (registration == null ) {
540612 throw new AdempiereException ("@AD_AppRegistration_ID@ @NotFound@" );
541613 }
614+ // Process Folders
615+ processFolder (registration );
542616 // Load
543617 IAppSupport supportedApi = AppSupportHandler .getInstance ().getAppSupport (MADAppRegistration .getById (context , fileHandlerId , transactionName ));
544618 if (supportedApi == null ) {
@@ -551,4 +625,43 @@ private IWebDav getFileHandler() throws Exception {
551625 fileHandler = (IWebDav ) supportedApi ;
552626 return fileHandler ;
553627 }
628+
629+ /**
630+ * Process folder for a valid name
631+ * @param folder
632+ * @return
633+ */
634+ private void processFolder (MADAppRegistration registration ) {
635+ baseFolder = getValidFolder (registration .getParameterValue (BASE_FOLDER_PARAMETER ));
636+ imageFolder = getValidFolder (registration .getParameterValue (IMAGE_FOLDER_PARAMETER ));
637+ if (Util .isEmpty (imageFolder )) {
638+ imageFolder = "Images" ;
639+ }
640+ attachmentFolder = getValidFolder (registration .getParameterValue (ATTACHMENT_FOLDER_PARAMETER ));
641+ if (Util .isEmpty (attachmentFolder )) {
642+ attachmentFolder = "Attachments" ;
643+ }
644+ archiveFolder = getValidFolder (registration .getParameterValue (ARCHIVE_FOLDER_PARAMETER ));
645+ if (Util .isEmpty (archiveFolder )) {
646+ archiveFolder = "Archives" ;
647+ }
648+ tmpFolder = getValidFolder (registration .getParameterValue (TMP_FOLDER_PARAMETER ));
649+ if (Util .isEmpty (tmpFolder )) {
650+ tmpFolder = "Tmp" ;
651+ }
652+ }
653+
654+ /**
655+ * get valid Folder Name
656+ * @param folder
657+ * @return
658+ */
659+ private String getValidFolder (String folder ) {
660+ if (Util .isEmpty (folder )) {
661+ folder = "" ;
662+ } else if (folder .endsWith ("/" )) {
663+ folder .substring (0 , folder .length () - 1 );
664+ }
665+ return folder ;
666+ }
554667}
0 commit comments