001/*
002 * (C) Copyright 2011 Nuxeo SA (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 */
019
020package org.nuxeo.ecm.platform.userpreferences;
021
022import static org.nuxeo.ecm.localconf.SimpleConfiguration.SIMPLE_CONFIGURATION_FACET;
023
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.localconfiguration.LocalConfigurationService;
027import org.nuxeo.ecm.localconf.SimpleConfiguration;
028import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService;
029import org.nuxeo.runtime.api.Framework;
030import org.nuxeo.runtime.model.DefaultComponent;
031
032public class UserPreferencesServiceImpl extends DefaultComponent implements UserPreferencesService {
033
034    UserWorkspaceService userWorkspaceService;
035
036    LocalConfigurationService localConfigurationService;
037
038    @Override
039    public SimpleUserPreferences getSimpleUserPreferences(CoreSession session) {
040        DocumentModel userWorkspace = getUserWorkspaceService().getCurrentUserPersonalWorkspace(session, null);
041        if (!userWorkspace.hasFacet(SIMPLE_CONFIGURATION_FACET)) {
042            userWorkspace.addFacet(SIMPLE_CONFIGURATION_FACET);
043            userWorkspace = session.saveDocument(userWorkspace);
044        }
045        SimpleConfiguration simpleConfiguration = getLocalConfigurationService().getConfiguration(
046                SimpleConfiguration.class, SIMPLE_CONFIGURATION_FACET, userWorkspace);
047        SimpleUserPreferences userPref = new SimpleUserPreferences(
048                session.getDocument(simpleConfiguration.getDocumentRef()));
049        return userPref;
050    }
051
052    @Override
053    @SuppressWarnings("rawtypes")
054    public <T extends UserPreferences> T getUserPreferences(CoreSession session, Class<T> userPrefClass,
055            String userPrefFacet) {
056        DocumentModel userWorkspace = getUserWorkspaceService().getCurrentUserPersonalWorkspace(session, null);
057        return getLocalConfigurationService().getConfiguration(userPrefClass, userPrefFacet, userWorkspace);
058    }
059
060    protected UserWorkspaceService getUserWorkspaceService() {
061        if (userWorkspaceService == null) {
062            userWorkspaceService = Framework.getLocalService(UserWorkspaceService.class);
063        }
064        return userWorkspaceService;
065    }
066
067    protected LocalConfigurationService getLocalConfigurationService() {
068        if (localConfigurationService == null) {
069            localConfigurationService = Framework.getLocalService(LocalConfigurationService.class);
070        }
071        return localConfigurationService;
072    }
073
074}