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