1 /*******************************************************************************
2 * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
3 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
4 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
6 * This file is part of the work and learning management system Pentila Nero.
8 * Pentila Nero is free software. You can redistribute it and/or modify since
9 * you respect the terms of either (at least one of the both license) :
10 * - under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 * - the CeCILL-C as published by CeCILL-C; either version 1 of the
14 * License, or any later version
15 * - the GNU Lesser General Public License as published by the
16 * Free Software Foundation, either version 3 of the license,
17 * or (at your option) any later version.
19 * There are special exceptions to the terms and conditions of the
20 * licenses as they are applied to this software. View the full text of
21 * the exception in file LICENSE-PROJECT.txt in the directory of this software
24 * Pentila Nero is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * Licenses for more details.
29 * You should have received a copy of the GNU Affero General Public License
30 * and the CeCILL-C and the GNU Lesser General Public License along with
31 * Pentila Nero. If not, see :
32 * <http://www.gnu.org/licenses/> and
33 * <http://www.cecill.info/licences.fr.html>.
34 ******************************************************************************/
35 package com.pentila.entSavoie.utils;
37 import java.io.IOException;
40 import javax.mail.internet.AddressException;
41 import javax.mail.internet.InternetAddress;
43 import com.liferay.portal.kernel.exception.PortalException;
44 import com.liferay.portal.kernel.exception.SystemException;
45 import com.liferay.portal.kernel.json.JSONArray;
46 import com.liferay.portal.kernel.json.JSONObject;
47 import com.liferay.portal.kernel.log.Log;
48 import com.liferay.portal.kernel.log.LogFactoryUtil;
49 import com.liferay.portal.kernel.messaging.DestinationNames;
50 import com.liferay.portal.kernel.messaging.MessageBusUtil;
51 import com.liferay.portal.kernel.repository.model.FileEntry;
52 import com.liferay.portal.kernel.repository.model.Folder;
53 import com.liferay.portal.kernel.util.Validator;
54 import com.liferay.portal.model.User;
55 import com.liferay.portal.security.permission.ActionKeys;
56 import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
57 import com.liferay.portal.service.UserLocalServiceUtil;
58 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
59 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
60 import com.pentila.entSavoie.cartable.model.DlLink;
61 import com.pentila.entSavoie.cartable.model.FileTag;
62 import com.pentila.entSavoie.cartable.service.CasierEntryLocalServiceUtil;
63 import com.pentila.entSavoie.cartable.service.DlLinkLocalServiceUtil;
64 import com.pentila.entSavoie.cartable.service.FileTagLocalServiceUtil;
65 import com.pentila.entSavoie.internalMessaging.IMUtils;
66 import com.pentila.entSavoie.service.DiskUsageUserLocalServiceUtil;
68 public class ENTSendingUtil {
70 private static Log _log = LogFactoryUtil.getLog(ENTSendingUtil.class);
73 * Envoi dans la messagerie ou dans le casier des destinataires
76 public static void sendMessageToUsers(User user, Set<Long> destFinal, String subject, String content, JSONArray attachFiles, long messageId, String destMail, String ccMail, String cciMail, Long accountId, String accountPassword, boolean toIM, boolean toCasier) throws PortalException, SystemException, IOException {
78 if (destFinal != null && destFinal.size() > 0) {
80 IMUtils.sendMessage(user, destFinal, subject, content, attachFiles, messageId);
84 // boite d'envoi de la personne
85 final Folder senderTmpBox = FoldersUtil.getSendingBox(user.getUserId());
87 for (int i=0; i<attachFiles.length(); i++) {
88 JSONObject pjobj = attachFiles.getJSONObject(i);
89 if (pjobj.getString("type").equals("Fichier")) {
90 // copy the file inside his sending box
91 // if it's ever a reference with take the source
92 Long sourceFileId = pjobj.getLong("id");
94 DlLink dll = DlLinkLocalServiceUtil.getDlFileLinkByTarget(sourceFileId);
95 sourceFileId = dll.getSourceId();
97 catch(Exception nsdle) {
98 // no link so we have an original
101 FileEntry referenceFile = DLEntryUtil.copyFileEntry(user.getUserId(), sourceFileId, senderTmpBox.getFolderId());
102 long size = DLFileEntryLocalServiceUtil.getDLFileEntry(sourceFileId).getSize();
105 boolean isBookmark = false;
107 DLFileEntry dlFileEntry = DLFileEntryLocalServiceUtil.getFileEntry(sourceFileId);
108 if (DLEntryUtil.isSignetFile(dlFileEntry)) {
110 DLFileEntry refDlFileEntry = DLFileEntryLocalServiceUtil.getFileEntry(referenceFile.getFileEntryId());
111 refDlFileEntry.setMimeType("text/bmk");
112 DLFileEntryLocalServiceUtil.updateDLFileEntry(refDlFileEntry);
114 } catch (Exception e) {
115 _log.error("Error while reading isSignet from dlFileEntryId "+sourceFileId, e);
118 // to get the stream of this file late we need to set the permission View for all profile
119 PermissionsUtils.setViewPermissionForRessources(referenceFile);
121 //Add disk usage to sender because file is store on it's senderbox
122 DiskUsageUserLocalServiceUtil.addDiskUsage(referenceFile, user.getUserId(), PortletConstant.CARTABLE_PORTLET_ID);
125 for (final long userId : destFinal) {
127 // boite de reception du destinataire
128 final User receiver = UserLocalServiceUtil.getUser(userId);
129 final Folder reveiverDropBox = FoldersUtil.getDropBox(receiver.getUserId());
130 // copy the file without the content by linking it to the reference file
131 FileEntry distantEntry = DLEntryUtil.copyLinkedFileEntry(receiver.getUserId(), referenceFile.getFileEntryId(), reveiverDropBox.getFolderId(),PortletConstant.CARTABLE_PORTLET_ID);
133 DLFileEntry dlFileEntry = DLFileEntryLocalServiceUtil.getFileEntry(distantEntry.getFileEntryId());
134 dlFileEntry.setMimeType("text/bmk");
135 DLFileEntryLocalServiceUtil.updateDLFileEntry(dlFileEntry);
137 CasierEntryLocalServiceUtil.addCasierEntry(reveiverDropBox.getCompanyId(), reveiverDropBox.getGroupId(), distantEntry.getFileEntryId(), user.getUserId(), content, true, size);
139 // Copy all the tags, keywords and links
140 FileTag fileTag = FileTagLocalServiceUtil.getFileTagByFileEntryId(sourceFileId);
141 if (fileTag != null){
142 FileTagLocalServiceUtil.update(distantEntry.getFileEntryId(), fileTag.getTagList(), fileTag.getLinkList(), fileTag.getKeywordList());
145 catch (Exception e) {
146 _log.error("Erreur durant l envoi a " + userId);
151 // copy the folder inside his sending box
152 // if it's ever a reference with take the source
153 Long sourceFolderId = pjobj.getLong("id");
155 DlLink dll = DlLinkLocalServiceUtil.getDlFolderLinkByTarget(sourceFolderId);
156 sourceFolderId = dll.getSourceId();
158 catch(Exception nsdle) {
159 // no link so we have an original
161 Folder referenceFolder = DLEntryUtil.copyFolder(user.getUserId(), sourceFolderId, senderTmpBox.getFolderId());
162 // to get the stream of this folder late we need to set the permission View for all profile
163 PermissionsUtils.setViewPermissionForRessources(referenceFolder);
165 for (final long userId : destFinal) {
167 // boite de reception du destinataire
168 final User receiver = UserLocalServiceUtil.getUser(userId);
169 final Folder reveiverDropBox = FoldersUtil.getDropBox(receiver.getUserId());
170 // copy the folder without the content by linking it to the reference folder
171 Folder distantFolder = DLEntryUtil.copyLinkedFolder(receiver.getUserId(), referenceFolder.getFolderId(), reveiverDropBox.getFolderId(),PortletConstant.CARTABLE_PORTLET_ID);
172 CasierEntryLocalServiceUtil.addCasierEntry(reveiverDropBox.getCompanyId(), reveiverDropBox.getGroupId(), distantFolder.getFolderId(), user.getUserId(), content, true);
174 catch (Exception e) {
175 _log.error("Error when sending folder to userId " + userId, e);
176 _log.error(e.getMessage());
184 if (!destMail.isEmpty()) {
186 com.liferay.portal.kernel.messaging.Message message = new com.liferay.portal.kernel.messaging.Message();
188 message.put("command", "sendMail");
190 message.put("userId", user.getUserId());
191 message.put("accountId", accountId);
192 message.put("accountPassword", accountPassword);
193 message.put("destMail", destMail);
194 message.put("ccMail", ccMail);
195 message.put("cciMail", cciMail);
196 message.put("subject", subject);
197 message.put("content", content);
198 message.put("attachFiles", attachFiles.toString());
199 message.put("messageId", messageId);
201 MessageBusUtil.sendMessage(DestinationNames.MAIL_SYNCHRONIZER, message);
206 public static boolean isAdressesValid(String addresses) throws PortalException {
208 InternetAddress[] internetAddresses = new InternetAddress[0];
211 internetAddresses = InternetAddress.parse(addresses, true);
213 for (int i = 0; i < internetAddresses.length; i++) {
214 InternetAddress internetAddress = internetAddresses[i];
216 if (!Validator.isEmailAddress(internetAddress.getAddress())) {
221 catch (AddressException ae) {