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.configuration.Filter;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.InstancePool;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.util.PropsUtil;
025    
026    import java.util.Collection;
027    import java.util.HashMap;
028    import java.util.Map;
029    
030    /**
031     * @author Bruno Farache
032     */
033    public class SharepointUtil {
034    
035            public static final String VEERMER_URLENCODED =
036                    "application/x-vermeer-urlencoded";
037    
038            public static final String VERSION = "6.0.2.8117";
039    
040            public static void addBottom(StringBuilder sb) {
041                    sb.append("</body>");
042                    sb.append(StringPool.NEW_LINE);
043                    sb.append("</html>");
044            }
045    
046            public static void addTop(StringBuilder sb, String methodName) {
047                    sb.append("<html><head><title>vermeer RPC packet</title></head>");
048                    sb.append(StringPool.NEW_LINE);
049                    sb.append("<body>");
050                    sb.append(StringPool.NEW_LINE);
051    
052                    Property method = new Property("method", methodName + ":" + VERSION);
053    
054                    sb.append(method.parse());
055            }
056    
057            public static long getGroupId(String path) {
058                    long groupId = 0;
059    
060                    String[] pathArray = getPathArray(path);
061    
062                    String groupFolderName = pathArray[0];
063    
064                    if (groupFolderName != null) {
065                            int pos = groupFolderName.lastIndexOf(StringPool.OPEN_BRACKET);
066    
067                            if (pos != -1) {
068                                     groupId = GetterUtil.getLong(
069                                            groupFolderName.substring(
070                                                    pos, groupFolderName.length() - 1));
071                            }
072    
073                    }
074    
075                    return groupId;
076            }
077    
078            public static String[] getPathArray(String path) {
079                    return StringUtil.split(path, StringPool.SLASH);
080            }
081    
082            public static SharepointStorage getStorage(String path) {
083                    String storageClass = null;
084    
085                    if (path == null) {
086                            return null;
087                    }
088    
089                    String[] pathArray = getPathArray(path);
090    
091                    if (pathArray.length == 0) {
092                            storageClass = CompanySharepointStorageImpl.class.getName();
093                    }
094                    else if (pathArray.length == 1) {
095                            storageClass = GroupSharepointStorageImpl.class.getName();
096                    }
097                    else if (pathArray.length >= 2) {
098                            storageClass = getStorageClass(pathArray[1]);
099                    }
100    
101                    return (SharepointStorage)InstancePool.get(storageClass);
102            }
103    
104            public static String getStorageClass(String token) {
105                    return _instance._getStorageClass(token);
106            }
107    
108            public static String getStorageToken(String className) {
109                    return _instance._getStorageToken(className);
110            }
111    
112            public static Collection<String> getStorageTokens() {
113                    return _instance._getStorageTokens();
114            }
115    
116            public static String replaceBackSlashes(String value) {
117                    return value.replaceAll("\\\\", StringPool.BLANK);
118            }
119    
120            private SharepointUtil() {
121                    _storageMap = new HashMap<String, String>();
122    
123                    String[] tokens = PropsUtil.getArray(
124                            PropsKeys.SHAREPOINT_STORAGE_TOKENS);
125    
126                    for (String token: tokens) {
127                            Filter filter = new Filter(token);
128    
129                            String className = PropsUtil.get(
130                                    PropsKeys.SHAREPOINT_STORAGE_CLASS, filter);
131    
132                            if (Validator.isNotNull(className)) {
133                                    _storageMap.put(className, token);
134                            }
135                    }
136            }
137    
138            private String _getStorageClass(String token) {
139                    for (Map.Entry<String, String> entry : _storageMap.entrySet()) {
140                            String value = entry.getValue();
141    
142                            if (value.equals(token)) {
143                                    return entry.getKey();
144                            }
145                    }
146    
147                    return null;
148            }
149    
150            private String _getStorageToken(String className) {
151                    return _storageMap.get(className);
152            }
153    
154            private Collection<String> _getStorageTokens() {
155                    return _storageMap.values();
156            }
157    
158            private static SharepointUtil _instance = new SharepointUtil();
159    
160            private final Map<String, String> _storageMap;
161    
162    }