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