--- /dev/null
+/*******************************************************************************
+ * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
+ *
+ * This file is part of the work and learning management system Pentila Nero.
+ *
+ * Pentila Nero is free software. You can redistribute it and/or modify since
+ * you respect the terms of either (at least one of the both license) :
+ * - under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ * - the CeCILL-C as published by CeCILL-C; either version 1 of the
+ * License, or any later version
+ * - the GNU Lesser General Public License as published by the
+ * Free Software Foundation, either version 3 of the license,
+ * or (at your option) any later version.
+ *
+ * There are special exceptions to the terms and conditions of the
+ * licenses as they are applied to this software. View the full text of
+ * the exception in file LICENSE-PROJECT.txt in the directory of this software
+ * distribution.
+ *
+ * Pentila Nero is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Licenses for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * and the CeCILL-C and the GNU Lesser General Public License along with
+ * Pentila Nero. If not, see :
+ * <http://www.gnu.org/licenses/> and
+ * <http://www.cecill.info/licences.fr.html>.
+ ******************************************************************************/
+package com.pentila.entSavoie.utils;
+
+import java.io.IOException;
+import java.util.Set;
+
+import javax.mail.internet.AddressException;
+import javax.mail.internet.InternetAddress;
+
+import com.liferay.portal.kernel.exception.PortalException;
+import com.liferay.portal.kernel.exception.SystemException;
+import com.liferay.portal.kernel.json.JSONArray;
+import com.liferay.portal.kernel.json.JSONObject;
+import com.liferay.portal.kernel.log.Log;
+import com.liferay.portal.kernel.log.LogFactoryUtil;
+import com.liferay.portal.kernel.messaging.DestinationNames;
+import com.liferay.portal.kernel.messaging.MessageBusUtil;
+import com.liferay.portal.kernel.repository.model.FileEntry;
+import com.liferay.portal.kernel.repository.model.Folder;
+import com.liferay.portal.kernel.util.Validator;
+import com.liferay.portal.model.User;
+import com.liferay.portal.security.permission.ActionKeys;
+import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
+import com.liferay.portal.service.UserLocalServiceUtil;
+import com.liferay.portlet.documentlibrary.model.DLFileEntry;
+import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
+import com.pentila.entSavoie.cartable.model.DlLink;
+import com.pentila.entSavoie.cartable.model.FileTag;
+import com.pentila.entSavoie.cartable.service.CasierEntryLocalServiceUtil;
+import com.pentila.entSavoie.cartable.service.DlLinkLocalServiceUtil;
+import com.pentila.entSavoie.cartable.service.FileTagLocalServiceUtil;
+import com.pentila.entSavoie.internalMessaging.IMUtils;
+import com.pentila.entSavoie.service.DiskUsageUserLocalServiceUtil;
+
+public class ENTSendingUtil {
+
+ private static Log _log = LogFactoryUtil.getLog(ENTSendingUtil.class);
+
+ /**
+ * Envoi dans la messagerie ou dans le casier des destinataires
+ * @throws IOException
+ */
+ 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 {
+
+ if (destFinal != null && destFinal.size() > 0) {
+ if (toIM) {
+ IMUtils.sendMessage(user, destFinal, subject, content, attachFiles, messageId);
+ }
+ if (toCasier) {
+
+ // boite d'envoi de la personne
+ final Folder senderTmpBox = FoldersUtil.getSendingBox(user.getUserId());
+
+ for (int i=0; i<attachFiles.length(); i++) {
+ JSONObject pjobj = attachFiles.getJSONObject(i);
+ if (pjobj.getString("type").equals("Fichier")) {
+ // copy the file inside his sending box
+ // if it's ever a reference with take the source
+ Long sourceFileId = pjobj.getLong("id");
+ try {
+ DlLink dll = DlLinkLocalServiceUtil.getDlFileLinkByTarget(sourceFileId);
+ sourceFileId = dll.getSourceId();
+ }
+ catch(Exception nsdle) {
+ // no link so we have an original
+ }
+
+ FileEntry referenceFile = DLEntryUtil.copyFileEntry(user.getUserId(), sourceFileId, senderTmpBox.getFolderId());
+ long size = DLFileEntryLocalServiceUtil.getDLFileEntry(sourceFileId).getSize();
+
+ // Bookmark
+ boolean isBookmark = false;
+ try {
+ DLFileEntry dlFileEntry = DLFileEntryLocalServiceUtil.getFileEntry(sourceFileId);
+ if (DLEntryUtil.isSignetFile(dlFileEntry)) {
+ isBookmark = true;
+ DLFileEntry refDlFileEntry = DLFileEntryLocalServiceUtil.getFileEntry(referenceFile.getFileEntryId());
+ refDlFileEntry.setMimeType("text/bmk");
+ DLFileEntryLocalServiceUtil.updateDLFileEntry(refDlFileEntry);
+ }
+ } catch (Exception e) {
+ _log.error("Error while reading isSignet from dlFileEntryId "+sourceFileId, e);
+ }
+
+ // to get the stream of this file late we need to set the permission View for all profile
+ PermissionsUtils.setViewPermissionForRessources(referenceFile);
+
+ //Add disk usage to sender because file is store on it's senderbox
+ DiskUsageUserLocalServiceUtil.addDiskUsage(referenceFile, user.getUserId(), PortletConstant.CARTABLE_PORTLET_ID);
+
+
+ for (final long userId : destFinal) {
+ try {
+ // boite de reception du destinataire
+ final User receiver = UserLocalServiceUtil.getUser(userId);
+ final Folder reveiverDropBox = FoldersUtil.getDropBox(receiver.getUserId());
+ // copy the file without the content by linking it to the reference file
+ FileEntry distantEntry = DLEntryUtil.copyLinkedFileEntry(receiver.getUserId(), referenceFile.getFileEntryId(), reveiverDropBox.getFolderId(),PortletConstant.CARTABLE_PORTLET_ID);
+ if (isBookmark) {
+ DLFileEntry dlFileEntry = DLFileEntryLocalServiceUtil.getFileEntry(distantEntry.getFileEntryId());
+ dlFileEntry.setMimeType("text/bmk");
+ DLFileEntryLocalServiceUtil.updateDLFileEntry(dlFileEntry);
+ }
+ CasierEntryLocalServiceUtil.addCasierEntry(reveiverDropBox.getCompanyId(), reveiverDropBox.getGroupId(), distantEntry.getFileEntryId(), user.getUserId(), content, true, size);
+
+ // Copy all the tags, keywords and links
+ FileTag fileTag = FileTagLocalServiceUtil.getFileTagByFileEntryId(sourceFileId);
+ if (fileTag != null){
+ FileTagLocalServiceUtil.update(distantEntry.getFileEntryId(), fileTag.getTagList(), fileTag.getLinkList(), fileTag.getKeywordList());
+ }
+ }
+ catch (Exception e) {
+ _log.error("Erreur durant l envoi a " + userId);
+ _log.error(e);
+ }
+ }
+ } else {
+ // copy the folder inside his sending box
+ // if it's ever a reference with take the source
+ Long sourceFolderId = pjobj.getLong("id");
+ try {
+ DlLink dll = DlLinkLocalServiceUtil.getDlFolderLinkByTarget(sourceFolderId);
+ sourceFolderId = dll.getSourceId();
+ }
+ catch(Exception nsdle) {
+ // no link so we have an original
+ }
+ Folder referenceFolder = DLEntryUtil.copyFolder(user.getUserId(), sourceFolderId, senderTmpBox.getFolderId());
+ // to get the stream of this folder late we need to set the permission View for all profile
+ PermissionsUtils.setViewPermissionForRessources(referenceFolder);
+
+ for (final long userId : destFinal) {
+ try {
+ // boite de reception du destinataire
+ final User receiver = UserLocalServiceUtil.getUser(userId);
+ final Folder reveiverDropBox = FoldersUtil.getDropBox(receiver.getUserId());
+ // copy the folder without the content by linking it to the reference folder
+ Folder distantFolder = DLEntryUtil.copyLinkedFolder(receiver.getUserId(), referenceFolder.getFolderId(), reveiverDropBox.getFolderId(),PortletConstant.CARTABLE_PORTLET_ID);
+ CasierEntryLocalServiceUtil.addCasierEntry(reveiverDropBox.getCompanyId(), reveiverDropBox.getGroupId(), distantFolder.getFolderId(), user.getUserId(), content, true);
+ }
+ catch (Exception e) {
+ _log.error("Error when sending folder to userId " + userId, e);
+ _log.error(e.getMessage());
+ }
+ }
+ }
+ }
+ }
+ }
+
+ if (!destMail.isEmpty()) {
+
+ com.liferay.portal.kernel.messaging.Message message = new com.liferay.portal.kernel.messaging.Message();
+
+ message.put("command", "sendMail");
+
+ message.put("userId", user.getUserId());
+ message.put("accountId", accountId);
+ message.put("accountPassword", accountPassword);
+ message.put("destMail", destMail);
+ message.put("ccMail", ccMail);
+ message.put("cciMail", cciMail);
+ message.put("subject", subject);
+ message.put("content", content);
+ message.put("attachFiles", attachFiles.toString());
+ message.put("messageId", messageId);
+
+ MessageBusUtil.sendMessage(DestinationNames.MAIL_SYNCHRONIZER, message);
+ }
+ }
+
+
+ public static boolean isAdressesValid(String addresses) throws PortalException {
+
+ InternetAddress[] internetAddresses = new InternetAddress[0];
+
+ try {
+ internetAddresses = InternetAddress.parse(addresses, true);
+
+ for (int i = 0; i < internetAddresses.length; i++) {
+ InternetAddress internetAddress = internetAddresses[i];
+
+ if (!Validator.isEmailAddress(internetAddress.getAddress())) {
+ return false;
+ }
+ }
+ }
+ catch (AddressException ae) {
+ return false;
+ }
+
+ return true;
+ }
+}