001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.portletfilerepository;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.kernel.repository.model.Folder;
022    import com.liferay.portal.kernel.util.FileUtil;
023    import com.liferay.portal.kernel.util.MimeTypesUtil;
024    import com.liferay.portal.kernel.util.ObjectValuePair;
025    import com.liferay.portal.kernel.util.OrderByComparator;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.UnicodeProperties;
028    import com.liferay.portal.kernel.util.UnmodifiableList;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.model.Group;
031    import com.liferay.portal.model.Repository;
032    import com.liferay.portal.model.User;
033    import com.liferay.portal.repository.liferayrepository.LiferayRepository;
034    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
035    import com.liferay.portal.service.GroupLocalServiceUtil;
036    import com.liferay.portal.service.RepositoryLocalServiceUtil;
037    import com.liferay.portal.service.ServiceContext;
038    import com.liferay.portal.service.UserLocalServiceUtil;
039    import com.liferay.portal.util.PortalUtil;
040    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
041    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
042    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
043    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
044    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
045    import com.liferay.portlet.documentlibrary.util.DLAppHelperThreadLocal;
046    
047    import java.io.File;
048    import java.io.IOException;
049    import java.io.InputStream;
050    
051    import java.util.ArrayList;
052    import java.util.List;
053    
054    /**
055     * @author Eudaldo Alonso
056     * @author Alexander Chow
057     */
058    public class PortletFileRepositoryImpl implements PortletFileRepository {
059    
060            public void addPortletFileEntries(
061                            long groupId, long userId, String className, long classPK,
062                            String portletId, long folderId,
063                            List<ObjectValuePair<String, InputStream>> inputStreamOVPs)
064                    throws PortalException, SystemException {
065    
066                    for (int i = 0; i < inputStreamOVPs.size(); i++) {
067                            ObjectValuePair<String, InputStream> inputStreamOVP =
068                                    inputStreamOVPs.get(i);
069    
070                            InputStream inputStream = inputStreamOVP.getValue();
071                            String fileName = inputStreamOVP.getKey();
072    
073                            File file = null;
074    
075                            try {
076                                    file = FileUtil.createTempFile(inputStream);
077    
078                                    String mimeType = MimeTypesUtil.getContentType(file, fileName);
079    
080                                    addPortletFileEntry(
081                                            groupId, userId, className, classPK, portletId, folderId,
082                                            file, fileName, mimeType);
083                            }
084                            catch (IOException ioe) {
085                                    throw new SystemException(
086                                            "Unable to write temporary file", ioe);
087                            }
088                            finally {
089                                    FileUtil.delete(file);
090                            }
091                    }
092            }
093    
094            public FileEntry addPortletFileEntry(
095                            long groupId, long userId, String className, long classPK,
096                            String portletId, long folderId, File file, String fileName,
097                            String mimeType)
098                    throws PortalException, SystemException {
099    
100                    if (Validator.isNull(fileName)) {
101                            return null;
102                    }
103    
104                    ServiceContext serviceContext = new ServiceContext();
105    
106                    serviceContext.setAddGroupPermissions(true);
107                    serviceContext.setAddGuestPermissions(true);
108    
109                    long repositoryId = getPortletRepositoryId(
110                            groupId, portletId, serviceContext);
111    
112                    serviceContext.setAttribute("className", className);
113                    serviceContext.setAttribute("classPK", String.valueOf(classPK));
114    
115                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
116    
117                    try {
118                            DLAppHelperThreadLocal.setEnabled(false);
119    
120                            return DLAppLocalServiceUtil.addFileEntry(
121                                    userId, repositoryId, folderId, fileName, mimeType, fileName,
122                                    StringPool.BLANK, StringPool.BLANK, file, serviceContext);
123                    }
124                    finally {
125                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
126                    }
127            }
128    
129            public FileEntry addPortletFileEntry(
130                            long groupId, long userId, String className, long classPK,
131                            String portletId, long folderId, InputStream inputStream,
132                            String fileName, String mimeType)
133                    throws PortalException, SystemException {
134    
135                    if (inputStream == null) {
136                            return null;
137                    }
138    
139                    byte[] bytes = null;
140    
141                    try {
142                            bytes = FileUtil.getBytes(inputStream, -1, false);
143                    }
144                    catch (IOException ioe) {
145                            return null;
146                    }
147    
148                    ServiceContext serviceContext = new ServiceContext();
149    
150                    serviceContext.setAddGroupPermissions(true);
151                    serviceContext.setAddGuestPermissions(true);
152    
153                    long repositoryId = getPortletRepositoryId(
154                            groupId, portletId, serviceContext);
155    
156                    serviceContext.setAttribute("className", className);
157                    serviceContext.setAttribute("classPK", String.valueOf(classPK));
158    
159                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
160    
161                    try {
162                            DLAppHelperThreadLocal.setEnabled(false);
163    
164                            return DLAppLocalServiceUtil.addFileEntry(
165                                    userId, repositoryId, folderId, fileName, mimeType, fileName,
166                                    StringPool.BLANK, StringPool.BLANK, bytes, serviceContext);
167                    }
168                    finally {
169                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
170                    }
171            }
172    
173            public void deleteFolder(long folderId)
174                    throws PortalException, SystemException {
175    
176                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
177    
178                    try {
179                            DLAppHelperThreadLocal.setEnabled(false);
180    
181                            DLAppLocalServiceUtil.deleteFolder(folderId);
182                    }
183                    finally {
184                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
185                    }
186            }
187    
188            public void deletePortletFileEntries(long groupId, long folderId)
189                    throws PortalException, SystemException {
190    
191                    List<DLFileEntry> dlFileEntries =
192                            DLFileEntryLocalServiceUtil.getFileEntries(groupId, folderId);
193    
194                    for (DLFileEntry dlFileEntry : dlFileEntries) {
195                            deletePortletFileEntry(dlFileEntry.getFileEntryId());
196                    }
197            }
198    
199            public void deletePortletFileEntries(
200                            long groupId, long folderId, int status)
201                    throws PortalException, SystemException {
202    
203                    List<DLFileEntry> dlFileEntries =
204                            DLFileEntryLocalServiceUtil.getFileEntries(
205                                    groupId, folderId, status, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
206                                    null);
207    
208                    for (DLFileEntry dlFileEntry : dlFileEntries) {
209                            deletePortletFileEntry(dlFileEntry.getFileEntryId());
210                    }
211            }
212    
213            public void deletePortletFileEntry(long fileEntryId)
214                    throws PortalException, SystemException {
215    
216                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
217    
218                    try {
219                            DLAppHelperThreadLocal.setEnabled(false);
220    
221                            DLAppLocalServiceUtil.deleteFileEntry(fileEntryId);
222                    }
223                    finally {
224                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
225                    }
226            }
227    
228            public void deletePortletFileEntry(
229                            long groupId, long folderId, String fileName)
230                    throws PortalException, SystemException {
231    
232                    FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(
233                            groupId, folderId, fileName);
234    
235                    deletePortletFileEntry(fileEntry.getFileEntryId());
236            }
237    
238            public void deletePortletRepository(long groupId, String portletId)
239                    throws PortalException, SystemException {
240    
241                    Repository repository = RepositoryLocalServiceUtil.fetchRepository(
242                            groupId, portletId);
243    
244                    if (repository != null) {
245                            RepositoryLocalServiceUtil.deleteRepository(
246                                    repository.getRepositoryId());
247                    }
248            }
249    
250            public List<FileEntry> getPortletFileEntries(long groupId, long folderId)
251                    throws SystemException {
252    
253                    return toFileEntries(
254                            DLFileEntryLocalServiceUtil.getFileEntries(groupId, folderId));
255            }
256    
257            public List<FileEntry> getPortletFileEntries(
258                            long groupId, long folderId, int status)
259                    throws SystemException {
260    
261                    return getPortletFileEntries(
262                            groupId, folderId, status, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
263                            null);
264            }
265    
266            public List<FileEntry> getPortletFileEntries(
267                            long groupId, long folderId, int status, int start, int end,
268                            OrderByComparator obc)
269                    throws SystemException {
270    
271                    return toFileEntries(
272                            DLFileEntryLocalServiceUtil.getFileEntries(
273                                    groupId, folderId, status, start, end, obc));
274            }
275    
276            public int getPortletFileEntriesCount(long groupId, long folderId)
277                    throws SystemException {
278    
279                    return DLFileEntryLocalServiceUtil.getFileEntriesCount(
280                            groupId, folderId);
281            }
282    
283            public int getPortletFileEntriesCount(
284                            long groupId, long folderId, int status)
285                    throws SystemException {
286    
287                    return DLFileEntryLocalServiceUtil.getFileEntriesCount(
288                            groupId, folderId, status);
289            }
290    
291            public FileEntry getPortletFileEntry(long fileEntryId)
292                    throws PortalException, SystemException {
293    
294                    return DLAppLocalServiceUtil.getFileEntry(fileEntryId);
295            }
296    
297            public FileEntry getPortletFileEntry(
298                            long groupId, long folderId, String fileName)
299                    throws PortalException, SystemException {
300    
301                    return DLAppLocalServiceUtil.getFileEntry(groupId, folderId, fileName);
302            }
303    
304            public Folder getPortletFolder(long folderId)
305                    throws PortalException, SystemException {
306    
307                    return DLAppLocalServiceUtil.getFolder(folderId);
308            }
309    
310            public Folder getPortletFolder(
311                            long userId, long repositoryId, long parentFolderId,
312                            String folderName, ServiceContext serviceContext)
313                    throws PortalException, SystemException {
314    
315                    Folder folder = null;
316    
317                    try {
318                            folder = DLAppLocalServiceUtil.getFolder(
319                                    repositoryId, parentFolderId, folderName);
320                    }
321                    catch (NoSuchFolderException nsfe) {
322                            folder = DLAppLocalServiceUtil.addFolder(
323                                    userId, repositoryId, parentFolderId, folderName,
324                                    StringPool.BLANK, serviceContext);
325                    }
326    
327                    return folder;
328            }
329    
330            public long getPortletRepositoryId(
331                            long groupId, String portletId, ServiceContext serviceContext)
332                    throws PortalException, SystemException {
333    
334                    Repository repository = RepositoryLocalServiceUtil.fetchRepository(
335                            groupId, portletId);
336    
337                    if (repository != null) {
338                            return repository.getRepositoryId();
339                    }
340    
341                    Group group = GroupLocalServiceUtil.getGroup(groupId);
342    
343                    User user = UserLocalServiceUtil.getDefaultUser(group.getCompanyId());
344    
345                    long classNameId = PortalUtil.getClassNameId(
346                            LiferayRepository.class.getName());
347                    UnicodeProperties typeSettingsProperties = new UnicodeProperties(true);
348    
349                    return RepositoryLocalServiceUtil.addRepository(
350                            user.getUserId(), groupId, classNameId,
351                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, portletId,
352                            StringPool.BLANK, portletId, typeSettingsProperties, true,
353                            serviceContext);
354            }
355    
356            public void movePortletFileEntryToTrash(long userId, long fileEntryId)
357                    throws PortalException, SystemException {
358    
359                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
360    
361                    try {
362                            DLAppHelperThreadLocal.setEnabled(false);
363    
364                            DLAppLocalServiceUtil.moveFileEntryToTrash(userId, fileEntryId);
365                    }
366                    finally {
367                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
368                    }
369            }
370    
371            public void movePortletFileEntryToTrash(
372                            long groupId, long userId, long folderId, String fileName)
373                    throws PortalException, SystemException {
374    
375                    FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(
376                            groupId, folderId, fileName);
377    
378                    movePortletFileEntryToTrash(userId, fileEntry.getFileEntryId());
379            }
380    
381            public void restorePortletFileEntryFromTrash(long userId, long fileEntryId)
382                    throws PortalException, SystemException {
383    
384                    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();
385    
386                    try {
387                            DLAppHelperThreadLocal.setEnabled(false);
388    
389                            DLAppLocalServiceUtil.restoreFileEntryFromTrash(
390                                    userId, fileEntryId);
391                    }
392                    finally {
393                            DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
394                    }
395            }
396    
397            public void restorePortletFileEntryFromTrash(
398                            long groupId, long userId, long folderId, String fileName)
399                    throws PortalException, SystemException {
400    
401                    FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(
402                            groupId, folderId, fileName);
403    
404                    restorePortletFileEntryFromTrash(userId, fileEntry.getFileEntryId());
405            }
406    
407            /**
408             * @see {@link
409             *      com.liferay.portal.repository.liferayrepository.util.LiferayBase#toFileEntries}
410             */
411            protected List<FileEntry> toFileEntries(List<DLFileEntry> dlFileEntries) {
412                    List<FileEntry> fileEntries = new ArrayList<FileEntry>(
413                            dlFileEntries.size());
414    
415                    for (DLFileEntry dlFileEntry : dlFileEntries) {
416                            FileEntry fileEntry = new LiferayFileEntry(dlFileEntry);
417    
418                            fileEntries.add(fileEntry);
419                    }
420    
421                    if (dlFileEntries instanceof UnmodifiableList) {
422                            return new UnmodifiableList<FileEntry>(fileEntries);
423                    }
424                    else {
425                            return fileEntries;
426                    }
427            }
428    
429    }