001/*
002 * (C) Copyright 2006-2007 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021package org.nuxeo.ecm.webapp.versioning;
022
023import static org.jboss.seam.ScopeType.CONVERSATION;
024import static org.jboss.seam.ScopeType.EVENT;
025import static org.jboss.seam.annotations.Install.FRAMEWORK;
026
027import java.io.Serializable;
028import java.util.Collection;
029import java.util.Collections;
030import java.util.LinkedHashMap;
031import java.util.List;
032import java.util.Locale;
033import java.util.Map;
034
035import javax.faces.application.FacesMessage;
036import javax.faces.component.UIComponent;
037import javax.faces.context.FacesContext;
038import javax.faces.validator.ValidatorException;
039
040import org.apache.commons.logging.Log;
041import org.apache.commons.logging.LogFactory;
042import org.jboss.seam.annotations.Factory;
043import org.jboss.seam.annotations.In;
044import org.jboss.seam.annotations.Install;
045import org.jboss.seam.annotations.Name;
046import org.jboss.seam.annotations.Observer;
047import org.jboss.seam.annotations.Scope;
048import org.jboss.seam.annotations.intercept.BypassInterceptors;
049import org.nuxeo.common.utils.i18n.I18NUtils;
050import org.nuxeo.ecm.core.api.CoreSession;
051import org.nuxeo.ecm.core.api.DocumentModel;
052import org.nuxeo.ecm.core.api.VersionModel;
053import org.nuxeo.ecm.core.api.facet.VersioningDocument;
054import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
055import org.nuxeo.ecm.platform.versioning.api.VersionIncEditOptions;
056import org.nuxeo.ecm.platform.versioning.api.VersioningActions;
057import org.nuxeo.ecm.platform.versioning.api.VersioningManager;
058import org.nuxeo.ecm.webapp.helpers.EventNames;
059import org.nuxeo.ecm.webapp.helpers.ResourcesAccessor;
060
061/**
062 * Web action bean for document versioning. Used also by other seam components through injection.
063 *
064 * @author Dragos Mihalache
065 */
066@Name("documentVersioning")
067@Scope(CONVERSATION)
068@Install(precedence = FRAMEWORK)
069public class DocumentVersioningBean implements DocumentVersioning, Serializable {
070
071    private static final long serialVersionUID = 75409841629876L;
072
073    private static final Log log = LogFactory.getLog(DocumentVersioningBean.class);
074
075    @In(create = true)
076    protected transient ResourcesAccessor resourcesAccessor;
077
078    @In(create = true, required = false)
079    protected transient CoreSession documentManager;
080
081    @In(create = true)
082    protected transient NavigationContext navigationContext;
083
084    @In(create = true)
085    private transient VersioningManager versioningManager;
086
087    /**
088     * field used for deciding whether or not to display versioning controls section (in document editing)
089     */
090    private Boolean rendered;
091
092    private VersioningActions selectedOption;
093
094    @Override
095    public Collection<VersionModel> getItemVersioningHistory(DocumentModel document) {
096        List<VersionModel> versions = Collections.emptyList();
097        versions = documentManager.getVersionsForDocument(document.getRef());
098        for (VersionModel model : versions) {
099            DocumentModel ver = documentManager.getDocumentWithVersion(document.getRef(), model);
100            if (ver != null) {
101                model.setDescription(ver.getAdapter(VersioningDocument.class).getVersionLabel());
102            }
103        }
104        return versions;
105    }
106
107    @Override
108    public Collection<VersionModel> getCurrentItemVersioningHistory() {
109        return getItemVersioningHistory(navigationContext.getCurrentDocument());
110    }
111
112    @Factory(autoCreate = true, value = "currentDocumentVersionInfo", scope = EVENT)
113    public VersionInfo getCurrentDocumentVersionInfo() {
114        DocumentModel doc = navigationContext.getCurrentDocument();
115        if (doc == null) {
116            return null;
117        }
118        String versionLabel = versioningManager.getVersionLabel(doc);
119        boolean available = versionLabel != null && versionLabel.length() != 0;
120        return new VersionInfo(versionLabel, available);
121    }
122
123    @Observer(value = { EventNames.DOCUMENT_SELECTION_CHANGED, EventNames.DOCUMENT_CHANGED }, create = false)
124    @BypassInterceptors
125    public void resetVersioningOption() {
126        selectedOption = null;
127        rendered = null;
128    }
129
130    @Override
131    public Map<String, String> getVersioningOptionsMap(DocumentModel doc) {
132        Map<String, String> map = new LinkedHashMap<String, String>();
133        VersionIncEditOptions options = getAvailableVersioningOptions(doc);
134        if (options != null) {
135            for (VersioningActions option : options.getOptions()) {
136                String label = "label.versioning.option." + option.toString();
137                if (resourcesAccessor != null) {
138                    label = resourcesAccessor.getMessages().get(label);
139                }
140                map.put(option.name(), label);
141            }
142        }
143        return map;
144    }
145
146    public VersionIncEditOptions getAvailableVersioningOptions(DocumentModel doc) {
147        return versioningManager.getVersionIncEditOptions(doc);
148    }
149
150    @Override
151    public String getVersionLabel(DocumentModel doc) {
152        return versioningManager.getVersionLabel(doc);
153    }
154
155    @Override
156    public void validateOptionSelection(FacesContext context, UIComponent component, Object value) {
157        if (value != null) {
158            // ok
159            return;
160        }
161        String bundleName = context.getApplication().getMessageBundle();
162        Locale locale = context.getViewRoot().getLocale();
163        String msg = I18NUtils.getMessageString(bundleName, "error.versioning.none_selected", null, locale);
164        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
165
166        throw new ValidatorException(message);
167    }
168
169}