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