001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public void addDocumentElements(
036                            SharepointRequest sharepointRequest, Element element)
037                    throws Exception {
038            }
039    
040            public void createFolder(SharepointRequest sharepointRequest)
041                    throws Exception {
042            }
043    
044            public InputStream getDocumentInputStream(
045                            SharepointRequest sharepointRequest)
046                    throws Exception {
047    
048                    return null;
049            }
050    
051            public Tree getDocumentTree(SharepointRequest sharepointRequest)
052                    throws Exception {
053    
054                    return new Tree();
055            }
056    
057            public Tree getDocumentsTree(SharepointRequest sharepointRequest)
058                    throws Exception {
059    
060                    return new Tree();
061            }
062    
063            public Tree getFolderTree(SharepointRequest sharepointRequest)
064                    throws Exception {
065    
066                    return new Tree();
067            }
068    
069            public Tree getFoldersTree(SharepointRequest sharepointRequest)
070                    throws Exception {
071    
072                    return new Tree();
073            }
074    
075            public void getParentFolderIds(
076                            long groupId, String path, List<Long> folderIds)
077                    throws Exception {
078            }
079    
080            public Tree[] moveDocument(SharepointRequest sharepointRequest)
081                    throws Exception {
082    
083                    return null;
084            }
085    
086            public void putDocument(SharepointRequest sharepointRequest)
087                    throws Exception {
088            }
089    
090            public Tree[] removeDocument(SharepointRequest sharepointRequest)
091                    throws Exception {
092    
093                    return null;
094            }
095    
096            protected void addDocumentElement(
097                            Element element, String documentName, Date createDate,
098                            Date modifiedDate, String userName)
099                    throws Exception {
100    
101                    element.addNamespace("z", "#RowsetSchema");
102    
103                    Element rowEl = element.addElement("z:row");
104    
105                    rowEl.addAttribute("ows_FileRef", documentName);
106                    rowEl.addAttribute("ows_FSObjType", "0");
107                    rowEl.addAttribute("ows_Created", getDate(createDate, true));
108                    rowEl.addAttribute("ows_Author", userName);
109                    rowEl.addAttribute("ows_Modified", getDate(modifiedDate, true));
110                    rowEl.addAttribute("ows_Editor", userName);
111            }
112    
113            protected String getDate(Date date, boolean xml) {
114                    if (date == null) {
115                            return StringPool.BLANK;
116                    }
117    
118                    StringBundler sb = new StringBundler(2);
119    
120                    if (xml) {
121                            sb.append(
122                                    DateUtil.getDate(date, "yyyy-mm-dd HH:mm:ss Z", Locale.US));
123                    }
124                    else {
125                            sb.append("TR|");
126                            sb.append(
127                                    DateUtil.getDate(date, "dd MMM yyyy HH:mm:ss Z", Locale.US));
128                    }
129    
130                    return sb.toString();
131            }
132    
133            protected Tree getDocumentTree(
134                    String documentName, Date createDate, Date modifiedDate, long size,
135                    String userName, String version) {
136    
137                    Tree documentTree = new Tree();
138    
139                    documentName = SharepointUtil.replaceBackSlashes(documentName);
140    
141                    documentTree.addChild(new Leaf("document_name", documentName, true));
142    
143                    String createDateString = getDate(createDate, false);
144                    String modifiedDateString = getDate(modifiedDate, false);
145    
146                    Tree metaInfoTree = new Tree();
147    
148                    metaInfoTree.addChild(
149                            new Leaf("vti_timecreated", createDateString, false));
150                    metaInfoTree.addChild(
151                            new Leaf("vti_timelastmodified", modifiedDateString, false));
152                    metaInfoTree.addChild(
153                            new Leaf("vti_timelastwritten", modifiedDateString, false));
154                    metaInfoTree.addChild(new Leaf("vti_filesize", "IR|" + size, false));
155                    metaInfoTree.addChild(
156                            new Leaf("vti_sourcecontrolcheckedoutby", "SR|" + userName, false));
157                    metaInfoTree.addChild(
158                            new Leaf(
159                                    "vti_sourcecontroltimecheckedout", createDateString, false));
160                    metaInfoTree.addChild(
161                            new Leaf("vti_sourcecontrolversion", "SR|V" + version, false));
162                    metaInfoTree.addChild(
163                            new Leaf("vti_sourcecontrollockexpires", createDateString, false));
164    
165                    documentTree.addChild(new Leaf("meta_info", metaInfoTree));
166    
167                    return documentTree;
168            }
169    
170            protected Tree getFolderTree(String name) {
171                    Date now = new Date();
172    
173                    return getFolderTree(name, now, now, now);
174            }
175    
176            protected Tree getFolderTree(
177                    String name, Date createDate, Date modifiedDate, Date lastPostDate) {
178    
179                    Tree folderTree = new Tree();
180    
181                    Tree metaInfoTree = new Tree();
182    
183                    name = SharepointUtil.replaceBackSlashes(name);
184    
185                    metaInfoTree.addChild(
186                            new Leaf("vti_timecreated", getDate(createDate, false), false));
187                    metaInfoTree.addChild(
188                            new Leaf(
189                                    "vti_timelastmodified", getDate(modifiedDate, false), false));
190                    metaInfoTree.addChild(
191                            new Leaf(
192                                    "vti_timelastwritten", getDate(lastPostDate, false), false));
193                    metaInfoTree.addChild(new Leaf("vti_hassubdirs", "BR|true", false));
194                    metaInfoTree.addChild(new Leaf("vti_isbrowsable", "BR|true", false));
195                    metaInfoTree.addChild(new Leaf("vti_isexecutable", "BR|false", false));
196                    metaInfoTree.addChild(new Leaf("vti_isscriptable", "BR|false", false));
197    
198                    folderTree.addChild(new Leaf("url", name, true));
199                    folderTree.addChild(new Leaf("meta_info", metaInfoTree));
200    
201                    return folderTree;
202            }
203    
204            protected long getLastFolderId(
205                            long groupId, String path, long defaultParentFolderId)
206                    throws Exception {
207    
208                    List<Long> folderIds = new ArrayList<Long>();
209    
210                    folderIds.add(defaultParentFolderId);
211    
212                    String[] pathArray = SharepointUtil.getPathArray(path);
213    
214                    if (pathArray.length > 2) {
215                            path = removeFoldersFromPath(path, 2);
216    
217                            getParentFolderIds(groupId, path, folderIds);
218                    }
219    
220                    return folderIds.get(folderIds.size() - 1);
221            }
222    
223            protected String getParentFolderPath(String path) {
224                    int pos = path.lastIndexOf(CharPool.FORWARD_SLASH);
225    
226                    return path.substring(0, pos);
227            }
228    
229            protected String getResourceName(String path) {
230                    int pos = path.lastIndexOf(CharPool.FORWARD_SLASH);
231    
232                    return path.substring(pos + 1);
233            }
234    
235            protected String removeFoldersFromPath(String path, int index) {
236                    for (int i = 0; i < index; i++) {
237                            int pos = path.indexOf(CharPool.SLASH);
238    
239                            path = path.substring(pos + 1);
240                    }
241    
242                    return path;
243            }
244    
245    }