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