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.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.xml.Element;
022    
023    import java.io.InputStream;
024    
025    import java.util.ArrayList;
026    import java.util.Date;
027    import java.util.List;
028    import java.util.Locale;
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", Locale.US));
134                    }
135                    else {
136                            sb.append("TR|");
137                            sb.append(
138                                    DateUtil.getDate(date, "dd MMM yyyy HH:mm:ss Z", Locale.US));
139                    }
140    
141                    return sb.toString();
142            }
143    
144            protected Tree getDocumentTree(
145                    String documentName, Date createDate, Date modifiedDate, long size,
146                    String userName, String version) {
147    
148                    Tree documentTree = new Tree();
149    
150                    documentName = SharepointUtil.replaceBackSlashes(documentName);
151    
152                    documentTree.addChild(new Leaf("document_name", documentName, true));
153    
154                    String createDateString = getDate(createDate, false);
155                    String modifiedDateString = getDate(modifiedDate, false);
156    
157                    Tree metaInfoTree = new Tree();
158    
159                    metaInfoTree.addChild(
160                            new Leaf("vti_timecreated", createDateString, false));
161                    metaInfoTree.addChild(
162                            new Leaf("vti_timelastmodified", modifiedDateString, false));
163                    metaInfoTree.addChild(
164                            new Leaf("vti_timelastwritten", modifiedDateString, false));
165                    metaInfoTree.addChild(new Leaf("vti_filesize", "IR|" + size, false));
166                    metaInfoTree.addChild(
167                            new Leaf("vti_sourcecontrolcheckedoutby", "SR|" + userName, false));
168                    metaInfoTree.addChild(
169                            new Leaf(
170                                    "vti_sourcecontroltimecheckedout", createDateString, false));
171                    metaInfoTree.addChild(
172                            new Leaf("vti_sourcecontrolversion", "SR|V" + version, false));
173                    metaInfoTree.addChild(
174                            new Leaf("vti_sourcecontrollockexpires", createDateString, false));
175    
176                    documentTree.addChild(new Leaf("meta_info", metaInfoTree));
177    
178                    return documentTree;
179            }
180    
181            protected Tree getFolderTree(String name) {
182                    Date now = new Date();
183    
184                    return getFolderTree(name, now, now, now);
185            }
186    
187            protected Tree getFolderTree(
188                    String name, Date createDate, Date modifiedDate, Date lastPostDate) {
189    
190                    Tree folderTree = new Tree();
191    
192                    Tree metaInfoTree = new Tree();
193    
194                    name = SharepointUtil.replaceBackSlashes(name);
195    
196                    metaInfoTree.addChild(
197                            new Leaf("vti_timecreated", getDate(createDate, false), false));
198                    metaInfoTree.addChild(
199                            new Leaf(
200                                    "vti_timelastmodified", getDate(modifiedDate, false), false));
201                    metaInfoTree.addChild(
202                            new Leaf(
203                                    "vti_timelastwritten", getDate(lastPostDate, false), false));
204                    metaInfoTree.addChild(new Leaf("vti_hassubdirs", "BR|true", false));
205                    metaInfoTree.addChild(new Leaf("vti_isbrowsable", "BR|true", false));
206                    metaInfoTree.addChild(new Leaf("vti_isexecutable", "BR|false", false));
207                    metaInfoTree.addChild(new Leaf("vti_isscriptable", "BR|false", false));
208    
209                    folderTree.addChild(new Leaf("url", name, true));
210                    folderTree.addChild(new Leaf("meta_info", metaInfoTree));
211    
212                    return folderTree;
213            }
214    
215            protected long getLastFolderId(
216                            long groupId, String path, long defaultParentFolderId)
217                    throws Exception {
218    
219                    List<Long> folderIds = new ArrayList<Long>();
220    
221                    folderIds.add(defaultParentFolderId);
222    
223                    String[] pathArray = SharepointUtil.getPathArray(path);
224    
225                    if (pathArray.length > 2) {
226                            path = removeFoldersFromPath(path, 2);
227    
228                            getParentFolderIds(groupId, path, folderIds);
229                    }
230    
231                    return folderIds.get(folderIds.size() - 1);
232            }
233    
234            protected String getParentFolderPath(String path) {
235                    int pos = path.lastIndexOf(CharPool.FORWARD_SLASH);
236    
237                    return path.substring(0, pos);
238            }
239    
240            protected String getResourceName(String path) {
241                    int pos = path.lastIndexOf(CharPool.FORWARD_SLASH);
242    
243                    return path.substring(pos + 1);
244            }
245    
246            protected String removeFoldersFromPath(String path, int index) {
247                    for (int i = 0; i < index; i++) {
248                            int pos = path.indexOf(CharPool.SLASH);
249    
250                            path = path.substring(pos + 1);
251                    }
252    
253                    return path;
254            }
255    
256    }