001/*
002 * (C) Copyright 2011-2017 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Quentin Lamerand <qlamerand@nuxeo.com>
018 */
019package org.nuxeo.ecm.platform.userpreferences;
020
021import static org.nuxeo.ecm.localconf.SimpleConfiguration.SIMPLE_CONFIGURATION_FACET;
022
023import org.nuxeo.ecm.core.api.CoreSession;
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.api.localconfiguration.LocalConfigurationService;
026import org.nuxeo.ecm.localconf.SimpleConfiguration;
027import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService;
028import org.nuxeo.runtime.api.Framework;
029import org.nuxeo.runtime.model.DefaultComponent;
030
031public class UserPreferencesServiceImpl extends DefaultComponent implements UserPreferencesService {
032
033    UserWorkspaceService userWorkspaceService;
034
035    LocalConfigurationService localConfigurationService;
036
037    @Override
038    public SimpleUserPreferences getSimpleUserPreferences(CoreSession session) {
039        DocumentModel userWorkspace = getUserWorkspaceService().getCurrentUserPersonalWorkspace(session, null);
040        if (!userWorkspace.hasFacet(SIMPLE_CONFIGURATION_FACET)) {
041            userWorkspace.addFacet(SIMPLE_CONFIGURATION_FACET);
042            userWorkspace = session.saveDocument(userWorkspace);
043        }
044        SimpleConfiguration simpleConfiguration = getLocalConfigurationService().getConfiguration(
045                SimpleConfiguration.class, SIMPLE_CONFIGURATION_FACET, userWorkspace);
046        SimpleUserPreferences userPref = new SimpleUserPreferences(
047                session.getDocument(simpleConfiguration.getDocumentRef()));
048        return userPref;
049    }
050
051    @Override
052    @SuppressWarnings("rawtypes")
053    public <T extends UserPreferences> T getUserPreferences(CoreSession session, Class<T> userPrefClass,
054            String userPrefFacet) {
055        DocumentModel userWorkspace = getUserWorkspaceService().getCurrentUserPersonalWorkspace(session, null);
056        return getLocalConfigurationService().getConfiguration(userPrefClass, userPrefFacet, userWorkspace);
057    }
058
059    protected UserWorkspaceService getUserWorkspaceService() {
060        if (userWorkspaceService == null) {
061            userWorkspaceService = Framework.getService(UserWorkspaceService.class);
062        }
063        return userWorkspaceService;
064    }
065
066    protected LocalConfigurationService getLocalConfigurationService() {
067        if (localConfigurationService == null) {
068            localConfigurationService = Framework.getService(LocalConfigurationService.class);
069        }
070        return localConfigurationService;
071    }
072
073}