1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.portal.sharepoint;
24  
25  import com.liferay.portal.kernel.util.DateUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.xml.Element;
28  
29  import java.io.InputStream;
30  
31  import java.util.ArrayList;
32  import java.util.Date;
33  import java.util.List;
34  import java.util.Locale;
35  
36  /**
37   * <a href="BaseSharepointStorageImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Bruno Farache
40   */
41  public abstract class BaseSharepointStorageImpl implements SharepointStorage {
42  
43      public void addDocumentElements(
44              SharepointRequest sharepointRequest, Element element)
45          throws Exception {
46      }
47  
48      public void createFolder(SharepointRequest sharepointRequest)
49          throws Exception {
50      }
51  
52      public InputStream getDocumentInputStream(
53              SharepointRequest sharepointRequest)
54          throws Exception {
55  
56          return null;
57      }
58  
59      public Tree getDocumentTree(SharepointRequest sharepointRequest)
60          throws Exception {
61  
62          return new Tree();
63      }
64  
65      public Tree getDocumentsTree(SharepointRequest sharepointRequest)
66          throws Exception {
67  
68          return new Tree();
69      }
70  
71      public Tree getFolderTree(SharepointRequest sharepointRequest)
72          throws Exception {
73  
74          return new Tree();
75      }
76  
77      public Tree getFoldersTree(SharepointRequest sharepointRequest)
78          throws Exception {
79  
80          return new Tree();
81      }
82  
83      public void getParentFolderIds(
84              long groupId, String path, List<Long> folderIds)
85          throws Exception {
86      }
87  
88      public Tree[] moveDocument(SharepointRequest sharepointRequest)
89          throws Exception {
90  
91          return null;
92      }
93  
94      public void putDocument(SharepointRequest sharepointRequest)
95          throws Exception {
96      }
97  
98      public Tree[] removeDocument(SharepointRequest sharepointRequest)
99          throws Exception {
100 
101         return null;
102     }
103 
104     protected void addDocumentElement(
105             Element element, String documentName, Date createDate,
106             Date modifiedDate, String userName)
107         throws Exception {
108 
109         element.addNamespace("z", "#RowsetSchema");
110 
111         Element rowEl = element.addElement("z:row");
112 
113         rowEl.addAttribute("ows_FileRef", documentName);
114         rowEl.addAttribute("ows_FSObjType", "0");
115         rowEl.addAttribute("ows_Created", getDate(createDate, true));
116         rowEl.addAttribute("ows_Author", userName);
117         rowEl.addAttribute("ows_Modified", getDate(modifiedDate, true));
118         rowEl.addAttribute("ows_Editor", userName);
119     }
120 
121     protected String getDate(Date date, boolean xml) {
122         if (date == null) {
123             return StringPool.BLANK;
124         }
125 
126         StringBuilder sb = new StringBuilder();
127 
128         if (xml) {
129             sb.append(
130                 DateUtil.getDate(date, "yyyy-mm-dd HH:mm:ss Z", Locale.US));
131         }
132         else {
133             sb.append("TR|");
134             sb.append(
135                 DateUtil.getDate(date, "dd MMM yyyy HH:mm:ss Z", Locale.US));
136         }
137 
138         return sb.toString();
139     }
140 
141     protected Tree getDocumentTree(
142         String documentName, Date createDate, Date modifiedDate, int size,
143         String userName, double version) {
144 
145         Tree documentTree = new Tree();
146 
147         documentName = SharepointUtil.replaceBackSlashes(documentName);
148 
149         documentTree.addChild(new Leaf("document_name", documentName, true));
150 
151         String createDateString = getDate(createDate, false);
152         String modifiedDateString = getDate(modifiedDate, false);
153 
154         Tree metaInfoTree = new Tree();
155 
156         metaInfoTree.addChild(
157             new Leaf("vti_timecreated", createDateString, false));
158         metaInfoTree.addChild(
159             new Leaf("vti_timelastmodified", modifiedDateString, false));
160         metaInfoTree.addChild(
161             new Leaf("vti_timelastwritten", modifiedDateString, false));
162         metaInfoTree.addChild(new Leaf("vti_filesize", "IR|" + size, false));
163         metaInfoTree.addChild(
164             new Leaf("vti_sourcecontrolcheckedoutby", "SR|" + userName, false));
165         metaInfoTree.addChild(
166             new Leaf(
167                 "vti_sourcecontroltimecheckedout", createDateString, false));
168         metaInfoTree.addChild(
169             new Leaf("vti_sourcecontrolversion", "SR|V" + version, false));
170         metaInfoTree.addChild(
171             new Leaf("vti_sourcecontrollockexpires", createDateString, false));
172 
173         documentTree.addChild(new Leaf("meta_info", metaInfoTree));
174 
175         return documentTree;
176     }
177 
178     protected Tree getFolderTree(String name) {
179         Date now = new Date();
180 
181         return getFolderTree(name, now, now, now);
182     }
183 
184     protected Tree getFolderTree(
185         String name, Date createDate, Date modifiedDate, Date lastPostDate) {
186 
187         Tree folderTree = new Tree();
188 
189         Tree metaInfoTree = new Tree();
190 
191         name = SharepointUtil.replaceBackSlashes(name);
192 
193         metaInfoTree.addChild(
194             new Leaf("vti_timecreated", getDate(createDate, false), false));
195         metaInfoTree.addChild(
196             new Leaf(
197                 "vti_timelastmodified", getDate(modifiedDate, false), false));
198         metaInfoTree.addChild(
199             new Leaf(
200                 "vti_timelastwritten", getDate(lastPostDate, false), false));
201         metaInfoTree.addChild(new Leaf("vti_hassubdirs", "BR|true", false));
202         metaInfoTree.addChild(new Leaf("vti_isbrowsable", "BR|true", false));
203         metaInfoTree.addChild(new Leaf("vti_isexecutable", "BR|false", false));
204         metaInfoTree.addChild(new Leaf("vti_isscriptable", "BR|false", false));
205 
206         folderTree.addChild(new Leaf("url", name, true));
207         folderTree.addChild(new Leaf("meta_info", metaInfoTree));
208 
209         return folderTree;
210     }
211 
212     protected long getLastFolderId(
213             long groupId, String path, long defaultParentFolderId)
214         throws Exception {
215 
216         List<Long> folderIds = new ArrayList<Long>();
217 
218         folderIds.add(defaultParentFolderId);
219 
220         String[] pathArray = SharepointUtil.getPathArray(path);
221 
222         if (pathArray.length > 2) {
223             path = removeFoldersFromPath(path, 2);
224 
225             getParentFolderIds(groupId, path, folderIds);
226         }
227 
228         return folderIds.get(folderIds.size() - 1);
229     }
230 
231     protected String getParentFolderPath(String path) {
232         int pos = path.lastIndexOf(StringPool.FORWARD_SLASH);
233 
234         return path.substring(0, pos);
235     }
236 
237     protected String getResourceName(String path) {
238         int pos = path.lastIndexOf(StringPool.FORWARD_SLASH);
239 
240         return path.substring(pos + 1);
241     }
242 
243     protected String removeFoldersFromPath(String path, int index) {
244         for (int i = 0; i < index; i++) {
245             int pos = path.indexOf(StringPool.SLASH);
246 
247             path = path.substring(pos + 1);
248         }
249 
250         return path;
251     }
252 
253 }