001/*
002 * (C) Copyright 2012 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 *     Arnaud Kervern
018 */
019package org.nuxeo.ecm.user.registration.actions;
020
021import static org.jboss.seam.international.StatusMessage.Severity.ERROR;
022import static org.jboss.seam.international.StatusMessage.Severity.INFO;
023import static org.nuxeo.ecm.user.registration.UserRegistrationService.CONFIGURATION_NAME;
024
025import java.io.Serializable;
026import java.util.Set;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.jboss.seam.ScopeType;
031import org.jboss.seam.annotations.In;
032import org.jboss.seam.annotations.Name;
033import org.jboss.seam.annotations.Observer;
034import org.jboss.seam.annotations.Scope;
035import org.jboss.seam.faces.FacesMessages;
036import org.nuxeo.ecm.core.api.CoreSession;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.ecm.core.api.NuxeoException;
039import org.nuxeo.ecm.user.invite.RegistrationRules;
040import org.nuxeo.ecm.user.registration.UserRegistrationService;
041import org.nuxeo.ecm.webapp.helpers.EventNames;
042import org.nuxeo.ecm.webapp.helpers.ResourcesAccessor;
043
044/**
045 * @author <a href="mailto:akervern@nuxeo.com">Arnaud Kervern</a>
046 */
047
048@Name("userRegistrationConfigurationActions")
049@Scope(ScopeType.CONVERSATION)
050public class UserRegistrationConfigurationActions implements Serializable {
051
052    private static Log log = LogFactory.getLog(UserRegistrationConfigurationActions.class);
053
054    private static final long serialVersionUID = 53124326502194L;
055
056    @In(create = true, required = false)
057    protected transient CoreSession documentManager;
058
059    @In(create = true, required = false)
060    protected transient FacesMessages facesMessages;
061
062    @In(create = true)
063    protected transient ResourcesAccessor resourcesAccessor;
064
065    @In(create = true)
066    protected transient UserRegistrationService userRegistrationService;
067
068    protected DocumentModel selectedConfigurationDocument;
069
070    protected String selectedConfiguration = CONFIGURATION_NAME;
071
072    public String getSelectedConfiguration() {
073        return selectedConfiguration;
074    }
075
076    public Set<String> getConfigurationsName() {
077        return userRegistrationService.getConfigurationsName();
078    }
079
080    public void setSelectedConfiguration(String selectedConfiguration) {
081        this.selectedConfiguration = selectedConfiguration;
082        selectedConfigurationDocument = null;
083    }
084
085    public RegistrationRules getRules(String configurationName) {
086        return userRegistrationService.getRegistrationRules(configurationName);
087    }
088
089    public DocumentModel getConfigurationDocument() {
090        if (selectedConfigurationDocument == null) {
091            selectedConfigurationDocument = userRegistrationService.getRegistrationRulesDocument(documentManager,
092                    selectedConfiguration);
093        }
094        return selectedConfigurationDocument;
095    }
096
097    public void saveConfiguration() {
098        try {
099            documentManager.saveDocument(selectedConfigurationDocument);
100            selectedConfigurationDocument = null;
101            facesMessages.add(INFO, resourcesAccessor.getMessages().get("label.save.configuration.registration"));
102        } catch (NuxeoException e) {
103            log.warn("Unable to save configuration document: " + e.getMessage());
104            log.info(e);
105            facesMessages.add(ERROR,
106                    resourcesAccessor.getMessages().get("label.unable.save.configuration.registration"));
107        }
108    }
109
110    @Observer({ EventNames.DOCUMENT_CHANGED })
111    public void resetState() {
112        selectedConfiguration = CONFIGURATION_NAME;
113        selectedConfigurationDocument = null;
114    }
115}