001/*
002 * (C) Copyright 2007 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 *     Nuxeo - initial API and implementation
016 *
017 * $Id: NodeInfoImpl.java 21693 2007-07-01 08:00:36Z sfermigier $
018 */
019
020package org.nuxeo.ecm.platform.relations.web;
021
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.platform.relations.api.Literal;
024import org.nuxeo.ecm.platform.relations.api.Node;
025import org.nuxeo.ecm.platform.relations.api.NodeType;
026import org.nuxeo.ecm.platform.relations.api.QNameResource;
027import org.nuxeo.ecm.platform.relations.api.Resource;
028import org.nuxeo.ecm.platform.ui.web.tag.fn.DocumentModelFunctions;
029
030/**
031 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
032 */
033public class NodeInfoImpl implements NodeInfo {
034
035    private static final long serialVersionUID = -5807130430819964154L;
036
037    protected final Node node;
038
039    protected DocumentModel documentModel;
040
041    protected boolean visible = false;
042
043    public NodeInfoImpl(Node node) {
044        this.node = node;
045    }
046
047    public NodeInfoImpl(Node node, DocumentModel documentModel) {
048        this.node = node;
049        this.documentModel = documentModel;
050    }
051
052    public NodeInfoImpl(Node node, DocumentModel documentModel, boolean visible) {
053        this.node = node;
054        this.documentModel = documentModel;
055        this.visible = visible;
056    }
057
058    // Node interface
059
060    public NodeType getNodeType() {
061        return node.getNodeType();
062    }
063
064    public boolean isBlank() {
065        return node.isBlank();
066    }
067
068    public boolean isLiteral() {
069        return node.isLiteral();
070    }
071
072    public boolean isQNameResource() {
073        return node.isQNameResource();
074    }
075
076    public boolean isResource() {
077        return node.isResource();
078    }
079
080    // NodeInfo interface
081
082    public int compareTo(Node o) {
083        return node.compareTo(o);
084    }
085
086    // Node Representation interface
087
088    public String getAction() {
089        if (visible && documentModel != null) {
090            String docId = documentModel.getId();
091            String actionValue = String.format("#{navigationContext.navigateToId('%s')}", docId);
092            return actionValue;
093        }
094        return null;
095    }
096
097    public DocumentModel getDocumentModel() {
098        return documentModel;
099    }
100
101    public String getHref() {
102        if (documentModel == null && isResource()) {
103            return ((Resource) node).getUri();
104        }
105        return null;
106    }
107
108    public String getIcon() {
109        return DocumentModelFunctions.iconPath(documentModel);
110    }
111
112    public String getTitle() {
113        String title = null;
114        if (node.isLiteral()) {
115            title = ((Literal) node).getValue();
116        } else if (node.isQNameResource()) {
117            String resourceTitle = null;
118            QNameResource resource = (QNameResource) node;
119            if (documentModel != null) {
120                String documentTitle = (String) documentModel.getProperty("dublincore", "title");
121                if (documentTitle != null && documentTitle.length() > 0) {
122                    resourceTitle = documentTitle;
123                }
124            }
125            if (resourceTitle == null) {
126                title = resource.getUri();
127            } else {
128                title = resourceTitle;
129            }
130        } else if (node.isResource()) {
131            title = ((Resource) node).getUri();
132        }
133        return title;
134    }
135
136    public boolean isLink() {
137        return getHref() != null;
138    }
139
140    public boolean isDocument() {
141        return documentModel != null;
142    }
143
144    public boolean isDocumentVisible() {
145        return documentModel != null && visible;
146    }
147
148    public boolean isText() {
149        return !(isDocumentVisible() || isLink());
150    }
151
152}