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