001/*
002 * (C) Copyright 2010 Nuxeo SAS (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 * Contributors:
014 * Nuxeo - initial API and implementation
015 */
016
017package org.nuxeo.ecm.webapp.localconfiguration;
018
019import static org.jboss.seam.ScopeType.CONVERSATION;
020
021import java.io.Serializable;
022
023import org.jboss.seam.annotations.In;
024import org.jboss.seam.annotations.Install;
025import org.jboss.seam.annotations.Name;
026import org.jboss.seam.annotations.Scope;
027import org.jboss.seam.core.Events;
028import org.jboss.seam.faces.FacesMessages;
029import org.jboss.seam.international.StatusMessage;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
033import org.nuxeo.ecm.webapp.helpers.EventNames;
034import org.nuxeo.ecm.webapp.helpers.ResourcesAccessor;
035
036/**
037 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
038 */
039@Name("localConfigurationActions")
040@Scope(CONVERSATION)
041@Install(precedence = Install.FRAMEWORK)
042public class LocalConfigurationActions implements Serializable {
043
044    private static final long serialVersionUID = 1L;
045
046    public static final String LOCAL_CONFIGURATION_CHANGED_LABEL = "label.local.configuration.modified";
047
048    @In(create = true, required = false)
049    protected transient CoreSession documentManager;
050
051    @In(create = true, required = false)
052    protected transient NavigationContext navigationContext;
053
054    @In(create = true, required = false)
055    protected transient FacesMessages facesMessages;
056
057    @In(create = true)
058    protected ResourcesAccessor resourcesAccessor;
059
060    public void toggleConfigurationForCurrentDocument(String configurationFacet) {
061        DocumentModel currentDocument = navigationContext.getCurrentDocument();
062        if (currentDocument.hasFacet(configurationFacet)) {
063            currentDocument.removeFacet(configurationFacet);
064        } else {
065            currentDocument.addFacet(configurationFacet);
066        }
067        documentManager.saveDocument(currentDocument);
068        navigationContext.invalidateCurrentDocument();
069        documentManager.save();
070
071        Events.instance().raiseEvent(EventNames.LOCAL_CONFIGURATION_CHANGED, navigationContext.getCurrentDocument());
072    }
073
074    public void saveLocalConfiguration() {
075        documentManager.saveDocument(navigationContext.getCurrentDocument());
076        navigationContext.invalidateCurrentDocument();
077
078        Events.instance().raiseEvent(EventNames.LOCAL_CONFIGURATION_CHANGED, navigationContext.getCurrentDocument());
079        facesMessages.add(StatusMessage.Severity.INFO,
080                resourcesAccessor.getMessages().get(LOCAL_CONFIGURATION_CHANGED_LABEL));
081    }
082
083}