001    /**
002     * Copyright (c) 2000-2013 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.CharPool;
018    import com.liferay.portal.kernel.util.DateUtil;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.xml.Element;
023    
024    import java.io.InputStream;
025    
026    import java.util.ArrayList;
027    import java.util.Date;
028    import java.util.List;
029    
030    /**
031     * @author Bruno Farache
032     */
033    public abstract class BaseSharepointStorageImpl implements SharepointStorage {
034    
035            @Override
036            public void addDocumentElements(
037                            SharepointRequest sharepointRequest, Element element)
038                    throws Exception {
039            }
040    
041            @Override
042            public void createFolder(SharepointRequest sharepointRequest)
043                    throws Exception {
044            }
045    
046            @Override
047            public InputStream getDocumentInputStream(
048                            SharepointRequest sharepointRequest)
049                    throws Exception {
050    
051                    return null;
052            }
053    
054            @Override
055            public Tree getDocumentsTree(SharepointRequest sharepointRequest)
056                    throws Exception {
057    
058                    return new Tree();
059            }
060    
061            @Override
062            public Tree getDocumentTree(SharepointRequest sharepointRequest)
063                    throws Exception {
064    
065                    return new Tree();
066            }
067    
068            @Override
069            public Tree getFoldersTree(SharepointRequest sharepointRequest)
070                    throws Exception {
071    
072                    return new Tree();
073            }
074    
075            @Override
076            public Tree getFolderTree(SharepointRequest sharepointRequest)
077                    throws Exception {
078    
079                    return new Tree();
080            }
081    
082            @Override
083            public void getParentFolderIds(
084                            long groupId, String path, List<Long> folderIds)
085                    throws Exception {
086            }
087    
088            @Override
089            public Tree[] moveDocument(SharepointRequest sharepointRequest)
090                    throws Exception {
091    
092                    return null;
093            }
094    
095            @Override
096            public void putDocument(SharepointRequest sharepointRequest)
097                    throws Exception {
098            }
099    
100            @Override
101            public Tree[] removeDocument(SharepointRequest sharepointRequest)
102                    throws Exception {
103    
104                    return null;
105            }
106    
107            protected void addDocumentElement(
108                            Element element, String documentName, Date createDate,
109                            Date modifiedDate, String userName)
110                    throws Exception {
111    
112                    element.addNamespace("z", "#RowsetSchema");
113    
114                    Element rowEl = element.addElement("z:row");
115    
116                    rowEl.addAttribute("ows_FileRef", documentName);
117                    rowEl.addAttribute("ows_FSObjType", "0");
118                    rowEl.addAttribute("ows_Created", getDate(createDate, true));
119                    rowEl.addAttribute("ows_Author", userName);
120                    rowEl.addAttribute("ows_Modified", getDate(modifiedDate, true));
121                    rowEl.addAttribute("ows_Editor", userName);
122            }
123    
124            protected String getDate(Date date, boolean xml) {
125                    if (date == null) {
126                            return StringPool.BLANK;
127                    }
128    
129                    StringBundler sb = new StringBundler(2);
130    
131                    if (xml) {
132                            sb.append(
133                                    DateUtil.getDate(date, "yyyy-mm-dd HH:mm:ss Z", LocaleUtil.US));
134                    }
135                    else {
136                            sb.append("TR|");
137                            sb.append(
138                                    DateUtil.getDate(
139                                            date, "dd MMM yyyy HH:mm:ss Z", LocaleUtil.US));
140                    }
141    
142                    return sb.toString();
143            }
144    
145            protected Tree getDocumentTree(
146                    String documentName, Date createDate, Date modifiedDate, long size,
147                    String userName, String version) {
148    
149                    Tree documentTree = new Tree();
150    
151                    documentName = SharepointUtil.replaceBackSlashes(documentName);
152    
153                    documentTree.addChild(new Leaf("document_name", documentName, true));
154    
155                    String createDateString = getDate(createDate, false);
156                    String modifiedDateString = getDate(modifiedDate, false);
157    
158                    Tree metaInfoTree = new Tree();
159    
160                    metaInfoTree.addChild(
161                            new Leaf("vti_timecreated", createDateString, false));
162                    metaInfoTree.addChild(
163                            new Leaf("vti_timelastmodified", modifiedDateString, false));
164                    metaInfoTree.addChild(
165                            new Leaf("vti_timelastwritten", modifiedDateString, false));
166                    metaInfoTree.addChild(new Leaf("vti_filesize", "IR|" + size, false));
167                    metaInfoTree.addChild(
168                            new Leaf("vti_sourcecontrolcheckedoutby", "SR|" + userName, false));
169                    metaInfoTree.addChild(
170                            new Leaf(
171                                    "vti_sourcecontroltimecheckedout", createDateString, false));
172                    metaInfoTree.addChild(
173                            new Leaf("vti_sourcecontrolversion", "SR|V" + version, false));
174                    metaInfoTree.addChild(
175                            new Leaf("vti_sourcecontrollockexpires", createDateString, false));
176    
177                    documentTree.addChild(new Leaf("meta_info", metaInfoTree));
178    
179                    return documentTree;
180            }
181    
182            protected Tree getFolderTree(String name) {
183                    Date now = new Date();
184    
185                    return getFolderTree(name, now, now, now);
186            }
187    
188            protected Tree getFolderTree(
189                    String name, Date createDate, Date modifiedDate, Date lastPostDate) {
190    
191                    Tree folderTree = new Tree();
192    
193                    Tree metaInfoTree = new Tree();
194    
195                    name = SharepointUtil.replaceBackSlashes(name);
196    
197                    metaInfoTree.addChild(
198                            new Leaf("vti_timecreated", getDate(createDate, false), false));
199                    metaInfoTree.addChild(
200                            new Leaf(
201                                    "vti_timelastmodified", getDate(modifiedDate, false), false));
202                    metaInfoTree.addChild(
203                            new Leaf(
204                                    "vti_timelastwritten", getDate(lastPostDate, false), false));
205                    metaInfoTree.addChild(new Leaf("vti_hassubdirs", "BR|true", false));
206                    metaInfoTree.addChild(new Leaf("vti_isbrowsable", "BR|true", false));
207                    metaInfoTree.addChild(new Leaf("vti_isexecutable", "BR|false", false));
208                    metaInfoTree.addChild(new Leaf("vti_isscriptable", "BR|false", false));
209    
210                    folderTree.addChild(new Leaf("url", name, true));
211                    folderTree.addChild(new Leaf("meta_info", metaInfoTree));
212    
213                    return folderTree;
214            }
215    
216            protected long getLastFolderId(
217                            long groupId, String path, long defaultParentFolderId)
218                    throws Exception {
219    
220                    List<Long> folderIds = new ArrayList<Long>();
221    
222                    folderIds.add(defaultParentFolderId);
223    
224                    String[] pathArray = SharepointUtil.getPathArray(path);
225    
226                    if (pathArray.length > 2) {
227                            path = removeFoldersFromPath(path, 2);
228    
229                            getParentFolderIds(groupId, path, folderIds);
230                    }
231    
232                    return folderIds.get(folderIds.size() - 1);
233            }
234    
235            protected String getParentFolderPath(String path) {
236                    int pos = path.lastIndexOf(CharPool.FORWARD_SLASH);
237    
238                    return path.substring(0, pos);
239            }
240    
241            protected String getResourceName(String path) {
242                    int pos = path.lastIndexOf(CharPool.FORWARD_SLASH);
243    
244                    return path.substring(pos + 1);
245            }
246    
247            protected String removeFoldersFromPath(String path, int index) {
248                    for (int i = 0; i < index; i++) {
249                            int pos = path.indexOf(CharPool.SLASH);
250    
251                            path = path.substring(pos + 1);
252                    }
253    
254                    return path;
255            }
256    
257    }