001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.webapp.versioning;
018
019import javax.faces.component.UIComponent;
020import javax.faces.context.FacesContext;
021import javax.faces.convert.Converter;
022
023import org.apache.commons.lang.StringUtils;
024import org.jboss.seam.annotations.Name;
025import org.jboss.seam.annotations.intercept.BypassInterceptors;
026import org.nuxeo.ecm.core.api.VersioningOption;
027import org.nuxeo.ecm.platform.versioning.api.VersioningActions;
028
029/**
030 * Converter for document versioning, setting the accurate {@link VersioningOption} on the document model context data
031 * instead of String selected in the interface.
032 *
033 * @since 5.7.3
034 */
035@Name("documentVersioningConverter")
036@org.jboss.seam.annotations.faces.Converter
037@BypassInterceptors
038public class DocumentVersioningConverter implements Converter {
039
040    @Override
041    public Object getAsObject(FacesContext context, UIComponent component, String value) {
042        if (StringUtils.isBlank(value)) {
043            return null;
044        }
045        VersioningActions option = VersioningActions.valueOf(value);
046        VersioningOption vo;
047        if (option == VersioningActions.ACTION_INCREMENT_MAJOR) {
048            vo = VersioningOption.MAJOR;
049        } else if (option == VersioningActions.ACTION_INCREMENT_MINOR) {
050            vo = VersioningOption.MINOR;
051        } else if (option == VersioningActions.ACTION_NO_INCREMENT) {
052            vo = VersioningOption.NONE;
053        } else {
054            vo = null;
055        }
056        return vo;
057    }
058
059    @Override
060    public String getAsString(FacesContext context, UIComponent component, Object value) {
061        if (value instanceof String) {
062            return (String) value;
063        }
064        if (value == null || (!(value instanceof VersioningOption))) {
065            return null;
066        }
067        VersioningOption option = (VersioningOption) value;
068        VersioningActions action = VersioningActions.getByVersioningOption(option);
069        if (action != null) {
070            return action.name();
071        }
072        return null;
073    }
074
075}