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.repository.cmis;
016    
017    import com.liferay.portal.InvalidRepositoryException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.repository.RepositoryException;
020    import com.liferay.portal.kernel.util.UnicodeProperties;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.Repository;
023    import com.liferay.portal.security.auth.PrincipalException;
024    import com.liferay.portal.service.persistence.RepositoryUtil;
025    
026    import java.util.HashSet;
027    import java.util.Map;
028    import java.util.Set;
029    
030    import org.apache.chemistry.opencmis.client.api.OperationContext;
031    import org.apache.chemistry.opencmis.client.api.Session;
032    import org.apache.chemistry.opencmis.client.api.SessionFactory;
033    import org.apache.chemistry.opencmis.client.runtime.OperationContextImpl;
034    import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
035    import org.apache.chemistry.opencmis.commons.PropertyIds;
036    import org.apache.chemistry.opencmis.commons.SessionParameter;
037    import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
038    import org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException;
039    import org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException;
040    
041    /**
042     * @author Alexander Chow
043     */
044    public class CMISRepositoryUtil {
045    
046            public static void checkRepository(
047                            long repositoryId, Map<String, String> parameters,
048                            UnicodeProperties typeSettingsProperties, String typeSettingsKey)
049                    throws PortalException, RepositoryException {
050    
051                    if (!typeSettingsProperties.containsKey(typeSettingsKey)) {
052                            org.apache.chemistry.opencmis.client.api.Repository cmisRepository =
053                                    _sessionFactory.getRepositories(parameters).get(0);
054    
055                            typeSettingsProperties.setProperty(
056                                    typeSettingsKey, cmisRepository.getId());
057    
058                            try {
059                                    Repository repository = RepositoryUtil.findByPrimaryKey(
060                                            repositoryId);
061    
062                                    repository.setTypeSettingsProperties(typeSettingsProperties);
063    
064                                    RepositoryUtil.update(repository);
065                            }
066                            catch (Exception e) {
067                                    throw new RepositoryException(e);
068                            }
069                    }
070    
071                    parameters.put(
072                            SessionParameter.REPOSITORY_ID,
073                            getTypeSettingsValue(typeSettingsProperties, typeSettingsKey));
074            }
075    
076            public static com.liferay.portal.kernel.repository.cmis.Session
077                            createSession(Map<String, String> parameters)
078                    throws PrincipalException, RepositoryException {
079    
080                    try {
081                            Session session = _sessionFactory.createSession(parameters);
082    
083                            session.setDefaultContext(_operationContext);
084    
085                            return new SessionImpl(session);
086                    }
087                    catch (CmisPermissionDeniedException cpde) {
088                            throw new PrincipalException(cpde);
089                    }
090                    catch (CmisUnauthorizedException cue) {
091                            throw new PrincipalException();
092                    }
093                    catch (Exception e) {
094                            throw new RepositoryException(e);
095                    }
096            }
097    
098            public static OperationContext getOperationContext() {
099                    return _operationContext;
100            }
101    
102            public static SessionFactory getSessionFactory() {
103                    return _sessionFactory;
104            }
105    
106            public static String getTypeSettingsValue(
107                            UnicodeProperties typeSettingsProperties, String typeSettingsKey)
108                    throws InvalidRepositoryException {
109    
110                    String value = typeSettingsProperties.getProperty(typeSettingsKey);
111    
112                    if (Validator.isNull(value)) {
113                            throw new InvalidRepositoryException(
114                                    "Properties value cannot be null for key " + typeSettingsKey);
115                    }
116    
117                    return value;
118            }
119    
120            private static OperationContext _operationContext;
121            private static SessionFactory _sessionFactory =
122                    SessionFactoryImpl.newInstance();
123    
124            static {
125                    Set<String> defaultFilterSet = new HashSet<String>();
126    
127                    // Base
128    
129                    defaultFilterSet.add(PropertyIds.BASE_TYPE_ID);
130                    defaultFilterSet.add(PropertyIds.CREATED_BY);
131                    defaultFilterSet.add(PropertyIds.CREATION_DATE);
132                    defaultFilterSet.add(PropertyIds.LAST_MODIFIED_BY);
133                    defaultFilterSet.add(PropertyIds.LAST_MODIFICATION_DATE);
134                    defaultFilterSet.add(PropertyIds.NAME);
135                    defaultFilterSet.add(PropertyIds.OBJECT_ID);
136                    defaultFilterSet.add(PropertyIds.OBJECT_TYPE_ID);
137    
138                    // Document
139    
140                    defaultFilterSet.add(PropertyIds.CONTENT_STREAM_LENGTH);
141                    defaultFilterSet.add(PropertyIds.CONTENT_STREAM_MIME_TYPE);
142                    defaultFilterSet.add(PropertyIds.IS_VERSION_SERIES_CHECKED_OUT);
143                    defaultFilterSet.add(PropertyIds.VERSION_LABEL);
144                    defaultFilterSet.add(PropertyIds.VERSION_SERIES_CHECKED_OUT_BY);
145                    defaultFilterSet.add(PropertyIds.VERSION_SERIES_CHECKED_OUT_ID);
146                    defaultFilterSet.add(PropertyIds.VERSION_SERIES_ID);
147    
148                    // Folder
149    
150                    defaultFilterSet.add(PropertyIds.PARENT_ID);
151                    defaultFilterSet.add(PropertyIds.PATH);
152    
153                    // Operation context
154    
155                    _operationContext = new OperationContextImpl(
156                            defaultFilterSet, false, true, false, IncludeRelationships.NONE,
157                            null, false, "cmis:name ASC", true, 1000);
158            }
159    
160    }