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