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 * <a href="mailto:grenard@nuxeo.com">Guillaume</a> 016 */ 017package org.nuxeo.ecm.collections.core.adapter; 018 019import java.io.Serializable; 020import java.util.List; 021 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024import org.nuxeo.ecm.collections.api.CollectionConstants; 025import org.nuxeo.ecm.core.api.DocumentModel; 026 027/** 028 * @since 5.9.3 029 */ 030public class Collection { 031 032 private static final Log log = LogFactory.getLog(Collection.class); 033 034 protected DocumentModel document; 035 036 public Collection(DocumentModel doc) { 037 document = doc; 038 } 039 040 public List<String> getCollectedDocumentIds() { 041 @SuppressWarnings("unchecked") 042 List<String> collected = (List<String>) document.getPropertyValue(CollectionConstants.COLLECTION_DOCUMENT_IDS_PROPERTY_NAME); 043 return collected; 044 } 045 046 public void addDocument(final String documentId) { 047 List<String> documentIds = getCollectedDocumentIds(); 048 if (!documentIds.contains(documentId)) { 049 documentIds.add(documentId); 050 } 051 setDocumentIds(documentIds); 052 } 053 054 public void removeDocument(final String documentId) { 055 List<String> documentIds = getCollectedDocumentIds(); 056 if (!documentIds.remove(documentId)) { 057 log.warn(String.format("Element '%s' is not present in the specified collection.", documentId)); 058 } 059 setDocumentIds(documentIds); 060 } 061 062 public void setDocumentIds(final List<String> documentIds) { 063 document.setPropertyValue(CollectionConstants.COLLECTION_DOCUMENT_IDS_PROPERTY_NAME, (Serializable) documentIds); 064 } 065 066 public DocumentModel getDocument() { 067 return document; 068 } 069 070}