1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.documentlibrary.util;
24  
25  import com.liferay.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  
30  import java.io.File;
31  
32  /**
33   * <a href="AdvancedFileSystemHook.java.html"><b><i>View Source</i></b></a>
34   *
35   * <p>
36   * See http://issues.liferay.com/browse/LPS-1976.
37   * </p>
38   *
39   * @author Jorge Ferrer
40   */
41  public class AdvancedFileSystemHook extends FileSystemHook {
42  
43      protected void buildPath(StringBuilder sb, String fileNameFragment) {
44          if (fileNameFragment.length() <= 2) {
45              return;
46          }
47  
48          if (getDepth(sb.toString()) > 3) {
49              return;
50          }
51  
52          sb.append(fileNameFragment.substring(0, 2) + StringPool.SLASH);
53  
54          buildPath(sb, fileNameFragment.substring(2));
55      }
56  
57      protected int getDepth(String path) {
58          String[] fragments = StringUtil.split(path, StringPool.SLASH);
59  
60          return fragments.length;
61      }
62  
63      protected File getDirNameDir(
64          long companyId, long repositoryId, String dirName) {
65  
66          File repositoryDir = getRepositoryDir(companyId, repositoryId);
67  
68          return new File(repositoryDir + StringPool.SLASH + dirName);
69      }
70  
71      protected File getFileNameDir(
72          long companyId, long repositoryId, String fileName) {
73  
74          String ext = StringPool.PERIOD + FileUtil.getExtension(fileName);
75  
76          StringBuilder sb = new StringBuilder();
77  
78          String fileNameFragment = removeExtension(fileName);
79  
80          if (fileNameFragment.startsWith("DLFE-")) {
81              fileNameFragment = fileNameFragment.substring(5);
82  
83              sb.append("DLFE" + StringPool.SLASH);
84          }
85  
86          buildPath(sb, fileNameFragment);
87  
88          File repositoryDir = getRepositoryDir(companyId, repositoryId);
89  
90          File fileNameDir = new File(
91              repositoryDir + StringPool.SLASH + sb.toString() +
92                  StringPool.SLASH + fileNameFragment + ext);
93  
94          return fileNameDir;
95      }
96  
97      protected File getFileNameVersionFile(
98          long companyId, long repositoryId, String fileName, double version) {
99  
100         String ext = StringPool.PERIOD + FileUtil.getExtension(fileName);
101 
102         int pos = fileName.lastIndexOf(StringPool.SLASH);
103 
104         if (pos == -1) {
105             StringBuilder sb = new StringBuilder();
106 
107             String fileNameFragment = removeExtension(fileName);
108 
109             if (fileNameFragment.startsWith("DLFE-")) {
110                 fileNameFragment = fileNameFragment.substring(5);
111 
112                 sb.append("DLFE" + StringPool.SLASH);
113             }
114 
115             buildPath(sb, fileNameFragment);
116 
117             File repositoryDir = getRepositoryDir(companyId, repositoryId);
118 
119             return new File(
120                 repositoryDir + StringPool.SLASH + sb.toString() +
121                     StringPool.SLASH + fileNameFragment + ext +
122                         StringPool.SLASH + fileNameFragment +
123                             StringPool.UNDERLINE + version + ext);
124         }
125         else {
126             File fileNameDir = getDirNameDir(companyId, repositoryId, fileName);
127 
128             String fileNameFragment = removeExtension(
129                 fileName.substring(pos + 1));
130 
131             return new File(
132                 fileNameDir + StringPool.SLASH + fileNameFragment +
133                     StringPool.UNDERLINE + version + ext);
134         }
135     }
136 
137     protected double getHeadVersionNumber(
138         long companyId, long repositoryId, String fileName) {
139 
140         File fileNameDir = getFileNameDir(companyId, repositoryId, fileName);
141 
142         if (!fileNameDir.exists()) {
143             return DEFAULT_VERSION;
144         }
145 
146         String[] versionNumbers = FileUtil.listFiles(fileNameDir);
147 
148         double headVersionNumber = DEFAULT_VERSION;
149 
150         for (int i = 0; i < versionNumbers.length; i++) {
151             String versionNumberFragment = versionNumbers[i];
152 
153             versionNumberFragment = removeExtension(versionNumberFragment);
154 
155             int pos = versionNumberFragment.lastIndexOf(StringPool.UNDERLINE);
156 
157             if (pos > -1) {
158                 versionNumberFragment = versionNumberFragment.substring(
159                     pos + 1);
160             }
161 
162             double versionNumber = GetterUtil.getDouble(versionNumberFragment);
163 
164             if (versionNumber > headVersionNumber) {
165                 headVersionNumber = versionNumber;
166             }
167         }
168 
169         return headVersionNumber;
170     }
171 
172     protected String removeExtension(String fileName) {
173         String ext = FileUtil.getExtension(fileName);
174 
175         if (ext != null) {
176             fileName = fileName.substring(
177                 0, fileName.length() - ext.length() - 1);
178         }
179 
180         return fileName;
181     }
182 
183 }