--- /dev/null
+package com.pentila.entSavoie.utils;
+
+import java.awt.Graphics;
+import java.awt.Image;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.text.Normalizer;
+import java.util.Enumeration;
+
+import javax.imageio.ImageIO;
+import javax.portlet.ResourceRequest;
+
+import org.apache.commons.compress.utils.IOUtils;
+
+import com.liferay.portal.kernel.exception.PortalException;
+import com.liferay.portal.kernel.exception.SystemException;
+import com.liferay.portal.kernel.log.Log;
+import com.liferay.portal.kernel.log.LogFactoryUtil;
+import com.liferay.portal.kernel.repository.model.FileEntry;
+import com.liferay.portal.kernel.repository.model.Folder;
+import com.liferay.portal.kernel.search.Indexer;
+import com.liferay.portal.kernel.search.IndexerRegistryUtil;
+import com.liferay.portal.kernel.upload.UploadPortletRequest;
+import com.liferay.portal.kernel.util.FileUtil;
+import com.liferay.portal.kernel.util.MimeTypesOverrideUtil;
+import com.liferay.portal.kernel.util.ParamUtil;
+import com.liferay.portal.kernel.util.StringPool;
+import com.liferay.portal.kernel.util.Validator;
+import com.liferay.portal.model.CompanyConstants;
+import com.liferay.portal.model.Organization;
+import com.liferay.portal.model.User;
+import com.liferay.portal.service.ServiceContext;
+import com.liferay.portal.theme.ThemeDisplay;
+import com.liferay.portal.util.PortalUtil;
+import com.liferay.portlet.documentlibrary.DuplicateFileException;
+import com.liferay.portlet.documentlibrary.FileSizeException;
+import com.liferay.portlet.documentlibrary.model.DLFileEntry;
+import com.liferay.portlet.documentlibrary.model.DLFolder;
+import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
+import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
+import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
+import com.liferay.portlet.documentlibrary.util.DLUtil;
+import com.liferay.portlet.documentlibrary.util.ImageProcessorUtil;
+import com.pentila.entSavoie.cartable.service.CasierEntryLocalServiceUtil;
+import com.pentila.entSavoie.directory.OrganizationFinderServiceUtil;
+
+public class DLAppUtil {
+
+ private final static int NEWS_THUMBNAIL_SIZE = 500;
+
+ /**
+ * Add Image From Existing File
+ * @param user
+ * @param tmpImageId
+ * @param type
+ * @return
+ * @throws PortalException
+ * @throws SystemException
+ */
+ public static long addImage(User user, long imageId, String portlet) throws PortalException, SystemException{
+
+ // Get temporary image
+ DLFileEntry dlfe = DLFileEntryLocalServiceUtil.getFileEntry(imageId);
+
+ // Get folder
+ Organization cds = ENTOrganizationsUtil.getOrCreateRootOrg(user.getCompanyId());
+
+ Long cdsGpId = cds.getGroup().getGroupId();
+ Folder folder = null;
+ if(portlet.equals(PortletConstant.ACTUALITES)){
+ folder = IGFoldersUtil.getActuFolder(user.getUserId(), cdsGpId);
+ }
+ else if(portlet.equals(PortletConstant.CANTINE)){
+ folder = IGFoldersUtil.getRecetteFolder(user.getUserId(), cdsGpId);
+ }
+ else if(portlet.equals(PortletConstant.GESTION_MENU)){
+ folder = IGFoldersUtil.getMenuImgFolder(user.getUserId(), cdsGpId);
+ }
+
+ // Copy image
+ FileEntry tempFe = DLAppLocalServiceUtil.getFileEntry(imageId);
+ FileEntry fe = null;
+
+ // Title
+ String originalTitle = dlfe.getTitle();
+ String[] splitFN = DLEntryUtil.splitFileName(originalTitle);
+ originalTitle = splitFN[0];
+ String extension = "." + splitFN[1];
+
+ // Start copy
+ boolean finished = false;
+ int count = 1;
+ String name = "";
+ String suffixe = "";
+ while (!finished && count<50) {
+ try {
+ name = originalTitle+suffixe+extension;
+
+ fe = DLAppLocalServiceUtil.addFileEntry(
+ user.getUserId(),
+ folder.getGroupId(),
+ folder.getFolderId(),
+ name,
+ dlfe.getMimeType(),
+ name,
+ dlfe.getDescription(),
+ "",
+ tempFe.getContentStream(),
+ tempFe.getSize(),
+ new ServiceContext());
+
+ ImageProcessorUtil.generateImages(fe.getFileVersion());
+
+ // Permissions management
+ PermissionsUtils.setParentPermissionToFile(fe);
+ finished = true;
+ }
+ catch (Exception e) {
+ suffixe = "_" + count;
+ count ++;
+ }
+ }
+
+ // Remove temporary image
+ try {
+ DLAppLocalServiceUtil.deleteFileEntry(dlfe.getFileEntryId());
+ }
+ catch(Exception e){}
+
+ // Return new image id
+ return fe.getFileEntryId();
+ }
+
+ /**
+ * Add Temporary Image From Existing File
+ * @param fileEntryId
+ * @param actionRequest
+ * @return
+ * @throws PortalException
+ * @throws SystemException
+ */
+ public static FileEntry addTemporaryImage(long fileEntryId, User user, String resize) throws PortalException, SystemException {
+
+ boolean fileResized = false;
+
+ // Get file
+ DLFileEntry dlfe = DLFileEntryLocalServiceUtil.getDLFileEntry(fileEntryId);
+
+ // Get temporary folder
+ long tmpUserFolderGroupId = user.getGroup().getGroupId();
+ Folder folder = IGFoldersUtil.getTmpFolder(user.getUserId(), tmpUserFolderGroupId);
+
+ // Image file
+ FileEntry fe = null;
+
+ // Title
+ String originalTitle = dlfe.getTitle();
+ String[] splitFN = DLEntryUtil.splitFileName(originalTitle);
+ originalTitle = splitFN[0];
+ String extension = "." + splitFN[1];
+
+ File tempFile = null;
+
+ if (resize.equals("newsImage")) {
+ try {
+
+ tempFile = File.createTempFile(originalTitle, extension);
+ tempFile.deleteOnExit();
+
+ InputStream in = dlfe.getContentStream();
+ FileOutputStream out;
+ out = new FileOutputStream(tempFile);
+ IOUtils.copy(in, out);
+ in.close();
+ out.close();
+
+ tempFile = resizeImages(tempFile, splitFN[1], NEWS_THUMBNAIL_SIZE, NEWS_THUMBNAIL_SIZE);
+
+ fileResized = true;
+
+ } catch (FileNotFoundException e) {
+ _log.error(e);
+ } catch (IOException e) {
+ _log.error(e);
+ }
+ }
+
+ // Start creation
+ boolean finished = false;
+ int count = 1;
+ String name = "";
+ String suffixe = "";
+ while (!finished && count<50) {
+ try {
+ name = originalTitle+suffixe+extension;
+
+ if (fileResized) {
+ fe = DLAppLocalServiceUtil.addFileEntry(
+ user.getUserId(),
+ folder.getGroupId(),
+ folder.getFolderId(),
+ name,
+ dlfe.getMimeType(),
+ name,
+ "",
+ "",
+ tempFile,
+ new ServiceContext());
+ }
+ else {
+ fe = DLAppLocalServiceUtil.addFileEntry(
+ user.getUserId(),
+ folder.getGroupId(),
+ folder.getFolderId(),
+ name,
+ dlfe.getMimeType(),
+ name,
+ dlfe.getDescription(),
+ "",
+ dlfe.getContentStream(),
+ dlfe.getSize(),
+ new ServiceContext());
+ }
+
+ //ImageProcessorUtil.generateImages(fe.getFileVersion());
+
+ // Permissions management
+ PermissionsUtils.setParentPermissionToFile(fe);
+ finished = true;
+ }
+ catch (Exception e) {
+ count ++;
+ suffixe = "_" + count;
+ }
+ }
+
+ // Return temporary file entry
+ return fe;
+ }
+
+ /**
+ * Add Temporary Image From Upload Request
+ * @param fileEntryId
+ * @param actionRequest
+ * @return
+ * @throws PortalException
+ * @throws SystemException
+ */
+ public static FileEntry addTemporaryImage(UploadPortletRequest uploadRequest, User user, String resize) throws PortalException, SystemException {
+
+ // Get file parameter name
+ String fileNameParam = "";
+ Enumeration<String> enumeration = uploadRequest.getParameterNames();
+ while (enumeration.hasMoreElements()) {
+ String ee = (String) enumeration.nextElement();
+ if (ee.startsWith("ext-gen") || ee.startsWith("uploadedFile") || ee.startsWith("files")){
+ fileNameParam = ee;
+ }
+ }
+
+ // Get file from upload request
+ File file = uploadRequest.getFile(fileNameParam);
+ String sourceFileName = uploadRequest.getFileName(fileNameParam);
+
+ // Image file
+ FileEntry fe = null;
+
+ // Get temporary folder
+ long tmpUserFolderGroupId = user.getGroup().getGroupId();
+ Folder folder = IGFoldersUtil.getTmpFolder(user.getUserId(), tmpUserFolderGroupId);
+
+ // Title
+ String originalTitle = sourceFileName;
+ String[] splitFN = DLEntryUtil.splitFileName(originalTitle);
+ originalTitle = splitFN[0];
+ String extension = "." + splitFN[1];
+ if (resize.equals("newsImage")) {
+ file = resizeImages(file, splitFN[1], NEWS_THUMBNAIL_SIZE, NEWS_THUMBNAIL_SIZE);
+ }
+
+ // Start creation
+ boolean finished = false;
+ int count = 1;
+ String name = "";
+ String suffixe = "";
+ while (!finished && count<50) {
+ try {
+ name = originalTitle+suffixe+extension;
+
+ fe = DLAppLocalServiceUtil.addFileEntry(
+ user.getUserId(),
+ folder.getGroupId(),
+ folder.getFolderId(),
+ name,
+ MimeTypesOverrideUtil.getContentType(file),
+ name,
+ "",
+ "",
+ file,
+ new ServiceContext());
+
+ ImageProcessorUtil.generateImages(fe.getFileVersion());
+
+ // Permissions management
+ PermissionsUtils.setParentPermissionToFile(fe);
+ finished = true;
+ }
+ catch (Exception e) {
+ count ++;
+ suffixe = "_" + count;
+ }
+ }
+
+ // Return temporary file entry
+ return fe;
+ }
+
+ /**
+ * Resize an image received as a file. The resize will keep image proportion
+ * @param file : image to resize
+ * @param extension : image extension
+ * @param maxWidth target max width
+ * @param maxHeight target max height
+ * @return the image resized as a file
+ * @throws IOException
+ */
+ public static File resizeImages(File file, String extension, int maxWidth, int maxHeight) {
+ try {
+ FileInputStream in = new FileInputStream(file);
+ BufferedImage image = ImageIO.read(in);
+
+ if (image == null) {
+ _log.error("ERROR : Null image for file=" + file.getName());
+ in.close();
+ return file;
+ }
+
+ Image scaledImg;
+ if (image.getHeight() <= maxHeight && image.getWidth() <= maxWidth) {
+ in.close();
+ return file;
+ }
+ else if (image.getHeight() < image.getWidth()) {
+ scaledImg = image.getScaledInstance(maxWidth, -1, BufferedImage.SCALE_AREA_AVERAGING);
+ }
+ else {
+ scaledImg = image.getScaledInstance(-1, maxHeight, Image.SCALE_AREA_AVERAGING);
+ }
+
+ image = new BufferedImage(scaledImg.getWidth(null), scaledImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
+
+ Graphics g = image.createGraphics();
+ g.drawImage(scaledImg, 0, 0, null);
+ g.dispose();
+
+ ImageIO.write(image, extension, file);
+ in.close();
+ } catch (IOException e) {
+ _log.error("Error with filename : " + file.getName());
+ _log.error(e);
+ }
+
+ return file;
+ }
+
+ /**
+ * Add temporary file entry from existing file
+ * @param fileEntryId
+ * @return
+ * @throws SystemException
+ * @throws PortalException
+ * @throws IOException
+ */
+ public static FileEntry addTemporaryFileEntry(User user, long fileEntryId) throws PortalException, SystemException, IOException{
+
+ // Get exisiting file
+ FileEntry fe = DLAppLocalServiceUtil.getFileEntry(fileEntryId);
+ InputStream is = fe.getContentStream();
+
+ // Get temporary folder
+ long folderId = user.getGroup().getGroupId();
+ Folder folder = FoldersUtil.getTmpFileFolderBox(user.getUserId(), folderId);
+
+ // Add temporary file entry
+ try {
+ FileEntry fileEntry = addFileEntry(user, folder, fe.getTitle(), is);
+ return fileEntry;
+ } catch (FileSizeException e) {
+ throw new FileSizeException();
+ } catch (IOException e) {
+ throw new IOException();
+ }
+ }
+
+ /**
+ * Add temporary file entry from upload request
+ * @param uploadRequest
+ * @param actionRequest
+ * @return
+ * @throws PortalException
+ * @throws SystemException
+ * @throws IOException
+ */
+ public static FileEntry addTemporaryFileEntry(UploadPortletRequest uploadRequest, User user, long folderId) throws PortalException, SystemException, IOException{
+ return addFileEntry(uploadRequest, user, folderId, true);
+ }
+
+ /**
+ * Add file entry from upload request
+ * @param resourceRequest
+ * @return
+ * @throws PortalException
+ * @throws SystemException
+ * @throws IOException
+ */
+ public static FileEntry addFileEntry(ResourceRequest resourceRequest) throws PortalException, SystemException, IOException{
+ // Get user
+ final User user = PortalUtil.getUser(resourceRequest);
+
+ // Get folder from request
+ long folderId = ParamUtil.getLong(resourceRequest, "folderId");
+
+ return addFileEntry(PortalUtil.getUploadPortletRequest(resourceRequest), user, folderId, false);
+ }
+
+ /**
+ * Add file entry from upload request
+ * @param uploadRequest
+ * @param user
+ * @return
+ * @throws PortalException
+ * @throws SystemException
+ * @throws IOException
+ */
+ public static FileEntry addFileEntry(UploadPortletRequest uploadRequest, User user, long folderId) throws PortalException, SystemException, IOException{
+ return addFileEntry(uploadRequest, user, folderId, false);
+ }
+
+ /**
+ * Add file entry from upload request
+ * @param uploadRequest
+ * @param user
+ * @param temporary
+ * @return
+ * @throws PortalException
+ * @throws SystemException
+ * @throws IOException
+ */
+ public static FileEntry addFileEntry(UploadPortletRequest uploadRequest, User user, long folderId, boolean temporary) throws PortalException, SystemException, IOException {
+
+ // Get file parameter name
+ String fileNameParam = "";
+ Enumeration<String> enumeration = uploadRequest.getParameterNames();
+ while (enumeration.hasMoreElements()) {
+ String ee = enumeration.nextElement();
+ if (ee.startsWith("ext-gen") || ee.startsWith("uploadedFile") || ee.startsWith("files")){
+ fileNameParam=ee;
+ }
+ }
+
+ if(!fileNameParam.isEmpty()){
+ // Get file from upload request
+ final java.io.File file = uploadRequest.getFile(fileNameParam);
+ String sourceFileName = uploadRequest.getFileName(fileNameParam);
+
+ // Get folder
+ Folder folder;
+ if(temporary){
+ // Get temporary folder
+ folderId = user.getGroup().getGroupId();
+ folder = FoldersUtil.getTmpFileFolderBox(user.getUserId(), folderId);
+ }
+ else {
+ if (folderId==0) {
+ folderId = Long.valueOf(uploadRequest.getParameter("folderId")).longValue();
+ }
+ folder = DLAppLocalServiceUtil.getFolder(folderId);
+ }
+
+ // Add file entry
+ try {
+ FileEntry fileEntry = addFileEntry(user, folder, sourceFileName, file);
+ return fileEntry;
+ } catch (FileSizeException e) {
+ throw new FileSizeException();
+ } catch (IOException e) {
+ throw new IOException();
+ }
+ } else {
+ throw new FileSizeException();
+ }
+ }
+
+ /**
+ * Add file entry from repository
+ * @param user
+ * @param dir
+ * @param fileName
+ * @return
+ * @throws PortalException
+ * @throws SystemException
+ * @throws IOException
+ */
+ public static FileEntry addFileEntry(User user, String dir, String fileName) throws PortalException, SystemException, IOException{
+ return addFileEntry(user, FoldersUtil.getDropBox(user.getUserId()), dir, fileName);
+ }
+
+ /**
+ * Add file entry from repository
+ * @param user
+ * @param receiverDropBox
+ * @param dir
+ * @param fileName
+ * @return
+ * @throws PortalException
+ * @throws SystemException
+ * @throws IOException
+ */
+ public static FileEntry addFileEntry(User user, Folder folder, String dir, String fileName) throws PortalException, SystemException, IOException{
+
+ // Get file path
+ if(!dir.endsWith(StringPool.SLASH)){
+ dir += StringPool.SLASH;
+ }
+ String path = dir + fileName;
+
+ // Get file content from disk repository
+ InputStream is = DLStoreUtil.getFileAsStream(
+ user.getCompanyId(), CompanyConstants.SYSTEM, path);
+
+ // Add file entry
+ try {
+ FileEntry fileEntry = addFileEntry(user, folder, fileName, is);
+ return fileEntry;
+ } catch (FileSizeException e) {
+ throw new FileSizeException();
+ } catch (IOException e) {
+ throw new IOException();
+ }
+ }
+
+ /**
+ * Add file entry from file
+ * @param user
+ * @param folder
+ * @param fileName
+ * @param is
+ * @return
+ * @throws PortalException
+ * @throws SystemException
+ * @throws IOException
+ */
+ public static FileEntry addFileEntry(User user, Folder folder, String fileName, InputStream is) throws PortalException, SystemException, IOException{
+ return addFileEntry(user, folder, fileName, FileUtil.getBytes(is));
+ }
+
+ /**
+ * Add file entry from input stream
+ * @param user
+ * @param folder
+ * @param fileName
+ * @param file
+ * @return
+ * @throws PortalException
+ * @throws SystemException
+ * @throws IOException
+ */
+ public static FileEntry addFileEntry(User user, Folder folder, String fileName, File file) throws PortalException, SystemException, IOException{
+ return addFileEntry(user, folder, fileName, FileUtil.getBytes(file));
+ }
+
+ /**
+ * Add file entry from bytes
+ * @param user
+ * @param folder
+ * @param fileName
+ * @param bytes
+ * @return
+ * @throws PortalException
+ * @throws SystemException
+ */
+ public static FileEntry addFileEntry(User user, Folder folder, String fileName, byte[] bytes) throws PortalException, SystemException{
+
+ // Set default permissions
+ ServiceContext serviceContext = new ServiceContext();
+ serviceContext.setAddGroupPermissions(true);
+ serviceContext.setAddGuestPermissions(true);
+
+ // Title
+ String[] splitFN = DLEntryUtil.splitFileName(fileName);
+ String originalTitle = FileNameUtil.getValidName(splitFN[0]);
+ String extension = "." + splitFN[1];
+ String name = "";
+
+ // Start creation
+ boolean finished = false;
+ int count = 0;
+ String suffixe = "";
+ FileEntry fe = null;
+ while (!finished && count<50) {
+ try {
+ name = originalTitle+suffixe+extension;
+ //Normalize the name before adding it to DB. (Solve some issues with accented character with a form "plain char+combining accent" -- e' insteand of é)
+ name = Normalizer.normalize (name, Normalizer.Form.NFC);
+
+ fe = DLAppLocalServiceUtil.addFileEntry(
+ user.getUserId(),
+ folder.getGroupId(),
+ folder.getFolderId(),
+ name,
+ MimeTypesOverrideUtil.getContentType(name),
+ name,
+ "",
+ "",
+ bytes,
+ serviceContext);
+
+ // Permissions management
+ PermissionsUtils.setParentPermissionToFile(fe);
+ finished = true;
+
+ } catch (DuplicateFileException exception) {
+ count ++;
+ suffixe = " (" + count + ")";
+ } catch (FileSizeException e) {
+ throw new FileSizeException();
+ }
+ }
+
+ return fe;
+ }
+
+ /**
+ * Update file entry title
+ * @param fileEntry
+ * @param title
+ * @return
+ */
+ public static FileEntry updateFileEntry(FileEntry fileEntry, String title){
+ byte[] bytes = null;
+ return updateFileEntry(fileEntry, title, bytes);
+ }
+
+ /**
+ * Update file entry content
+ * @param fileEntry
+ * @param is
+ * @return
+ * @throws IOException
+ */
+ public static FileEntry updateFileEntry(FileEntry fileEntry, InputStream is) throws IOException{
+ return updateFileEntry(fileEntry, null, FileUtil.getBytes(is));
+ }
+
+ /**
+ * Update file entry content
+ * @param fileEntry
+ * @param file
+ * @return
+ * @throws IOException
+ */
+ public static FileEntry updateFileEntry(FileEntry fileEntry, File file) throws IOException{
+ return updateFileEntry(fileEntry, null, FileUtil.getBytes(file));
+ }
+
+ /**
+ * Update file entry content
+ * @param fileEntry
+ * @param bytes
+ * @return
+ */
+ public static FileEntry updateFileEntry(FileEntry fileEntry, byte[] bytes){
+ return updateFileEntry(fileEntry, null, bytes);
+ }
+
+ /**
+ * Update file entry title and content
+ * @param fileEntry
+ * @param title
+ * @param is
+ * @return
+ * @throws IOException
+ */
+ public static FileEntry updateFileEntry(FileEntry fileEntry, String title, InputStream is) throws IOException{
+ return updateFileEntry(fileEntry, title, FileUtil.getBytes(is));
+ }
+
+ /**
+ * Update file entry title and content
+ * @param fileEntry
+ * @param title
+ * @param file
+ * @return
+ * @throws IOException
+ */
+ public static FileEntry updateFileEntry(FileEntry fileEntry, String title, File file) throws IOException{
+ return updateFileEntry(fileEntry, title, FileUtil.getBytes(file));
+ }
+
+ /**
+ * Update file entry title and content
+ * @param fileEntry
+ * @param title
+ * @param bytes
+ * @return
+ */
+ public static FileEntry updateFileEntry(FileEntry fileEntry, String title, byte[] bytes){
+ // Set default permissions
+ ServiceContext serviceContext = new ServiceContext();
+ serviceContext.setAddGroupPermissions(true);
+ serviceContext.setAddGuestPermissions(true);
+
+ boolean finished = false;
+ int count = 0;
+ title = Validator.isNull(title)?fileEntry.getTitle():title;
+ String fileTitle = FileNameUtil.getValidName(title);
+
+ //Check if filename contains extension
+ if (fileTitle.endsWith(fileEntry.getExtension())) {
+ // Add one to extension length to count the dot
+ int substringEndIndex = fileTitle.length() - (fileEntry.getExtension().length() + 1);
+ fileTitle = fileTitle.substring(0, substringEndIndex);
+ }
+ String suffixe = "";
+
+ while(!finished && count<50) {
+ try {
+
+ fileEntry = DLAppLocalServiceUtil.updateFileEntry(
+ fileEntry.getUserId(),
+ fileEntry.getFileEntryId(),
+ fileEntry.getTitle(),
+ fileEntry.getMimeType(),
+ fileTitle + suffixe + "." + fileEntry.getExtension(),
+ fileEntry.getDescription(),
+ "",
+ false,
+ bytes,
+ serviceContext);
+
+ // No permissions management because this method just update a file
+
+ try {
+ Indexer indexer = IndexerRegistryUtil.getIndexer(DLFileEntry.class);
+ indexer.reindex(fileEntry);
+ } catch (Exception e) {
+ _log.error("Error when reindexing file after updating file "+fileEntry.getFileEntryId());
+ }
+ finished = true;
+ } catch (Exception exception) {
+ _log.error(exception);
+ count ++;
+ suffixe = " (" + count + ")";
+ }
+ }
+
+ return fileEntry;
+ }
+
+ /**
+ * Add new folder
+ * @param userId
+ * @param groupId
+ * @param parentFolderId
+ * @param name
+ * @return
+ * @throws PortalException
+ * @throws SystemException
+ */
+ public static Folder addFolder(long userId, long groupId, long parentFolderId, String name) throws PortalException, SystemException{
+
+ // Set default permissions
+ ServiceContext serviceContext = new ServiceContext();
+ serviceContext.setAddGroupPermissions(true);
+ serviceContext.setAddGuestPermissions(true);
+
+ boolean finished = false;
+ int count = 0;
+ String folderTitle = FileNameUtil.getValidName(name);
+ String suffixe = "";
+ String title = "";
+ Folder folder = null;
+
+ while(!finished && count<50) {
+ try {
+ title = folderTitle+suffixe;
+ folder = DLAppLocalServiceUtil.addFolder(
+ userId,
+ groupId,
+ parentFolderId,
+ title,
+ "",
+ serviceContext);
+ finished = true;
+ } catch (Exception exception) {
+ count ++;
+ suffixe = " (" + count + ")";
+ }
+ }
+
+ DLAppLocalServiceUtil.updateFolder(
+ folder.getFolderId(),
+ parentFolderId,
+ title,
+ "",
+ serviceContext);
+
+ Indexer indexer = IndexerRegistryUtil.getIndexer(DLFolder.class);
+ indexer.reindex(folder);
+
+ // Permissions management
+ PermissionsUtils.setParentPermissionToFolder(folder);
+ return folder;
+ }
+
+ /**
+ * Update folder name
+ * @param folder
+ * @param name
+ * @return
+ */
+ public static Folder updateFolder(Folder folder, String name){
+ return updateFolder(folder, -1, name);
+ }
+
+ /**
+ * Update folder parent
+ * @param folder
+ * @param parentFolderId
+ * @return
+ */
+ public static Folder updateFolder(Folder folder, long parentFolderId){
+ return updateFolder(folder, parentFolderId, null);
+ }
+
+ /**
+ * Update folder name and parent
+ * @param folder
+ * @param parentFolderId
+ * @param name
+ * @return
+ */
+ public static Folder updateFolder(Folder folder, long parentFolderId, String name){
+
+ // Set default permissions
+ ServiceContext serviceContext = new ServiceContext();
+ serviceContext.setAddGroupPermissions(true);
+ serviceContext.setAddGuestPermissions(true);
+ boolean isRename = (parentFolderId == -1)? true : false;
+
+ boolean finished = false;
+ int count = 0;
+ String folderTitle = Validator.isNull(name)?folder.getName():FileNameUtil.getValidName(name);
+ parentFolderId = isRename?folder.getParentFolderId():parentFolderId;
+ String suffixe = "";
+
+ while(!finished && count<50) {
+ try {
+ folder = DLAppLocalServiceUtil.updateFolder(
+ folder.getFolderId(),
+ parentFolderId,
+ folderTitle + suffixe,
+ folder.getDescription(),
+ serviceContext);
+
+ Indexer indexer = IndexerRegistryUtil.getIndexer(DLFolder.class);
+ indexer.reindex(folder);
+
+ // Permissions management, don't change permission if it's just a rename
+ if(!isRename){
+ PermissionsUtils.setParentPermissionToFolder(folder);
+ }
+ finished = true;
+ } catch (Exception exception) {
+ count ++;
+ suffixe = " (" + count + ")";
+ }
+ }
+
+ return folder;
+ }
+
+ /**
+ * Get folder path
+ * @param folder
+ * @return
+ * @throws PortalException
+ * @throws SystemException
+ */
+ public static String getPath(Folder folder) throws PortalException, SystemException{
+ StringBuilder sb = new StringBuilder();
+
+ while (folder != null) {
+ sb.insert(0, folder.getName());
+ sb.insert(0, StringPool.SLASH);
+
+ folder = folder.getParentFolder();
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * Get image link from file entry
+ * @param fileEntry
+ * @param themeDisplay
+ * @param index
+ * @return
+ * @throws SystemException
+ * @throws PortalException
+ */
+ public static String getImageSrc(FileEntry fileEntry, ThemeDisplay themeDisplay, int index) throws PortalException, SystemException{
+ return DLUtil.getPreviewURL(
+ fileEntry, fileEntry.getFileVersion(), themeDisplay, "&imageThumbnail=" + index,
+ true, true);
+ }
+
+ /**
+ * Get image link from file entry
+ * @param fileEntry
+ * @param themeDisplay
+ * @param index
+ * @return
+ * @throws SystemException
+ * @throws PortalException
+ */
+ public static String getImageSrcRelative(FileEntry fileEntry, ThemeDisplay themeDisplay, int index) throws PortalException, SystemException{
+ return DLUtil.getPreviewURL(
+ fileEntry, fileEntry.getFileVersion(), themeDisplay, "&imageThumbnail=" + index,
+ true, false);
+ }
+
+ /**
+ * Send file to drop box
+ * @param actionRequest
+ * @param user
+ * @param bytes
+ * @param fileName
+ * @return
+ * @throws PortalException
+ * @throws SystemException
+ */
+ public static String sendToDropbox(User user, byte[] bytes, String fileName) throws PortalException, SystemException {
+
+ try {
+ // Get target folder
+ Folder folder = FoldersUtil.getDropBox(user.getUserId());
+
+ // Create file entry from bytes array
+ FileEntry fe = DLAppUtil.addFileEntry(user, folder, fileName, bytes);
+
+ // Send file to drop box
+ CasierEntryLocalServiceUtil.addCasierEntry(user.getCompanyId(), folder.getGroupId(), fe.getFileEntryId(), fe.getUserId(), "", false);
+
+ return "&fileEntryId=" + fe.getFileEntryId();
+
+ } catch (Exception e) {
+ _log.error(e);
+ }
+
+ return "";
+ }
+
+ private static Log _log = LogFactoryUtil.getLog(DLAppUtil.class);
+
+}