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.file.adapter; 020 021import org.nuxeo.box.api.BoxConstants; 022import org.nuxeo.box.api.adapter.BoxAdapter; 023import org.nuxeo.box.api.comment.adapter.BoxCommentAdapter; 024import org.nuxeo.box.api.marshalling.dao.BoxCollection; 025import org.nuxeo.box.api.marshalling.dao.BoxComment; 026import org.nuxeo.box.api.marshalling.dao.BoxFile; 027import org.nuxeo.box.api.marshalling.dao.BoxItem; 028import org.nuxeo.box.api.marshalling.dao.BoxLock; 029import org.nuxeo.box.api.marshalling.dao.BoxUser; 030import org.joda.time.DateTime; 031import org.joda.time.format.ISODateTimeFormat; 032import org.nuxeo.ecm.core.api.Blob; 033import org.nuxeo.ecm.core.api.DocumentModel; 034import org.nuxeo.ecm.core.api.Lock; 035import org.nuxeo.ecm.core.api.NuxeoPrincipal; 036import org.nuxeo.ecm.platform.comment.api.CommentManager; 037import org.nuxeo.ecm.platform.usermanager.UserManager; 038import org.nuxeo.runtime.api.Framework; 039 040import java.util.ArrayList; 041import java.util.Collections; 042import java.util.HashMap; 043import java.util.List; 044import java.util.Map; 045 046/** 047 * Box File Adapter 048 * 049 * @since 5.9.2 050 */ 051public class BoxFileAdapter extends BoxAdapter { 052 053 /** 054 * Instantiate the adapter and the Box File from Nuxeo Document and load its properties into json format 055 */ 056 public BoxFileAdapter(DocumentModel doc) { 057 super(doc); 058 059 // MD5 060 Blob blob = (Blob) doc.getPropertyValue("file:content"); 061 if (blob != null) { 062 boxProperties.put(BoxFile.FIELD_SHA1, blob.getDigest()); 063 } 064 065 // Lock 066 Map<String, Object> boxLockProperties = new HashMap<>(); 067 Lock lockInfo = doc.getLockInfo(); 068 if (lockInfo != null) { 069 boxLockProperties.put(BoxItem.FIELD_ID, null); 070 final UserManager userManager = Framework.getLocalService(UserManager.class); 071 final NuxeoPrincipal lockCreator = userManager.getPrincipal(lockInfo.getOwner()); 072 final BoxUser boxLockCreator = boxService.fillUser(lockCreator); 073 boxLockProperties.put(BoxItem.FIELD_CREATED_BY, boxLockCreator); 074 boxLockProperties.put(BoxItem.FIELD_CREATED_AT, 075 ISODateTimeFormat.dateTime().print(new DateTime(lockInfo.getCreated()))); 076 boxLockProperties.put(BoxLock.FIELD_EXPIRES_AT, null); 077 boxLockProperties.put(BoxLock.FIELD_IS_DOWNLOAD_PREVENTED, false); 078 BoxLock boxLock = new BoxLock(boxLockProperties); 079 boxProperties.put(BoxConstants.BOX_LOCK, boxLock); 080 } 081 082 boxItem = new BoxFile(Collections.unmodifiableMap(boxProperties)); 083 084 } 085 086 @Override 087 public BoxItem getMiniItem() { 088 Map<String, Object> boxProperties = new HashMap<>(); 089 boxProperties.put(BoxItem.FIELD_ID, boxItem.getId()); 090 boxProperties.put(BoxItem.FIELD_SEQUENCE_ID, boxItem.getSequenceId()); 091 boxProperties.put(BoxItem.FIELD_NAME, boxItem.getName()); 092 boxProperties.put(BoxItem.FIELD_ETAG, boxItem.getEtag()); 093 return new BoxFile(boxProperties); 094 } 095 096 public BoxCollection getComments() { 097 List<BoxComment> boxComments = new ArrayList<>(); 098 Map<String, Object> collectionProperties = new HashMap<>(); 099 CommentManager commentManager = Framework.getLocalService(CommentManager.class); 100 List<DocumentModel> comments = commentManager.getComments(doc); 101 for (DocumentModel comment : comments) { 102 BoxCommentAdapter boxCommentAdapter = comment.getAdapter(BoxCommentAdapter.class); 103 boxComments.add(boxCommentAdapter.getBoxComment()); 104 } 105 collectionProperties.put(BoxCollection.FIELD_ENTRIES, boxComments); 106 collectionProperties.put(BoxCollection.FIELD_TOTAL_COUNT, comments.size()); 107 return new BoxCollection(Collections.unmodifiableMap(collectionProperties)); 108 } 109 110}