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