001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.sharepoint;
016    
017    import com.liferay.portal.kernel.util.DateUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.xml.Element;
021    
022    import java.io.InputStream;
023    
024    import java.util.ArrayList;
025    import java.util.Date;
026    import java.util.List;
027    import java.util.Locale;
028    
029    /**
030     * @author Bruno Farache
031     */
032    public abstract class BaseSharepointStorageImpl implements SharepointStorage {
033    
034            public void addDocumentElements(
035                            SharepointRequest sharepointRequest, Element element)
036                    throws Exception {
037            }
038    
039            public void createFolder(SharepointRequest sharepointRequest)
040                    throws Exception {
041            }
042    
043            public InputStream getDocumentInputStream(
044                            SharepointRequest sharepointRequest)
045                    throws Exception {
046    
047                    return null;
048            }
049    
050            public Tree getDocumentTree(SharepointRequest sharepointRequest)
051                    throws Exception {
052    
053                    return new Tree();
054            }
055    
056            public Tree getDocumentsTree(SharepointRequest sharepointRequest)
057                    throws Exception {
058    
059                    return new Tree();
060            }
061    
062            public Tree getFolderTree(SharepointRequest sharepointRequest)
063                    throws Exception {
064    
065                    return new Tree();
066            }
067    
068            public Tree getFoldersTree(SharepointRequest sharepointRequest)
069                    throws Exception {
070    
071                    return new Tree();
072            }
073    
074            public void getParentFolderIds(
075                            long groupId, String path, List<Long> folderIds)
076                    throws Exception {
077            }
078    
079            public Tree[] moveDocument(SharepointRequest sharepointRequest)
080                    throws Exception {
081    
082                    return null;
083            }
084    
085            public void putDocument(SharepointRequest sharepointRequest)
086                    throws Exception {
087            }
088    
089            public Tree[] removeDocument(SharepointRequest sharepointRequest)
090                    throws Exception {
091    
092                    return null;
093            }
094    
095            protected void addDocumentElement(
096                            Element element, String documentName, Date createDate,
097                            Date modifiedDate, String userName)
098                    throws Exception {
099    
100                    element.addNamespace("z", "#RowsetSchema");
101    
102                    Element rowEl = element.addElement("z:row");
103    
104                    rowEl.addAttribute("ows_FileRef", documentName);
105                    rowEl.addAttribute("ows_FSObjType", "0");
106                    rowEl.addAttribute("ows_Created", getDate(createDate, true));
107                    rowEl.addAttribute("ows_Author", userName);
108                    rowEl.addAttribute("ows_Modified", getDate(modifiedDate, true));
109                    rowEl.addAttribute("ows_Editor", userName);
110            }
111    
112            protected String getDate(Date date, boolean xml) {
113                    if (date == null) {
114                            return StringPool.BLANK;
115                    }
116    
117                    StringBundler sb = new StringBundler(2);
118    
119                    if (xml) {
120                            sb.append(
121                                    DateUtil.getDate(date, "yyyy-mm-dd HH:mm:ss Z", Locale.US));
122                    }
123                    else {
124                            sb.append("TR|");
125                            sb.append(
126                                    DateUtil.getDate(date, "dd MMM yyyy HH:mm:ss Z", Locale.US));
127                    }
128    
129                    return sb.toString();
130            }
131    
132            protected Tree getDocumentTree(
133                    String documentName, Date createDate, Date modifiedDate, long size,
134                    String userName, String version) {
135    
136                    Tree documentTree = new Tree();
137    
138                    documentName = SharepointUtil.replaceBackSlashes(documentName);
139    
140                    documentTree.addChild(new Leaf("document_name", documentName, true));
141    
142                    String createDateString = getDate(createDate, false);
143                    String modifiedDateString = getDate(modifiedDate, false);
144    
145                    Tree metaInfoTree = new Tree();
146    
147                    metaInfoTree.addChild(
148                            new Leaf("vti_timecreated", createDateString, false));
149                    metaInfoTree.addChild(
150                            new Leaf("vti_timelastmodified", modifiedDateString, false));
151                    metaInfoTree.addChild(
152                            new Leaf("vti_timelastwritten", modifiedDateString, false));
153                    metaInfoTree.addChild(new Leaf("vti_filesize", "IR|" + size, false));
154                    metaInfoTree.addChild(
155                            new Leaf("vti_sourcecontrolcheckedoutby", "SR|" + userName, false));
156                    metaInfoTree.addChild(
157                            new Leaf(
158                                    "vti_sourcecontroltimecheckedout", createDateString, false));
159                    metaInfoTree.addChild(
160                            new Leaf("vti_sourcecontrolversion", "SR|V" + version, false));
161                    metaInfoTree.addChild(
162                            new Leaf("vti_sourcecontrollockexpires", createDateString, false));
163    
164                    documentTree.addChild(new Leaf("meta_info", metaInfoTree));
165    
166                    return documentTree;
167            }
168    
169            protected Tree getFolderTree(String name) {
170                    Date now = new Date();
171    
172                    return getFolderTree(name, now, now, now);
173            }
174    
175            protected Tree getFolderTree(
176                    String name, Date createDate, Date modifiedDate, Date lastPostDate) {
177    
178                    Tree folderTree = new Tree();
179    
180                    Tree metaInfoTree = new Tree();
181    
182                    name = SharepointUtil.replaceBackSlashes(name);
183    
184                    metaInfoTree.addChild(
185                            new Leaf("vti_timecreated", getDate(createDate, false), false));
186                    metaInfoTree.addChild(
187                            new Leaf(
188                                    "vti_timelastmodified", getDate(modifiedDate, false), false));
189                    metaInfoTree.addChild(
190                            new Leaf(
191                                    "vti_timelastwritten", getDate(lastPostDate, false), false));
192                    metaInfoTree.addChild(new Leaf("vti_hassubdirs", "BR|true", false));
193                    metaInfoTree.addChild(new Leaf("vti_isbrowsable", "BR|true", false));
194                    metaInfoTree.addChild(new Leaf("vti_isexecutable", "BR|false", false));
195                    metaInfoTree.addChild(new Leaf("vti_isscriptable", "BR|false", false));
196    
197                    folderTree.addChild(new Leaf("url", name, true));
198                    folderTree.addChild(new Leaf("meta_info", metaInfoTree));
199    
200                    return folderTree;
201            }
202    
203            protected long getLastFolderId(
204                            long groupId, String path, long defaultParentFolderId)
205                    throws Exception {
206    
207                    List<Long> folderIds = new ArrayList<Long>();
208    
209                    folderIds.add(defaultParentFolderId);
210    
211                    String[] pathArray = SharepointUtil.getPathArray(path);
212    
213                    if (pathArray.length > 2) {
214                            path = removeFoldersFromPath(path, 2);
215    
216                            getParentFolderIds(groupId, path, folderIds);
217                    }
218    
219                    return folderIds.get(folderIds.size() - 1);
220            }
221    
222            protected String getParentFolderPath(String path) {
223                    int pos = path.lastIndexOf(StringPool.FORWARD_SLASH);
224    
225                    return path.substring(0, pos);
226            }
227    
228            protected String getResourceName(String path) {
229                    int pos = path.lastIndexOf(StringPool.FORWARD_SLASH);
230    
231                    return path.substring(pos + 1);
232            }
233    
234            protected String removeFoldersFromPath(String path, int index) {
235                    for (int i = 0; i < index; i++) {
236                            int pos = path.indexOf(StringPool.SLASH);
237    
238                            path = path.substring(pos + 1);
239                    }
240    
241                    return path;
242            }
243    
244    }