001/* 002 * (C) Copyright 2006-2011 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 * Florent Guillaume 018 */ 019 020package org.nuxeo.ecm.core.storage.sql; 021 022import java.io.Serializable; 023import java.util.Arrays; 024 025import org.nuxeo.ecm.core.storage.sql.RowMapper.RowUpdate; 026 027/** 028 * A type of fragment corresponding to several rows with the same id. 029 */ 030public class CollectionFragment extends Fragment { 031 032 private static final long serialVersionUID = 1L; 033 034 /** 035 * Constructs a {@link CollectionFragment} from a {@link Row}. 036 * 037 * @param row the row 038 * @param state the initial state for the fragment 039 * @param context the persistence context to which the fragment is tied, or {@code null} 040 */ 041 public CollectionFragment(Row row, State state, PersistenceContext context) { 042 super(row, state, context); 043 } 044 045 /** 046 * Sets a collection value. 047 * 048 * @param value the value 049 */ 050 public void set(Serializable[] value) { 051 // unless invalidated (in which case don't try to refetch the value just 052 // to compare state), don't mark modified or dirty if there is no change 053 if (getState() != State.INVALIDATED_MODIFIED) { 054 // not invalidated, so no need to call accessed() 055 if (Arrays.equals(row.values, value)) { 056 return; 057 } 058 } 059 row.values = value.clone(); 060 markModified(); 061 } 062 063 /** 064 * Gets the collection value. 065 * 066 * @return the value 067 */ 068 public Serializable[] get() { 069 accessed(); 070 return row.values.clone(); 071 } 072 073 /** 074 * Checks if the array is dirty (values changed since last clear). 075 * 076 * @return {@code true} if the array changed 077 */ 078 public boolean isDirty() { 079 return !Arrays.equals(row.values, oldvalues); 080 } 081 082 @Override 083 public RowUpdate getRowUpdate() { 084 if (Arrays.equals(row.values, oldvalues)) { 085 return null; 086 } 087 // check if we have a list append 088 if (oldvalues == null) { 089 // row.values != null otherwise we would have returned already 090 return new RowUpdate(row, 0); 091 } else if (row.values != null && isPrefix(oldvalues, row.values)) { 092 return new RowUpdate(row, oldvalues.length); 093 } else { 094 // full update, row.values may be null 095 return new RowUpdate(row); 096 } 097 } 098 099 /** 100 * Checks if the left array is a strict prefix of the right one. 101 * 102 * @since 8.3 103 */ 104 public static boolean isPrefix(Serializable[] left, Serializable[] right) { 105 if (left.length >= right.length) { 106 return false; 107 } 108 for (int i = 0; i < left.length; i++) { 109 Serializable o1 = left[i]; 110 Serializable o2 = right[i]; 111 if (!(o1 == null ? o2 == null : o1.equals(o2))) { 112 return false; 113 } 114 } 115 return true; 116 } 117 118 @Override 119 protected State refetch() { 120 row.values = context.mapper.readCollectionRowArray(row); 121 clearDirty(); 122 return State.PRISTINE; 123 } 124 125 @Override 126 protected State refetchDeleted() { 127 row.values = context.model.getCollectionFragmentType(row.tableName).getEmptyArray(); 128 clearDirty(); 129 return State.PRISTINE; 130 } 131 132}