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