001/* 002 * (C) Copyright 2014 Nuxeo SA (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-2.1.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 * Vladimir Pasquier <vpasquier@nuxeo.com> 016 */ 017package org.nuxeo.box.api.comment.adapter; 018 019import org.nuxeo.box.api.marshalling.dao.BoxComment; 020import org.nuxeo.box.api.marshalling.dao.BoxFile; 021import org.nuxeo.box.api.marshalling.dao.BoxItem; 022import org.nuxeo.box.api.marshalling.dao.BoxTypedObject; 023import org.nuxeo.box.api.marshalling.dao.BoxUser; 024import org.nuxeo.box.api.marshalling.exceptions.BoxJSONException; 025import org.nuxeo.box.api.marshalling.exceptions.BoxRestException; 026import org.nuxeo.box.api.service.BoxService; 027import org.joda.time.DateTime; 028import org.joda.time.format.ISODateTimeFormat; 029import org.nuxeo.ecm.core.api.CoreSession; 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.core.api.NuxeoPrincipal; 032import org.nuxeo.ecm.platform.comment.api.CommentManager; 033import org.nuxeo.ecm.platform.usermanager.UserManager; 034import org.nuxeo.runtime.api.Framework; 035 036import java.lang.reflect.InvocationTargetException; 037import java.text.ParseException; 038import java.util.HashMap; 039import java.util.List; 040import java.util.Map; 041 042/** 043 * Box Comment Adapter 044 * 045 * @since 5.9.3 046 */ 047public class BoxCommentAdapter { 048 049 protected BoxComment boxComment; 050 051 protected final Map<String, Object> boxProperties = new HashMap<>(); 052 053 protected DocumentModel comment; 054 055 /** 056 * Instantiate the adapter and the Box Comment from Nuxeo Document and load its properties into json format 057 */ 058 public BoxCommentAdapter(DocumentModel doc) { 059 BoxService boxService = Framework.getLocalService(BoxService.class); 060 061 comment = doc; 062 063 boxProperties.put(BoxComment.FIELD_ID, doc.getId()); 064 boxProperties.put(BoxComment.FIELD_CREATED_AT, 065 ISODateTimeFormat.dateTime().print(new DateTime(doc.getPropertyValue("comment:creationDate")))); 066 067 // Nuxeo comment doesn't provide modified date 068 boxProperties.put(BoxComment.FIELD_MODIFIED_AT, 069 ISODateTimeFormat.dateTime().print(new DateTime(doc.getPropertyValue("dc:modified")))); 070 071 // Comment Author 072 final UserManager userManager = Framework.getLocalService(UserManager.class); 073 final NuxeoPrincipal creator = userManager.getPrincipal((String) doc.getPropertyValue("comment:author")); 074 final BoxUser boxCreator = boxService.fillUser(creator); 075 boxProperties.put(BoxComment.FIELD_CREATED_BY, boxCreator); 076 077 boxProperties.put(BoxComment.FIELD_MESSAGE, doc.getPropertyValue("comment:text")); 078 boxProperties.put(BoxComment.FIELD_IS_REPLY_COMMENT, null); 079 boxProperties.put(BoxComment.FIELD_ITEM, fillItem(doc)); 080 boxComment = new BoxComment(boxProperties); 081 } 082 083 public void setBoxComment(BoxComment boxComment) { 084 for (String field : boxComment.getKeySet()) { 085 this.boxComment.put(field, boxComment.getValue(field)); 086 } 087 } 088 089 private BoxTypedObject fillItem(DocumentModel doc) { 090 CommentManager commentManager = Framework.getLocalService(CommentManager.class); 091 List<DocumentModel> targetList = commentManager.getDocumentsForComment(doc); 092 if (targetList.isEmpty()) { 093 throw new BoxRestException("Cannot find any document for the " + "comment with id " + doc.getId()); 094 } 095 DocumentModel target = targetList.get(0); 096 Map<String, Object> itemProperties = new HashMap<>(); 097 itemProperties.put(BoxItem.FIELD_ID, target.getId()); 098 BoxTypedObject boxItem = new BoxFile(itemProperties); 099 return boxItem; 100 } 101 102 /** 103 * Update the comment (nx/box sides) 104 */ 105 public void save(CoreSession session) throws ParseException, InvocationTargetException, 106 IllegalAccessException, BoxJSONException { 107 comment.setPropertyValue("comment:text", boxComment.getMessage()); 108 session.saveDocument(comment); 109 session.save(); 110 } 111 112 public BoxComment getBoxComment() { 113 return boxComment; 114 } 115 116}