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.portlet.trash.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.search.Document;
022    import com.liferay.portal.kernel.search.Field;
023    import com.liferay.portal.kernel.search.Hits;
024    import com.liferay.portal.kernel.trash.TrashHandler;
025    import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
026    import com.liferay.portal.kernel.util.CharPool;
027    import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
028    import com.liferay.portal.kernel.util.FileUtil;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.OrderByComparator;
031    import com.liferay.portal.kernel.util.PrefsPropsUtil;
032    import com.liferay.portal.kernel.util.PropsKeys;
033    import com.liferay.portal.kernel.util.PropsUtil;
034    import com.liferay.portal.kernel.util.StringBundler;
035    import com.liferay.portal.kernel.util.StringPool;
036    import com.liferay.portal.kernel.util.StringUtil;
037    import com.liferay.portal.kernel.util.UnicodeProperties;
038    import com.liferay.portal.kernel.util.Validator;
039    import com.liferay.portal.model.Group;
040    import com.liferay.portal.service.GroupLocalServiceUtil;
041    import com.liferay.portal.theme.ThemeDisplay;
042    import com.liferay.portlet.documentlibrary.NoSuchFileException;
043    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
044    import com.liferay.portlet.trash.model.TrashEntry;
045    import com.liferay.portlet.trash.model.impl.TrashEntryImpl;
046    import com.liferay.portlet.trash.service.TrashEntryLocalServiceUtil;
047    import com.liferay.portlet.trash.util.comparator.EntryCreateDateComparator;
048    import com.liferay.portlet.trash.util.comparator.EntryTypeComparator;
049    import com.liferay.portlet.trash.util.comparator.EntryUserNameComparator;
050    
051    import java.text.Format;
052    
053    import java.util.ArrayList;
054    import java.util.Date;
055    import java.util.List;
056    
057    /**
058     * @author Sergio González
059     * @author Julio Camarero
060     */
061    public class TrashImpl implements Trash {
062    
063            public String appendTrashNamespace(String title) {
064                    return appendTrashNamespace(title, StringPool.SLASH);
065            }
066    
067            public String appendTrashNamespace(String title, String separator) {
068                    StringBundler sb = new StringBundler(3);
069    
070                    sb.append(title);
071                    sb.append(separator);
072                    sb.append(System.currentTimeMillis());
073    
074                    return sb.toString();
075            }
076    
077            public void deleteEntriesAttachments(
078                            long companyId, long repositoryId, Date date,
079                            String[] attachmentFileNames)
080                    throws PortalException, SystemException {
081    
082                    for (String attachmentFileName : attachmentFileNames) {
083                            String trashTime = TrashUtil.getTrashTime(
084                                    attachmentFileName, TrashUtil.TRASH_TIME_SEPARATOR);
085    
086                            long timestamp = GetterUtil.getLong(trashTime);
087    
088                            if (timestamp < date.getTime()) {
089                                    DLStoreUtil.deleteDirectory(
090                                            companyId, repositoryId, attachmentFileName);
091                            }
092                    }
093            }
094    
095            public List<TrashEntry> getEntries(Hits hits) {
096                    List<TrashEntry> entries = new ArrayList<TrashEntry>();
097    
098                    for (Document document : hits.getDocs()) {
099                            String entryClassName = GetterUtil.getString(
100                                    document.get(Field.ENTRY_CLASS_NAME));
101                            long classPK = GetterUtil.getLong(
102                                    document.get(Field.ENTRY_CLASS_PK));
103    
104                            try {
105                                    TrashEntry entry = TrashEntryLocalServiceUtil.fetchEntry(
106                                            entryClassName, classPK);
107    
108                                    if (entry == null) {
109                                            String userName = GetterUtil.getString(
110                                                    document.get(Field.REMOVED_BY_USER_NAME));
111    
112                                            Date removedDate = document.getDate(Field.REMOVED_DATE);
113    
114                                            entry = new TrashEntryImpl();
115    
116                                            entry.setClassName(entryClassName);
117                                            entry.setClassPK(classPK);
118    
119                                            entry.setUserName(userName);
120                                            entry.setCreateDate(removedDate);
121    
122                                            String rootEntryClassName = GetterUtil.getString(
123                                                    document.get(Field.ROOT_ENTRY_CLASS_NAME));
124                                            long rootEntryClassPK = GetterUtil.getLong(
125                                                    document.get(Field.ROOT_ENTRY_CLASS_PK));
126    
127                                            TrashEntry rootTrashEntry =
128                                                    TrashEntryLocalServiceUtil.fetchEntry(
129                                                            rootEntryClassName, rootEntryClassPK);
130    
131                                            if (rootTrashEntry != null) {
132                                                    entry.setRootEntry(rootTrashEntry);
133                                            }
134                                    }
135    
136                                    entries.add(entry);
137                            }
138                            catch (Exception e) {
139                                    if (_log.isWarnEnabled()) {
140                                            _log.warn(
141                                                    "Unable to find trash entry for " + entryClassName +
142                                                            " with primary key " + classPK);
143                                    }
144                            }
145                    }
146    
147                    return entries;
148            }
149    
150            public OrderByComparator getEntryOrderByComparator(
151                    String orderByCol, String orderByType) {
152    
153                    boolean orderByAsc = false;
154    
155                    if (orderByType.equals("asc")) {
156                            orderByAsc = true;
157                    }
158    
159                    OrderByComparator orderByComparator = null;
160    
161                    if (orderByCol.equals("removed-by")) {
162                            orderByComparator = new EntryUserNameComparator(orderByAsc);
163                    }
164                    else if (orderByCol.equals("removed-date")) {
165                            orderByComparator = new EntryCreateDateComparator(orderByAsc);
166                    }
167                    else if (orderByCol.equals("type")) {
168                            orderByComparator = new EntryTypeComparator(orderByAsc);
169                    }
170    
171                    return orderByComparator;
172            }
173    
174            public int getMaxAge(Group group) throws PortalException, SystemException {
175                    if (group.isLayout()) {
176                            group = group.getParentGroup();
177                    }
178    
179                    int trashEntriesMaxAge = PrefsPropsUtil.getInteger(
180                            group.getCompanyId(), PropsKeys.TRASH_ENTRIES_MAX_AGE,
181                            GetterUtil.getInteger(
182                                    PropsUtil.get(PropsKeys.TRASH_ENTRIES_MAX_AGE)));
183    
184                    UnicodeProperties typeSettingsProperties =
185                            group.getTypeSettingsProperties();
186    
187                    return GetterUtil.getInteger(
188                            typeSettingsProperties.getProperty("trashEntriesMaxAge"),
189                            trashEntriesMaxAge);
190            }
191    
192            public String getNewName(ThemeDisplay themeDisplay, String oldName) {
193                    Format dateFormatDateTime = FastDateFormatFactoryUtil.getDateTime(
194                            themeDisplay.getLocale(), themeDisplay.getTimeZone());
195    
196                    StringBundler sb = new StringBundler(5);
197    
198                    sb.append(oldName);
199                    sb.append(StringPool.SPACE);
200                    sb.append(StringPool.OPEN_PARENTHESIS);
201                    sb.append(
202                            StringUtil.replace(
203                                    dateFormatDateTime.format(new Date()), CharPool.SLASH,
204                                    CharPool.PERIOD));
205                    sb.append(StringPool.CLOSE_PARENTHESIS);
206    
207                    return sb.toString();
208            }
209    
210            public String getTrashTime(String title, String separator) {
211                    int index = title.lastIndexOf(separator);
212    
213                    if (index < 0) {
214                            return StringPool.BLANK;
215                    }
216    
217                    return title.substring(index + 1, title.length());
218            }
219    
220            public boolean isInTrash(String className, long classPK)
221                    throws PortalException, SystemException {
222    
223                    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
224                            className);
225    
226                    if (trashHandler == null) {
227                            return false;
228                    }
229    
230                    return trashHandler.isInTrash(classPK);
231            }
232    
233            public boolean isTrashEnabled(long groupId)
234                    throws PortalException, SystemException {
235    
236                    Group group = GroupLocalServiceUtil.getGroup(groupId);
237    
238                    if (group.isLayout()) {
239                            group = group.getParentGroup();
240                    }
241    
242                    UnicodeProperties typeSettingsProperties =
243                            group.getTypeSettingsProperties();
244    
245                    int trashEnabledCompany = PrefsPropsUtil.getInteger(
246                            group.getCompanyId(), PropsKeys.TRASH_ENABLED);
247    
248                    if (trashEnabledCompany == TrashUtil.TRASH_DISABLED) {
249                            return false;
250                    }
251    
252                    int trashEnabledGroup = GetterUtil.getInteger(
253                            typeSettingsProperties.getProperty("trashEnabled"),
254                            TrashUtil.TRASH_DEFAULT_VALUE);
255    
256                    if ((trashEnabledGroup == TrashUtil.TRASH_ENABLED) ||
257                            ((trashEnabledCompany == TrashUtil.TRASH_ENABLED_BY_DEFAULT) &&
258                             (trashEnabledGroup == TrashUtil.TRASH_DEFAULT_VALUE))) {
259    
260                            return true;
261                    }
262    
263                    return false;
264            }
265    
266            public void moveAttachmentFromTrash(
267                            long companyId, long repositoryId, String deletedFileName,
268                            String attachmentsDir)
269                    throws PortalException, SystemException {
270    
271                    moveAttachmentFromTrash(
272                            companyId, repositoryId, deletedFileName, attachmentsDir,
273                            StringPool.UNDERLINE);
274            }
275    
276            public void moveAttachmentFromTrash(
277                            long companyId, long repositoryId, String deletedFileName,
278                            String attachmentsDir, String separator)
279                    throws PortalException, SystemException {
280    
281                    if (Validator.isNull(deletedFileName)) {
282                            return;
283                    }
284    
285                    if (!DLStoreUtil.hasDirectory(
286                                    companyId, repositoryId, attachmentsDir)) {
287    
288                            DLStoreUtil.addDirectory(companyId, repositoryId, attachmentsDir);
289                    }
290    
291                    StringBundler sb = new StringBundler(3);
292    
293                    sb.append(attachmentsDir);
294                    sb.append(StringPool.FORWARD_SLASH);
295                    sb.append(
296                            stripTrashNamespace(
297                                    FileUtil.getShortFileName(deletedFileName), separator));
298    
299                    String fileName = sb.toString();
300    
301                    try {
302                            DLStoreUtil.updateFile(
303                                    companyId, repositoryId, deletedFileName, fileName);
304                    }
305                    catch (NoSuchFileException nsfe) {
306                    }
307            }
308    
309            public String moveAttachmentToTrash(
310                            long companyId, long repositoryId, String fileName,
311                            String deletedAttachmentsDir)
312                            throws PortalException, SystemException {
313    
314                    return moveAttachmentToTrash(
315                            companyId, repositoryId, fileName, deletedAttachmentsDir,
316                            StringPool.UNDERLINE);
317            }
318    
319            public String moveAttachmentToTrash(
320                            long companyId, long repositoryId, String fileName,
321                            String deletedAttachmentsDir, String separator)
322                    throws PortalException, SystemException {
323    
324                    if (Validator.isNull(fileName)) {
325                            return StringPool.BLANK;
326                    }
327    
328                    if (!DLStoreUtil.hasDirectory(
329                                    companyId, repositoryId, deletedAttachmentsDir)) {
330    
331                            DLStoreUtil.addDirectory(
332                                    companyId, repositoryId, deletedAttachmentsDir);
333                    }
334    
335                    StringBundler sb = new StringBundler(3);
336    
337                    sb.append(deletedAttachmentsDir);
338                    sb.append(StringPool.FORWARD_SLASH);
339                    sb.append(
340                            appendTrashNamespace(
341                                    FileUtil.getShortFileName(fileName), separator));
342    
343                    String deletedFileName = sb.toString();
344    
345                    try {
346                            DLStoreUtil.updateFile(
347                                    companyId, repositoryId, fileName, deletedFileName);
348                    }
349                    catch (NoSuchFileException nsfe) {
350                            DLStoreUtil.deleteDirectory(
351                                    companyId, repositoryId, deletedAttachmentsDir);
352                    }
353    
354                    return deletedFileName;
355            }
356    
357            public String stripTrashNamespace(String title) {
358                    return stripTrashNamespace(title, StringPool.SLASH);
359            }
360    
361            public String stripTrashNamespace(String title, String separator) {
362                    int index = title.lastIndexOf(separator);
363    
364                    if (index < 0) {
365                            return title;
366                    }
367    
368                    return title.substring(0, index);
369            }
370    
371            private Log _log = LogFactoryUtil.getLog(TrashImpl.class);
372    
373    }