Author: nmalin
Date: Tue Jan 12 20:27:55 2016
New Revision: 1724312

URL: http://svn.apache.org/viewvc?rev=1724312&view=rev
Log:
Correct the service CommunicationServices.createAttachmentContent that 
duplicates attachments for existing CommunicationEvents. Issue reported and 
solved by Gareth Carter on issue OFBIZ-6697.Backport from trunk

Modified:
    
ofbiz/branches/release14.12/applications/party/src/org/ofbiz/party/communication/CommunicationEventServices.java

Modified: 
ofbiz/branches/release14.12/applications/party/src/org/ofbiz/party/communication/CommunicationEventServices.java
URL: 
http://svn.apache.org/viewvc/ofbiz/branches/release14.12/applications/party/src/org/ofbiz/party/communication/CommunicationEventServices.java?rev=1724312&r1=1724311&r2=1724312&view=diff
==============================================================================
--- 
ofbiz/branches/release14.12/applications/party/src/org/ofbiz/party/communication/CommunicationEventServices.java
 (original)
+++ 
ofbiz/branches/release14.12/applications/party/src/org/ofbiz/party/communication/CommunicationEventServices.java
 Tue Jan 12 20:27:55 2016
@@ -26,6 +26,7 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.nio.ByteBuffer;
 import java.sql.Timestamp;
+import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.LinkedList;
@@ -644,8 +645,8 @@ public class CommunicationEventServices
 
         // attachments
         try {
-            createAttachmentContent(dispatcher, wrapper, communicationEventId, 
userLogin);
-        } catch (GenericServiceException e) {
+            createAttachmentContent(dispatcher, dctx.getDelegator(), wrapper, 
communicationEventId, userLogin);
+        } catch (GenericServiceException | GenericEntityException e) {
             return ServiceUtil.returnError(e.getMessage());
         }
 
@@ -901,7 +902,7 @@ public class CommunicationEventServices
             Debug.logInfo("Persisting New Email: " + aboutThisEmail + " into 
CommunicationEventId: " + communicationEventId, module);
 
             // handle the attachments
-            createAttachmentContent(dispatcher, wrapper, communicationEventId, 
userLogin);
+            createAttachmentContent(dispatcher, delegator, wrapper, 
communicationEventId, userLogin);
 
             // For all addresses create a CommunicationEventRoles
             createCommEventRoles(userLogin, delegator, dispatcher, 
communicationEventId, toParties, "ADDRESSEE");
@@ -972,14 +973,37 @@ public class CommunicationEventServices
         if (UtilValidate.isNotEmpty(bccString)) commEventMap.put("bccString", 
bccString);
     }
 
-    private static void createAttachmentContent(LocalDispatcher dispatcher, 
MimeMessageWrapper wrapper, String communicationEventId, GenericValue 
userLogin) throws GenericServiceException {
+    private static List<String> getCommEventAttachmentNames(final Delegator 
delegator, final String communicationEventId) throws GenericEntityException {
+        List<GenericValue> commEventContentAssocList = 
EntityQuery.use(delegator)
+            .from("CommEventContentDataResource")
+            .where(EntityCondition.makeCondition("communicationEventId", 
communicationEventId))
+            .filterByDate()
+            .queryList();
+
+        List<String> attachmentNames = new ArrayList<String>();
+        for (GenericValue commEventContentAssoc : commEventContentAssocList) {
+            String dataResourceName = 
commEventContentAssoc.getString("drDataResourceName");
+            attachmentNames.add(dataResourceName);
+        }
+
+        return attachmentNames;
+    }
+
+    private static void createAttachmentContent(LocalDispatcher dispatcher, 
Delegator delegator, MimeMessageWrapper wrapper, String communicationEventId, 
GenericValue userLogin) throws GenericServiceException, GenericEntityException {
         // handle the attachments
         String subject = wrapper.getSubject();
         List<String> attachmentIndexes = wrapper.getAttachmentIndexes();
+        List<String> currentAttachmentNames = 
getCommEventAttachmentNames(delegator, communicationEventId);
 
         if (attachmentIndexes.size() > 0) {
             Debug.logInfo("=== message has attachments [" + 
attachmentIndexes.size() + "] =====", module);
             for (String attachmentIdx : attachmentIndexes) {
+                String attFileName = wrapper.getPartFilename(attachmentIdx);
+                if (currentAttachmentNames.contains(attFileName)) {
+                    Debug.logWarning(String.format("CommunicationEvent [%s] 
already has attachment named '%s'", communicationEventId, attFileName), module);
+                    continue;
+                }
+
                 Map<String, Object> attachmentMap = new HashMap<String, 
Object>();
                 attachmentMap.put("communicationEventId", 
communicationEventId);
                 attachmentMap.put("contentTypeId", "DOCUMENT");
@@ -989,7 +1013,6 @@ public class CommunicationEventServices
                     subject = subject.substring(0,80); // make sure not too 
big for database field. (20 characters for filename)
                 }
 
-                String attFileName = wrapper.getPartFilename(attachmentIdx);
                 String attContentType = 
wrapper.getPartContentType(attachmentIdx);
                 if (attContentType != null && attContentType.indexOf(";") > 
-1) {
                     attContentType = attContentType.toLowerCase().substring(0, 
attContentType.indexOf(";"));


Reply via email to