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