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