001/*
002 * (C) Copyright 2013 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 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.webapp.versioning;
020
021import javax.faces.component.UIComponent;
022import javax.faces.context.FacesContext;
023import javax.faces.convert.Converter;
024
025import org.apache.commons.lang.StringUtils;
026import org.jboss.seam.annotations.Name;
027import org.jboss.seam.annotations.intercept.BypassInterceptors;
028import org.nuxeo.ecm.core.api.VersioningOption;
029import org.nuxeo.ecm.platform.versioning.api.VersioningActions;
030
031/**
032 * Converter for document versioning, setting the accurate {@link VersioningOption} on the document model context data
033 * instead of String selected in the interface.
034 *
035 * @since 5.7.3
036 */
037@Name("documentVersioningConverter")
038@org.jboss.seam.annotations.faces.Converter
039@BypassInterceptors
040public class DocumentVersioningConverter implements Converter {
041
042    @Override
043    public Object getAsObject(FacesContext context, UIComponent component, String value) {
044        if (StringUtils.isBlank(value)) {
045            return null;
046        }
047        VersioningActions option = VersioningActions.valueOf(value);
048        VersioningOption vo;
049        if (option == VersioningActions.ACTION_INCREMENT_MAJOR) {
050            vo = VersioningOption.MAJOR;
051        } else if (option == VersioningActions.ACTION_INCREMENT_MINOR) {
052            vo = VersioningOption.MINOR;
053        } else if (option == VersioningActions.ACTION_NO_INCREMENT) {
054            vo = VersioningOption.NONE;
055        } else {
056            vo = null;
057        }
058        return vo;
059    }
060
061    @Override
062    public String getAsString(FacesContext context, UIComponent component, Object value) {
063        if (value instanceof String) {
064            return (String) value;
065        }
066        if (value == null || (!(value instanceof VersioningOption))) {
067            return null;
068        }
069        VersioningOption option = (VersioningOption) value;
070        VersioningActions action = VersioningActions.getByVersioningOption(option);
071        if (action != null) {
072            return action.name();
073        }
074        return null;
075    }
076
077}