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: StatementInfoImpl.java 22098 2007-07-06 12:39:33Z gracinet $
018 */
019
020package org.nuxeo.ecm.platform.relations.web;
021
022import java.util.Date;
023
024import org.nuxeo.ecm.platform.relations.api.Literal;
025import org.nuxeo.ecm.platform.relations.api.Node;
026import org.nuxeo.ecm.platform.relations.api.Resource;
027import org.nuxeo.ecm.platform.relations.api.Statement;
028import org.nuxeo.ecm.platform.relations.api.Subject;
029import org.nuxeo.ecm.platform.relations.api.impl.RelationDate;
030import org.nuxeo.ecm.platform.relations.api.util.RelationConstants;
031
032/**
033 * Statement representation for easier display.
034 *
035 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
036 */
037public class StatementInfoImpl implements StatementInfo {
038
039    private static final long serialVersionUID = 8474035719439311510L;
040
041    private final Statement statement;
042
043    private NodeInfo subjectRepresentation;
044
045    private NodeInfo predicateRepresentation;
046
047    private NodeInfo objectRepresentation;
048
049    private boolean incoming = false;
050
051    private boolean outgoing = false;
052
053    public StatementInfoImpl() {
054        this(null);
055    }
056
057    public StatementInfoImpl(Statement statement) {
058        this.statement = statement;
059    }
060
061    public StatementInfoImpl(Statement statement, NodeInfo subjectRepresentation, NodeInfo predicateRepresentation,
062            NodeInfo objectRepresentation) {
063        this.statement = statement;
064        this.subjectRepresentation = subjectRepresentation;
065        this.predicateRepresentation = predicateRepresentation;
066        this.objectRepresentation = objectRepresentation;
067    }
068
069    public boolean isIncoming() {
070        return incoming;
071    }
072
073    public boolean isOutgoing() {
074        return outgoing;
075    }
076
077    public void setIncoming(boolean incoming) {
078        this.incoming = incoming;
079    }
080
081    public void setOutgoing(boolean outgoing) {
082        this.outgoing = outgoing;
083    }
084
085    public Statement getStatement() {
086        return statement;
087    }
088
089    public Subject getSubject() {
090        return statement.getSubject();
091    }
092
093    public NodeInfo getSubjectInfo() {
094        if (subjectRepresentation == null) {
095            return new NodeInfoImpl(getSubject());
096        }
097        return subjectRepresentation;
098    }
099
100    public Resource getPredicate() {
101        return statement.getPredicate();
102    }
103
104    public NodeInfo getPredicateInfo() {
105        if (predicateRepresentation == null) {
106            return new NodeInfoImpl(getPredicate());
107        }
108        return predicateRepresentation;
109    }
110
111    public Node getObject() {
112        return statement.getObject();
113    }
114
115    public NodeInfo getObjectInfo() {
116        if (objectRepresentation == null) {
117            return new NodeInfoImpl(getObject());
118        }
119        return objectRepresentation;
120    }
121
122    // metadata
123
124    public String getComment() {
125        String comment = null;
126        Node node = statement.getProperty(RelationConstants.COMMENT);
127        if (node != null && node.isLiteral()) {
128            comment = ((Literal) node).getValue();
129        }
130        return comment;
131    }
132
133    public Date getCreationDate() {
134        Date date = null;
135        Node dateNode = statement.getProperty(RelationConstants.CREATION_DATE);
136        if (dateNode != null && dateNode.isLiteral()) {
137            date = RelationDate.getDate((Literal) dateNode);
138        }
139        return date;
140    }
141
142    public Date getModificationDate() {
143        Date date = null;
144        Node dateNode = statement.getProperty(RelationConstants.MODIFICATION_DATE);
145        if (dateNode != null && dateNode.isLiteral()) {
146            date = RelationDate.getDate((Literal) dateNode);
147        }
148        return date;
149    }
150
151    public String getAuthor() {
152        String source = null;
153        Node node = statement.getProperty(RelationConstants.AUTHOR);
154        if (node != null && node.isLiteral()) {
155            source = ((Literal) node).getValue();
156        }
157        return source;
158    }
159
160}