001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Florent Guillaume 011 */ 012 013package org.nuxeo.ecm.core.storage.sql; 014 015import java.io.Serializable; 016import java.util.Arrays; 017 018/** 019 * A type of fragment corresponding to several rows with the same id. 020 */ 021public class CollectionFragment extends Fragment { 022 023 private static final long serialVersionUID = 1L; 024 025 /** 026 * Constructs a {@link CollectionFragment} from a {@link Row}. 027 * 028 * @param row the row 029 * @param state the initial state for the fragment 030 * @param context the persistence context to which the fragment is tied, or {@code null} 031 */ 032 public CollectionFragment(Row row, State state, PersistenceContext context) { 033 super(row, state, context); 034 } 035 036 /** 037 * Sets a collection value. 038 * 039 * @param value the value 040 */ 041 public void set(Serializable[] value) { 042 // unless invalidated (in which case don't try to refetch the value just 043 // to compare state), don't mark modified or dirty if there is no change 044 if (getState() != State.INVALIDATED_MODIFIED) { 045 // not invalidated, so no need to call accessed() 046 if (Arrays.equals(row.values, value)) { 047 return; 048 } 049 } 050 row.values = value.clone(); 051 markModified(); 052 } 053 054 /** 055 * Gets the collection value. 056 * 057 * @return the value 058 */ 059 public Serializable[] get() { 060 accessed(); 061 return row.values.clone(); 062 } 063 064 /** 065 * Checks if the array is dirty (values changed since last clear). 066 * 067 * @return {@code true} if the array changed 068 */ 069 public boolean isDirty() { 070 return !Arrays.equals(row.values, oldvalues); 071 } 072 073 @Override 074 protected State refetch() { 075 row.values = context.mapper.readCollectionRowArray(row); 076 clearDirty(); 077 return State.PRISTINE; 078 } 079 080 @Override 081 protected State refetchDeleted() { 082 row.values = context.model.getCollectionFragmentType(row.tableName).getEmptyArray(); 083 clearDirty(); 084 return State.PRISTINE; 085 } 086 087}