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; 023 024/** 025 * A {@link CollectionProperty} gives access to a collection value stored in an underlying {@link Fragment}. 026 * 027 * @author Florent Guillaume 028 */ 029public class CollectionProperty extends BaseProperty { 030 031 /** The {@link Fragment} holding the information. */ 032 private final Fragment fragment; 033 034 /** The key in the row if it is a SimpleFragment otherwise null */ 035 private final String key; 036 037 /** 038 * Creates a {@link CollectionProperty}. 039 */ 040 public CollectionProperty(String name, PropertyType type, boolean readonly, CollectionFragment fragment) { 041 super(name, type, readonly); 042 this.fragment = fragment; 043 this.key = null; 044 } 045 046 /** 047 * Creates a {@link CollectionProperty}. 048 */ 049 public CollectionProperty(String name, PropertyType type, boolean readonly, SimpleFragment fragment, String key) { 050 super(name, type, readonly); 051 this.fragment = fragment; 052 this.key = key; 053 } 054 055 // ----- getters ----- 056 057 public Serializable[] getValue() { 058 Serializable[] value = null; 059 if (hasCollectionFragment()) { 060 value = ((CollectionFragment) fragment).get(); 061 } else { 062 value = (Serializable[]) ((SimpleFragment) fragment).get(key); 063 if (value == null) { 064 value = type.getEmptyArray(); 065 } 066 } 067 return value; 068 } 069 070 public String[] getStrings() { 071 switch (type) { 072 case ARRAY_STRING: 073 Serializable[] res = getValue(); 074 if (res.length == 0) { 075 // special case because we may have an empty Serializable[] 076 res = new String[0]; 077 } 078 return (String[]) res; 079 default: 080 throw new UnsupportedOperationException("Not implemented: " + type); 081 } 082 } 083 084 // ----- setters ----- 085 086 public void setValue(Object[] value) { 087 checkWritable(); 088 try { 089 if (hasCollectionFragment()) { 090 ((CollectionFragment) fragment).set(type.normalize(value)); 091 } else { 092 ((SimpleFragment) fragment).put(key, type.normalize(value)); 093 } 094 } catch (IllegalArgumentException e) { 095 throw new IllegalArgumentException("item of list property '" + name + "': " + e.getMessage()); 096 } 097 // mark fragment dirty! 098 } 099 100 private boolean hasCollectionFragment() { 101 return key == null; 102 } 103 104}