001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Quentin Lamerand <qlamerand@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.platform.userpreferences;
019
020import static org.nuxeo.ecm.localconf.SimpleConfiguration.SIMPLE_CONFIGURATION_FACET;
021
022import org.nuxeo.ecm.core.api.CoreSession;
023import org.nuxeo.ecm.core.api.DocumentModel;
024import org.nuxeo.ecm.core.api.localconfiguration.LocalConfigurationService;
025import org.nuxeo.ecm.localconf.SimpleConfiguration;
026import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService;
027import org.nuxeo.runtime.api.Framework;
028import org.nuxeo.runtime.model.DefaultComponent;
029
030public class UserPreferencesServiceImpl extends DefaultComponent implements UserPreferencesService {
031
032    UserWorkspaceService userWorkspaceService;
033
034    LocalConfigurationService localConfigurationService;
035
036    @Override
037    public SimpleUserPreferences getSimpleUserPreferences(CoreSession session) {
038        DocumentModel userWorkspace = getUserWorkspaceService().getCurrentUserPersonalWorkspace(session, null);
039        if (!userWorkspace.hasFacet(SIMPLE_CONFIGURATION_FACET)) {
040            userWorkspace.addFacet(SIMPLE_CONFIGURATION_FACET);
041            userWorkspace = session.saveDocument(userWorkspace);
042        }
043        SimpleConfiguration simpleConfiguration = getLocalConfigurationService().getConfiguration(
044                SimpleConfiguration.class, SIMPLE_CONFIGURATION_FACET, userWorkspace);
045        SimpleUserPreferences userPref = new SimpleUserPreferences(
046                session.getDocument(simpleConfiguration.getDocumentRef()));
047        return userPref;
048    }
049
050    @Override
051    @SuppressWarnings("rawtypes")
052    public <T extends UserPreferences> T getUserPreferences(CoreSession session, Class<T> userPrefClass,
053            String userPrefFacet) {
054        DocumentModel userWorkspace = getUserWorkspaceService().getCurrentUserPersonalWorkspace(session, null);
055        return getLocalConfigurationService().getConfiguration(userPrefClass, userPrefFacet, userWorkspace);
056    }
057
058    protected UserWorkspaceService getUserWorkspaceService() {
059        if (userWorkspaceService == null) {
060            userWorkspaceService = Framework.getLocalService(UserWorkspaceService.class);
061        }
062        return userWorkspaceService;
063    }
064
065    protected LocalConfigurationService getLocalConfigurationService() {
066        if (localConfigurationService == null) {
067            localConfigurationService = Framework.getLocalService(LocalConfigurationService.class);
068        }
069        return localConfigurationService;
070    }
071
072}