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.dynamicdatamapping.service.impl;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.util.ListUtil;
021    import com.liferay.portal.kernel.util.OrderByComparator;
022    import com.liferay.portlet.dynamicdatamapping.NoSuchStructureVersionException;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureVersion;
024    import com.liferay.portlet.dynamicdatamapping.service.base.DDMStructureVersionLocalServiceBaseImpl;
025    import com.liferay.portlet.dynamicdatamapping.util.comparator.StructureVersionVersionComparator;
026    
027    import java.util.Collections;
028    import java.util.List;
029    
030    /**
031     * @author Pablo Carvalho
032     */
033    @ProviderType
034    public class DDMStructureVersionLocalServiceImpl
035            extends DDMStructureVersionLocalServiceBaseImpl {
036    
037            @Override
038            public DDMStructureVersion getLatestStructureVersion(long structureId)
039                    throws PortalException {
040    
041                    List<DDMStructureVersion> structureVersions =
042                            ddmStructureVersionPersistence.findByStructureId(structureId);
043    
044                    if (structureVersions.isEmpty()) {
045                            throw new NoSuchStructureVersionException(
046                                    "No structure versions found for structure ID " + structureId);
047                    }
048    
049                    structureVersions = ListUtil.copy(structureVersions);
050    
051                    Collections.sort(
052                            structureVersions, new StructureVersionVersionComparator());
053    
054                    return structureVersions.get(0);
055            }
056    
057            @Override
058            public DDMStructureVersion getStructureVersion(long structureVersionId)
059                    throws PortalException {
060    
061                    return ddmStructureVersionPersistence.findByPrimaryKey(
062                            structureVersionId);
063            }
064    
065            @Override
066            public DDMStructureVersion getStructureVersion(
067                            long structureId, String version)
068                    throws PortalException {
069    
070                    return ddmStructureVersionPersistence.findByS_V(structureId, version);
071            }
072    
073            @Override
074            public List<DDMStructureVersion> getStructureVersions(
075                    long structureId, int start, int end,
076                    OrderByComparator<DDMStructureVersion> orderByComparator) {
077    
078                    return ddmStructureVersionPersistence.findByStructureId(
079                            structureId, start, end, orderByComparator);
080            }
081    
082            @Override
083            public int getStructureVersionsCount(long structureId) {
084                    return ddmStructureVersionPersistence.countByStructureId(structureId);
085            }
086    
087    }