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$
018 */
019
020package org.nuxeo.ecm.platform.comment.web;
021
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.Calendar;
025import java.util.List;
026
027import javax.faces.model.DataModel;
028import javax.faces.model.ListDataModel;
029
030import org.nuxeo.ecm.core.api.DocumentModel;
031
032/**
033 * @author <a href="mailto:glefter@nuxeo.com">George Lefter</a>
034 */
035public class UIComment implements Comparable, Serializable {
036
037    private static final long serialVersionUID = 2457051749449691092L;
038
039    // final Comment comment;
040
041    private final DocumentModel comment;
042
043    private final UIComment parent;
044
045    private List<UIComment> children;
046
047    public UIComment(UIComment parent, DocumentModel docModel) {
048        this.parent = parent;
049        comment = docModel;
050        children = new ArrayList<UIComment>();
051    }
052
053    public String getId() {
054        return comment.getId();
055    }
056
057    public List<UIComment> getChildren() {
058        return children;
059    }
060
061    public void setChildren(List<UIComment> children) {
062        this.children = children;
063    }
064
065    public boolean addChild(UIComment child) {
066        return children.add(child);
067    }
068
069    public UIComment getParent() {
070        return parent;
071    }
072
073    public boolean removeChild(UIComment child) {
074        return children.remove(child);
075    }
076
077    public DataModel getDataModel() {
078        return new ListDataModel(children);
079    }
080
081    // TODO : override equals and hashCode
082    @Override
083    public boolean equals(Object other) {
084        if (this == other) {
085            return true;
086        }
087        if (!(other instanceof UIComment)) {
088            return false;
089        }
090        UIComment temp = (UIComment) other;
091        return comment.getId().equals(temp.comment.getId());
092    }
093
094    @Override
095    public int hashCode() {
096        return comment.getId().hashCode();
097    }
098
099    public int compareTo(Object o) {
100        if (!(o instanceof UIComment)) {
101            return -1;
102        }
103        DocumentModel other = ((UIComment) o).comment;
104        Calendar myDate = (Calendar) comment.getProperty("dublincore", "created");
105        Calendar otherDate = (Calendar) other.getProperty("dublincore", "created");
106        return myDate.compareTo(otherDate);
107    }
108
109    public DocumentModel getComment() {
110        return comment;
111    }
112
113}