/**
 * Copyright (c) 2000-2017 Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * pSubscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */

package com.liferay.portal.repository.cmis;

import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;

import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;

/**
 * @author Ivan Zaera
 */
public class CMISRepositoryDetector {

	public CMISRepositoryDetector(RepositoryInfo repositoryInfo) {
		detectVendor(repositoryInfo);
	}

	public boolean isNuxeo() {
		return _nuxeo;
	}

	public boolean isNuxeo5_4() {
		return _nuxeo5_4;
	}

	public boolean isNuxeo5_5OrHigher() {
		return _nuxeo5_5OrHigher;
	}

	public boolean isNuxeo5_8OrHigher() {
		return _nuxeo5_8OrHigher;
	}

	protected void detectNuxeo(RepositoryInfo repositoryInfo) {
		String productVersion = repositoryInfo.getProductVersion();

		String[] versionParts = StringUtil.split(
			productVersion, StringPool.PERIOD);

		int major = GetterUtil.getInteger(versionParts[0]);
		int minor = GetterUtil.getInteger(versionParts[1]);

		if (major > 5) {
			_nuxeo5_8OrHigher = true;
			_nuxeo5_5OrHigher = true;
		}
		else if (major == 5) {
			if (minor >= 8) {
				_nuxeo5_8OrHigher = true;
			}

			if (minor >= 5) {
				_nuxeo5_5OrHigher = true;
			}

			if (minor == 4) {
				_nuxeo5_4 = true;
			}
		}
	}

	private void detectVendor(RepositoryInfo repositoryInfo) {
		String productName = repositoryInfo.getProductName();

		if (productName.contains(_NUXEO_ID)) {
			_nuxeo = true;

			detectNuxeo(repositoryInfo);
		}
	}

	private static final String _NUXEO_ID = "Nuxeo";

	private boolean _nuxeo;
	private boolean _nuxeo5_4;
	private boolean _nuxeo5_5OrHigher;
	private boolean _nuxeo5_8OrHigher;

}