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.portal.repository.cmis;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    
021    import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;
022    
023    /**
024     * Implements the logic for CMIS repository vendor and version detection.
025     *
026     * @author Ivan Zaera
027     */
028    public class CMISRepositoryDetector {
029    
030            /**
031             * Creates a detector for the given CMIS repository. The detection routines
032             * are run once and cached inside the object for future reference.
033             *
034             * @param repositoryInfo the repository description
035             */
036            public CMISRepositoryDetector(RepositoryInfo repositoryInfo) {
037                    detectVendor(repositoryInfo);
038            }
039    
040            public boolean isNuxeo() {
041                    return _nuxeo;
042            }
043    
044            public boolean isNuxeo5_4() {
045                    return _nuxeo5_4;
046            }
047    
048            public boolean isNuxeo5_5OrHigher() {
049                    return _nuxeo5_5OrHigher;
050            }
051    
052            public boolean isNuxeo5_8OrHigher() {
053                    return _nuxeo5_8OrHigher;
054            }
055    
056            /**
057             * Detects the version number for the Nuxeo repository.
058             *
059             * @param repositoryInfo the repository description
060             */
061            protected void detectNuxeo(RepositoryInfo repositoryInfo) {
062                    String productVersion = repositoryInfo.getProductVersion();
063    
064                    String[] versionParts = StringUtil.split(
065                            productVersion, StringPool.PERIOD);
066    
067                    int major = GetterUtil.getInteger(versionParts[0]);
068                    int minor = GetterUtil.getInteger(versionParts[1]);
069    
070                    if (major > 5) {
071                            _nuxeo5_8OrHigher = true;
072                            _nuxeo5_5OrHigher = true;
073                    }
074                    else if (major == 5) {
075                            if (minor >= 8) {
076                                    _nuxeo5_8OrHigher = true;
077                            }
078    
079                            if (minor >= 5) {
080                                    _nuxeo5_5OrHigher = true;
081                            }
082    
083                            if (minor == 4) {
084                                    _nuxeo5_4 = true;
085                            }
086                    }
087            }
088    
089            /**
090             * Detects the vendor's name for the CMIS repository.
091             *
092             * @param repositoryInfo the repository description
093             */
094            private void detectVendor(RepositoryInfo repositoryInfo) {
095                    String productName = repositoryInfo.getProductName();
096    
097                    if (productName.contains(_NUXEO_ID)) {
098                            _nuxeo = true;
099    
100                            detectNuxeo(repositoryInfo);
101                    }
102            }
103    
104            private static final String _NUXEO_ID = "Nuxeo";
105    
106            private boolean _nuxeo;
107            private boolean _nuxeo5_4;
108            private boolean _nuxeo5_5OrHigher;
109            private boolean _nuxeo5_8OrHigher;
110    
111    }