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.awt.image.RenderedImage;
38 import java.io.ByteArrayInputStream;
39 import java.io.ByteArrayOutputStream;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.text.NumberFormat;
44 import java.util.Iterator;
45 import java.util.List;
46 import java.util.Locale;
48 import javax.imageio.IIOImage;
49 import javax.imageio.ImageIO;
50 import javax.imageio.ImageWriteParam;
51 import javax.imageio.ImageWriter;
52 import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
53 import javax.imageio.stream.ImageOutputStream;
55 import org.jsoup.Jsoup;
56 import org.jsoup.nodes.Document;
57 import org.jsoup.nodes.DocumentType;
58 import org.jsoup.nodes.Element;
59 import org.jsoup.nodes.Node;
60 import org.jsoup.select.Elements;
62 import com.liferay.portal.kernel.dao.orm.QueryUtil;
63 import com.liferay.portal.kernel.exception.PortalException;
64 import com.liferay.portal.kernel.exception.SystemException;
65 import com.liferay.portal.kernel.image.ImageToolUtil;
66 import com.liferay.portal.kernel.log.Log;
67 import com.liferay.portal.kernel.log.LogFactoryUtil;
68 import com.liferay.portal.kernel.repository.model.FileEntry;
69 import com.liferay.portal.kernel.repository.model.Folder;
70 import com.liferay.portal.kernel.search.Indexer;
71 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
72 import com.liferay.portal.kernel.util.Base64;
73 import com.liferay.portal.kernel.util.FileUtil;
74 import com.liferay.portal.kernel.workflow.WorkflowConstants;
75 import com.liferay.portal.model.User;
76 import com.liferay.portal.service.ServiceContext;
77 import com.liferay.portal.service.UserLocalServiceUtil;
78 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
79 import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
80 import com.liferay.portlet.documentlibrary.model.DLFolder;
81 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
82 import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
83 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
84 import com.liferay.portlet.documentlibrary.service.DLFileShortcutLocalServiceUtil;
85 import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
86 import com.pentila.entSavoie.cartable.model.DlLink;
87 import com.pentila.entSavoie.cartable.service.CasierEntryLocalServiceUtil;
88 import com.pentila.entSavoie.cartable.service.DlLinkLocalServiceUtil;
90 public class DLEntryUtil {
92 private static Log logger = LogFactoryUtil.getLog(DLEntryUtil.class);
94 public static void deleteFolder(long folderId) throws PortalException, SystemException {
95 //supprime effectivement le fichier
96 DLAppLocalServiceUtil.deleteFolder(folderId);
99 public static void deleteFile(long fileId) throws PortalException, SystemException {
100 final FileEntry file = DLAppLocalServiceUtil.getFileEntry(fileId);
102 CasierEntryLocalServiceUtil.deleteBuC_G_Id(file.getCompanyId(), file.getGroupId(), file.getFileEntryId());
103 } catch (PortalException e){}
104 DLAppLocalServiceUtil.deleteFileEntry(fileId);
107 public static void deleteDLFileShortcut(long shId) throws PortalException, SystemException {
109 final DLFileShortcut file = DLFileShortcutLocalServiceUtil.getDLFileShortcut(shId);
110 CasierEntryLocalServiceUtil.deleteBuC_G_Id(file.getCompanyId(), file.getGroupId(), file.getFileShortcutId());
111 } catch (PortalException e){}
112 DLFileShortcutLocalServiceUtil.deleteDLFileShortcut(shId);
115 public static DLFileShortcut copyDLFileShortcut(long userId, long shortCutId, long destFolderId) throws PortalException, SystemException {
116 final User user = UserLocalServiceUtil.getUser(userId);
117 final DLFileShortcut originShortcut = DLFileShortcutLocalServiceUtil.getFileShortcut(shortCutId);
118 final Folder destFolder = DLAppLocalServiceUtil.getFolder(destFolderId);
120 ServiceContext serviceContext = new ServiceContext();
121 serviceContext.setAddGroupPermissions(true);
122 serviceContext.setAddGuestPermissions(true);
124 DLFileShortcut ret = DLFileShortcutLocalServiceUtil.addFileShortcut(
126 destFolder.getGroupId(),
127 destFolder.getFolderId(),
128 originShortcut.getToFileEntryId(),
133 public static FileEntry moveFileEntry(long userId, long fileId, long destFolderId) throws PortalException, SystemException {
134 final User user = UserLocalServiceUtil.getUser(userId);
136 // Ajout des permissions
137 ServiceContext serviceContext = new ServiceContext();
138 serviceContext.setAddGroupPermissions(true);
139 serviceContext.setAddGuestPermissions(true);
140 FileEntry ret = null;
145 // if we have link copy the source dans delete the link file
146 dll = DlLinkLocalServiceUtil.fetchDlLinkByTargetType(fileId, "file");
147 if( (dll != null) && (dll.getSourceId() != 0) ){
148 FileEntry sourceLinkedFile = DLAppLocalServiceUtil.getFileEntry(dll.getSourceId());
149 FileEntry targetLinkedFile = DLAppLocalServiceUtil.getFileEntry(dll.getTargetId());
151 ret = DLEntryUtil.copyFileEntry(user.getUserId(), dll.getSourceId(), destFolderId, true);
153 // in case it change user change the title, get it
154 if(!sourceLinkedFile.getTitle().equals(targetLinkedFile.getTitle())){
156 ret = DLAppLocalServiceUtil.updateFileEntry(ret.getUserId(),
157 ret.getFileEntryId(),
160 targetLinkedFile.getTitle(),
161 ret.getDescription(),
164 ret.getContentStream(),
171 if (sourceLinkedFile !=null) {
172 // we have a link so we delete it
173 DlLinkLocalServiceUtil.removeDlFileLinkByTarget(dll.getTargetId());
174 int linkLeft = DlLinkLocalServiceUtil.countDlFileLinkBySourceId(sourceLinkedFile.getFileEntryId());
176 // plus de lien donc on supprime la source
177 DLAppLocalServiceUtil.deleteFileEntry(sourceLinkedFile.getFileEntryId());
178 // on verifie le dossier contenant pour suppression le cas echeant en recurssif
179 removeLinkedFileContainer(sourceLinkedFile.getFolderId());
182 DLAppLocalServiceUtil.deleteFileEntry(fileId);
185 // no dll link just move the file
186 ret = DLAppLocalServiceUtil.moveFileEntry(
192 // update file permission to get the new parent folder permission
193 PermissionsUtils.setParentPermissionToFile(ret);
194 } catch (SystemException se) {
196 } catch (IOException ioe) {
203 * this method move a pFolderToMove in pTargetFolderIf the file inside are links, this method get the reals file and write them into folder instead of links
204 * @param pFolderToMove The Folder object you will move
205 * @param pTargetFolder the folder id of target folder
206 * @throws PortalException
207 * @throws SystemException
209 public static Folder moveFolder(Folder pFolderToMove, long pTargetFolder) throws PortalException, SystemException{
210 Folder targetFolder = DLAppLocalServiceUtil.getFolder(pTargetFolder);
211 Folder newFolder = DLAppUtil.addFolder(targetFolder.getUserId(), targetFolder.getGroupId(), pTargetFolder, pFolderToMove.getName());
212 List<FileEntry> files = DLAppLocalServiceUtil.getFileEntries(pFolderToMove.getGroupId(), pFolderToMove.getFolderId());
213 List<Folder> folders = DLAppLocalServiceUtil.getFolders(pFolderToMove.getGroupId(), pFolderToMove.getFolderId());
215 for (Folder aFolder: folders) {
216 moveFolder(aFolder, newFolder.getFolderId());
219 for (FileEntry aFile: files) {
220 moveFileEntry(targetFolder.getUserId(), aFile.getFileEntryId(), newFolder.getFolderId());
223 DLAppLocalServiceUtil.deleteFolder(pFolderToMove.getFolderId());
228 public static FileEntry copyFileEntry(long userId, long fileId, long destFolderId) throws PortalException, SystemException, IOException {
229 return copyFileEntry(userId, fileId, destFolderId, true);
233 * Copy a file as a link (without it's content)
234 * @param pUserId long which represent the receiver0
235 * @param pFileId file id (as long) which represent the file to copy and create the link
236 * @param pDestFolderId the folder id where the file will be copy (as long)
237 * @param pSourcePortlet a String representing the orgin protlet. It's specifically for quota
238 * @return The linked file entry (FileEntry object)
240 public static FileEntry copyLinkedFileEntry(long pUserId, long pFileId, long pDestFolderId, String pSourcePortlet) throws PortalException, SystemException, IOException {
241 // copy the file without content
242 FileEntry linkedDLFE = copyFileEntry(pUserId, pFileId, pDestFolderId, false);
245 DlLinkLocalServiceUtil.addDlFileLink(pFileId, linkedDLFE.getFileEntryId(), pSourcePortlet);
247 // reindex the target file due test of link inside documentLibrary.util.indexer.java
248 Indexer indexer = IndexerRegistryUtil.getIndexer(DLFileEntry.class);
251 indexer.reindex(linkedDLFE);
252 } catch (Exception e) {
253 logger.error("Error during reindexing file "+linkedDLFE.getFileEntryId());
258 public static FileEntry copyFileEntry(long userId, long fileId, long destFolderId, boolean copyFileContent) throws PortalException, SystemException, IOException {
259 final User user = UserLocalServiceUtil.getUser(userId);
260 final FileEntry originFile = DLAppLocalServiceUtil.getFileEntry(fileId);
261 final Folder destFolder = DLAppLocalServiceUtil.getFolder(destFolderId);
263 // Ajout des permissions
264 ServiceContext serviceContext = new ServiceContext();
265 serviceContext.setAddGroupPermissions(true);
266 serviceContext.setAddGuestPermissions(true);
268 FileEntry ret = null;
270 InputStream is = null;
271 if (copyFileContent) {
272 // on prend le contenu du fichier a copier
274 * We are using the getContentStream from FileEntryUtil because the same methods on FileEntry check the permission, in some case this throw and error
276 is = DLFileEntryLocalServiceUtil.getFileAsStream(originFile.getUserId(), originFile.getFileEntryId(), originFile.getVersion());
279 // on sauve un fichier vide!!!
280 is = new ByteArrayInputStream("".getBytes());
284 ret = DLAppUtil.addFileEntry(user, destFolder, originFile.getTitle(), is);
290 * Copy a folder as a link (with it's content)
291 * @param pUserId long which represent the receiver0
292 * @param pFolderId folder id (as long) which represent the folder to copy and create the link
293 * @param pDestFolderId the folder id where the file will be copy (as long)
294 * @param pSourcePortlet a String representing the origin portlet. It's specifically for quota
295 * @return The folder copied (Folder object)
297 public static Folder copyFolder(long pUserId, long pFolderId, long pDestFolderId) throws PortalException, SystemException, IOException {
298 return copyFolder(pUserId, pFolderId, pDestFolderId, true, null);
302 * Copy a folder as a link (without it's content)
303 * @param pUserId long which represent the receiver0
304 * @param pFolderId folder id (as long) which represent the folder to copy and create the link
305 * @param pDestFolderId the folder id where the file will be copy (as long)
306 * @param pSourcePortlet a String representing the origin portlet. It's specifically for quota
307 * @return The folder copied (Folder object)
309 public static Folder copyLinkedFolder(long pUserId, long pFolderId, long pDestFolderId, String pOriginPortlet) throws PortalException, SystemException, IOException {
310 return copyFolder(pUserId, pFolderId, pDestFolderId, false, pOriginPortlet);
314 * Copy a folder (with or without it's content)
315 * @param pUserId long which represent the receiver0
316 * @param pFolderId folder id (as long) which represent the folder to copy and create the link
317 * @param pDestFolderId the folder id where the file will be copy (as long)
318 * @param pCopyFileContent boolean which represent if it's necessary to sopy the file content or juste create a link
319 * @param pSourcePortlet a String representing the origin portlet. It's specifically for quota
320 * @return The folder copied (Folder object)
322 public static Folder copyFolder(long pUserId, long pFolderId, long pDestFolderId, boolean pCopyFileContent, String pOriginPortlet) throws PortalException, SystemException, IOException {
323 final User user = UserLocalServiceUtil.getUser(pUserId);
324 final Folder folder = DLAppLocalServiceUtil.getFolder(pFolderId);
325 final Folder destFolder = DLAppLocalServiceUtil.getFolder(pDestFolderId);
326 //Verifie que le dossier de destination n'es pas un sous dossier du dossier a copier.
327 long currFolderId = destFolder.getFolderId();
328 while (currFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
329 if (currFolderId == pFolderId)
331 currFolderId = DLAppLocalServiceUtil.getFolder(currFolderId).getParentFolderId();
333 // Ajout des permissions
334 ServiceContext serviceContext = new ServiceContext();
335 serviceContext.setAddGroupPermissions(true);
336 serviceContext.setAddGuestPermissions(true);
338 Folder newFolder = DLAppUtil.addFolder(user.getUserId(), destFolder.getGroupId(), destFolder.getFolderId(), folder.getName());
340 //si pas de probleme de nom (qui auraient leve une DuplicateFolderNameException)
342 for (Object entry : DLAppLocalServiceUtil.getFoldersAndFileEntriesAndFileShortcuts(folder.getGroupId(), folder.getFolderId(), WorkflowConstants.STATUS_ANY, true, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
343 if (entry instanceof Folder) {
344 final Folder currFolder = (Folder) entry;
345 copyFolder(user.getUserId(), currFolder.getFolderId(), newFolder.getFolderId(), pCopyFileContent, pOriginPortlet);
346 } else if (entry instanceof FileEntry) {
347 final FileEntry currFe = (FileEntry) entry;
349 if (pCopyFileContent) {
350 copyFileEntry(user.getUserId(), currFe.getFileEntryId(), newFolder.getFolderId());
353 copyLinkedFileEntry(user.getUserId(), currFe.getFileEntryId(), newFolder.getFolderId(), pOriginPortlet);
356 } else if (entry instanceof DLFileShortcut) {
363 public static boolean isSignetFile(InputStream is){
366 file = FileUtil.createTempFile();
367 FileUtil.write(file, is);
369 Document doc = Jsoup.parse(file, null);
370 List<Node>nodes = doc.childNodes();
371 Elements dts = doc.select("DT");
372 for (Node node : nodes) {
373 if (node instanceof DocumentType) {
374 DocumentType documentType = (DocumentType)node;
375 return documentType.toString().toLowerCase().contains("bookmark") && (dts.size() == 1);
378 } catch (Exception e){
388 public static boolean isSignetFile(DLFileEntry dlFileEntry){
390 InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(dlFileEntry.getUserId(), dlFileEntry.getFileEntryId(), dlFileEntry.getVersion());
391 return isSignetFile(is);
392 } catch (Exception e){
398 public static boolean isSignetFile(FileEntry fileEntry){
400 InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(fileEntry.getUserId(), fileEntry.getFileEntryId(), fileEntry.getVersion());
401 return isSignetFile(is);
402 } catch (Exception e){
408 public static String getUrlSignet(InputStream is){
411 file = FileUtil.createTempFile();
412 FileUtil.write(file, is);
414 Document doc = Jsoup.parse(file, null);
415 Element a = doc.select("A").first();
416 return a.attr("HREF");
417 } catch (Exception e) {
428 public static String getUrlSignet(FileEntry fileEntry) throws IOException{
429 InputStream is = null;
431 is = DLFileEntryLocalServiceUtil.getFileAsStream(fileEntry.getUserId(), fileEntry.getFileEntryId(), fileEntry.getVersion());
432 return getUrlSignet(is);
433 } catch (Exception e) {
440 public static String[] splitFileName(String fname) {
441 // retourne le nom du fichier et l'extension du fichier "String" en se basant sur le dernier '.' du nom
442 // --> retourne chaine vide si pas de point dans le nom !!!!!
443 String[] result = new String[2];
444 int dotIndex = fname.lastIndexOf(".");
445 if (dotIndex != -1) {
446 result[0] = fname.substring(0, dotIndex);
447 result[1] = fname.substring(dotIndex+1);
457 * Convert an image as a file into a base 64 string by compressing and resizing it
463 * @throws PortalException
464 * @throws SystemException
465 * @throws IOException
467 public static String convertImageToBase64(File file, String extension, boolean compress, double maxWidth, double maxHeight) throws PortalException, SystemException, IOException {
468 byte[] fileArray = reduceImageSize(file, extension, compress, maxWidth, maxHeight);
469 String fileBase64 = Base64.encode(fileArray);
474 * Convert an image as a byte array into a base 64 string by compressing and resizing it
480 * @throws PortalException
481 * @throws SystemException
482 * @throws IOException
484 public static String convertImageToBase64(byte[] byteArray, String extension, boolean compress, double maxWidth, double maxHeight) throws PortalException, SystemException, IOException {
485 byte[] fileArray = reduceImageSize(byteArray, extension, compress, maxWidth, maxHeight);
486 String fileBase64 = Base64.encode(fileArray);
491 * Reduces the size of an image by compressing and resizing it
492 * Doesn't work for tiff files
498 * @throws IOException
500 public static byte[] reduceImageSize(File file, String extension, boolean compress, double maxWidth, double maxHeight) throws IOException {
502 // Le format tiff n'est pas supporte
503 if(extension.equals("tiff") || extension.equals("tif")){
504 return FileUtil.getBytes(file);
507 byte[] fileArray = null;
508 RenderedImage renderedImage = ImageToolUtil.read(file).getRenderedImage();
510 maxWidth = maxWidth==0?renderedImage.getWidth():maxWidth;
511 maxHeight = maxHeight==0?renderedImage.getHeight():maxHeight;
514 * Redimensionne l'image + compression
516 if((renderedImage.getWidth()>maxWidth || renderedImage.getHeight()>maxHeight)){
517 double widthScale = maxWidth/renderedImage.getWidth();
518 double heightScale = maxHeight/renderedImage.getHeight();
519 double scale = Math.min(widthScale, heightScale);
521 double width = renderedImage.getWidth() * scale;
522 double height = renderedImage.getHeight() * scale;
524 RenderedImage thumbnail = ImageToolUtil.scale(renderedImage, (int) height, (int) width);
527 fileArray = compressImage(thumbnail, extension);
530 if(!compress || fileArray == null){
531 ByteArrayOutputStream bam = new ByteArrayOutputStream();
532 ImageIO.write(thumbnail, extension, bam);
533 fileArray = bam.toByteArray();
538 * Compression uniquement
541 fileArray = compressImage(renderedImage, extension);
542 if(fileArray == null){
543 fileArray = FileUtil.getBytes(file);
547 * Aucun traitement sur l'image
550 fileArray = FileUtil.getBytes(file);
558 * Reduces the size of an image by compressing and resizing it
559 * Doesn't work for tiff files
565 * @throws IOException
567 public static byte[] reduceImageSize(byte[] byteArray, String extension, boolean compress, double maxWidth, double maxHeight) throws IOException {
569 // Le format tiff n'est pas supporte
570 if(extension.equals("tiff") || extension.equals("tif")){
574 byte[] fileArray = null;
575 RenderedImage renderedImage = ImageToolUtil.read(byteArray).getRenderedImage();
577 maxWidth = maxWidth==0?renderedImage.getWidth():maxWidth;
578 maxHeight = maxHeight==0?renderedImage.getHeight():maxHeight;
581 * Redimensionne l'image + compression
583 if((renderedImage.getWidth()>maxWidth || renderedImage.getHeight()>maxHeight)){
584 double widthScale = maxWidth/renderedImage.getWidth();
585 double heightScale = maxHeight/renderedImage.getHeight();
586 double scale = Math.min(widthScale, heightScale);
588 double width = renderedImage.getWidth() * scale;
589 double height = renderedImage.getHeight() * scale;
591 RenderedImage thumbnail = ImageToolUtil.scale(renderedImage, (int) height, (int) width);
594 fileArray = compressImage(thumbnail, extension);
597 if(!compress || fileArray == null){
598 ByteArrayOutputStream bam = new ByteArrayOutputStream();
599 ImageIO.write(thumbnail, extension, bam);
600 fileArray = bam.toByteArray();
605 * Compression uniquement
608 fileArray = compressImage(renderedImage, extension);
609 if(fileArray == null){
610 fileArray = byteArray;
614 * Aucun traitement sur l'image
617 fileArray = byteArray;
624 * Compress an image (only for JPEG)
625 * See lib jai_imageio-1.1.jar for other formats
629 * @throws IOException
631 public static byte[] compressImage(RenderedImage image, String extension) throws IOException {
633 Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(extension);
636 ImageWriter writer = (ImageWriter) iter.next();
638 ByteArrayOutputStream bos = new ByteArrayOutputStream();
639 ImageOutputStream ios = ImageIO.createImageOutputStream(bos);
640 writer.setOutput(ios);
642 ImageWriteParam iwparam = writer.getDefaultWriteParam();
643 if(extension.equals("jpg") || extension.equals("jpeg")){
644 iwparam = new JPEGImageWriteParam(Locale.getDefault());
645 iwparam.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
646 iwparam.setCompressionQuality(0.7F);
649 writer.write(null, new IIOImage(image, null, null), iwparam);
655 return bos.toByteArray();
665 public static void updateLinkedFolderContent(long groupId, long folderId) throws PortalException, SystemException {
666 // check a folder and its content to reintegrate all file linked content
667 List<Folder> folders = DLAppLocalServiceUtil.getFolders(groupId, folderId);
668 for (Folder dlf: folders) {
669 updateLinkedFolderContent(groupId, dlf.getFolderId());
671 List<FileEntry> files = DLAppLocalServiceUtil.getFileEntries(groupId, folderId);
672 for (FileEntry dlfe: files) {
673 updateLinkedFileContent(dlfe.getFileEntryId());
677 public static void updateLinkedFileContent(long oldFileId, long newFileId) throws PortalException, SystemException {
678 // update file content after a move of it so new file....
681 dll = DlLinkLocalServiceUtil.getDlFileLinkByTarget(oldFileId);
682 dll.setTargetId(newFileId);
683 DlLinkLocalServiceUtil.updateDlLink(dll);
684 updateLinkedFileContent(newFileId);
686 catch(Exception exc) {
691 public static void updateLinkedFileContent(long fileId) throws PortalException, SystemException {
693 InputStream is = null;
694 FileEntry sourceLinkedFile = null;
697 dll = DlLinkLocalServiceUtil.getDlFileLinkByTarget(fileId);
698 sourceLinkedFile = DLAppLocalServiceUtil.getFileEntry(dll.getSourceId());
699 is = sourceLinkedFile.getContentStream();
700 } catch (Exception e1) {
701 String stackString = "";
702 for(int iterator = 0 ; iterator < e1.getStackTrace().length ; iterator++){
703 stackString += e1.getStackTrace()[iterator].toString() + "\n";
705 logger.warn(stackString);
709 catch (IOException e) {
718 // on met a jour le file!!!!!
719 FileEntry f = DLAppLocalServiceUtil.getFileEntry(fileId);
720 DLStoreUtil.updateFile(
729 // set new permission to file
730 PermissionsUtils.setParentPermissionToFile(f);
733 catch (Exception e) {
740 catch (IOException e){
747 if (sourceLinkedFile!=null) {
748 // we have a link so we delete it
749 DlLinkLocalServiceUtil.removeDlFileLinkByTarget(dll.getTargetId());
750 int linkLeft = DlLinkLocalServiceUtil.countDlFileLinkBySourceId(sourceLinkedFile.getFileEntryId());
752 // plus de lien donc on supprime la source
753 DLAppLocalServiceUtil.deleteFileEntry(sourceLinkedFile.getFileEntryId());
754 // on verifie le dossier contenant pour suppression le cas echeant en recurssif
755 removeLinkedFileContainer(sourceLinkedFile.getFolderId());
760 public static void removeLinkedFileContainer(long folderId) throws SystemException, PortalException {
761 // on verifie le nombre de fichier dans le dossier pour le supprimer si besoin
762 Folder folder = DLAppLocalServiceUtil.getFolder(folderId);
763 if (folder.getParentFolderId() == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
764 // on est au niveau de sending box donc on arrete
767 // on supprime les dossiers vers le bas!
768 List<Folder> childFolders = DLAppLocalServiceUtil.getFolders(folder.getGroupId(), folderId);
769 for (Folder child: childFolders) {
770 removeEmptyChildsFolders(child.getFolderId());
773 if (DLAppLocalServiceUtil.getFoldersAndFileEntriesAndFileShortcutsCount(folder.getGroupId(), folderId, WorkflowConstants.STATUS_ANY, true)==0) {
774 // on supprime le dossier et on remonte
775 DLAppLocalServiceUtil.deleteFolder(folderId);
776 removeLinkedFileContainer(folder.getParentFolderId());
780 public static void removeEmptyChildsFolders(long folderId) throws PortalException, SystemException {
781 Folder folder = DLAppLocalServiceUtil.getFolder(folderId);
782 List<Folder> childFolders = DLAppLocalServiceUtil.getFolders(folder.getGroupId(), folderId);
783 for (Folder child: childFolders) {
784 removeEmptyChildsFolders(child.getFolderId());
787 if (DLAppLocalServiceUtil.getFoldersAndFileEntriesAndFileShortcutsCount(folder.getGroupId(), folderId, WorkflowConstants.STATUS_ANY, true)==0) {
788 // on supprime le dossier et on remonte
789 DLAppLocalServiceUtil.deleteFolder(folderId);
793 public static String formatFileSize(double size, Locale locale) {
794 NumberFormat nf = NumberFormat.getInstance(locale);
795 nf.setMaximumFractionDigits(1);
796 nf.setMinimumFractionDigits(1);
798 double koSize = size / 1024.0;
799 double moSize = koSize / 1024.0;
800 double goSize = moSize / 1024.0;
801 double toSize = goSize / 1024.0;
802 if (goSize > 1024.0) {
803 return nf.format(toSize) + " To";
804 } else if (moSize > 1024.0) {
805 return nf.format(goSize) + " Go";
806 } else if (koSize > 1024.0) {
807 return nf.format(moSize) + " Mo";
808 } else if (size > 1024.0) {
809 return nf.format(koSize) + " Ko";
811 return nf.format(size) + " o";
815 public static String formatFileSizeToKo(double size, Locale locale) {
816 NumberFormat nf = NumberFormat.getInstance(locale);
817 nf.setMaximumFractionDigits(1);
818 nf.setMinimumFractionDigits(1);
820 double koSize = size / 1024.0;
821 return nf.format(koSize) + " Ko";
825 * This method return the size of the folder in parameter
826 * @param folderId the folder id, this is the id of folder you want to define the size
827 * @return long which contain the size in octet of the folder
828 * @throws PortalException liferay exception
829 * @throws SystemException liferay exception
831 public static long getFolderTotalSize(long folderId) throws PortalException, SystemException{
834 Folder folder = DLAppLocalServiceUtil.getFolder(folderId);
836 // get the size of sub-folder
837 for (DLFolder subFolder : FoldersUtil.getSubFolders(folder)) {
838 totalSize += getFolderTotalSize (subFolder.getFolderId());
841 for (DLFileEntry file : FoldersUtil.getFilesEntriesInFolder(folder)) {
842 totalSize += file.getSize();