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