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