001/*
002 * (C) Copyright 2010 Nuxeo SAS (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.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 *    Mariana Cedica
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.routing.web;
021
022import javax.faces.component.UIComponent;
023import javax.faces.context.FacesContext;
024import javax.faces.convert.Converter;
025
026import org.apache.commons.lang.StringUtils;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.IdRef;
030
031/**
032 * JSF Converter used for rendering, transforming a docId into the document title
033 *
034 * @author Mariana Cedica
035 */
036public class DocumentModelConvertor implements Converter {
037
038    CoreSession session;
039
040    public DocumentModelConvertor(CoreSession session) {
041        this.session = session;
042    }
043
044    /**
045     * Returns given value (does not do any reverse conversion)
046     */
047    @Override
048    public Object getAsObject(FacesContext arg0, UIComponent component, String value) {
049        return value;
050    }
051
052    /**
053     * Returns the document title using the docId passed as value
054     */
055    @Override
056    public String getAsString(FacesContext arg0, UIComponent component, Object value) {
057        if (value instanceof String && !StringUtils.isEmpty((String) value)) {
058            DocumentModel doc = session.getDocument((new IdRef((String) value)));
059            return doc.getTitle();
060        }
061        if (value != null) {
062            return value.toString();
063        } else {
064            return null;
065        }
066    }
067}