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