001    /**
002     * Copyright (c) 2000-present 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.dynamicdatalists.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.ListUtil;
019    import com.liferay.portal.kernel.util.OrderByComparator;
020    import com.liferay.portlet.dynamicdatalists.NoSuchRecordVersionException;
021    import com.liferay.portlet.dynamicdatalists.model.DDLRecordVersion;
022    import com.liferay.portlet.dynamicdatalists.service.base.DDLRecordVersionLocalServiceBaseImpl;
023    import com.liferay.portlet.dynamicdatalists.util.comparator.DDLRecordVersionVersionComparator;
024    
025    import java.util.Collections;
026    import java.util.List;
027    
028    /**
029     * @author Marcellus Tavares
030     */
031    public class DDLRecordVersionLocalServiceImpl
032            extends DDLRecordVersionLocalServiceBaseImpl {
033    
034            @Override
035            public DDLRecordVersion getLatestRecordVersion(long recordId)
036                    throws PortalException {
037    
038                    List<DDLRecordVersion> recordVersions =
039                            ddlRecordVersionPersistence.findByRecordId(recordId);
040    
041                    if (recordVersions.isEmpty()) {
042                            throw new NoSuchRecordVersionException(
043                                    "No record versions found for record ID " + recordId);
044                    }
045    
046                    recordVersions = ListUtil.copy(recordVersions);
047    
048                    Collections.sort(
049                            recordVersions, new DDLRecordVersionVersionComparator());
050    
051                    return recordVersions.get(0);
052            }
053    
054            @Override
055            public DDLRecordVersion getRecordVersion(long recordVersionId)
056                    throws PortalException {
057    
058                    return ddlRecordVersionPersistence.findByPrimaryKey(recordVersionId);
059            }
060    
061            @Override
062            public DDLRecordVersion getRecordVersion(long recordId, String version)
063                    throws PortalException {
064    
065                    return ddlRecordVersionPersistence.findByR_V(recordId, version);
066            }
067    
068            @Override
069            public List<DDLRecordVersion> getRecordVersions(
070                    long recordId, int start, int end,
071                    OrderByComparator<DDLRecordVersion> orderByComparator) {
072    
073                    return ddlRecordVersionPersistence.findByRecordId(
074                            recordId, start, end, orderByComparator);
075            }
076    
077            @Override
078            public int getRecordVersionsCount(long recordId) {
079                    return ddlRecordVersionPersistence.countByRecordId(recordId);
080            }
081    
082    }