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