001/* 002 * (C) Copyright 2018-2020 Nuxeo (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 * Funsho David 018 * Nuno Cunha <ncunha@nuxeo.com> 019 */ 020 021package org.nuxeo.ecm.platform.comment.api; 022 023import static org.nuxeo.common.utils.DateUtils.toCalendar; 024import static org.nuxeo.common.utils.DateUtils.toInstant; 025import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_ANCESTOR_IDS_PROPERTY; 026import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_AUTHOR_PROPERTY; 027import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_CREATION_DATE_PROPERTY; 028import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_DOC_TYPE; 029import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_MODIFICATION_DATE_PROPERTY; 030import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_PARENT_ID_PROPERTY; 031import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_TEXT_PROPERTY; 032import static org.nuxeo.ecm.platform.comment.api.ExternalEntityConstants.EXTERNAL_ENTITY_FACET; 033import static org.nuxeo.ecm.platform.comment.api.ExternalEntityConstants.EXTERNAL_ENTITY_ID_PROPERTY; 034import static org.nuxeo.ecm.platform.comment.api.ExternalEntityConstants.EXTERNAL_ENTITY_ORIGIN_PROPERTY; 035import static org.nuxeo.ecm.platform.comment.api.ExternalEntityConstants.EXTERNAL_ENTITY_PROPERTY; 036 037import java.time.Instant; 038import java.util.Calendar; 039import java.util.Collection; 040import java.util.HashSet; 041 042import org.apache.commons.lang3.builder.EqualsBuilder; 043import org.apache.commons.lang3.builder.ToStringBuilder; 044import org.nuxeo.ecm.core.api.DocumentModel; 045import org.nuxeo.ecm.core.api.impl.SimpleDocumentModel; 046 047/** 048 * @since 10.3 049 */ 050public class CommentImpl implements Comment, ExternalEntity { 051 052 /** 053 * @deprecated since 11.1, not used due to {@link #docModel} usage 054 */ 055 @Deprecated(since = "11.1") 056 protected String id; 057 058 /** 059 * @deprecated since 11.1, not used due to {@link #docModel} usage 060 */ 061 @Deprecated(since = "11.1") 062 protected String parentId; 063 064 /** 065 * @deprecated since 11.1, not used due to {@link #docModel} usage 066 */ 067 @Deprecated(since = "11.1") 068 protected Collection<String> ancestorIds = new HashSet<>(); 069 070 /** 071 * @deprecated since 11.1, not used due to {@link #docModel} usage 072 */ 073 @Deprecated(since = "11.1") 074 protected String author; 075 076 /** 077 * @deprecated since 11.1, not used due to {@link #docModel} usage 078 */ 079 @Deprecated(since = "11.1") 080 protected String text; 081 082 /** 083 * @deprecated since 11.1, not used due to {@link #docModel} usage 084 */ 085 @Deprecated(since = "11.1") 086 protected Instant creationDate; 087 088 /** 089 * @deprecated since 11.1, not used due to {@link #docModel} usage 090 */ 091 @Deprecated(since = "11.1") 092 protected Instant modificationDate; 093 094 /** 095 * @deprecated since 11.1, not used due to {@link #docModel} usage 096 */ 097 @Deprecated(since = "11.1") 098 protected String entityId; 099 100 /** 101 * @deprecated since 11.1, not used due to {@link #docModel} usage 102 */ 103 @Deprecated(since = "11.1") 104 protected String origin; 105 106 /** 107 * @deprecated since 11.1, not used due to {@link #docModel} usage 108 */ 109 @Deprecated(since = "11.1") 110 protected String entity; 111 112 /** 113 * {@link DocumentModel} storing the {@link Comment} metadata. 114 * 115 * @since 11.1 116 */ 117 protected DocumentModel docModel; 118 119 /** 120 * @since 11.1 121 */ 122 public CommentImpl() { 123 this(SimpleDocumentModel.ofType(COMMENT_DOC_TYPE)); 124 } 125 126 /** 127 * Constructor for the document adapter factory. 128 * 129 * @since 11.1 130 */ 131 protected CommentImpl(DocumentModel docModel) { 132 this.docModel = docModel; 133 this.docModel.detach(true); 134 } 135 136 @Override 137 public String getId() { 138 try { 139 return docModel.getId(); 140 } catch (UnsupportedOperationException e) { 141 // don't fail when docModel is SimpleDocumentModel 142 return null; 143 } 144 } 145 146 @Override 147 @Deprecated(since = "11.1") 148 public void setId(String id) { 149 // not used 150 } 151 152 @Override 153 public String getParentId() { 154 return (String) docModel.getPropertyValue(COMMENT_PARENT_ID_PROPERTY); 155 } 156 157 @Override 158 public void setParentId(String parentId) { 159 docModel.setPropertyValue(COMMENT_PARENT_ID_PROPERTY, parentId); 160 } 161 162 @Override 163 @SuppressWarnings("unchecked") 164 public Collection<String> getAncestorIds() { 165 return (Collection<String>) docModel.getPropertyValue(COMMENT_ANCESTOR_IDS_PROPERTY); 166 } 167 168 @Override 169 @Deprecated(since = "11.1") 170 public void addAncestorId(String ancestorId) { 171 // not used 172 } 173 174 @Override 175 public String getAuthor() { 176 return (String) docModel.getPropertyValue(COMMENT_AUTHOR_PROPERTY); 177 } 178 179 @Override 180 public void setAuthor(String author) { 181 docModel.setPropertyValue(COMMENT_AUTHOR_PROPERTY, author); 182 } 183 184 @Override 185 public String getText() { 186 return (String) docModel.getPropertyValue(COMMENT_TEXT_PROPERTY); 187 } 188 189 @Override 190 public void setText(String text) { 191 docModel.setPropertyValue(COMMENT_TEXT_PROPERTY, text); 192 } 193 194 @Override 195 public Instant getCreationDate() { 196 return toInstant((Calendar) docModel.getPropertyValue(COMMENT_CREATION_DATE_PROPERTY)); 197 } 198 199 @Override 200 public void setCreationDate(Instant creationDate) { 201 docModel.setPropertyValue(COMMENT_CREATION_DATE_PROPERTY, toCalendar(creationDate)); 202 } 203 204 @Override 205 public Instant getModificationDate() { 206 return toInstant((Calendar) docModel.getPropertyValue(COMMENT_MODIFICATION_DATE_PROPERTY)); 207 } 208 209 @Override 210 public void setModificationDate(Instant modificationDate) { 211 docModel.setPropertyValue(COMMENT_MODIFICATION_DATE_PROPERTY, toCalendar(modificationDate)); 212 } 213 214 @Override 215 public String getEntityId() { 216 if (docModel.hasFacet(EXTERNAL_ENTITY_FACET)) { 217 return (String) docModel.getPropertyValue(EXTERNAL_ENTITY_ID_PROPERTY); 218 } 219 return null; 220 } 221 222 @Override 223 public void setEntityId(String entityId) { 224 docModel.addFacet(EXTERNAL_ENTITY_FACET); 225 docModel.setPropertyValue(EXTERNAL_ENTITY_ID_PROPERTY, entityId); 226 } 227 228 @Override 229 public String getOrigin() { 230 if (docModel.hasFacet(EXTERNAL_ENTITY_FACET)) { 231 return (String) docModel.getPropertyValue(EXTERNAL_ENTITY_ORIGIN_PROPERTY); 232 } 233 return null; 234 } 235 236 @Override 237 public void setOrigin(String origin) { 238 docModel.addFacet(EXTERNAL_ENTITY_FACET); 239 docModel.setPropertyValue(EXTERNAL_ENTITY_ORIGIN_PROPERTY, origin); 240 } 241 242 @Override 243 public String getEntity() { 244 if (docModel.hasFacet(EXTERNAL_ENTITY_FACET)) { 245 return (String) docModel.getPropertyValue(EXTERNAL_ENTITY_PROPERTY); 246 } 247 return null; 248 } 249 250 @Override 251 public void setEntity(String entity) { 252 docModel.addFacet(EXTERNAL_ENTITY_FACET); 253 docModel.setPropertyValue(EXTERNAL_ENTITY_PROPERTY, entity); 254 } 255 256 @Override 257 public DocumentModel getDocument() { 258 return docModel; 259 } 260 261 @Override 262 public boolean equals(Object o) { 263 return EqualsBuilder.reflectionEquals(this, o); 264 } 265 266 @Override 267 public int hashCode() { 268 return docModel.hashCode(); 269 } 270 271 @Override 272 public String toString() { 273 return new ToStringBuilder(this).append("id", getId()).toString(); 274 } 275}