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:[email protected]">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 * @param member1Id 077 * @param member2Id 078 * @return true if successful 079 * @since 8.4 080 */ 081 public boolean moveMembers(String member1Id, String member2Id) { 082 List<String> documentIds = getCollectedDocumentIds(); 083 int member1IdIndex = documentIds.indexOf(member1Id); 084 if (member1IdIndex < 0) { 085 return false; 086 } 087 if (StringUtils.isBlank(member2Id)) { 088 documentIds.remove(member1IdIndex); 089 documentIds.add(0, member1Id); 090 setDocumentIds(documentIds); 091 return true; 092 } 093 else { 094 int member2IdIndex = documentIds.indexOf(member2Id); 095 if (member2IdIndex < 0) { 096 return false; 097 } 098 if (member1IdIndex == member2IdIndex) { 099 return false; 100 } 101 if (member2IdIndex > member1IdIndex) { 102 documentIds.remove(member1IdIndex); 103 int newMember2IdIndex = documentIds.indexOf(member2Id); 104 documentIds.add(newMember2IdIndex + 1, member1Id); 105 } else { 106 documentIds.remove(member2IdIndex); 107 int newMember1IdIndex = documentIds.indexOf(member1Id); 108 documentIds.add(newMember1IdIndex + 1, member2Id); 109 } 110 setDocumentIds(documentIds); 111 return true; 112 } 113 } 114 115 /** 116 * @since 8.3 117 */ 118 public int size() { 119 return getCollectedDocumentIds().size(); 120 } 121 122}