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