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.lang3.StringUtils; 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.nuxeo.ecm.collections.api.CollectionConstants; 028import org.nuxeo.ecm.core.api.DocumentModel; 029 030/** 031 * @since 5.9.3 032 */ 033public class Collection { 034 035 private static final Log log = LogFactory.getLog(Collection.class); 036 037 protected DocumentModel document; 038 039 public Collection(DocumentModel doc) { 040 document = doc; 041 } 042 043 public List<String> getCollectedDocumentIds() { 044 @SuppressWarnings("unchecked") 045 List<String> collected = (List<String>) document.getPropertyValue(CollectionConstants.COLLECTION_DOCUMENT_IDS_PROPERTY_NAME); 046 return collected; 047 } 048 049 public void addDocument(final String documentId) { 050 List<String> documentIds = getCollectedDocumentIds(); 051 if (!documentIds.contains(documentId)) { 052 documentIds.add(documentId); 053 } 054 setDocumentIds(documentIds); 055 } 056 057 public void removeDocument(final String documentId) { 058 List<String> documentIds = getCollectedDocumentIds(); 059 if (!documentIds.remove(documentId)) { 060 log.warn(String.format("Element '%s' is not present in the specified collection.", documentId)); 061 } 062 setDocumentIds(documentIds); 063 } 064 065 public void setDocumentIds(final List<String> documentIds) { 066 document.setPropertyValue(CollectionConstants.COLLECTION_DOCUMENT_IDS_PROPERTY_NAME, (Serializable) documentIds); 067 } 068 069 public DocumentModel getDocument() { 070 return document; 071 } 072 073 /** 074 * Move member1Id right after the member2Id of at first position if member2Id is null. 075 * 076 * @return true if successful 077 * @since 8.4 078 */ 079 public boolean moveMembers(String member1Id, String member2Id) { 080 List<String> documentIds = getCollectedDocumentIds(); 081 int member1IdIndex = documentIds.indexOf(member1Id); 082 if (member1IdIndex < 0) { 083 return false; 084 } 085 if (StringUtils.isBlank(member2Id)) { 086 documentIds.remove(member1IdIndex); 087 documentIds.add(0, member1Id); 088 setDocumentIds(documentIds); 089 return true; 090 } 091 else { 092 int member2IdIndex = documentIds.indexOf(member2Id); 093 if (member2IdIndex < 0) { 094 return false; 095 } 096 if (member1IdIndex == member2IdIndex) { 097 return false; 098 } 099 if (member2IdIndex > member1IdIndex) { 100 documentIds.remove(member1IdIndex); 101 int newMember2IdIndex = documentIds.indexOf(member2Id); 102 documentIds.add(newMember2IdIndex + 1, member1Id); 103 } else { 104 documentIds.remove(member2IdIndex); 105 int newMember1IdIndex = documentIds.indexOf(member1Id); 106 documentIds.add(newMember1IdIndex + 1, member2Id); 107 } 108 setDocumentIds(documentIds); 109 return true; 110 } 111 } 112 113 /** 114 * @since 8.3 115 */ 116 public int size() { 117 return getCollectedDocumentIds().size(); 118 } 119 120}