2 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License as published by the Free
6 * Software Foundation; either version 2.1 of the License, or (at your option)
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 package com.liferay.portlet.documentlibrary.store;
17 import com.liferay.portal.kernel.exception.PortalException;
18 import com.liferay.portal.kernel.util.CharPool;
19 import com.liferay.portal.kernel.util.FileUtil;
20 import com.liferay.portal.kernel.util.GetterUtil;
21 import com.liferay.portal.kernel.util.StringBundler;
22 import com.liferay.portal.kernel.util.StringPool;
23 import com.liferay.portal.kernel.util.StringUtil;
24 import com.liferay.portal.kernel.util.Validator;
25 import com.liferay.portlet.documentlibrary.util.DLUtil;
28 import java.util.ArrayList;
29 import java.util.List;
33 * See http://issues.liferay.com/browse/LPS-1976.
36 * @author Jorge Ferrer
38 * @author Brian Wing Shun Chan
40 public class AdvancedFileSystemStoreCO3 extends FileSystemStore {
43 public String[] getFileNames(long companyId, long repositoryId) {
44 File repositoryDir = getRepositoryDir(companyId, repositoryId);
46 String[] directories = FileUtil.listDirs(repositoryDir);
48 List<String> fileNames = new ArrayList<String>();
50 for (String directory : directories) {
53 companyId, repositoryId,
54 repositoryDir.getPath() + StringPool.SLASH + directory));
57 return fileNames.toArray(new String[fileNames.size()]);
61 public void updateFile(
62 long companyId, long repositoryId, String fileName,
64 throws PortalException {
66 super.updateFile(companyId, repositoryId, fileName, newFileName);
68 File newFileNameDir = getFileNameDir(
69 companyId, repositoryId, newFileName);
71 String[] fileNameVersions = FileUtil.listFiles(newFileNameDir);
73 for (String fileNameVersion : fileNameVersions) {
74 String ext = FileUtil.getExtension(fileNameVersion);
76 if (ext.equals(_HOOK_EXTENSION)) {
80 File fileNameVersionFile = new File(
81 newFileNameDir + StringPool.SLASH + fileNameVersion);
82 File newFileNameVersionFile = new File(
83 newFileNameDir + StringPool.SLASH +
84 FileUtil.stripExtension(fileNameVersion) +
85 StringPool.PERIOD + _HOOK_EXTENSION);
87 fileNameVersionFile.renameTo(newFileNameVersionFile);
91 protected void buildPath(StringBundler sb, String fileNameFragment) {
92 if (fileNameFragment.length() <= 2) {
96 if (getDepth(sb.toString()) > 3) {
100 sb.append(fileNameFragment.substring(0, 2) + StringPool.SLASH);
102 buildPath(sb, fileNameFragment.substring(2));
105 protected List<String> getAdvancedFileNames(
106 long companyId, long repositoryId, String fileName) {
108 List<String> fileNames = new ArrayList<String>();
110 String shortFileName = FileUtil.getShortFileName(fileName);
112 if (shortFileName.equals("DLFE") || Validator.isNumber(shortFileName)) {
113 String[] curFileNames = FileUtil.listDirs(fileName);
115 for (String curFileName : curFileNames) {
117 getAdvancedFileNames(
118 companyId, repositoryId,
119 fileName + StringPool.SLASH + curFileName));
123 if (shortFileName.endsWith(_HOOK_EXTENSION)) {
124 shortFileName = FileUtil.stripExtension(shortFileName);
127 fileNames.add(shortFileName);
133 protected int getDepth(String path) {
134 String[] fragments = StringUtil.split(path, CharPool.SLASH);
136 return fragments.length;
140 protected File getDirNameDir(
141 long companyId, long repositoryId, String dirName) {
143 File repositoryDir = getRepositoryDir(companyId, repositoryId);
145 return new File(repositoryDir + StringPool.SLASH + dirName);
149 protected File getFileNameDir(
150 long companyId, long repositoryId, String fileName) {
152 String ext = StringPool.PERIOD + FileUtil.getExtension(fileName);
153 int pos = fileName.lastIndexOf(StringPool.SLASH);
156 StringBundler sb = new StringBundler();
158 String fileNameFragment = removeExtension(fileName);
160 if (fileNameFragment.startsWith("DLFE-")) {
161 fileNameFragment = fileNameFragment.substring(5);
163 sb.append("DLFE" + StringPool.SLASH);
166 buildPath(sb, fileNameFragment);
168 File repositoryDir = getRepositoryDir(companyId, repositoryId);
170 repositoryDir + StringPool.SLASH + sb.toString() +
171 StringPool.SLASH + fileNameFragment + ext);
174 File fileNameDir = getDirNameDir(companyId, repositoryId, fileName);
179 protected String removeExtension(String fileName) {
180 String ext = FileUtil.getExtension(fileName);
182 if (ext != null && !ext.equals("")) {
183 fileName = fileName.substring(
184 0, fileName.length() - ext.length() - 1);
192 protected File getFileNameVersionFile(
193 long companyId, long repositoryId, String fileName, String version) {
195 String ext = StringPool.PERIOD + FileUtil.getExtension(fileName);
197 if (version.equals("1")) {
201 int pos = fileName.lastIndexOf(CharPool.SLASH);
204 StringBundler sb = new StringBundler();
206 String fileNameFragment = removeExtension(fileName);
208 if (fileNameFragment.startsWith("DLFE-")) {
209 fileNameFragment = fileNameFragment.substring(5);
211 sb.append("DLFE" + StringPool.SLASH);
214 buildPath(sb, fileNameFragment);
216 File repositoryDir = getRepositoryDir(companyId, repositoryId);
219 repositoryDir + StringPool.SLASH + sb.toString() +
220 StringPool.SLASH + fileNameFragment + ext +
221 StringPool.SLASH + fileNameFragment +
222 StringPool.UNDERLINE + version + ext);
225 File fileNameDir = getDirNameDir(companyId, repositoryId, fileName);
227 String fileNameFragment = removeExtension(
228 fileName.substring(pos + 1));
231 fileNameDir + StringPool.SLASH + fileNameFragment +
232 StringPool.UNDERLINE + version + ext);
237 protected String getHeadVersionLabel(
238 long companyId, long repositoryId, String fileName) {
240 File fileNameDir = getFileNameDir(companyId, repositoryId, fileName);
242 if (!fileNameDir.exists()) {
243 return VERSION_DEFAULT;
246 String[] versionLabels = FileUtil.listFiles(fileNameDir);
248 String headVersionLabel = VERSION_DEFAULT;
250 for (int i = 0; i < versionLabels.length; i++) {
251 String versionLabelFragment = versionLabels[i];
253 int x = versionLabelFragment.lastIndexOf(CharPool.UNDERLINE);
254 int y = versionLabelFragment.lastIndexOf(CharPool.PERIOD);
256 // If, for any reason, the file name does not end with '_<version>.<extension>', we may have x+1 > y,
257 // what causes a StringArrayOutOfBoundsException, that is why we use default version '1.0'
259 return VERSION_DEFAULT;
263 versionLabelFragment = versionLabelFragment.substring(x + 1, y);
266 String versionLabel = versionLabelFragment;
268 if (DLUtil.compareVersions(versionLabel, headVersionLabel) > 0) {
269 headVersionLabel = versionLabel;
273 return headVersionLabel;
276 protected double getHeadVersionNumber(
277 long companyId, long repositoryId, String fileName) {
279 File fileNameDir = getFileNameDir(companyId, repositoryId, fileName);
280 if (!fileNameDir.exists()) {
281 return DEFAULT_VERSION;
284 String[] versionNumbers = FileUtil.listFiles(fileNameDir);
286 double headVersionNumber = DEFAULT_VERSION;
288 for (int i = 0; i < versionNumbers.length; i++) {
289 String fileNameVersion = versionNumbers[i];
290 fileNameVersion = removeExtension(fileNameVersion);
292 int pos = fileNameVersion.lastIndexOf(StringPool.UNDERLINE);
294 double versionNumber = GetterUtil.getDouble(fileNameVersion.substring(pos + 1));
296 if (versionNumber > headVersionNumber) {
297 headVersionNumber = versionNumber;
300 return headVersionNumber;
303 private static final String _HOOK_EXTENSION = "afsh";
304 public static final double DEFAULT_VERSION = 1.0;