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