--- /dev/null
+/**
+ * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ */
+
+package com.liferay.portlet.documentlibrary.store;
+
+import com.liferay.portal.kernel.exception.PortalException;
+import com.liferay.portal.kernel.util.CharPool;
+import com.liferay.portal.kernel.util.FileUtil;
+import com.liferay.portal.kernel.util.GetterUtil;
+import com.liferay.portal.kernel.util.StringBundler;
+import com.liferay.portal.kernel.util.StringPool;
+import com.liferay.portal.kernel.util.StringUtil;
+import com.liferay.portal.kernel.util.Validator;
+import com.liferay.portlet.documentlibrary.util.DLUtil;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * <p>
+ * See http://issues.liferay.com/browse/LPS-1976.
+ * </p>
+ *
+ * @author Jorge Ferrer
+ * @author Ryan Park
+ * @author Brian Wing Shun Chan
+ */
+public class AdvancedFileSystemStoreCO3 extends FileSystemStore {
+
+ @Override
+ public String[] getFileNames(long companyId, long repositoryId) {
+ File repositoryDir = getRepositoryDir(companyId, repositoryId);
+
+ String[] directories = FileUtil.listDirs(repositoryDir);
+
+ List<String> fileNames = new ArrayList<String>();
+
+ for (String directory : directories) {
+ fileNames.addAll(
+ getAdvancedFileNames(
+ companyId, repositoryId,
+ repositoryDir.getPath() + StringPool.SLASH + directory));
+ }
+
+ return fileNames.toArray(new String[fileNames.size()]);
+ }
+
+ @Override
+ public void updateFile(
+ long companyId, long repositoryId, String fileName,
+ String newFileName)
+ throws PortalException {
+
+ super.updateFile(companyId, repositoryId, fileName, newFileName);
+
+ File newFileNameDir = getFileNameDir(
+ companyId, repositoryId, newFileName);
+
+ String[] fileNameVersions = FileUtil.listFiles(newFileNameDir);
+
+ for (String fileNameVersion : fileNameVersions) {
+ String ext = FileUtil.getExtension(fileNameVersion);
+
+ if (ext.equals(_HOOK_EXTENSION)) {
+ continue;
+ }
+
+ File fileNameVersionFile = new File(
+ newFileNameDir + StringPool.SLASH + fileNameVersion);
+ File newFileNameVersionFile = new File(
+ newFileNameDir + StringPool.SLASH +
+ FileUtil.stripExtension(fileNameVersion) +
+ StringPool.PERIOD + _HOOK_EXTENSION);
+
+ fileNameVersionFile.renameTo(newFileNameVersionFile);
+ }
+ }
+
+ protected void buildPath(StringBundler sb, String fileNameFragment) {
+ if (fileNameFragment.length() <= 2) {
+ return;
+ }
+
+ if (getDepth(sb.toString()) > 3) {
+ return;
+ }
+
+ sb.append(fileNameFragment.substring(0, 2) + StringPool.SLASH);
+
+ buildPath(sb, fileNameFragment.substring(2));
+ }
+
+ protected List<String> getAdvancedFileNames(
+ long companyId, long repositoryId, String fileName) {
+
+ List<String> fileNames = new ArrayList<String>();
+
+ String shortFileName = FileUtil.getShortFileName(fileName);
+
+ if (shortFileName.equals("DLFE") || Validator.isNumber(shortFileName)) {
+ String[] curFileNames = FileUtil.listDirs(fileName);
+
+ for (String curFileName : curFileNames) {
+ fileNames.addAll(
+ getAdvancedFileNames(
+ companyId, repositoryId,
+ fileName + StringPool.SLASH + curFileName));
+ }
+ }
+ else {
+ if (shortFileName.endsWith(_HOOK_EXTENSION)) {
+ shortFileName = FileUtil.stripExtension(shortFileName);
+ }
+
+ fileNames.add(shortFileName);
+ }
+
+ return fileNames;
+ }
+
+ protected int getDepth(String path) {
+ String[] fragments = StringUtil.split(path, CharPool.SLASH);
+
+ return fragments.length;
+ }
+
+ @Override
+ protected File getDirNameDir(
+ long companyId, long repositoryId, String dirName) {
+
+ File repositoryDir = getRepositoryDir(companyId, repositoryId);
+
+ return new File(repositoryDir + StringPool.SLASH + dirName);
+ }
+
+ @Override
+ protected File getFileNameDir(
+ long companyId, long repositoryId, String fileName) {
+
+ String ext = StringPool.PERIOD + FileUtil.getExtension(fileName);
+ int pos = fileName.lastIndexOf(StringPool.SLASH);
+
+ if (pos == -1) {
+ StringBundler sb = new StringBundler();
+
+ String fileNameFragment = removeExtension(fileName);
+
+ if (fileNameFragment.startsWith("DLFE-")) {
+ fileNameFragment = fileNameFragment.substring(5);
+
+ sb.append("DLFE" + StringPool.SLASH);
+ }
+
+ buildPath(sb, fileNameFragment);
+
+ File repositoryDir = getRepositoryDir(companyId, repositoryId);
+ return new File(
+ repositoryDir + StringPool.SLASH + sb.toString() +
+ StringPool.SLASH + fileNameFragment + ext);
+ }
+ else {
+ File fileNameDir = getDirNameDir(companyId, repositoryId, fileName);
+ return fileNameDir;
+ }
+ }
+
+ protected String removeExtension(String fileName) {
+ String ext = FileUtil.getExtension(fileName);
+
+ if (ext != null && !ext.equals("")) {
+ fileName = fileName.substring(
+ 0, fileName.length() - ext.length() - 1);
+ }
+
+ return fileName;
+ }
+
+
+ @Override
+ protected File getFileNameVersionFile(
+ long companyId, long repositoryId, String fileName, String version) {
+
+ String ext = StringPool.PERIOD + FileUtil.getExtension(fileName);
+
+ if (version.equals("1")) {
+ version = "1.0";
+ }
+
+ int pos = fileName.lastIndexOf(CharPool.SLASH);
+
+ if (pos == -1) {
+ StringBundler sb = new StringBundler();
+
+ String fileNameFragment = removeExtension(fileName);
+
+ if (fileNameFragment.startsWith("DLFE-")) {
+ fileNameFragment = fileNameFragment.substring(5);
+
+ sb.append("DLFE" + StringPool.SLASH);
+ }
+
+ buildPath(sb, fileNameFragment);
+
+ File repositoryDir = getRepositoryDir(companyId, repositoryId);
+
+ return new File(
+ repositoryDir + StringPool.SLASH + sb.toString() +
+ StringPool.SLASH + fileNameFragment + ext +
+ StringPool.SLASH + fileNameFragment +
+ StringPool.UNDERLINE + version + ext);
+ }
+ else {
+ File fileNameDir = getDirNameDir(companyId, repositoryId, fileName);
+
+ String fileNameFragment = removeExtension(
+ fileName.substring(pos + 1));
+
+ return new File(
+ fileNameDir + StringPool.SLASH + fileNameFragment +
+ StringPool.UNDERLINE + version + ext);
+ }
+ }
+
+ @Override
+ protected String getHeadVersionLabel(
+ long companyId, long repositoryId, String fileName) {
+
+ File fileNameDir = getFileNameDir(companyId, repositoryId, fileName);
+
+ if (!fileNameDir.exists()) {
+ return VERSION_DEFAULT;
+ }
+
+ String[] versionLabels = FileUtil.listFiles(fileNameDir);
+
+ String headVersionLabel = VERSION_DEFAULT;
+
+ for (int i = 0; i < versionLabels.length; i++) {
+ String versionLabelFragment = versionLabels[i];
+
+ int x = versionLabelFragment.lastIndexOf(CharPool.UNDERLINE);
+ int y = versionLabelFragment.lastIndexOf(CharPool.PERIOD);
+
+ // If, for any reason, the file name does not end with '_<version>.<extension>', we may have x+1 > y,
+ // what causes a StringArrayOutOfBoundsException, that is why we use default version '1.0'
+ if (x + 1 > y) {
+ return VERSION_DEFAULT;
+ }
+
+ if (x > -1) {
+ versionLabelFragment = versionLabelFragment.substring(x + 1, y);
+ }
+
+ String versionLabel = versionLabelFragment;
+
+ if (DLUtil.compareVersions(versionLabel, headVersionLabel) > 0) {
+ headVersionLabel = versionLabel;
+ }
+ }
+
+ return headVersionLabel;
+ }
+
+ protected double getHeadVersionNumber(
+ long companyId, long repositoryId, String fileName) {
+
+ File fileNameDir = getFileNameDir(companyId, repositoryId, fileName);
+ if (!fileNameDir.exists()) {
+ return DEFAULT_VERSION;
+ }
+
+ String[] versionNumbers = FileUtil.listFiles(fileNameDir);
+
+ double headVersionNumber = DEFAULT_VERSION;
+
+ for (int i = 0; i < versionNumbers.length; i++) {
+ String fileNameVersion = versionNumbers[i];
+ fileNameVersion = removeExtension(fileNameVersion);
+
+ int pos = fileNameVersion.lastIndexOf(StringPool.UNDERLINE);
+
+ double versionNumber = GetterUtil.getDouble(fileNameVersion.substring(pos + 1));
+
+ if (versionNumber > headVersionNumber) {
+ headVersionNumber = versionNumber;
+ }
+ }
+ return headVersionNumber;
+ }
+
+ private static final String _HOOK_EXTENSION = "afsh";
+ public static final double DEFAULT_VERSION = 1.0;
+
+}
\ No newline at end of file