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.folder.adapter; 018 019import org.nuxeo.box.api.BoxConstants; 020import org.nuxeo.box.api.adapter.BoxAdapter; 021import org.nuxeo.box.api.marshalling.dao.BoxCollaboration; 022import org.nuxeo.box.api.marshalling.dao.BoxCollection; 023import org.nuxeo.box.api.marshalling.dao.BoxEmail; 024import org.nuxeo.box.api.marshalling.dao.BoxFolder; 025import org.nuxeo.box.api.marshalling.dao.BoxItem; 026import org.nuxeo.box.api.service.BoxService; 027import org.nuxeo.ecm.core.api.CoreSession; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.api.DocumentModelList; 030import org.nuxeo.ecm.core.api.IdRef; 031import org.nuxeo.ecm.core.api.security.ACE; 032import org.nuxeo.ecm.core.api.security.ACL; 033import org.nuxeo.runtime.api.Framework; 034 035import java.util.ArrayList; 036import java.util.Collections; 037import java.util.HashMap; 038import java.util.List; 039import java.util.Map; 040 041/** 042 * Box Folder Adapter 043 * 044 * @since 5.9.2 045 */ 046public class BoxFolderAdapter extends BoxAdapter { 047 048 /** 049 * Instantiate the adapter and the Box Folder from Nuxeo Document and load its properties into json format 050 */ 051 public BoxFolderAdapter(DocumentModel doc) { 052 super(doc); 053 CoreSession session = doc.getCoreSession(); 054 // Email update 055 final Map<String, Object> boxEmailProperties = new HashMap<>(); 056 boxEmailProperties.put(BoxEmail.FIELD_ACCESS, null); 057 boxEmailProperties.put(BoxEmail.FIELD_EMAIL, null); 058 final BoxEmail boxEmail = new BoxEmail(Collections.unmodifiableMap(boxEmailProperties)); 059 boxProperties.put(BoxFolder.FIELD_FOLDER_UPLOAD_EMAIL, boxEmail); 060 061 // Children 062 boxProperties.put(BoxFolder.FIELD_ITEM_COLLECTION, 063 getItemCollection(session, BoxConstants.BOX_LIMIT, BoxConstants.BOX_OFFSET, BoxConstants.BOX_FIELDS)); 064 065 boxItem = new BoxFolder(Collections.unmodifiableMap(boxProperties)); 066 } 067 068 @Override 069 public BoxItem getMiniItem() { 070 Map<String, Object> boxProperties = new HashMap<>(); 071 boxProperties.put(BoxItem.FIELD_ID, boxItem.getId()); 072 boxProperties.put(BoxItem.FIELD_SEQUENCE_ID, boxItem.getSequenceId()); 073 boxProperties.put(BoxItem.FIELD_NAME, boxItem.getName()); 074 return new BoxFolder(boxProperties); 075 } 076 077 /** 078 * Fill item collection entries box object 079 * 080 * @return the list of children in item collection 081 */ 082 public BoxCollection getItemCollection(CoreSession session, String limit, String offset, String fields) 083 { 084 final Map<String, Object> collectionProperties = new HashMap<>(); 085 // Fetch items 086 StringBuilder query = new StringBuilder(); 087 query.append("SELECT * FROM Document WHERE ecm:parentId="); 088 query.append("'" + doc.getId() + "'"); 089 DocumentModelList children = session.query(query.toString(), null, Long.parseLong(limit), 090 Long.parseLong(offset), false); 091 collectionProperties.put(BoxCollection.FIELD_ENTRIES, boxService.getBoxDocumentCollection(children, fields)); 092 collectionProperties.put(BoxCollection.FIELD_TOTAL_COUNT, children.size()); 093 return new BoxCollection(Collections.unmodifiableMap(collectionProperties)); 094 } 095 096 /** 097 * @return the ACLs set as a BoxCollection containing box collaborations listing 098 */ 099 public BoxCollection getCollaborations() { 100 BoxService boxService = Framework.getLocalService(BoxService.class); 101 List<BoxCollaboration> boxCollaborations = new ArrayList<>(); 102 Map<String, Object> collectionProperties = new HashMap<>(); 103 CoreSession session = doc.getCoreSession(); 104 for (ACL acl : session.getACP(new IdRef(doc.getId())).getACLs()) { 105 if (!(ACL.LOCAL_ACL.equals(acl.getName()) || ACL.INHERITED_ACL.equals(acl.getName()))) { 106 for (ACE ace : acl.getACEs()) { 107 if (ace.isGranted()) { 108 boxCollaborations.add(boxService.getBoxCollaboration(this, ace, acl.getName())); 109 } 110 } 111 } 112 } 113 collectionProperties.put(BoxCollection.FIELD_ENTRIES, boxCollaborations); 114 collectionProperties.put(BoxCollection.FIELD_TOTAL_COUNT, boxCollaborations.size()); 115 return new BoxCollection(Collections.unmodifiableMap(collectionProperties)); 116 } 117 118 /** 119 * @return the related collaboration on a specific folder 120 */ 121 public BoxCollaboration getCollaboration(String collaborationId) { 122 CoreSession session = doc.getCoreSession(); 123 ACL acl = session.getACP(new IdRef(doc.getId())).getACL(collaborationId); 124 if (acl == null) { 125 return null; 126 } 127 return boxService.getBoxCollaboration(this, acl.getACEs()[0], collaborationId); 128 } 129}