001    /**
002     * Copyright (c) 2000-2013 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.service.impl;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.util.UnicodeProperties;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.util.PortalUtil;
021    import com.liferay.portlet.trash.model.TrashVersion;
022    import com.liferay.portlet.trash.service.base.TrashVersionLocalServiceBaseImpl;
023    
024    import java.util.List;
025    
026    /**
027     * @author Zsolt Berentey
028     */
029    public class TrashVersionLocalServiceImpl
030            extends TrashVersionLocalServiceBaseImpl {
031    
032            @Override
033            public void addTrashVersion(
034                            long trashEntryId, String className, long classPK, int status,
035                            UnicodeProperties typeSettingsProperties)
036                    throws SystemException {
037    
038                    long versionId = counterLocalService.increment();
039    
040                    TrashVersion trashVersion = trashVersionPersistence.create(versionId);
041    
042                    trashVersion.setEntryId(trashEntryId);
043                    trashVersion.setClassName(className);
044                    trashVersion.setClassPK(classPK);
045    
046                    if (typeSettingsProperties != null) {
047                            trashVersion.setTypeSettingsProperties(typeSettingsProperties);
048                    }
049    
050                    trashVersion.setStatus(status);
051    
052                    trashVersionPersistence.update(trashVersion);
053            }
054    
055            @Override
056            public TrashVersion deleteTrashVersion(
057                            long entryId, String className, long classPK)
058                    throws SystemException {
059    
060                    TrashVersion trashVersion = fetchVersion(entryId, className, classPK);
061    
062                    if (trashVersion != null) {
063                            return deleteTrashVersion(trashVersion);
064                    }
065    
066                    return null;
067            }
068    
069            @Override
070            public TrashVersion fetchVersion(
071                            long entryId, String className, long classPK)
072                    throws SystemException {
073    
074                    List<TrashVersion> trashVersions = getVersions(className, classPK);
075    
076                    if (trashVersions.isEmpty()) {
077                            return null;
078                    }
079    
080                    return trashVersions.get(0);
081            }
082    
083            @Override
084            public List<TrashVersion> getVersions(long entryId) throws SystemException {
085                    return trashVersionPersistence.findByEntryId(entryId);
086            }
087    
088            @Override
089            public List<TrashVersion> getVersions(long entryId, String className)
090                    throws SystemException {
091    
092                    if (Validator.isNull(className)) {
093                            return trashVersionPersistence.findByEntryId(entryId);
094                    }
095    
096                    long classNameId = PortalUtil.getClassNameId(className);
097    
098                    return trashVersionPersistence.findByE_C(entryId, classNameId);
099            }
100    
101            /**
102             * Returns all the trash versions associated with the trash entry.
103             *
104             * @param  className the class name of the trash entity
105             * @param  classPK the primary key of the trash entity
106             * @return all the trash versions associated with the trash entry
107             * @throws SystemException if a system exception occurred
108             */
109            @Override
110            public List<TrashVersion> getVersions(String className, long classPK)
111                    throws SystemException {
112    
113                    long classNameId = PortalUtil.getClassNameId(className);
114    
115                    return trashVersionPersistence.findByC_C(classNameId, classPK);
116            }
117    
118    }