diff options
Diffstat (limited to 'server/src/com/vaadin/data/util')
98 files changed, 0 insertions, 22338 deletions
diff --git a/server/src/com/vaadin/data/util/AbstractBeanContainer.java b/server/src/com/vaadin/data/util/AbstractBeanContainer.java deleted file mode 100644 index 6dcfbb2b84..0000000000 --- a/server/src/com/vaadin/data/util/AbstractBeanContainer.java +++ /dev/null @@ -1,927 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import java.io.Serializable; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import com.vaadin.data.Container; -import com.vaadin.data.Container.Filterable; -import com.vaadin.data.Container.PropertySetChangeNotifier; -import com.vaadin.data.Container.SimpleFilterable; -import com.vaadin.data.Container.Sortable; -import com.vaadin.data.Item; -import com.vaadin.data.Property; -import com.vaadin.data.Property.ValueChangeEvent; -import com.vaadin.data.Property.ValueChangeListener; -import com.vaadin.data.Property.ValueChangeNotifier; -import com.vaadin.data.util.MethodProperty.MethodException; -import com.vaadin.data.util.filter.SimpleStringFilter; -import com.vaadin.data.util.filter.UnsupportedFilterException; - -/** - * An abstract base class for in-memory containers for JavaBeans. - * - * <p> - * The properties of the container are determined automatically by introspecting - * the used JavaBean class and explicitly adding or removing properties is not - * supported. Only beans of the same type can be added to the container. - * </p> - * - * <p> - * Subclasses should implement any public methods adding items to the container, - * typically calling the protected methods {@link #addItem(Object, Object)}, - * {@link #addItemAfter(Object, Object, Object)} and - * {@link #addItemAt(int, Object, Object)}. - * </p> - * - * @param <IDTYPE> - * The type of the item identifier - * @param <BEANTYPE> - * The type of the Bean - * - * @since 6.5 - */ -public abstract class AbstractBeanContainer<IDTYPE, BEANTYPE> extends - AbstractInMemoryContainer<IDTYPE, String, BeanItem<BEANTYPE>> implements - Filterable, SimpleFilterable, Sortable, ValueChangeListener, - PropertySetChangeNotifier { - - /** - * Resolver that maps beans to their (item) identifiers, removing the need - * to explicitly specify item identifiers when there is no need to customize - * this. - * - * Note that beans can also be added with an explicit id even if a resolver - * has been set. - * - * @param <IDTYPE> - * @param <BEANTYPE> - * - * @since 6.5 - */ - public static interface BeanIdResolver<IDTYPE, BEANTYPE> extends - Serializable { - /** - * Return the item identifier for a bean. - * - * @param bean - * @return - */ - public IDTYPE getIdForBean(BEANTYPE bean); - } - - /** - * A item identifier resolver that returns the value of a bean property. - * - * The bean must have a getter for the property, and the getter must return - * an object of type IDTYPE. - */ - protected class PropertyBasedBeanIdResolver implements - BeanIdResolver<IDTYPE, BEANTYPE> { - - private final Object propertyId; - - public PropertyBasedBeanIdResolver(Object propertyId) { - if (propertyId == null) { - throw new IllegalArgumentException( - "Property identifier must not be null"); - } - this.propertyId = propertyId; - } - - @Override - @SuppressWarnings("unchecked") - public IDTYPE getIdForBean(BEANTYPE bean) - throws IllegalArgumentException { - VaadinPropertyDescriptor<BEANTYPE> pd = model.get(propertyId); - if (null == pd) { - throw new IllegalStateException("Property " + propertyId - + " not found"); - } - try { - Property<IDTYPE> property = (Property<IDTYPE>) pd - .createProperty(bean); - return property.getValue(); - } catch (MethodException e) { - throw new IllegalArgumentException(e); - } - } - - } - - /** - * The resolver that finds the item ID for a bean, or null not to use - * automatic resolving. - * - * Methods that add a bean without specifying an ID must not be called if no - * resolver has been set. - */ - private BeanIdResolver<IDTYPE, BEANTYPE> beanIdResolver = null; - - /** - * Maps all item ids in the container (including filtered) to their - * corresponding BeanItem. - */ - private final Map<IDTYPE, BeanItem<BEANTYPE>> itemIdToItem = new HashMap<IDTYPE, BeanItem<BEANTYPE>>(); - - /** - * The type of the beans in the container. - */ - private final Class<? super BEANTYPE> type; - - /** - * A description of the properties found in beans of type {@link #type}. - * Determines the property ids that are present in the container. - */ - private final LinkedHashMap<String, VaadinPropertyDescriptor<BEANTYPE>> model; - - /** - * Constructs a {@code AbstractBeanContainer} for beans of the given type. - * - * @param type - * the type of the beans that will be added to the container. - * @throws IllegalArgumentException - * If {@code type} is null - */ - protected AbstractBeanContainer(Class<? super BEANTYPE> type) { - if (type == null) { - throw new IllegalArgumentException( - "The bean type passed to AbstractBeanContainer must not be null"); - } - this.type = type; - model = BeanItem.getPropertyDescriptors((Class<BEANTYPE>) type); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#getType(java.lang.Object) - */ - @Override - public Class<?> getType(Object propertyId) { - VaadinPropertyDescriptor<BEANTYPE> descriptor = model.get(propertyId); - if (descriptor == null) { - return null; - } - return descriptor.getPropertyType(); - } - - /** - * Create a BeanItem for a bean using pre-parsed bean metadata (based on - * {@link #getBeanType()}). - * - * @param bean - * @return created {@link BeanItem} or null if bean is null - */ - protected BeanItem<BEANTYPE> createBeanItem(BEANTYPE bean) { - return bean == null ? null : new BeanItem<BEANTYPE>(bean, model); - } - - /** - * Returns the type of beans this Container can contain. - * - * This comes from the bean type constructor parameter, and bean metadata - * (including container properties) is based on this. - * - * @return - */ - public Class<? super BEANTYPE> getBeanType() { - return type; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#getContainerPropertyIds() - */ - @Override - public Collection<String> getContainerPropertyIds() { - return model.keySet(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#removeAllItems() - */ - @Override - public boolean removeAllItems() { - int origSize = size(); - IDTYPE firstItem = getFirstVisibleItem(); - - internalRemoveAllItems(); - - // detach listeners from all Items - for (Item item : itemIdToItem.values()) { - removeAllValueChangeListeners(item); - } - itemIdToItem.clear(); - - // fire event only if the visible view changed, regardless of whether - // filtered out items were removed or not - if (origSize != 0) { - fireItemsRemoved(0, firstItem, origSize); - } - - return true; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#getItem(java.lang.Object) - */ - @Override - public BeanItem<BEANTYPE> getItem(Object itemId) { - // TODO return only if visible? - return getUnfilteredItem(itemId); - } - - @Override - protected BeanItem<BEANTYPE> getUnfilteredItem(Object itemId) { - return itemIdToItem.get(itemId); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#getItemIds() - */ - @Override - @SuppressWarnings("unchecked") - public List<IDTYPE> getItemIds() { - return (List<IDTYPE>) super.getItemIds(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#getContainerProperty(java.lang.Object, - * java.lang.Object) - */ - @Override - public Property getContainerProperty(Object itemId, Object propertyId) { - Item item = getItem(itemId); - if (item == null) { - return null; - } - return item.getItemProperty(propertyId); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#removeItem(java.lang.Object) - */ - @Override - public boolean removeItem(Object itemId) { - // TODO should also remove items that are filtered out - int origSize = size(); - Item item = getItem(itemId); - int position = indexOfId(itemId); - - if (internalRemoveItem(itemId)) { - // detach listeners from Item - removeAllValueChangeListeners(item); - - // remove item - itemIdToItem.remove(itemId); - - // fire event only if the visible view changed, regardless of - // whether filtered out items were removed or not - if (size() != origSize) { - fireItemRemoved(position, itemId); - } - - return true; - } else { - return false; - } - } - - /** - * Re-filter the container when one of the monitored properties changes. - */ - @Override - public void valueChange(ValueChangeEvent event) { - // if a property that is used in a filter is changed, refresh filtering - filterAll(); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.Container.Filterable#addContainerFilter(java.lang.Object, - * java.lang.String, boolean, boolean) - */ - @Override - public void addContainerFilter(Object propertyId, String filterString, - boolean ignoreCase, boolean onlyMatchPrefix) { - try { - addFilter(new SimpleStringFilter(propertyId, filterString, - ignoreCase, onlyMatchPrefix)); - } catch (UnsupportedFilterException e) { - // the filter instance created here is always valid for in-memory - // containers - } - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Filterable#removeAllContainerFilters() - */ - @Override - public void removeAllContainerFilters() { - if (!getFilters().isEmpty()) { - for (Item item : itemIdToItem.values()) { - removeAllValueChangeListeners(item); - } - removeAllFilters(); - } - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.Container.Filterable#removeContainerFilters(java.lang - * .Object) - */ - @Override - public void removeContainerFilters(Object propertyId) { - Collection<Filter> removedFilters = super.removeFilters(propertyId); - if (!removedFilters.isEmpty()) { - // stop listening to change events for the property - for (Item item : itemIdToItem.values()) { - removeValueChangeListener(item, propertyId); - } - } - } - - @Override - public void addContainerFilter(Filter filter) - throws UnsupportedFilterException { - addFilter(filter); - } - - @Override - public void removeContainerFilter(Filter filter) { - removeFilter(filter); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.AbstractInMemoryContainer#hasContainerFilters() - */ - @Override - public boolean hasContainerFilters() { - return super.hasContainerFilters(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.AbstractInMemoryContainer#getContainerFilters() - */ - @Override - public Collection<Filter> getContainerFilters() { - return super.getContainerFilters(); - } - - /** - * Make this container listen to the given property provided it notifies - * when its value changes. - * - * @param item - * The {@link Item} that contains the property - * @param propertyId - * The id of the property - */ - private void addValueChangeListener(Item item, Object propertyId) { - Property<?> property = item.getItemProperty(propertyId); - if (property instanceof ValueChangeNotifier) { - // avoid multiple notifications for the same property if - // multiple filters are in use - ValueChangeNotifier notifier = (ValueChangeNotifier) property; - notifier.removeListener(this); - notifier.addListener(this); - } - } - - /** - * Remove this container as a listener for the given property. - * - * @param item - * The {@link Item} that contains the property - * @param propertyId - * The id of the property - */ - private void removeValueChangeListener(Item item, Object propertyId) { - Property<?> property = item.getItemProperty(propertyId); - if (property instanceof ValueChangeNotifier) { - ((ValueChangeNotifier) property).removeListener(this); - } - } - - /** - * Remove this contains as a listener for all the properties in the given - * {@link Item}. - * - * @param item - * The {@link Item} that contains the properties - */ - private void removeAllValueChangeListeners(Item item) { - for (Object propertyId : item.getItemPropertyIds()) { - removeValueChangeListener(item, propertyId); - } - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Sortable#getSortableContainerPropertyIds() - */ - @Override - public Collection<?> getSortableContainerPropertyIds() { - return getSortablePropertyIds(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Sortable#sort(java.lang.Object[], - * boolean[]) - */ - @Override - public void sort(Object[] propertyId, boolean[] ascending) { - sortContainer(propertyId, ascending); - } - - @Override - public ItemSorter getItemSorter() { - return super.getItemSorter(); - } - - @Override - public void setItemSorter(ItemSorter itemSorter) { - super.setItemSorter(itemSorter); - } - - @Override - protected void registerNewItem(int position, IDTYPE itemId, - BeanItem<BEANTYPE> item) { - itemIdToItem.put(itemId, item); - - // add listeners to be able to update filtering on property - // changes - for (Filter filter : getFilters()) { - for (String propertyId : getContainerPropertyIds()) { - if (filter.appliesToProperty(propertyId)) { - // addValueChangeListener avoids adding duplicates - addValueChangeListener(item, propertyId); - } - } - } - } - - /** - * Check that a bean can be added to the container (is of the correct type - * for the container). - * - * @param bean - * @return - */ - private boolean validateBean(BEANTYPE bean) { - return bean != null && getBeanType().isAssignableFrom(bean.getClass()); - } - - /** - * Adds the bean to the Container. - * - * Note: the behavior of this method changed in Vaadin 6.6 - now items are - * added at the very end of the unfiltered container and not after the last - * visible item if filtering is used. - * - * @see com.vaadin.data.Container#addItem(Object) - */ - protected BeanItem<BEANTYPE> addItem(IDTYPE itemId, BEANTYPE bean) { - if (!validateBean(bean)) { - return null; - } - return internalAddItemAtEnd(itemId, createBeanItem(bean), true); - } - - /** - * Adds the bean after the given bean. - * - * @see com.vaadin.data.Container.Ordered#addItemAfter(Object, Object) - */ - protected BeanItem<BEANTYPE> addItemAfter(IDTYPE previousItemId, - IDTYPE newItemId, BEANTYPE bean) { - if (!validateBean(bean)) { - return null; - } - return internalAddItemAfter(previousItemId, newItemId, - createBeanItem(bean), true); - } - - /** - * Adds a new bean at the given index. - * - * The bean is used both as the item contents and as the item identifier. - * - * @param index - * Index at which the bean should be added. - * @param newItemId - * The item id for the bean to add to the container. - * @param bean - * The bean to add to the container. - * - * @return Returns the new BeanItem or null if the operation fails. - */ - protected BeanItem<BEANTYPE> addItemAt(int index, IDTYPE newItemId, - BEANTYPE bean) { - if (!validateBean(bean)) { - return null; - } - return internalAddItemAt(index, newItemId, createBeanItem(bean), true); - } - - /** - * Adds a bean to the container using the bean item id resolver to find its - * identifier. - * - * A bean id resolver must be set before calling this method. - * - * @see #addItem(Object, Object) - * - * @param bean - * the bean to add - * @return BeanItem<BEANTYPE> item added or null - * @throws IllegalStateException - * if no bean identifier resolver has been set - * @throws IllegalArgumentException - * if an identifier cannot be resolved for the bean - */ - protected BeanItem<BEANTYPE> addBean(BEANTYPE bean) - throws IllegalStateException, IllegalArgumentException { - if (bean == null) { - return null; - } - IDTYPE itemId = resolveBeanId(bean); - if (itemId == null) { - throw new IllegalArgumentException( - "Resolved identifier for a bean must not be null"); - } - return addItem(itemId, bean); - } - - /** - * Adds a bean to the container after a specified item identifier, using the - * bean item id resolver to find its identifier. - * - * A bean id resolver must be set before calling this method. - * - * @see #addItemAfter(Object, Object, Object) - * - * @param previousItemId - * the identifier of the bean after which this bean should be - * added, null to add to the beginning - * @param bean - * the bean to add - * @return BeanItem<BEANTYPE> item added or null - * @throws IllegalStateException - * if no bean identifier resolver has been set - * @throws IllegalArgumentException - * if an identifier cannot be resolved for the bean - */ - protected BeanItem<BEANTYPE> addBeanAfter(IDTYPE previousItemId, - BEANTYPE bean) throws IllegalStateException, - IllegalArgumentException { - if (bean == null) { - return null; - } - IDTYPE itemId = resolveBeanId(bean); - if (itemId == null) { - throw new IllegalArgumentException( - "Resolved identifier for a bean must not be null"); - } - return addItemAfter(previousItemId, itemId, bean); - } - - /** - * Adds a bean at a specified (filtered view) position in the container - * using the bean item id resolver to find its identifier. - * - * A bean id resolver must be set before calling this method. - * - * @see #addItemAfter(Object, Object, Object) - * - * @param index - * the index (in the filtered view) at which to add the item - * @param bean - * the bean to add - * @return BeanItem<BEANTYPE> item added or null - * @throws IllegalStateException - * if no bean identifier resolver has been set - * @throws IllegalArgumentException - * if an identifier cannot be resolved for the bean - */ - protected BeanItem<BEANTYPE> addBeanAt(int index, BEANTYPE bean) - throws IllegalStateException, IllegalArgumentException { - if (bean == null) { - return null; - } - IDTYPE itemId = resolveBeanId(bean); - if (itemId == null) { - throw new IllegalArgumentException( - "Resolved identifier for a bean must not be null"); - } - return addItemAt(index, itemId, bean); - } - - /** - * Adds all the beans from a {@link Collection} in one operation using the - * bean item identifier resolver. More efficient than adding them one by - * one. - * - * A bean id resolver must be set before calling this method. - * - * Note: the behavior of this method changed in Vaadin 6.6 - now items are - * added at the very end of the unfiltered container and not after the last - * visible item if filtering is used. - * - * @param collection - * The collection of beans to add. Must not be null. - * @throws IllegalStateException - * if no bean identifier resolver has been set - * @throws IllegalArgumentException - * if the resolver returns a null itemId for one of the beans in - * the collection - */ - protected void addAll(Collection<? extends BEANTYPE> collection) - throws IllegalStateException, IllegalArgumentException { - boolean modified = false; - int origSize = size(); - - for (BEANTYPE bean : collection) { - // TODO skipping invalid beans - should not allow them in javadoc? - if (bean == null - || !getBeanType().isAssignableFrom(bean.getClass())) { - continue; - } - IDTYPE itemId = resolveBeanId(bean); - if (itemId == null) { - throw new IllegalArgumentException( - "Resolved identifier for a bean must not be null"); - } - - if (internalAddItemAtEnd(itemId, createBeanItem(bean), false) != null) { - modified = true; - } - } - - if (modified) { - // Filter the contents when all items have been added - if (isFiltered()) { - doFilterContainer(!getFilters().isEmpty()); - } - if (visibleNewItemsWasAdded(origSize)) { - // fire event about added items - int firstPosition = origSize; - IDTYPE firstItemId = getVisibleItemIds().get(firstPosition); - int affectedItems = size() - origSize; - fireItemsAdded(firstPosition, firstItemId, affectedItems); - } - } - } - - private boolean visibleNewItemsWasAdded(int origSize) { - return size() > origSize; - } - - /** - * Use the bean resolver to get the identifier for a bean. - * - * @param bean - * @return resolved bean identifier, null if could not be resolved - * @throws IllegalStateException - * if no bean resolver is set - */ - protected IDTYPE resolveBeanId(BEANTYPE bean) { - if (beanIdResolver == null) { - throw new IllegalStateException( - "Bean item identifier resolver is required."); - } - return beanIdResolver.getIdForBean(bean); - } - - /** - * Sets the resolver that finds the item id for a bean, or null not to use - * automatic resolving. - * - * Methods that add a bean without specifying an id must not be called if no - * resolver has been set. - * - * Note that methods taking an explicit id can be used whether a resolver - * has been defined or not. - * - * @param beanIdResolver - * to use or null to disable automatic id resolution - */ - protected void setBeanIdResolver( - BeanIdResolver<IDTYPE, BEANTYPE> beanIdResolver) { - this.beanIdResolver = beanIdResolver; - } - - /** - * Returns the resolver that finds the item ID for a bean. - * - * @return resolver used or null if automatic item id resolving is disabled - */ - public BeanIdResolver<IDTYPE, BEANTYPE> getBeanIdResolver() { - return beanIdResolver; - } - - /** - * Create an item identifier resolver using a named bean property. - * - * @param propertyId - * property identifier, which must map to a getter in BEANTYPE - * @return created resolver - */ - protected BeanIdResolver<IDTYPE, BEANTYPE> createBeanPropertyResolver( - Object propertyId) { - return new PropertyBasedBeanIdResolver(propertyId); - } - - /** - * @deprecated As of 7.0, replaced by {@link #addPropertySetChangeListener} - **/ - @Deprecated - @Override - public void addListener(Container.PropertySetChangeListener listener) { - addPropertySetChangeListener(listener); - } - - @Override - public void addPropertySetChangeListener( - Container.PropertySetChangeListener listener) { - super.addPropertySetChangeListener(listener); - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #removePropertySetChangeListener(com.vaadin.data.Container.PropertySetChangeListener)} - **/ - @Deprecated - @Override - public void removeListener(Container.PropertySetChangeListener listener) { - removePropertySetChangeListener(listener); - } - - @Override - public void removePropertySetChangeListener( - Container.PropertySetChangeListener listener) { - super.removePropertySetChangeListener(listener); - } - - @Override - public boolean addContainerProperty(Object propertyId, Class<?> type, - Object defaultValue) throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "Use addNestedContainerProperty(String) to add container properties to a " - + getClass().getSimpleName()); - } - - /** - * Adds a property for the container and all its items. - * - * Primarily for internal use, may change in future versions. - * - * @param propertyId - * @param propertyDescriptor - * @return true if the property was added - */ - protected final boolean addContainerProperty(String propertyId, - VaadinPropertyDescriptor<BEANTYPE> propertyDescriptor) { - if (null == propertyId || null == propertyDescriptor) { - return false; - } - - // Fails if the Property is already present - if (model.containsKey(propertyId)) { - return false; - } - - model.put(propertyId, propertyDescriptor); - for (BeanItem<BEANTYPE> item : itemIdToItem.values()) { - item.addItemProperty(propertyId, - propertyDescriptor.createProperty(item.getBean())); - } - - // Sends a change event - fireContainerPropertySetChange(); - - return true; - } - - /** - * Adds a nested container property for the container, e.g. - * "manager.address.street". - * - * All intermediate getters must exist and should return non-null values - * when the property value is accessed. If an intermediate getter returns - * null, a null value will be returned. - * - * @see NestedMethodProperty - * - * @param propertyId - * @return true if the property was added - */ - public boolean addNestedContainerProperty(String propertyId) { - return addContainerProperty(propertyId, new NestedPropertyDescriptor( - propertyId, type)); - } - - /** - * Adds a nested container properties for all sub-properties of a named - * property to the container. The named property itself is removed from the - * model as its subproperties are added. - * - * All intermediate getters must exist and should return non-null values - * when the property value is accessed. If an intermediate getter returns - * null, a null value will be returned. - * - * @see NestedMethodProperty - * @see #addNestedContainerProperty(String) - * - * @param propertyId - */ - @SuppressWarnings("unchecked") - public void addNestedContainerBean(String propertyId) { - Class<?> propertyType = getType(propertyId); - LinkedHashMap<String, VaadinPropertyDescriptor<Object>> pds = BeanItem - .getPropertyDescriptors((Class<Object>) propertyType); - for (String subPropertyId : pds.keySet()) { - String qualifiedPropertyId = propertyId + "." + subPropertyId; - NestedPropertyDescriptor<BEANTYPE> pd = new NestedPropertyDescriptor<BEANTYPE>( - qualifiedPropertyId, (Class<BEANTYPE>) type); - model.put(qualifiedPropertyId, pd); - model.remove(propertyId); - for (BeanItem<BEANTYPE> item : itemIdToItem.values()) { - item.addItemProperty(qualifiedPropertyId, - pd.createProperty(item.getBean())); - item.removeItemProperty(propertyId); - } - } - - // Sends a change event - fireContainerPropertySetChange(); - } - - @Override - public boolean removeContainerProperty(Object propertyId) - throws UnsupportedOperationException { - // Fails if the Property is not present - if (!model.containsKey(propertyId)) { - return false; - } - - // Removes the Property to Property list and types - model.remove(propertyId); - - // If remove the Property from all Items - for (final Iterator<IDTYPE> i = getAllItemIds().iterator(); i.hasNext();) { - getUnfilteredItem(i.next()).removeItemProperty(propertyId); - } - - // Sends a change event - fireContainerPropertySetChange(); - - return true; - } - -} diff --git a/server/src/com/vaadin/data/util/AbstractContainer.java b/server/src/com/vaadin/data/util/AbstractContainer.java deleted file mode 100644 index dafdd15b06..0000000000 --- a/server/src/com/vaadin/data/util/AbstractContainer.java +++ /dev/null @@ -1,303 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import java.io.Serializable; -import java.util.Collection; -import java.util.Collections; -import java.util.EventObject; -import java.util.LinkedList; - -import com.vaadin.data.Container; - -/** - * Abstract container class that manages event listeners and sending events to - * them ({@link PropertySetChangeNotifier}, {@link ItemSetChangeNotifier}). - * - * Note that this class provides the internal implementations for both types of - * events and notifiers as protected methods, but does not implement the - * {@link PropertySetChangeNotifier} and {@link ItemSetChangeNotifier} - * interfaces directly. This way, subclasses can choose not to implement them. - * Subclasses implementing those interfaces should also override the - * corresponding {@link #addListener()} and {@link #removeListener()} methods to - * make them public. - * - * @since 6.6 - */ -public abstract class AbstractContainer implements Container { - - /** - * List of all Property set change event listeners. - */ - private Collection<Container.PropertySetChangeListener> propertySetChangeListeners = null; - - /** - * List of all container Item set change event listeners. - */ - private Collection<Container.ItemSetChangeListener> itemSetChangeListeners = null; - - /** - * An <code>event</code> object specifying the container whose Property set - * has changed. - * - * This class does not provide information about which properties were - * concerned by the change, but subclasses can provide additional - * information about the changes. - */ - protected static class BasePropertySetChangeEvent extends EventObject - implements Container.PropertySetChangeEvent, Serializable { - - protected BasePropertySetChangeEvent(Container source) { - super(source); - } - - @Override - public Container getContainer() { - return (Container) getSource(); - } - } - - /** - * An <code>event</code> object specifying the container whose Item set has - * changed. - * - * This class does not provide information about the exact changes - * performed, but subclasses can add provide additional information about - * the changes. - */ - protected static class BaseItemSetChangeEvent extends EventObject implements - Container.ItemSetChangeEvent, Serializable { - - protected BaseItemSetChangeEvent(Container source) { - super(source); - } - - @Override - public Container getContainer() { - return (Container) getSource(); - } - } - - // PropertySetChangeNotifier - - /** - * Implementation of the corresponding method in - * {@link PropertySetChangeNotifier}, override with the corresponding public - * method and implement the interface to use this. - * - * @see PropertySetChangeNotifier#addListener(com.vaadin.data.Container.PropertySetChangeListener) - */ - protected void addPropertySetChangeListener( - Container.PropertySetChangeListener listener) { - if (getPropertySetChangeListeners() == null) { - setPropertySetChangeListeners(new LinkedList<Container.PropertySetChangeListener>()); - } - getPropertySetChangeListeners().add(listener); - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #addPropertySetChangeListener(com.vaadin.data.Container.PropertySetChangeListener)} - **/ - @Deprecated - protected void addListener(Container.PropertySetChangeListener listener) { - addPropertySetChangeListener(listener); - } - - /** - * Implementation of the corresponding method in - * {@link PropertySetChangeNotifier}, override with the corresponding public - * method and implement the interface to use this. - * - * @see PropertySetChangeNotifier#removeListener(com.vaadin.data.Container. - * PropertySetChangeListener) - */ - protected void removePropertySetChangeListener( - Container.PropertySetChangeListener listener) { - if (getPropertySetChangeListeners() != null) { - getPropertySetChangeListeners().remove(listener); - } - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #removePropertySetChangeListener(com.vaadin.data.Container.PropertySetChangeListener)} - **/ - @Deprecated - protected void removeListener(Container.PropertySetChangeListener listener) { - removePropertySetChangeListener(listener); - } - - // ItemSetChangeNotifier - - /** - * Implementation of the corresponding method in - * {@link ItemSetChangeNotifier}, override with the corresponding public - * method and implement the interface to use this. - * - * @see ItemSetChangeNotifier#addListener(com.vaadin.data.Container.ItemSetChangeListener) - */ - protected void addItemSetChangeListener( - Container.ItemSetChangeListener listener) { - if (getItemSetChangeListeners() == null) { - setItemSetChangeListeners(new LinkedList<Container.ItemSetChangeListener>()); - } - getItemSetChangeListeners().add(listener); - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #addItemSetChangeListener(com.vaadin.data.Container.ItemSetChangeListener)} - **/ - @Deprecated - protected void addListener(Container.ItemSetChangeListener listener) { - addItemSetChangeListener(listener); - } - - /** - * Implementation of the corresponding method in - * {@link ItemSetChangeNotifier}, override with the corresponding public - * method and implement the interface to use this. - * - * @see ItemSetChangeNotifier#removeListener(com.vaadin.data.Container.ItemSetChangeListener) - */ - protected void removeItemSetChangeListener( - Container.ItemSetChangeListener listener) { - if (getItemSetChangeListeners() != null) { - getItemSetChangeListeners().remove(listener); - } - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #addItemSetChangeListener(com.vaadin.data.Container.ItemSetChangeListener)} - **/ - @Deprecated - protected void removeListener(Container.ItemSetChangeListener listener) { - removeItemSetChangeListener(listener); - } - - /** - * Sends a simple Property set change event to all interested listeners. - */ - protected void fireContainerPropertySetChange() { - fireContainerPropertySetChange(new BasePropertySetChangeEvent(this)); - } - - /** - * Sends a Property set change event to all interested listeners. - * - * Use {@link #fireContainerPropertySetChange()} instead of this method - * unless additional information about the exact changes is available and - * should be included in the event. - * - * @param event - * the property change event to send, optionally with additional - * information - */ - protected void fireContainerPropertySetChange( - Container.PropertySetChangeEvent event) { - if (getPropertySetChangeListeners() != null) { - final Object[] l = getPropertySetChangeListeners().toArray(); - for (int i = 0; i < l.length; i++) { - ((Container.PropertySetChangeListener) l[i]) - .containerPropertySetChange(event); - } - } - } - - /** - * Sends a simple Item set change event to all interested listeners, - * indicating that anything in the contents may have changed (items added, - * removed etc.). - */ - protected void fireItemSetChange() { - fireItemSetChange(new BaseItemSetChangeEvent(this)); - } - - /** - * Sends an Item set change event to all registered interested listeners. - * - * @param event - * the item set change event to send, optionally with additional - * information - */ - protected void fireItemSetChange(ItemSetChangeEvent event) { - if (getItemSetChangeListeners() != null) { - final Object[] l = getItemSetChangeListeners().toArray(); - for (int i = 0; i < l.length; i++) { - ((Container.ItemSetChangeListener) l[i]) - .containerItemSetChange(event); - } - } - } - - /** - * Sets the property set change listener collection. For internal use only. - * - * @param propertySetChangeListeners - */ - protected void setPropertySetChangeListeners( - Collection<Container.PropertySetChangeListener> propertySetChangeListeners) { - this.propertySetChangeListeners = propertySetChangeListeners; - } - - /** - * Returns the property set change listener collection. For internal use - * only. - */ - protected Collection<Container.PropertySetChangeListener> getPropertySetChangeListeners() { - return propertySetChangeListeners; - } - - /** - * Sets the item set change listener collection. For internal use only. - * - * @param itemSetChangeListeners - */ - protected void setItemSetChangeListeners( - Collection<Container.ItemSetChangeListener> itemSetChangeListeners) { - this.itemSetChangeListeners = itemSetChangeListeners; - } - - /** - * Returns the item set change listener collection. For internal use only. - */ - protected Collection<Container.ItemSetChangeListener> getItemSetChangeListeners() { - return itemSetChangeListeners; - } - - public Collection<?> getListeners(Class<?> eventType) { - if (Container.PropertySetChangeEvent.class.isAssignableFrom(eventType)) { - if (propertySetChangeListeners == null) { - return Collections.EMPTY_LIST; - } else { - return Collections - .unmodifiableCollection(propertySetChangeListeners); - } - } else if (Container.ItemSetChangeEvent.class - .isAssignableFrom(eventType)) { - if (itemSetChangeListeners == null) { - return Collections.EMPTY_LIST; - } else { - return Collections - .unmodifiableCollection(itemSetChangeListeners); - } - } - - return Collections.EMPTY_LIST; - } -} diff --git a/server/src/com/vaadin/data/util/AbstractInMemoryContainer.java b/server/src/com/vaadin/data/util/AbstractInMemoryContainer.java deleted file mode 100644 index f7b1a4b0d8..0000000000 --- a/server/src/com/vaadin/data/util/AbstractInMemoryContainer.java +++ /dev/null @@ -1,1165 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import java.io.Serializable; -import java.util.Collection; -import java.util.Collections; -import java.util.EventObject; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; - -import com.vaadin.data.Container; -import com.vaadin.data.Container.ItemSetChangeNotifier; -import com.vaadin.data.Item; -import com.vaadin.data.util.filter.SimpleStringFilter; -import com.vaadin.data.util.filter.UnsupportedFilterException; - -/** - * Abstract {@link Container} class that handles common functionality for - * in-memory containers. Concrete in-memory container classes can either inherit - * this class, inherit {@link AbstractContainer}, or implement the - * {@link Container} interface directly. - * - * Adding and removing items (if desired) must be implemented in subclasses by - * overriding the appropriate add*Item() and remove*Item() and removeAllItems() - * methods, calling the corresponding - * {@link #internalAddItemAfter(Object, Object, Item)}, - * {@link #internalAddItemAt(int, Object, Item)}, - * {@link #internalAddItemAtEnd(Object, Item, boolean)}, - * {@link #internalRemoveItem(Object)} and {@link #internalRemoveAllItems()} - * methods. - * - * By default, adding and removing container properties is not supported, and - * subclasses need to implement {@link #getContainerPropertyIds()}. Optionally, - * subclasses can override {@link #addContainerProperty(Object, Class, Object)} - * and {@link #removeContainerProperty(Object)} to implement them. - * - * Features: - * <ul> - * <li> {@link Container.Ordered} - * <li> {@link Container.Indexed} - * <li> {@link Filterable} and {@link SimpleFilterable} (internal implementation, - * does not implement the interface directly) - * <li> {@link Sortable} (internal implementation, does not implement the - * interface directly) - * </ul> - * - * To implement {@link Sortable}, subclasses need to implement - * {@link #getSortablePropertyIds()} and call the superclass method - * {@link #sortContainer(Object[], boolean[])} in the method - * <code>sort(Object[], boolean[])</code>. - * - * To implement {@link Filterable}, subclasses need to implement the methods - * {@link Filterable#addContainerFilter(com.vaadin.data.Container.Filter)} - * (calling {@link #addFilter(Filter)}), - * {@link Filterable#removeAllContainerFilters()} (calling - * {@link #removeAllFilters()}) and - * {@link Filterable#removeContainerFilter(com.vaadin.data.Container.Filter)} - * (calling {@link #removeFilter(com.vaadin.data.Container.Filter)}). - * - * To implement {@link SimpleFilterable}, subclasses also need to implement the - * methods - * {@link SimpleFilterable#addContainerFilter(Object, String, boolean, boolean)} - * and {@link SimpleFilterable#removeContainerFilters(Object)} calling - * {@link #addFilter(com.vaadin.data.Container.Filter)} and - * {@link #removeFilters(Object)} respectively. - * - * @param <ITEMIDTYPE> - * the class of item identifiers in the container, use Object if can - * be any class - * @param <PROPERTYIDCLASS> - * the class of property identifiers for the items in the container, - * use Object if can be any class - * @param <ITEMCLASS> - * the (base) class of the Item instances in the container, use - * {@link Item} if unknown - * - * @since 6.6 - */ -public abstract class AbstractInMemoryContainer<ITEMIDTYPE, PROPERTYIDCLASS, ITEMCLASS extends Item> - extends AbstractContainer implements ItemSetChangeNotifier, - Container.Indexed { - - /** - * An ordered {@link List} of all item identifiers in the container, - * including those that have been filtered out. - * - * Must not be null. - */ - private List<ITEMIDTYPE> allItemIds; - - /** - * An ordered {@link List} of item identifiers in the container after - * filtering, excluding those that have been filtered out. - * - * This is what the external API of the {@link Container} interface and its - * subinterfaces shows (e.g. {@link #size()}, {@link #nextItemId(Object)}). - * - * If null, the full item id list is used instead. - */ - private List<ITEMIDTYPE> filteredItemIds; - - /** - * Filters that are applied to the container to limit the items visible in - * it - */ - private Set<Filter> filters = new HashSet<Filter>(); - - /** - * The item sorter which is used for sorting the container. - */ - private ItemSorter itemSorter = new DefaultItemSorter(); - - // Constructors - - /** - * Constructor for an abstract in-memory container. - */ - protected AbstractInMemoryContainer() { - setAllItemIds(new ListSet<ITEMIDTYPE>()); - } - - // Container interface methods with more specific return class - - // default implementation, can be overridden - @Override - public ITEMCLASS getItem(Object itemId) { - if (containsId(itemId)) { - return getUnfilteredItem(itemId); - } else { - return null; - } - } - - private static abstract class BaseItemAddOrRemoveEvent extends EventObject - implements Serializable { - protected Object itemId; - protected int index; - protected int count; - - public BaseItemAddOrRemoveEvent(Container source, Object itemId, - int index, int count) { - super(source); - this.itemId = itemId; - this.index = index; - this.count = count; - } - - public Container getContainer() { - return (Container) getSource(); - } - - public Object getFirstItemId() { - return itemId; - } - - public int getFirstIndex() { - return index; - } - - public int getAffectedItemsCount() { - return count; - } - } - - /** - * An <code>Event</code> object specifying information about the added - * items. - * - * <p> - * This class provides information about the first added item and the number - * of added items. - * </p> - * - * @since 7.4 - */ - protected static class BaseItemAddEvent extends BaseItemAddOrRemoveEvent - implements Container.Indexed.ItemAddEvent { - - public BaseItemAddEvent(Container source, Object itemId, int index, - int count) { - super(source, itemId, index, count); - } - - @Override - public int getAddedItemsCount() { - return getAffectedItemsCount(); - } - } - - /** - * An <code>Event</code> object specifying information about the removed - * items. - * - * <p> - * This class provides information about the first removed item and the - * number of removed items. - * </p> - * - * @since 7.4 - */ - protected static class BaseItemRemoveEvent extends BaseItemAddOrRemoveEvent - implements Container.Indexed.ItemRemoveEvent { - - public BaseItemRemoveEvent(Container source, Object itemId, int index, - int count) { - super(source, itemId, index, count); - } - - @Override - public int getRemovedItemsCount() { - return getAffectedItemsCount(); - } - } - - /** - * Get an item even if filtered out. - * - * For internal use only. - * - * @param itemId - * @return - */ - protected abstract ITEMCLASS getUnfilteredItem(Object itemId); - - // cannot override getContainerPropertyIds() and getItemIds(): if subclass - // uses Object as ITEMIDCLASS or PROPERTYIDCLASS, Collection<Object> cannot - // be cast to Collection<MyInterface> - - // public abstract Collection<PROPERTYIDCLASS> getContainerPropertyIds(); - // public abstract Collection<ITEMIDCLASS> getItemIds(); - - // Container interface method implementations - - @Override - public int size() { - return getVisibleItemIds().size(); - } - - @Override - public boolean containsId(Object itemId) { - // only look at visible items after filtering - if (itemId == null) { - return false; - } else { - return getVisibleItemIds().contains(itemId); - } - } - - @Override - public List<?> getItemIds() { - return Collections.unmodifiableList(getVisibleItemIds()); - } - - // Container.Ordered - - @Override - public ITEMIDTYPE nextItemId(Object itemId) { - int index = indexOfId(itemId); - if (index >= 0 && index < size() - 1) { - return getIdByIndex(index + 1); - } else { - // out of bounds - return null; - } - } - - @Override - public ITEMIDTYPE prevItemId(Object itemId) { - int index = indexOfId(itemId); - if (index > 0) { - return getIdByIndex(index - 1); - } else { - // out of bounds - return null; - } - } - - @Override - public ITEMIDTYPE firstItemId() { - if (size() > 0) { - return getIdByIndex(0); - } else { - return null; - } - } - - @Override - public ITEMIDTYPE lastItemId() { - if (size() > 0) { - return getIdByIndex(size() - 1); - } else { - return null; - } - } - - @Override - public boolean isFirstId(Object itemId) { - if (itemId == null) { - return false; - } - return itemId.equals(firstItemId()); - } - - @Override - public boolean isLastId(Object itemId) { - if (itemId == null) { - return false; - } - return itemId.equals(lastItemId()); - } - - // Container.Indexed - - @Override - public ITEMIDTYPE getIdByIndex(int index) { - return getVisibleItemIds().get(index); - } - - @Override - public List<ITEMIDTYPE> getItemIds(int startIndex, int numberOfIds) { - if (startIndex < 0) { - throw new IndexOutOfBoundsException( - "Start index cannot be negative! startIndex=" + startIndex); - } - - if (startIndex > getVisibleItemIds().size()) { - throw new IndexOutOfBoundsException( - "Start index exceeds container size! startIndex=" - + startIndex + " containerLastItemIndex=" - + (getVisibleItemIds().size() - 1)); - } - - if (numberOfIds < 1) { - if (numberOfIds == 0) { - return Collections.emptyList(); - } - - throw new IllegalArgumentException( - "Cannot get negative amount of items! numberOfItems=" - + numberOfIds); - } - - int endIndex = startIndex + numberOfIds; - - if (endIndex > getVisibleItemIds().size()) { - endIndex = getVisibleItemIds().size(); - } - - return Collections.unmodifiableList(getVisibleItemIds().subList( - startIndex, endIndex)); - - } - - @Override - public int indexOfId(Object itemId) { - return getVisibleItemIds().indexOf(itemId); - } - - // methods that are unsupported by default, override to support - - @Override - public Object addItemAt(int index) throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "Adding items not supported. Override the relevant addItem*() methods if required as specified in AbstractInMemoryContainer javadoc."); - } - - @Override - public Item addItemAt(int index, Object newItemId) - throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "Adding items not supported. Override the relevant addItem*() methods if required as specified in AbstractInMemoryContainer javadoc."); - } - - @Override - public Object addItemAfter(Object previousItemId) - throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "Adding items not supported. Override the relevant addItem*() methods if required as specified in AbstractInMemoryContainer javadoc."); - } - - @Override - public Item addItemAfter(Object previousItemId, Object newItemId) - throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "Adding items not supported. Override the relevant addItem*() methods if required as specified in AbstractInMemoryContainer javadoc."); - } - - @Override - public Item addItem(Object itemId) throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "Adding items not supported. Override the relevant addItem*() methods if required as specified in AbstractInMemoryContainer javadoc."); - } - - @Override - public Object addItem() throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "Adding items not supported. Override the relevant addItem*() methods if required as specified in AbstractInMemoryContainer javadoc."); - } - - @Override - public boolean removeItem(Object itemId) - throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "Removing items not supported. Override the removeItem() method if required as specified in AbstractInMemoryContainer javadoc."); - } - - @Override - public boolean removeAllItems() throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "Removing items not supported. Override the removeAllItems() method if required as specified in AbstractInMemoryContainer javadoc."); - } - - @Override - public boolean addContainerProperty(Object propertyId, Class<?> type, - Object defaultValue) throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "Adding container properties not supported. Override the addContainerProperty() method if required."); - } - - @Override - public boolean removeContainerProperty(Object propertyId) - throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "Removing container properties not supported. Override the addContainerProperty() method if required."); - } - - // ItemSetChangeNotifier - /** - * @deprecated As of 7.0, replaced by - * {@link #addItemSetChangeListener(com.vaadin.data.Container.ItemSetChangeListener)} - **/ - @Deprecated - @Override - public void addListener(Container.ItemSetChangeListener listener) { - addItemSetChangeListener(listener); - } - - @Override - public void addItemSetChangeListener( - Container.ItemSetChangeListener listener) { - super.addItemSetChangeListener(listener); - } - - @Override - public void removeItemSetChangeListener( - Container.ItemSetChangeListener listener) { - super.removeItemSetChangeListener(listener); - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #removeItemSetChangeListener(com.vaadin.data.Container.ItemSetChangeListener)} - **/ - @Deprecated - @Override - public void removeListener(Container.ItemSetChangeListener listener) { - removeItemSetChangeListener(listener); - } - - // internal methods - - // Filtering support - - /** - * Filter the view to recreate the visible item list from the unfiltered - * items, and send a notification if the set of visible items changed in any - * way. - */ - protected void filterAll() { - if (doFilterContainer(!getFilters().isEmpty())) { - fireItemSetChange(); - } - } - - /** - * Filters the data in the container and updates internal data structures. - * This method should reset any internal data structures and then repopulate - * them so {@link #getItemIds()} and other methods only return the filtered - * items. - * - * @param hasFilters - * true if filters has been set for the container, false - * otherwise - * @return true if the item set has changed as a result of the filtering - */ - protected boolean doFilterContainer(boolean hasFilters) { - if (!hasFilters) { - boolean changed = getAllItemIds().size() != getVisibleItemIds() - .size(); - setFilteredItemIds(null); - return changed; - } - - // Reset filtered list - List<ITEMIDTYPE> originalFilteredItemIds = getFilteredItemIds(); - boolean wasUnfiltered = false; - if (originalFilteredItemIds == null) { - originalFilteredItemIds = Collections.emptyList(); - wasUnfiltered = true; - } - setFilteredItemIds(new ListSet<ITEMIDTYPE>()); - - // Filter - boolean equal = true; - Iterator<ITEMIDTYPE> origIt = originalFilteredItemIds.iterator(); - for (final Iterator<ITEMIDTYPE> i = getAllItemIds().iterator(); i - .hasNext();) { - final ITEMIDTYPE id = i.next(); - if (passesFilters(id)) { - // filtered list comes from the full list, can use == - equal = equal && origIt.hasNext() && origIt.next() == id; - getFilteredItemIds().add(id); - } - } - - return (wasUnfiltered && !getAllItemIds().isEmpty()) || !equal - || origIt.hasNext(); - } - - /** - * Checks if the given itemId passes the filters set for the container. The - * caller should make sure the itemId exists in the container. For - * non-existing itemIds the behavior is undefined. - * - * @param itemId - * An itemId that exists in the container. - * @return true if the itemId passes all filters or no filters are set, - * false otherwise. - */ - protected boolean passesFilters(Object itemId) { - ITEMCLASS item = getUnfilteredItem(itemId); - if (getFilters().isEmpty()) { - return true; - } - final Iterator<Filter> i = getFilters().iterator(); - while (i.hasNext()) { - final Filter f = i.next(); - if (!f.passesFilter(itemId, item)) { - return false; - } - } - return true; - } - - /** - * Adds a container filter and re-filter the view. - * - * The filter must implement Filter and its sub-filters (if any) must also - * be in-memory filterable. - * - * This can be used to implement - * {@link Filterable#addContainerFilter(com.vaadin.data.Container.Filter)} - * and optionally also - * {@link SimpleFilterable#addContainerFilter(Object, String, boolean, boolean)} - * (with {@link SimpleStringFilter}). - * - * Note that in some cases, incompatible filters cannot be detected when - * added and an {@link UnsupportedFilterException} may occur when performing - * filtering. - * - * @throws UnsupportedFilterException - * if the filter is detected as not supported by the container - */ - protected void addFilter(Filter filter) throws UnsupportedFilterException { - getFilters().add(filter); - filterAll(); - } - - /** - * Returns true if any filters have been applied to the container. - * - * @return true if the container has filters applied, false otherwise - * @since 7.1 - */ - protected boolean hasContainerFilters() { - return !getContainerFilters().isEmpty(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Filterable#getContainerFilters() - */ - protected Collection<Filter> getContainerFilters() { - return Collections.unmodifiableCollection(filters); - } - - /** - * Remove a specific container filter and re-filter the view (if necessary). - * - * This can be used to implement - * {@link Filterable#removeContainerFilter(com.vaadin.data.Container.Filter)} - * . - */ - protected void removeFilter(Filter filter) { - for (Iterator<Filter> iterator = getFilters().iterator(); iterator - .hasNext();) { - Filter f = iterator.next(); - if (f.equals(filter)) { - iterator.remove(); - filterAll(); - return; - } - } - } - - /** - * Remove all container filters for all properties and re-filter the view. - * - * This can be used to implement - * {@link Filterable#removeAllContainerFilters()}. - */ - protected void removeAllFilters() { - if (getFilters().isEmpty()) { - return; - } - getFilters().clear(); - filterAll(); - } - - /** - * Checks if there is a filter that applies to a given property. - * - * @param propertyId - * @return true if there is an active filter for the property - */ - protected boolean isPropertyFiltered(Object propertyId) { - if (getFilters().isEmpty() || propertyId == null) { - return false; - } - final Iterator<Filter> i = getFilters().iterator(); - while (i.hasNext()) { - final Filter f = i.next(); - if (f.appliesToProperty(propertyId)) { - return true; - } - } - return false; - } - - /** - * Remove all container filters for a given property identifier and - * re-filter the view. This also removes filters applying to multiple - * properties including the one identified by propertyId. - * - * This can be used to implement - * {@link Filterable#removeContainerFilters(Object)}. - * - * @param propertyId - * @return Collection<Filter> removed filters - */ - protected Collection<Filter> removeFilters(Object propertyId) { - if (getFilters().isEmpty() || propertyId == null) { - return Collections.emptyList(); - } - List<Filter> removedFilters = new LinkedList<Filter>(); - for (Iterator<Filter> iterator = getFilters().iterator(); iterator - .hasNext();) { - Filter f = iterator.next(); - if (f.appliesToProperty(propertyId)) { - removedFilters.add(f); - iterator.remove(); - } - } - if (!removedFilters.isEmpty()) { - filterAll(); - return removedFilters; - } - return Collections.emptyList(); - } - - // sorting - - /** - * Returns the ItemSorter used for comparing items in a sort. See - * {@link #setItemSorter(ItemSorter)} for more information. - * - * @return The ItemSorter used for comparing two items in a sort. - */ - protected ItemSorter getItemSorter() { - return itemSorter; - } - - /** - * Sets the ItemSorter used for comparing items in a sort. The - * {@link ItemSorter#compare(Object, Object)} method is called with item ids - * to perform the sorting. A default ItemSorter is used if this is not - * explicitly set. - * - * @param itemSorter - * The ItemSorter used for comparing two items in a sort (not - * null). - */ - protected void setItemSorter(ItemSorter itemSorter) { - this.itemSorter = itemSorter; - } - - /** - * Sort base implementation to be used to implement {@link Sortable}. - * - * Subclasses should call this from a public - * {@link #sort(Object[], boolean[])} method when implementing Sortable. - * - * @see com.vaadin.data.Container.Sortable#sort(java.lang.Object[], - * boolean[]) - */ - protected void sortContainer(Object[] propertyId, boolean[] ascending) { - if (!(this instanceof Sortable)) { - throw new UnsupportedOperationException( - "Cannot sort a Container that does not implement Sortable"); - } - - // Set up the item sorter for the sort operation - getItemSorter().setSortProperties((Sortable) this, propertyId, - ascending); - - // Perform the actual sort - doSort(); - - // Post sort updates - if (isFiltered()) { - filterAll(); - } else { - fireItemSetChange(); - } - - } - - /** - * Perform the sorting of the data structures in the container. This is - * invoked when the <code>itemSorter</code> has been prepared for the sort - * operation. Typically this method calls - * <code>Collections.sort(aCollection, getItemSorter())</code> on all arrays - * (containing item ids) that need to be sorted. - * - */ - protected void doSort() { - Collections.sort(getAllItemIds(), getItemSorter()); - } - - /** - * Returns the sortable property identifiers for the container. Can be used - * to implement {@link Sortable#getSortableContainerPropertyIds()}. - */ - protected Collection<?> getSortablePropertyIds() { - LinkedList<Object> sortables = new LinkedList<Object>(); - for (Object propertyId : getContainerPropertyIds()) { - Class<?> propertyType = getType(propertyId); - if (Comparable.class.isAssignableFrom(propertyType) - || propertyType.isPrimitive()) { - sortables.add(propertyId); - } - } - return sortables; - } - - // removing items - - /** - * Removes all items from the internal data structures of this class. This - * can be used to implement {@link #removeAllItems()} in subclasses. - * - * No notification is sent, the caller has to fire a suitable item set - * change notification. - */ - protected void internalRemoveAllItems() { - // Removes all Items - getAllItemIds().clear(); - if (isFiltered()) { - getFilteredItemIds().clear(); - } - } - - /** - * Removes a single item from the internal data structures of this class. - * This can be used to implement {@link #removeItem(Object)} in subclasses. - * - * No notification is sent, the caller has to fire a suitable item set - * change notification. - * - * @param itemId - * the identifier of the item to remove - * @return true if an item was successfully removed, false if failed to - * remove or no such item - */ - protected boolean internalRemoveItem(Object itemId) { - if (itemId == null) { - return false; - } - - boolean result = getAllItemIds().remove(itemId); - if (result && isFiltered()) { - getFilteredItemIds().remove(itemId); - } - - return result; - } - - // adding items - - /** - * Adds the bean to all internal data structures at the given position. - * Fails if an item with itemId is already in the container. Returns a the - * item if it was added successfully, null otherwise. - * - * <p> - * Caller should initiate filtering after calling this method. - * </p> - * - * For internal use only - subclasses should use - * {@link #internalAddItemAtEnd(Object, Item, boolean)}, - * {@link #internalAddItemAt(int, Object, Item, boolean)} and - * {@link #internalAddItemAfter(Object, Object, Item, boolean)} instead. - * - * @param position - * The position at which the item should be inserted in the - * unfiltered collection of items - * @param itemId - * The item identifier for the item to insert - * @param item - * The item to insert - * - * @return ITEMCLASS if the item was added successfully, null otherwise - */ - private ITEMCLASS internalAddAt(int position, ITEMIDTYPE itemId, - ITEMCLASS item) { - if (position < 0 || position > getAllItemIds().size() || itemId == null - || item == null) { - return null; - } - // Make sure that the item has not been added previously - if (getAllItemIds().contains(itemId)) { - return null; - } - - // "filteredList" will be updated in filterAll() which should be invoked - // by the caller after calling this method. - getAllItemIds().add(position, itemId); - registerNewItem(position, itemId, item); - - return item; - } - - /** - * Add an item at the end of the container, and perform filtering if - * necessary. An event is fired if the filtered view changes. - * - * @param newItemId - * @param item - * new item to add - * @param filter - * true to perform filtering and send event after adding the - * item, false to skip these operations for batch inserts - if - * false, caller needs to make sure these operations are - * performed at the end of the batch - * @return item added or null if no item was added - */ - protected ITEMCLASS internalAddItemAtEnd(ITEMIDTYPE newItemId, - ITEMCLASS item, boolean filter) { - ITEMCLASS newItem = internalAddAt(getAllItemIds().size(), newItemId, - item); - if (newItem != null && filter) { - // TODO filter only this item, use fireItemAdded() - filterAll(); - if (!isFiltered()) { - // TODO hack: does not detect change in filterAll() in this case - fireItemAdded(indexOfId(newItemId), newItemId, item); - } - } - return newItem; - } - - /** - * Add an item after a given (visible) item, and perform filtering. An event - * is fired if the filtered view changes. - * - * The new item is added at the beginning if previousItemId is null. - * - * @param previousItemId - * item id of a visible item after which to add the new item, or - * null to add at the beginning - * @param newItemId - * @param item - * new item to add - * @param filter - * true to perform filtering and send event after adding the - * item, false to skip these operations for batch inserts - if - * false, caller needs to make sure these operations are - * performed at the end of the batch - * @return item added or null if no item was added - */ - protected ITEMCLASS internalAddItemAfter(ITEMIDTYPE previousItemId, - ITEMIDTYPE newItemId, ITEMCLASS item, boolean filter) { - // only add if the previous item is visible - ITEMCLASS newItem = null; - if (previousItemId == null) { - newItem = internalAddAt(0, newItemId, item); - } else if (containsId(previousItemId)) { - newItem = internalAddAt( - getAllItemIds().indexOf(previousItemId) + 1, newItemId, - item); - } - if (newItem != null && filter) { - // TODO filter only this item, use fireItemAdded() - filterAll(); - if (!isFiltered()) { - // TODO hack: does not detect change in filterAll() in this case - fireItemAdded(indexOfId(newItemId), newItemId, item); - } - } - return newItem; - } - - /** - * Add an item at a given (visible after filtering) item index, and perform - * filtering. An event is fired if the filtered view changes. - * - * @param index - * position where to add the item (visible/view index) - * @param newItemId - * @param item - * new item to add - * @param filter - * true to perform filtering and send event after adding the - * item, false to skip these operations for batch inserts - if - * false, caller needs to make sure these operations are - * performed at the end of the batch - * @return item added or null if no item was added - */ - protected ITEMCLASS internalAddItemAt(int index, ITEMIDTYPE newItemId, - ITEMCLASS item, boolean filter) { - if (index < 0 || index > size()) { - return null; - } else if (index == 0) { - // add before any item, visible or not - return internalAddItemAfter(null, newItemId, item, filter); - } else { - // if index==size(), adds immediately after last visible item - return internalAddItemAfter(getIdByIndex(index - 1), newItemId, - item, filter); - } - } - - /** - * Registers a new item as having been added to the container. This can - * involve storing the item or any relevant information about it in internal - * container-specific collections if necessary, as well as registering - * listeners etc. - * - * The full identifier list in {@link AbstractInMemoryContainer} has already - * been updated to reflect the new item when this method is called. - * - * @param position - * @param itemId - * @param item - */ - protected void registerNewItem(int position, ITEMIDTYPE itemId, - ITEMCLASS item) { - } - - // item set change notifications - - /** - * Notify item set change listeners that an item has been added to the - * container. - * - * @since 7.4 - * - * @param position - * position of the added item in the view - * @param itemId - * id of the added item - * @param item - * the added item - */ - protected void fireItemAdded(int position, ITEMIDTYPE itemId, ITEMCLASS item) { - fireItemsAdded(position, itemId, 1); - } - - /** - * Notify item set change listeners that items has been added to the - * container. - * - * @param firstPosition - * position of the first visible added item in the view - * @param firstItemId - * id of the first visible added item - * @param numberOfItems - * the number of visible added items - */ - protected void fireItemsAdded(int firstPosition, ITEMIDTYPE firstItemId, - int numberOfItems) { - BaseItemAddEvent addEvent = new BaseItemAddEvent(this, firstItemId, - firstPosition, numberOfItems); - fireItemSetChange(addEvent); - } - - /** - * Notify item set change listeners that an item has been removed from the - * container. - * - * @since 7.4 - * - * @param position - * position of the removed item in the view prior to removal (if - * was visible) - * @param itemId - * id of the removed item, of type {@link Object} to satisfy - * {@link Container#removeItem(Object)} API - */ - protected void fireItemRemoved(int position, Object itemId) { - fireItemsRemoved(position, itemId, 1); - } - - /** - * Notify item set change listeners that items has been removed from the - * container. - * - * @param firstPosition - * position of the first visible removed item in the view prior - * to removal - * @param firstItemId - * id of the first visible removed item, of type {@link Object} - * to satisfy {@link Container#removeItem(Object)} API - * @param numberOfItems - * the number of removed visible items - * - */ - protected void fireItemsRemoved(int firstPosition, Object firstItemId, - int numberOfItems) { - BaseItemRemoveEvent removeEvent = new BaseItemRemoveEvent(this, - firstItemId, firstPosition, numberOfItems); - fireItemSetChange(removeEvent); - } - - // visible and filtered item identifier lists - - /** - * Returns the internal list of visible item identifiers after filtering. - * - * For internal use only. - */ - protected List<ITEMIDTYPE> getVisibleItemIds() { - if (isFiltered()) { - return getFilteredItemIds(); - } else { - return getAllItemIds(); - } - } - - /** - * Returns the item id of the first visible item after filtering. 'Null' is - * returned if there is no visible items. - * <p> - * For internal use only. - * - * @since 7.4 - * - * @return item id of the first visible item - */ - protected ITEMIDTYPE getFirstVisibleItem() { - if (!getVisibleItemIds().isEmpty()) { - return getVisibleItemIds().get(0); - } - return null; - } - - /** - * Returns true is the container has active filters. - * - * @return true if the container is currently filtered - */ - protected boolean isFiltered() { - return filteredItemIds != null; - } - - /** - * Internal helper method to set the internal list of filtered item - * identifiers. Should not be used outside this class except for - * implementing clone(), may disappear from future versions. - * - * @param filteredItemIds - */ - @Deprecated - protected void setFilteredItemIds(List<ITEMIDTYPE> filteredItemIds) { - this.filteredItemIds = filteredItemIds; - } - - /** - * Internal helper method to get the internal list of filtered item - * identifiers. Should not be used outside this class except for - * implementing clone(), may disappear from future versions - use - * {@link #getVisibleItemIds()} in other contexts. - * - * @return List<ITEMIDTYPE> - */ - protected List<ITEMIDTYPE> getFilteredItemIds() { - return filteredItemIds; - } - - /** - * Internal helper method to set the internal list of all item identifiers. - * Should not be used outside this class except for implementing clone(), - * may disappear from future versions. - * - * @param allItemIds - */ - @Deprecated - protected void setAllItemIds(List<ITEMIDTYPE> allItemIds) { - this.allItemIds = allItemIds; - } - - /** - * Internal helper method to get the internal list of all item identifiers. - * Avoid using this method outside this class, may disappear in future - * versions. - * - * @return List<ITEMIDTYPE> - */ - protected List<ITEMIDTYPE> getAllItemIds() { - return allItemIds; - } - - /** - * Set the internal collection of filters without performing filtering. - * - * This method is mostly for internal use, use - * {@link #addFilter(com.vaadin.data.Container.Filter)} and - * <code>remove*Filter*</code> (which also re-filter the container) instead - * when possible. - * - * @param filters - */ - protected void setFilters(Set<Filter> filters) { - this.filters = filters; - } - - /** - * Returns the internal collection of filters. The returned collection - * should not be modified by callers outside this class. - * - * @return Set<Filter> - */ - protected Set<Filter> getFilters() { - return filters; - } - -} diff --git a/server/src/com/vaadin/data/util/AbstractProperty.java b/server/src/com/vaadin/data/util/AbstractProperty.java deleted file mode 100644 index ee79fcd91e..0000000000 --- a/server/src/com/vaadin/data/util/AbstractProperty.java +++ /dev/null @@ -1,299 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedList; -import java.util.logging.Logger; - -import com.vaadin.data.Property; - -/** - * Abstract base class for {@link Property} implementations. - * - * Handles listener management for {@link ValueChangeListener}s and - * {@link ReadOnlyStatusChangeListener}s. - * - * @since 6.6 - */ -public abstract class AbstractProperty<T> implements Property<T>, - Property.ValueChangeNotifier, Property.ReadOnlyStatusChangeNotifier { - - /** - * List of listeners who are interested in the read-only status changes of - * the Property - */ - private LinkedList<ReadOnlyStatusChangeListener> readOnlyStatusChangeListeners = null; - - /** - * List of listeners who are interested in the value changes of the Property - */ - private LinkedList<ValueChangeListener> valueChangeListeners = null; - - /** - * Is the Property read-only? - */ - private boolean readOnly; - - /** - * {@inheritDoc} - * - * Override for additional restrictions on what is considered a read-only - * property. - */ - @Override - public boolean isReadOnly() { - return readOnly; - } - - @Override - public void setReadOnly(boolean newStatus) { - boolean oldStatus = isReadOnly(); - readOnly = newStatus; - if (oldStatus != isReadOnly()) { - fireReadOnlyStatusChange(); - } - } - - /** - * Returns a string representation of this object. The returned string - * representation depends on if the legacy Property toString mode is enabled - * or disabled. - * <p> - * If legacy Property toString mode is enabled, returns the value of the - * <code>Property</code> converted to a String. - * </p> - * <p> - * If legacy Property toString mode is disabled, the string representation - * has no special meaning - * </p> - * - * @see LegacyPropertyHelper#isLegacyToStringEnabled() - * - * @return A string representation of the value value stored in the Property - * or a string representation of the Property object. - * @deprecated As of 7.0. To get the property value, use {@link #getValue()} - * instead (and possibly toString on that) - */ - @Deprecated - @Override - public String toString() { - if (!LegacyPropertyHelper.isLegacyToStringEnabled()) { - return super.toString(); - } else { - return LegacyPropertyHelper.legacyPropertyToString(this); - } - } - - /* Events */ - - /** - * An <code>Event</code> object specifying the Property whose read-only - * status has been changed. - */ - protected static class ReadOnlyStatusChangeEvent extends - java.util.EventObject implements Property.ReadOnlyStatusChangeEvent { - - /** - * Constructs a new read-only status change event for this object. - * - * @param source - * source object of the event. - */ - protected ReadOnlyStatusChangeEvent(Property source) { - super(source); - } - - /** - * Gets the Property whose read-only state has changed. - * - * @return source Property of the event. - */ - @Override - public Property getProperty() { - return (Property) getSource(); - } - - } - - /** - * Registers a new read-only status change listener for this Property. - * - * @param listener - * the new Listener to be registered. - */ - @Override - public void addReadOnlyStatusChangeListener( - Property.ReadOnlyStatusChangeListener listener) { - if (readOnlyStatusChangeListeners == null) { - readOnlyStatusChangeListeners = new LinkedList<ReadOnlyStatusChangeListener>(); - } - readOnlyStatusChangeListeners.add(listener); - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #addReadOnlyStatusChangeListener(com.vaadin.data.Property.ReadOnlyStatusChangeListener)} - **/ - @Override - @Deprecated - public void addListener(Property.ReadOnlyStatusChangeListener listener) { - addReadOnlyStatusChangeListener(listener); - } - - /** - * Removes a previously registered read-only status change listener. - * - * @param listener - * the listener to be removed. - */ - @Override - public void removeReadOnlyStatusChangeListener( - Property.ReadOnlyStatusChangeListener listener) { - if (readOnlyStatusChangeListeners != null) { - readOnlyStatusChangeListeners.remove(listener); - } - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #removeReadOnlyStatusChangeListener(com.vaadin.data.Property.ReadOnlyStatusChangeListener)} - **/ - @Override - @Deprecated - public void removeListener(Property.ReadOnlyStatusChangeListener listener) { - removeReadOnlyStatusChangeListener(listener); - } - - /** - * Sends a read only status change event to all registered listeners. - */ - protected void fireReadOnlyStatusChange() { - if (readOnlyStatusChangeListeners != null) { - final Object[] l = readOnlyStatusChangeListeners.toArray(); - final Property.ReadOnlyStatusChangeEvent event = new ReadOnlyStatusChangeEvent( - this); - for (int i = 0; i < l.length; i++) { - ((Property.ReadOnlyStatusChangeListener) l[i]) - .readOnlyStatusChange(event); - } - } - } - - /** - * An <code>Event</code> object specifying the Property whose value has been - * changed. - */ - private static class ValueChangeEvent extends java.util.EventObject - implements Property.ValueChangeEvent { - - /** - * Constructs a new value change event for this object. - * - * @param source - * source object of the event. - */ - protected ValueChangeEvent(Property source) { - super(source); - } - - /** - * Gets the Property whose value has changed. - * - * @return source Property of the event. - */ - @Override - public Property getProperty() { - return (Property) getSource(); - } - - } - - @Override - public void addValueChangeListener(ValueChangeListener listener) { - if (valueChangeListeners == null) { - valueChangeListeners = new LinkedList<ValueChangeListener>(); - } - valueChangeListeners.add(listener); - - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #addValueChangeListener(com.vaadin.data.Property.ValueChangeListener)} - **/ - @Override - @Deprecated - public void addListener(ValueChangeListener listener) { - addValueChangeListener(listener); - } - - @Override - public void removeValueChangeListener(ValueChangeListener listener) { - if (valueChangeListeners != null) { - valueChangeListeners.remove(listener); - } - - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #removeValueChangeListener(com.vaadin.data.Property.ValueChangeListener)} - **/ - @Override - @Deprecated - public void removeListener(ValueChangeListener listener) { - removeValueChangeListener(listener); - } - - /** - * Sends a value change event to all registered listeners. - */ - protected void fireValueChange() { - if (valueChangeListeners != null) { - final Object[] l = valueChangeListeners.toArray(); - final Property.ValueChangeEvent event = new ValueChangeEvent(this); - for (int i = 0; i < l.length; i++) { - ((Property.ValueChangeListener) l[i]).valueChange(event); - } - } - } - - public Collection<?> getListeners(Class<?> eventType) { - if (Property.ValueChangeEvent.class.isAssignableFrom(eventType)) { - if (valueChangeListeners == null) { - return Collections.EMPTY_LIST; - } else { - return Collections.unmodifiableCollection(valueChangeListeners); - } - } else if (Property.ReadOnlyStatusChangeEvent.class - .isAssignableFrom(eventType)) { - if (readOnlyStatusChangeListeners == null) { - return Collections.EMPTY_LIST; - } else { - return Collections - .unmodifiableCollection(readOnlyStatusChangeListeners); - } - } - - return Collections.EMPTY_LIST; - } - - private static Logger getLogger() { - return Logger.getLogger(AbstractProperty.class.getName()); - } -} diff --git a/server/src/com/vaadin/data/util/BeanContainer.java b/server/src/com/vaadin/data/util/BeanContainer.java deleted file mode 100644 index 9b878d627e..0000000000 --- a/server/src/com/vaadin/data/util/BeanContainer.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import java.util.Collection; - -/** - * An in-memory container for JavaBeans. - * - * <p> - * The properties of the container are determined automatically by introspecting - * the used JavaBean class. Only beans of the same type can be added to the - * container. - * </p> - * - * <p> - * In BeanContainer (unlike {@link BeanItemContainer}), the item IDs do not have - * to be the beans themselves. The container can be used either with explicit - * item IDs or the item IDs can be generated when adding beans. - * </p> - * - * <p> - * To use explicit item IDs, use the methods {@link #addItem(Object, Object)}, - * {@link #addItemAfter(Object, Object, Object)} and - * {@link #addItemAt(int, Object, Object)}. - * </p> - * - * <p> - * If a bean id resolver is set using - * {@link #setBeanIdResolver(com.vaadin.data.util.AbstractBeanContainer.BeanIdResolver)} - * or {@link #setBeanIdProperty(Object)}, the methods {@link #addBean(Object)}, - * {@link #addBeanAfter(Object, Object)}, {@link #addBeanAt(int, Object)} and - * {@link #addAll(java.util.Collection)} can be used to add items to the - * container. If one of these methods is called, the resolver is used to - * generate an identifier for the item (must not return null). - * </p> - * - * <p> - * Note that explicit item identifiers can also be used when a resolver has been - * set by calling the addItem*() methods - the resolver is only used when adding - * beans using the addBean*() or {@link #addAll(Collection)} methods. - * </p> - * - * <p> - * It is not possible to add additional properties to the container. - * </p> - * - * @param <IDTYPE> - * The type of the item identifier - * @param <BEANTYPE> - * The type of the Bean - * - * @see AbstractBeanContainer - * @see BeanItemContainer - * - * @since 6.5 - */ -public class BeanContainer<IDTYPE, BEANTYPE> extends - AbstractBeanContainer<IDTYPE, BEANTYPE> { - - public BeanContainer(Class<? super BEANTYPE> type) { - super(type); - } - - /** - * Adds the bean to the Container. - * - * @see com.vaadin.data.Container#addItem(Object) - */ - @Override - public BeanItem<BEANTYPE> addItem(IDTYPE itemId, BEANTYPE bean) { - if (itemId != null && bean != null) { - return super.addItem(itemId, bean); - } else { - return null; - } - } - - /** - * Adds the bean after the given item id. - * - * @see com.vaadin.data.Container.Ordered#addItemAfter(Object, Object) - */ - @Override - public BeanItem<BEANTYPE> addItemAfter(IDTYPE previousItemId, - IDTYPE newItemId, BEANTYPE bean) { - if (newItemId != null && bean != null) { - return super.addItemAfter(previousItemId, newItemId, bean); - } else { - return null; - } - } - - /** - * Adds a new bean at the given index. - * - * The bean is used both as the item contents and as the item identifier. - * - * @param index - * Index at which the bean should be added. - * @param newItemId - * The item id for the bean to add to the container. - * @param bean - * The bean to add to the container. - * - * @return Returns the new BeanItem or null if the operation fails. - */ - @Override - public BeanItem<BEANTYPE> addItemAt(int index, IDTYPE newItemId, - BEANTYPE bean) { - if (newItemId != null && bean != null) { - return super.addItemAt(index, newItemId, bean); - } else { - return null; - } - } - - // automatic item id resolution - - /** - * Sets the bean id resolver to use a property of the beans as the - * identifier. - * - * @param propertyId - * the identifier of the property to use to find item identifiers - */ - public void setBeanIdProperty(Object propertyId) { - setBeanIdResolver(createBeanPropertyResolver(propertyId)); - } - - @Override - // overridden to make public - public void setBeanIdResolver( - BeanIdResolver<IDTYPE, BEANTYPE> beanIdResolver) { - super.setBeanIdResolver(beanIdResolver); - } - - @Override - // overridden to make public - public BeanItem<BEANTYPE> addBean(BEANTYPE bean) - throws IllegalStateException, IllegalArgumentException { - return super.addBean(bean); - } - - @Override - // overridden to make public - public BeanItem<BEANTYPE> addBeanAfter(IDTYPE previousItemId, BEANTYPE bean) - throws IllegalStateException, IllegalArgumentException { - return super.addBeanAfter(previousItemId, bean); - } - - @Override - // overridden to make public - public BeanItem<BEANTYPE> addBeanAt(int index, BEANTYPE bean) - throws IllegalStateException, IllegalArgumentException { - return super.addBeanAt(index, bean); - } - - @Override - // overridden to make public - public void addAll(Collection<? extends BEANTYPE> collection) - throws IllegalStateException { - super.addAll(collection); - } - -} diff --git a/server/src/com/vaadin/data/util/BeanItem.java b/server/src/com/vaadin/data/util/BeanItem.java deleted file mode 100644 index 71f51c3feb..0000000000 --- a/server/src/com/vaadin/data/util/BeanItem.java +++ /dev/null @@ -1,264 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util; - -import java.beans.PropertyDescriptor; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * A wrapper class for adding the Item interface to any Java Bean. - * - * @author Vaadin Ltd. - * @since 3.0 - */ -@SuppressWarnings("serial") -public class BeanItem<BT> extends PropertysetItem { - - /** - * The bean which this Item is based on. - */ - private final BT bean; - - /** - * <p> - * Creates a new instance of <code>BeanItem</code> and adds all properties - * of a Java Bean to it. The properties are identified by their respective - * bean names. - * </p> - * - * <p> - * Note : This version only supports introspectable bean properties and - * their getter and setter methods. Stand-alone <code>is</code> and - * <code>are</code> methods are not supported. - * </p> - * - * @param bean - * the Java Bean to copy properties from. - * - */ - public BeanItem(BT bean) { - this(bean, (Class<BT>) bean.getClass()); - } - - /** - * <p> - * Creates a new instance of <code>BeanItem</code> and adds all properties - * of a Java Bean to it. The properties are identified by their respective - * bean names. - * </p> - * - * <p> - * Note : This version only supports introspectable bean properties and - * their getter and setter methods. Stand-alone <code>is</code> and - * <code>are</code> methods are not supported. - * </p> - * - * @since 7.4 - * - * @param bean - * the Java Bean to copy properties from. - * @param beanClass - * class of the {@code bean} - * - */ - public BeanItem(BT bean, Class<BT> beanClass) { - this(bean, getPropertyDescriptors(beanClass)); - } - - /** - * <p> - * Creates a new instance of <code>BeanItem</code> using a pre-computed set - * of properties. The properties are identified by their respective bean - * names. - * </p> - * - * @param bean - * the Java Bean to copy properties from. - * @param propertyDescriptors - * pre-computed property descriptors - */ - BeanItem(BT bean, - Map<String, VaadinPropertyDescriptor<BT>> propertyDescriptors) { - - this.bean = bean; - - for (VaadinPropertyDescriptor<BT> pd : propertyDescriptors.values()) { - addItemProperty(pd.getName(), pd.createProperty(bean)); - } - } - - /** - * <p> - * Creates a new instance of <code>BeanItem</code> and adds all listed - * properties of a Java Bean to it - in specified order. The properties are - * identified by their respective bean names. - * </p> - * - * <p> - * Note : This version only supports introspectable bean properties and - * their getter and setter methods. Stand-alone <code>is</code> and - * <code>are</code> methods are not supported. - * </p> - * - * @param bean - * the Java Bean to copy properties from. - * @param propertyIds - * id of the property. - */ - public BeanItem(BT bean, Collection<?> propertyIds) { - - this.bean = bean; - - // Create bean information - LinkedHashMap<String, VaadinPropertyDescriptor<BT>> pds = getPropertyDescriptors((Class<BT>) bean - .getClass()); - - // Add all the bean properties as MethodProperties to this Item - for (Object id : propertyIds) { - VaadinPropertyDescriptor<BT> pd = pds.get(id); - if (pd != null) { - addItemProperty(pd.getName(), pd.createProperty(bean)); - } - } - - } - - /** - * <p> - * Creates a new instance of <code>BeanItem</code> and adds all listed - * properties of a Java Bean to it - in specified order. The properties are - * identified by their respective bean names. - * </p> - * - * <p> - * Note : This version only supports introspectable bean properties and - * their getter and setter methods. Stand-alone <code>is</code> and - * <code>are</code> methods are not supported. - * </p> - * - * @param bean - * the Java Bean to copy properties from. - * @param propertyIds - * ids of the properties. - */ - public BeanItem(BT bean, String... propertyIds) { - this(bean, Arrays.asList(propertyIds)); - } - - /** - * <p> - * Perform introspection on a Java Bean class to find its properties. - * </p> - * - * <p> - * Note : This version only supports introspectable bean properties and - * their getter and setter methods. Stand-alone <code>is</code> and - * <code>are</code> methods are not supported. - * </p> - * - * @param beanClass - * the Java Bean class to get properties for. - * @return an ordered map from property names to property descriptors - */ - static <BT> LinkedHashMap<String, VaadinPropertyDescriptor<BT>> getPropertyDescriptors( - final Class<BT> beanClass) { - final LinkedHashMap<String, VaadinPropertyDescriptor<BT>> pdMap = new LinkedHashMap<String, VaadinPropertyDescriptor<BT>>(); - - // Try to introspect, if it fails, we just have an empty Item - try { - List<PropertyDescriptor> propertyDescriptors = BeanUtil - .getBeanPropertyDescriptor(beanClass); - - // Add all the bean properties as MethodProperties to this Item - // later entries on the list overwrite earlier ones - for (PropertyDescriptor pd : propertyDescriptors) { - final Method getMethod = pd.getReadMethod(); - if ((getMethod != null) - && getMethod.getDeclaringClass() != Object.class) { - VaadinPropertyDescriptor<BT> vaadinPropertyDescriptor = new MethodPropertyDescriptor<BT>( - pd.getName(), pd.getPropertyType(), - pd.getReadMethod(), pd.getWriteMethod()); - pdMap.put(pd.getName(), vaadinPropertyDescriptor); - } - } - } catch (final java.beans.IntrospectionException ignored) { - } - - return pdMap; - } - - /** - * Expands nested bean properties by replacing a top-level property with - * some or all of its sub-properties. The expansion is not recursive. - * - * @param propertyId - * property id for the property whose sub-properties are to be - * expanded, - * @param subPropertyIds - * sub-properties to expand, all sub-properties are expanded if - * not specified - */ - public void expandProperty(String propertyId, String... subPropertyIds) { - Set<String> subPropertySet = new HashSet<String>( - Arrays.asList(subPropertyIds)); - - if (0 == subPropertyIds.length) { - // Enumerate all sub-properties - Class<?> propertyType = getItemProperty(propertyId).getType(); - Map<String, ?> pds = getPropertyDescriptors(propertyType); - subPropertySet.addAll(pds.keySet()); - } - - for (String subproperty : subPropertySet) { - String qualifiedPropertyId = propertyId + "." + subproperty; - addNestedProperty(qualifiedPropertyId); - } - - removeItemProperty(propertyId); - } - - /** - * Adds a nested property to the item. The property must not exist in the - * item already and must of form "field1.field2" where field2 is a field in - * the object referenced to by field1. If an intermediate property returns - * null, the property will return a null value - * - * @param nestedPropertyId - * property id to add. - */ - public void addNestedProperty(String nestedPropertyId) { - addItemProperty(nestedPropertyId, new NestedMethodProperty<Object>( - getBean(), nestedPropertyId)); - } - - /** - * Gets the underlying JavaBean object. - * - * @return the bean object. - */ - public BT getBean() { - return bean; - } - -} diff --git a/server/src/com/vaadin/data/util/BeanItemContainer.java b/server/src/com/vaadin/data/util/BeanItemContainer.java deleted file mode 100644 index d5d399f0f8..0000000000 --- a/server/src/com/vaadin/data/util/BeanItemContainer.java +++ /dev/null @@ -1,253 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import java.util.Collection; - -/** - * An in-memory container for JavaBeans. - * - * <p> - * The properties of the container are determined automatically by introspecting - * the used JavaBean class. Only beans of the same type can be added to the - * container. - * </p> - * - * <p> - * BeanItemContainer uses the beans themselves as identifiers. The - * {@link Object#hashCode()} of a bean is used when storing and looking up beans - * so it must not change during the lifetime of the bean (it should not depend - * on any part of the bean that can be modified). Typically this restricts the - * implementation of {@link Object#equals(Object)} as well in order for it to - * fulfill the contract between {@code equals()} and {@code hashCode()}. - * </p> - * - * <p> - * To add items to the container, use the methods {@link #addBean(Object)}, - * {@link #addBeanAfter(Object, Object)} and {@link #addBeanAt(int, Object)}. - * Also {@link #addItem(Object)}, {@link #addItemAfter(Object, Object)} and - * {@link #addItemAt(int, Object)} can be used as synonyms for them. - * </p> - * - * <p> - * It is not possible to add additional properties to the container. - * </p> - * - * @param <BEANTYPE> - * The type of the Bean - * - * @since 5.4 - */ -@SuppressWarnings("serial") -public class BeanItemContainer<BEANTYPE> extends - AbstractBeanContainer<BEANTYPE, BEANTYPE> { - - /** - * Bean identity resolver that returns the bean itself as its item - * identifier. - * - * This corresponds to the old behavior of {@link BeanItemContainer}, and - * requires suitable (identity-based) equals() and hashCode() methods on the - * beans. - * - * @param <BT> - * - * @since 6.5 - */ - private static class IdentityBeanIdResolver<BT> implements - BeanIdResolver<BT, BT> { - - @Override - public BT getIdForBean(BT bean) { - return bean; - } - - } - - /** - * Constructs a {@code BeanItemContainer} for beans of the given type. - * - * @param type - * the type of the beans that will be added to the container. - * @throws IllegalArgumentException - * If {@code type} is null - */ - public BeanItemContainer(Class<? super BEANTYPE> type) - throws IllegalArgumentException { - super(type); - super.setBeanIdResolver(new IdentityBeanIdResolver<BEANTYPE>()); - } - - /** - * Constructs a {@code BeanItemContainer} and adds the given beans to it. - * The collection must not be empty. - * {@link BeanItemContainer#BeanItemContainer(Class)} can be used for - * creating an initially empty {@code BeanItemContainer}. - * - * Note that when using this constructor, the actual class of the first item - * in the collection is used to determine the bean properties supported by - * the container instance, and only beans of that class or its subclasses - * can be added to the collection. If this is problematic or empty - * collections need to be supported, use {@link #BeanItemContainer(Class)} - * and {@link #addAll(Collection)} instead. - * - * @param collection - * a non empty {@link Collection} of beans. - * @throws IllegalArgumentException - * If the collection is null or empty. - * - * @deprecated As of 6.5, use {@link #BeanItemContainer(Class, Collection)} - * instead - */ - @SuppressWarnings("unchecked") - @Deprecated - public BeanItemContainer(Collection<? extends BEANTYPE> collection) - throws IllegalArgumentException { - // must assume the class is BT - // the class information is erased by the compiler - this((Class<BEANTYPE>) getBeanClassForCollection(collection), - collection); - } - - /** - * Internal helper method to support the deprecated {@link Collection} - * container. - * - * @param <BT> - * @param collection - * @return - * @throws IllegalArgumentException - */ - @SuppressWarnings("unchecked") - @Deprecated - private static <BT> Class<? extends BT> getBeanClassForCollection( - Collection<? extends BT> collection) - throws IllegalArgumentException { - if (collection == null || collection.isEmpty()) { - throw new IllegalArgumentException( - "The collection passed to BeanItemContainer constructor must not be null or empty. Use the other BeanItemContainer constructor."); - } - return (Class<? extends BT>) collection.iterator().next().getClass(); - } - - /** - * Constructs a {@code BeanItemContainer} and adds the given beans to it. - * - * @param type - * the type of the beans that will be added to the container. - * @param collection - * a {@link Collection} of beans (can be empty or null). - * @throws IllegalArgumentException - * If {@code type} is null - */ - public BeanItemContainer(Class<? super BEANTYPE> type, - Collection<? extends BEANTYPE> collection) - throws IllegalArgumentException { - super(type); - super.setBeanIdResolver(new IdentityBeanIdResolver<BEANTYPE>()); - - if (collection != null) { - addAll(collection); - } - } - - /** - * Adds all the beans from a {@link Collection} in one go. More efficient - * than adding them one by one. - * - * @param collection - * The collection of beans to add. Must not be null. - */ - @Override - public void addAll(Collection<? extends BEANTYPE> collection) { - super.addAll(collection); - } - - /** - * Adds the bean after the given bean. - * - * The bean is used both as the item contents and as the item identifier. - * - * @param previousItemId - * the bean (of type BT) after which to add newItemId - * @param newItemId - * the bean (of type BT) to add (not null) - * - * @see com.vaadin.data.Container.Ordered#addItemAfter(Object, Object) - */ - @Override - @SuppressWarnings("unchecked") - public BeanItem<BEANTYPE> addItemAfter(Object previousItemId, - Object newItemId) throws IllegalArgumentException { - return super.addBeanAfter((BEANTYPE) previousItemId, - (BEANTYPE) newItemId); - } - - /** - * Adds a new bean at the given index. - * - * The bean is used both as the item contents and as the item identifier. - * - * @param index - * Index at which the bean should be added. - * @param newItemId - * The bean to add to the container. - * @return Returns the new BeanItem or null if the operation fails. - */ - @Override - @SuppressWarnings("unchecked") - public BeanItem<BEANTYPE> addItemAt(int index, Object newItemId) - throws IllegalArgumentException { - return super.addBeanAt(index, (BEANTYPE) newItemId); - } - - /** - * Adds the bean to the Container. - * - * The bean is used both as the item contents and as the item identifier. - * - * @see com.vaadin.data.Container#addItem(Object) - */ - @Override - @SuppressWarnings("unchecked") - public BeanItem<BEANTYPE> addItem(Object itemId) { - return super.addBean((BEANTYPE) itemId); - } - - /** - * Adds the bean to the Container. - * - * The bean is used both as the item contents and as the item identifier. - * - * @see com.vaadin.data.Container#addItem(Object) - */ - @Override - public BeanItem<BEANTYPE> addBean(BEANTYPE bean) { - return addItem(bean); - } - - /** - * Unsupported in BeanItemContainer. - */ - @Override - protected void setBeanIdResolver( - AbstractBeanContainer.BeanIdResolver<BEANTYPE, BEANTYPE> beanIdResolver) - throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "BeanItemContainer always uses an IdentityBeanIdResolver"); - } - -} diff --git a/server/src/com/vaadin/data/util/BeanUtil.java b/server/src/com/vaadin/data/util/BeanUtil.java deleted file mode 100644 index 1356cf5359..0000000000 --- a/server/src/com/vaadin/data/util/BeanUtil.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import java.beans.BeanInfo; -import java.beans.IntrospectionException; -import java.beans.Introspector; -import java.beans.PropertyDescriptor; -import java.io.Serializable; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; - -/** - * Utility class for Java Beans information access. - * - * @since 7.4 - * - * @author Vaadin Ltd - */ -public final class BeanUtil implements Serializable { - // Prevent instantiation of util class - private BeanUtil() { - } - - /** - * Returns the property descriptors of a class or an interface. - * - * For an interface, superinterfaces are also iterated as Introspector does - * not take them into account (Oracle Java bug 4275879), but in that case, - * both the setter and the getter for a property must be in the same - * interface and should not be overridden in subinterfaces for the discovery - * to work correctly. - * - * NOTE : This utility method relies on introspection (and returns - * PropertyDescriptor) which is a part of java.beans package. The latter - * package could require bigger JDK in the future (with Java 9+). So it may - * be changed in the future. - * - * For interfaces, the iteration is depth first and the properties of - * superinterfaces are returned before those of their subinterfaces. - * - * @param beanClass - * @return - * @throws IntrospectionException - */ - public static List<PropertyDescriptor> getBeanPropertyDescriptor( - final Class<?> beanClass) throws IntrospectionException { - // Oracle bug 4275879: Introspector does not consider superinterfaces of - // an interface - if (beanClass.isInterface()) { - List<PropertyDescriptor> propertyDescriptors = new ArrayList<PropertyDescriptor>(); - - for (Class<?> cls : beanClass.getInterfaces()) { - propertyDescriptors.addAll(getBeanPropertyDescriptor(cls)); - } - - BeanInfo info = Introspector.getBeanInfo(beanClass); - propertyDescriptors.addAll(getPropertyDescriptors(info)); - - return propertyDescriptors; - } else { - BeanInfo info = Introspector.getBeanInfo(beanClass); - return getPropertyDescriptors(info); - } - } - - /** - * Returns {@code propertyId} class for property declared in {@code clazz}. - * Property could be of form "property.subProperty[.subProperty2]" i.e. - * refer to some nested property. - * - * @param clazz - * class where property is declared - * @param propertyId - * property of form "property" or - * "property.subProperty[.subProperty2]" - * @return class of the property - * @throws IntrospectionException - */ - public static Class<?> getPropertyType(Class<?> clazz, String propertyId) - throws IntrospectionException { - if (propertyId.contains(".")) { - String[] parts = propertyId.split("\\.", 2); - // Get the type of the field in the "cls" class - Class<?> propertyBean = getPropertyType(clazz, parts[0]); - // Find the rest from the sub type - return getPropertyType(propertyBean, parts[1]); - } else { - List<PropertyDescriptor> descriptors = getBeanPropertyDescriptor(clazz); - - for (PropertyDescriptor descriptor : descriptors) { - final Method getMethod = descriptor.getReadMethod(); - if (descriptor.getName().equals(propertyId) - && getMethod != null - && getMethod.getDeclaringClass() != Object.class) { - return descriptor.getPropertyType(); - } - } - return null; - } - } - - // Workaround for Java6 bug JDK-6788525. Do nothing for JDK7+. - private static List<PropertyDescriptor> getPropertyDescriptors( - BeanInfo beanInfo) { - PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); - List<PropertyDescriptor> result = new ArrayList<PropertyDescriptor>( - descriptors.length); - for (PropertyDescriptor descriptor : descriptors) { - try { - Method readMethod = getMethodFromBridge(descriptor - .getReadMethod()); - if (readMethod != null) { - Method writeMethod = getMethodFromBridge( - descriptor.getWriteMethod(), - readMethod.getReturnType()); - if (writeMethod == null) { - writeMethod = descriptor.getWriteMethod(); - } - PropertyDescriptor descr = new PropertyDescriptor( - descriptor.getName(), readMethod, writeMethod); - result.add(descr); - } else { - result.add(descriptor); - } - } catch (SecurityException ignore) { - // handle next descriptor - } catch (IntrospectionException e) { - result.add(descriptor); - } - } - return result; - } - - /** - * Return declared method for which {@code bridgeMethod} is generated. If - * {@code bridgeMethod} is not a bridge method then return null. - */ - private static Method getMethodFromBridge(Method bridgeMethod) - throws SecurityException { - if (bridgeMethod == null) { - return null; - } - return getMethodFromBridge(bridgeMethod, - bridgeMethod.getParameterTypes()); - } - - /** - * Return declared method for which {@code bridgeMethod} is generated using - * its {@code paramTypes}. If {@code bridgeMethod} is not a bridge method - * then return null. - */ - private static Method getMethodFromBridge(Method bridgeMethod, - Class<?>... paramTypes) throws SecurityException { - if (bridgeMethod == null || !bridgeMethod.isBridge()) { - return null; - } - try { - return bridgeMethod.getDeclaringClass().getMethod( - bridgeMethod.getName(), paramTypes); - } catch (NoSuchMethodException e) { - return null; - } - } -} diff --git a/server/src/com/vaadin/data/util/ContainerHierarchicalWrapper.java b/server/src/com/vaadin/data/util/ContainerHierarchicalWrapper.java deleted file mode 100644 index 199d186fab..0000000000 --- a/server/src/com/vaadin/data/util/ContainerHierarchicalWrapper.java +++ /dev/null @@ -1,864 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util; - -import java.io.Serializable; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashSet; -import java.util.Hashtable; -import java.util.Iterator; -import java.util.LinkedHashSet; -import java.util.LinkedList; - -import com.vaadin.data.Container; -import com.vaadin.data.Item; -import com.vaadin.data.Property; - -/** - * <p> - * A wrapper class for adding external hierarchy to containers not implementing - * the {@link com.vaadin.data.Container.Hierarchical} interface. - * </p> - * - * <p> - * If the wrapped container is changed directly (that is, not through the - * wrapper), and does not implement Container.ItemSetChangeNotifier and/or - * Container.PropertySetChangeNotifier the hierarchy information must be updated - * with the {@link #updateHierarchicalWrapper()} method. - * </p> - * - * @author Vaadin Ltd. - * @since 3.0 - */ -@SuppressWarnings("serial") -public class ContainerHierarchicalWrapper implements Container.Hierarchical, - Container.ItemSetChangeNotifier, Container.PropertySetChangeNotifier { - - /** The wrapped container */ - private final Container container; - - /** Set of IDs of those contained Items that can't have children. */ - private HashSet<Object> noChildrenAllowed = null; - - /** Mapping from Item ID to parent Item ID */ - private Hashtable<Object, Object> parent = null; - - /** Mapping from Item ID to a list of child IDs */ - private Hashtable<Object, LinkedList<Object>> children = null; - - /** List that contains all root elements of the container. */ - private LinkedHashSet<Object> roots = null; - - /** Is the wrapped container hierarchical by itself ? */ - private boolean hierarchical; - - /** - * A comparator that sorts the listed items before other items. Otherwise, - * the order is undefined. - */ - private static class ListedItemsFirstComparator implements - Comparator<Object>, Serializable { - private final Collection<?> itemIds; - - private ListedItemsFirstComparator(Collection<?> itemIds) { - this.itemIds = itemIds; - } - - @Override - public int compare(Object o1, Object o2) { - if (o1.equals(o2)) { - return 0; - } - for (Object id : itemIds) { - if (id == o1) { - return -1; - } else if (id == o2) { - return 1; - } - } - return 0; - } - } - - /** - * Constructs a new hierarchical wrapper for an existing Container. Works - * even if the to-be-wrapped container already implements the - * <code>Container.Hierarchical</code> interface. - * - * @param toBeWrapped - * the container that needs to be accessed hierarchically - * @see #updateHierarchicalWrapper() - */ - public ContainerHierarchicalWrapper(Container toBeWrapped) { - - container = toBeWrapped; - hierarchical = container instanceof Container.Hierarchical; - - // Check arguments - if (container == null) { - throw new NullPointerException("Null can not be wrapped"); - } - - // Create initial order if needed - if (!hierarchical) { - noChildrenAllowed = new HashSet<Object>(); - parent = new Hashtable<Object, Object>(); - children = new Hashtable<Object, LinkedList<Object>>(); - roots = new LinkedHashSet<Object>(container.getItemIds()); - } - - updateHierarchicalWrapper(); - - } - - /** - * Updates the wrapper's internal hierarchy data to include all Items in the - * underlying container. If the contents of the wrapped container change - * without the wrapper's knowledge, this method needs to be called to update - * the hierarchy information of the Items. - */ - public void updateHierarchicalWrapper() { - - if (!hierarchical) { - - // Recreate hierarchy and data structures if missing - if (noChildrenAllowed == null || parent == null || children == null - || roots == null) { - noChildrenAllowed = new HashSet<Object>(); - parent = new Hashtable<Object, Object>(); - children = new Hashtable<Object, LinkedList<Object>>(); - roots = new LinkedHashSet<Object>(container.getItemIds()); - } - - // Check that the hierarchy is up-to-date - else { - - // ensure order of root and child lists is same as in wrapped - // container - Collection<?> itemIds = container.getItemIds(); - Comparator<Object> basedOnOrderFromWrappedContainer = new ListedItemsFirstComparator( - itemIds); - - // Calculate the set of all items in the hierarchy - final HashSet<Object> s = new HashSet<Object>(); - s.addAll(parent.keySet()); - s.addAll(children.keySet()); - s.addAll(roots); - - // Remove unnecessary items - for (final Iterator<Object> i = s.iterator(); i.hasNext();) { - final Object id = i.next(); - if (!container.containsId(id)) { - removeFromHierarchyWrapper(id); - } - } - - // Add all the missing items - final Collection<?> ids = container.getItemIds(); - for (final Iterator<?> i = ids.iterator(); i.hasNext();) { - final Object id = i.next(); - if (!s.contains(id)) { - addToHierarchyWrapper(id); - s.add(id); - } - } - - Object[] array = roots.toArray(); - Arrays.sort(array, basedOnOrderFromWrappedContainer); - roots = new LinkedHashSet<Object>(); - for (int i = 0; i < array.length; i++) { - roots.add(array[i]); - } - for (Object object : children.keySet()) { - LinkedList<Object> object2 = children.get(object); - Collections.sort(object2, basedOnOrderFromWrappedContainer); - } - - } - } - } - - /** - * Removes the specified Item from the wrapper's internal hierarchy - * structure. - * <p> - * Note : The Item is not removed from the underlying Container. - * </p> - * - * @param itemId - * the ID of the item to remove from the hierarchy. - */ - private void removeFromHierarchyWrapper(Object itemId) { - - LinkedList<Object> oprhanedChildren = children.remove(itemId); - if (oprhanedChildren != null) { - for (Object object : oprhanedChildren) { - // make orphaned children root nodes - setParent(object, null); - } - } - - roots.remove(itemId); - final Object p = parent.get(itemId); - if (p != null) { - final LinkedList<Object> c = children.get(p); - if (c != null) { - c.remove(itemId); - } - } - parent.remove(itemId); - noChildrenAllowed.remove(itemId); - } - - /** - * Adds the specified Item specified to the internal hierarchy structure. - * The new item is added as a root Item. The underlying container is not - * modified. - * - * @param itemId - * the ID of the item to add to the hierarchy. - */ - private void addToHierarchyWrapper(Object itemId) { - roots.add(itemId); - - } - - /* - * Can the specified Item have any children? Don't add a JavaDoc comment - * here, we use the default documentation from implemented interface. - */ - @Override - public boolean areChildrenAllowed(Object itemId) { - - // If the wrapped container implements the method directly, use it - if (hierarchical) { - return ((Container.Hierarchical) container) - .areChildrenAllowed(itemId); - } - - if (noChildrenAllowed.contains(itemId)) { - return false; - } - - return containsId(itemId); - } - - /* - * Gets the IDs of the children of the specified Item. Don't add a JavaDoc - * comment here, we use the default documentation from implemented - * interface. - */ - @Override - public Collection<?> getChildren(Object itemId) { - - // If the wrapped container implements the method directly, use it - if (hierarchical) { - return ((Container.Hierarchical) container).getChildren(itemId); - } - - final Collection<?> c = children.get(itemId); - if (c == null) { - return null; - } - return Collections.unmodifiableCollection(c); - } - - /* - * Gets the ID of the parent of the specified Item. Don't add a JavaDoc - * comment here, we use the default documentation from implemented - * interface. - */ - @Override - public Object getParent(Object itemId) { - - // If the wrapped container implements the method directly, use it - if (hierarchical) { - return ((Container.Hierarchical) container).getParent(itemId); - } - - return parent.get(itemId); - } - - /* - * Is the Item corresponding to the given ID a leaf node? Don't add a - * JavaDoc comment here, we use the default documentation from implemented - * interface. - */ - @Override - public boolean hasChildren(Object itemId) { - - // If the wrapped container implements the method directly, use it - if (hierarchical) { - return ((Container.Hierarchical) container).hasChildren(itemId); - } - - LinkedList<Object> list = children.get(itemId); - return (list != null && !list.isEmpty()); - } - - /* - * Is the Item corresponding to the given ID a root node? Don't add a - * JavaDoc comment here, we use the default documentation from implemented - * interface. - */ - @Override - public boolean isRoot(Object itemId) { - - // If the wrapped container implements the method directly, use it - if (hierarchical) { - return ((Container.Hierarchical) container).isRoot(itemId); - } - - if (parent.containsKey(itemId)) { - return false; - } - - return containsId(itemId); - } - - /* - * Gets the IDs of the root elements in the container. Don't add a JavaDoc - * comment here, we use the default documentation from implemented - * interface. - */ - @Override - public Collection<?> rootItemIds() { - - // If the wrapped container implements the method directly, use it - if (hierarchical) { - return ((Container.Hierarchical) container).rootItemIds(); - } - - return Collections.unmodifiableCollection(roots); - } - - /** - * <p> - * Sets the given Item's capability to have children. If the Item identified - * with the itemId already has children and the areChildrenAllowed is false - * this method fails and <code>false</code> is returned; the children must - * be first explicitly removed with - * {@link #setParent(Object itemId, Object newParentId)} or - * {@link com.vaadin.data.Container#removeItem(Object itemId)}. - * </p> - * - * @param itemId - * the ID of the Item in the container whose child capability is - * to be set. - * @param childrenAllowed - * the boolean value specifying if the Item can have children or - * not. - * @return <code>true</code> if the operation succeeded, <code>false</code> - * if not - */ - @Override - public boolean setChildrenAllowed(Object itemId, boolean childrenAllowed) { - - // If the wrapped container implements the method directly, use it - if (hierarchical) { - return ((Container.Hierarchical) container).setChildrenAllowed( - itemId, childrenAllowed); - } - - // Check that the item is in the container - if (!containsId(itemId)) { - return false; - } - - // Update status - if (childrenAllowed) { - noChildrenAllowed.remove(itemId); - } else { - noChildrenAllowed.add(itemId); - } - - return true; - } - - /** - * <p> - * Sets the parent of an Item. The new parent item must exist and be able to - * have children. (<code>canHaveChildren(newParentId) == true</code>). It is - * also possible to detach a node from the hierarchy (and thus make it root) - * by setting the parent <code>null</code>. - * </p> - * - * @param itemId - * the ID of the item to be set as the child of the Item - * identified with newParentId. - * @param newParentId - * the ID of the Item that's to be the new parent of the Item - * identified with itemId. - * @return <code>true</code> if the operation succeeded, <code>false</code> - * if not - */ - @Override - public boolean setParent(Object itemId, Object newParentId) { - - // If the wrapped container implements the method directly, use it - if (hierarchical) { - return ((Container.Hierarchical) container).setParent(itemId, - newParentId); - } - - // Check that the item is in the container - if (!containsId(itemId)) { - return false; - } - - // Get the old parent - final Object oldParentId = parent.get(itemId); - - // Check if no change is necessary - if ((newParentId == null && oldParentId == null) - || (newParentId != null && newParentId.equals(oldParentId))) { - return true; - } - - // Making root - if (newParentId == null) { - - // Remove from old parents children list - final LinkedList<Object> l = children.get(oldParentId); - if (l != null) { - l.remove(itemId); - if (l.isEmpty()) { - children.remove(itemId); - } - } - - // Add to be a root - roots.add(itemId); - - // Update parent - parent.remove(itemId); - - fireItemSetChangeIfAbstractContainer(); - - return true; - } - - // Check that the new parent exists in container and can have - // children - if (!containsId(newParentId) || noChildrenAllowed.contains(newParentId)) { - return false; - } - - // Check that setting parent doesn't result to a loop - Object o = newParentId; - while (o != null && !o.equals(itemId)) { - o = parent.get(o); - } - if (o != null) { - return false; - } - - // Update parent - parent.put(itemId, newParentId); - LinkedList<Object> pcl = children.get(newParentId); - if (pcl == null) { - pcl = new LinkedList<Object>(); - children.put(newParentId, pcl); - } - pcl.add(itemId); - - // Remove from old parent or root - if (oldParentId == null) { - roots.remove(itemId); - } else { - final LinkedList<Object> l = children.get(oldParentId); - if (l != null) { - l.remove(itemId); - if (l.isEmpty()) { - children.remove(oldParentId); - } - } - } - - fireItemSetChangeIfAbstractContainer(); - - return true; - } - - /** - * inform container (if it is instance of AbstractContainer) about the - * change in hierarchy (#15421) - */ - private void fireItemSetChangeIfAbstractContainer() { - if (container instanceof AbstractContainer) { - ((AbstractContainer) container).fireItemSetChange(); - } - } - - /** - * Creates a new Item into the Container, assigns it an automatic ID, and - * adds it to the hierarchy. - * - * @return the autogenerated ID of the new Item or <code>null</code> if the - * operation failed - * @throws UnsupportedOperationException - * if the addItem is not supported. - */ - @Override - public Object addItem() throws UnsupportedOperationException { - - final Object id = container.addItem(); - if (!hierarchical && id != null) { - addToHierarchyWrapper(id); - } - return id; - } - - /** - * Adds a new Item by its ID to the underlying container and to the - * hierarchy. - * - * @param itemId - * the ID of the Item to be created. - * @return the added Item or <code>null</code> if the operation failed. - * @throws UnsupportedOperationException - * if the addItem is not supported. - */ - @Override - public Item addItem(Object itemId) throws UnsupportedOperationException { - - // Null ids are not accepted - if (itemId == null) { - throw new NullPointerException("Container item id can not be null"); - } - - final Item item = container.addItem(itemId); - if (!hierarchical && item != null) { - addToHierarchyWrapper(itemId); - } - return item; - } - - /** - * Removes all items from the underlying container and from the hierarcy. - * - * @return <code>true</code> if the operation succeeded, <code>false</code> - * if not - * @throws UnsupportedOperationException - * if the removeAllItems is not supported. - */ - @Override - public boolean removeAllItems() throws UnsupportedOperationException { - - final boolean success = container.removeAllItems(); - - if (!hierarchical && success) { - roots.clear(); - parent.clear(); - children.clear(); - noChildrenAllowed.clear(); - } - return success; - } - - /** - * Removes an Item specified by the itemId from the underlying container and - * from the hierarchy. - * - * @param itemId - * the ID of the Item to be removed. - * @return <code>true</code> if the operation succeeded, <code>false</code> - * if not - * @throws UnsupportedOperationException - * if the removeItem is not supported. - */ - @Override - public boolean removeItem(Object itemId) - throws UnsupportedOperationException { - - final boolean success = container.removeItem(itemId); - - if (!hierarchical && success) { - removeFromHierarchyWrapper(itemId); - } - - return success; - } - - /** - * Removes the Item identified by given itemId and all its children. - * - * @see #removeItem(Object) - * @param itemId - * the identifier of the Item to be removed - * @return true if the operation succeeded - */ - public boolean removeItemRecursively(Object itemId) { - return HierarchicalContainer.removeItemRecursively(this, itemId); - } - - /** - * Adds a new Property to all Items in the Container. - * - * @param propertyId - * the ID of the new Property. - * @param type - * the Data type of the new Property. - * @param defaultValue - * the value all created Properties are initialized to. - * @return <code>true</code> if the operation succeeded, <code>false</code> - * if not - * @throws UnsupportedOperationException - * if the addContainerProperty is not supported. - */ - @Override - public boolean addContainerProperty(Object propertyId, Class<?> type, - Object defaultValue) throws UnsupportedOperationException { - - return container.addContainerProperty(propertyId, type, defaultValue); - } - - /** - * Removes the specified Property from the underlying container and from the - * hierarchy. - * <p> - * Note : The Property will be removed from all Items in the Container. - * </p> - * - * @param propertyId - * the ID of the Property to remove. - * @return <code>true</code> if the operation succeeded, <code>false</code> - * if not - * @throws UnsupportedOperationException - * if the removeContainerProperty is not supported. - */ - @Override - public boolean removeContainerProperty(Object propertyId) - throws UnsupportedOperationException { - return container.removeContainerProperty(propertyId); - } - - /* - * Does the container contain the specified Item? Don't add a JavaDoc - * comment here, we use the default documentation from implemented - * interface. - */ - @Override - public boolean containsId(Object itemId) { - return container.containsId(itemId); - } - - /* - * Gets the specified Item from the container. Don't add a JavaDoc comment - * here, we use the default documentation from implemented interface. - */ - @Override - public Item getItem(Object itemId) { - return container.getItem(itemId); - } - - /* - * Gets the ID's of all Items stored in the Container Don't add a JavaDoc - * comment here, we use the default documentation from implemented - * interface. - */ - @Override - public Collection<?> getItemIds() { - return container.getItemIds(); - } - - /* - * Gets the Property identified by the given itemId and propertyId from the - * Container Don't add a JavaDoc comment here, we use the default - * documentation from implemented interface. - */ - @Override - public Property getContainerProperty(Object itemId, Object propertyId) { - return container.getContainerProperty(itemId, propertyId); - } - - /* - * Gets the ID's of all Properties stored in the Container Don't add a - * JavaDoc comment here, we use the default documentation from implemented - * interface. - */ - @Override - public Collection<?> getContainerPropertyIds() { - return container.getContainerPropertyIds(); - } - - /* - * Gets the data type of all Properties identified by the given Property ID. - * Don't add a JavaDoc comment here, we use the default documentation from - * implemented interface. - */ - @Override - public Class<?> getType(Object propertyId) { - return container.getType(propertyId); - } - - /* - * Gets the number of Items in the Container. Don't add a JavaDoc comment - * here, we use the default documentation from implemented interface. - */ - @Override - public int size() { - int size = container.size(); - assert size >= 0; - return size; - } - - /* - * Registers a new Item set change listener for this Container. Don't add a - * JavaDoc comment here, we use the default documentation from implemented - * interface. - */ - @Override - public void addItemSetChangeListener( - Container.ItemSetChangeListener listener) { - if (container instanceof Container.ItemSetChangeNotifier) { - ((Container.ItemSetChangeNotifier) container) - .addItemSetChangeListener(new PiggybackListener(listener)); - } - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #addItemSetChangeListener(com.vaadin.data.Container.ItemSetChangeListener)} - **/ - @Override - @Deprecated - public void addListener(Container.ItemSetChangeListener listener) { - addItemSetChangeListener(listener); - } - - /* - * Removes a Item set change listener from the object. Don't add a JavaDoc - * comment here, we use the default documentation from implemented - * interface. - */ - @Override - public void removeItemSetChangeListener( - Container.ItemSetChangeListener listener) { - if (container instanceof Container.ItemSetChangeNotifier) { - ((Container.ItemSetChangeNotifier) container) - .removeItemSetChangeListener(new PiggybackListener(listener)); - } - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #removeItemSetChangeListener(com.vaadin.data.Container.ItemSetChangeListener)} - **/ - @Override - @Deprecated - public void removeListener(Container.ItemSetChangeListener listener) { - removeItemSetChangeListener(listener); - } - - /* - * Registers a new Property set change listener for this Container. Don't - * add a JavaDoc comment here, we use the default documentation from - * implemented interface. - */ - @Override - public void addPropertySetChangeListener( - Container.PropertySetChangeListener listener) { - if (container instanceof Container.PropertySetChangeNotifier) { - ((Container.PropertySetChangeNotifier) container) - .addPropertySetChangeListener(new PiggybackListener( - listener)); - } - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #addPropertySetChangeListener(com.vaadin.data.Container.PropertySetChangeListener)} - **/ - @Override - @Deprecated - public void addListener(Container.PropertySetChangeListener listener) { - addPropertySetChangeListener(listener); - } - - /* - * Removes a Property set change listener from the object. Don't add a - * JavaDoc comment here, we use the default documentation from implemented - * interface. - */ - @Override - public void removePropertySetChangeListener( - Container.PropertySetChangeListener listener) { - if (container instanceof Container.PropertySetChangeNotifier) { - ((Container.PropertySetChangeNotifier) container) - .removePropertySetChangeListener(new PiggybackListener( - listener)); - } - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #removePropertySetChangeListener(com.vaadin.data.Container.PropertySetChangeListener)} - **/ - @Override - @Deprecated - public void removeListener(Container.PropertySetChangeListener listener) { - removePropertySetChangeListener(listener); - } - - /** - * This listener 'piggybacks' on the real listener in order to update the - * wrapper when needed. It proxies equals() and hashCode() to the real - * listener so that the correct listener gets removed. - * - */ - private class PiggybackListener implements - Container.PropertySetChangeListener, - Container.ItemSetChangeListener { - - Object listener; - - public PiggybackListener(Object realListener) { - listener = realListener; - } - - @Override - public void containerItemSetChange(ItemSetChangeEvent event) { - updateHierarchicalWrapper(); - ((Container.ItemSetChangeListener) listener) - .containerItemSetChange(event); - - } - - @Override - public void containerPropertySetChange(PropertySetChangeEvent event) { - updateHierarchicalWrapper(); - ((Container.PropertySetChangeListener) listener) - .containerPropertySetChange(event); - - } - - @Override - public boolean equals(Object obj) { - return obj == listener || (obj != null && obj.equals(listener)); - } - - @Override - public int hashCode() { - return listener.hashCode(); - } - - } -} diff --git a/server/src/com/vaadin/data/util/ContainerOrderedWrapper.java b/server/src/com/vaadin/data/util/ContainerOrderedWrapper.java deleted file mode 100644 index 4329219e96..0000000000 --- a/server/src/com/vaadin/data/util/ContainerOrderedWrapper.java +++ /dev/null @@ -1,711 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util; - -import java.util.Collection; -import java.util.Hashtable; -import java.util.Iterator; -import java.util.LinkedList; - -import com.vaadin.data.Container; -import com.vaadin.data.Item; -import com.vaadin.data.Property; - -/** - * <p> - * A wrapper class for adding external ordering to containers not implementing - * the {@link com.vaadin.data.Container.Ordered} interface. - * </p> - * - * <p> - * If the wrapped container is changed directly (that is, not through the - * wrapper), and does not implement Container.ItemSetChangeNotifier and/or - * Container.PropertySetChangeNotifier the hierarchy information must be updated - * with the {@link #updateOrderWrapper()} method. - * </p> - * - * @author Vaadin Ltd. - * @since 3.0 - */ -@SuppressWarnings("serial") -public class ContainerOrderedWrapper implements Container.Ordered, - Container.ItemSetChangeNotifier, Container.PropertySetChangeNotifier { - - /** - * The wrapped container - */ - private final Container container; - - /** - * Ordering information, ie. the mapping from Item ID to the next item ID. - * The last item id should not be present - */ - private Hashtable<Object, Object> next; - - /** - * Reverse ordering information for convenience and performance reasons. The - * first item id should not be present - */ - private Hashtable<Object, Object> prev; - - /** - * ID of the first Item in the container. - */ - private Object first; - - /** - * ID of the last Item in the container. - */ - private Object last; - - /** - * Is the wrapped container ordered by itself, ie. does it implement the - * Container.Ordered interface by itself? If it does, this class will use - * the methods of the underlying container directly. - */ - private boolean ordered = false; - - /** - * The last known size of the wrapped container. Used to check whether items - * have been added or removed to the wrapped container, when the wrapped - * container does not send ItemSetChangeEvents. - */ - private int lastKnownSize = -1; - - /** - * Constructs a new ordered wrapper for an existing Container. Works even if - * the to-be-wrapped container already implements the Container.Ordered - * interface. - * - * @param toBeWrapped - * the container whose contents need to be ordered. - */ - public ContainerOrderedWrapper(Container toBeWrapped) { - - container = toBeWrapped; - ordered = container instanceof Container.Ordered; - - // Checks arguments - if (container == null) { - throw new NullPointerException("Null can not be wrapped"); - } - - // Creates initial order if needed - updateOrderWrapper(); - } - - /** - * Removes the specified Item from the wrapper's internal hierarchy - * structure. - * <p> - * Note : The Item is not removed from the underlying Container. - * </p> - * - * @param id - * the ID of the Item to be removed from the ordering. - */ - private void removeFromOrderWrapper(Object id) { - if (id != null) { - final Object pid = prev.get(id); - final Object nid = next.get(id); - if (first.equals(id)) { - first = nid; - } - if (last.equals(id)) { - last = pid; - } - if (nid != null) { - if (pid == null) { - prev.remove(nid); - } else { - prev.put(nid, pid); - } - } - if (pid != null) { - if (nid == null) { - next.remove(pid); - } else { - next.put(pid, nid); - } - } - next.remove(id); - prev.remove(id); - } - } - - /** - * Registers the specified Item to the last position in the wrapper's - * internal ordering. The underlying container is not modified. - * - * @param id - * the ID of the Item to be added to the ordering. - */ - private void addToOrderWrapper(Object id) { - - // Adds the if to tail - if (last != null) { - next.put(last, id); - prev.put(id, last); - last = id; - } else { - first = last = id; - } - } - - /** - * Registers the specified Item after the specified itemId in the wrapper's - * internal ordering. The underlying container is not modified. Given item - * id must be in the container, or must be null. - * - * @param id - * the ID of the Item to be added to the ordering. - * @param previousItemId - * the Id of the previous item. - */ - private void addToOrderWrapper(Object id, Object previousItemId) { - - if (last == previousItemId || last == null) { - addToOrderWrapper(id); - } else { - if (previousItemId == null) { - next.put(id, first); - prev.put(first, id); - first = id; - } else { - prev.put(id, previousItemId); - next.put(id, next.get(previousItemId)); - prev.put(next.get(previousItemId), id); - next.put(previousItemId, id); - } - } - } - - /** - * Updates the wrapper's internal ordering information to include all Items - * in the underlying container. - * <p> - * Note : If the contents of the wrapped container change without the - * wrapper's knowledge, this method needs to be called to update the - * ordering information of the Items. - * </p> - */ - public void updateOrderWrapper() { - - if (!ordered) { - - final Collection<?> ids = container.getItemIds(); - - // Recreates ordering if some parts of it are missing - if (next == null || first == null || last == null || prev == null) { - first = null; - last = null; - next = new Hashtable<Object, Object>(); - prev = new Hashtable<Object, Object>(); - } - - // Filter out all the missing items - final LinkedList<?> l = new LinkedList<Object>(next.keySet()); - for (final Iterator<?> i = l.iterator(); i.hasNext();) { - final Object id = i.next(); - if (!container.containsId(id)) { - removeFromOrderWrapper(id); - } - } - - // Adds missing items - for (final Iterator<?> i = ids.iterator(); i.hasNext();) { - final Object id = i.next(); - if (!next.containsKey(id) && last != id) { - addToOrderWrapper(id); - } - } - } - } - - /* - * Gets the first item stored in the ordered container Don't add a JavaDoc - * comment here, we use the default documentation from implemented - * interface. - */ - @Override - public Object firstItemId() { - if (ordered) { - return ((Container.Ordered) container).firstItemId(); - } - return first; - } - - /* - * Tests if the given item is the first item in the container Don't add a - * JavaDoc comment here, we use the default documentation from implemented - * interface. - */ - @Override - public boolean isFirstId(Object itemId) { - if (ordered) { - return ((Container.Ordered) container).isFirstId(itemId); - } - return first != null && first.equals(itemId); - } - - /* - * Tests if the given item is the last item in the container Don't add a - * JavaDoc comment here, we use the default documentation from implemented - * interface. - */ - @Override - public boolean isLastId(Object itemId) { - if (ordered) { - return ((Container.Ordered) container).isLastId(itemId); - } - return last != null && last.equals(itemId); - } - - /* - * Gets the last item stored in the ordered container Don't add a JavaDoc - * comment here, we use the default documentation from implemented - * interface. - */ - @Override - public Object lastItemId() { - if (ordered) { - return ((Container.Ordered) container).lastItemId(); - } - return last; - } - - /* - * Gets the item that is next from the specified item. Don't add a JavaDoc - * comment here, we use the default documentation from implemented - * interface. - */ - @Override - public Object nextItemId(Object itemId) { - if (ordered) { - return ((Container.Ordered) container).nextItemId(itemId); - } - if (itemId == null) { - return null; - } - return next.get(itemId); - } - - /* - * Gets the item that is previous from the specified item. Don't add a - * JavaDoc comment here, we use the default documentation from implemented - * interface. - */ - @Override - public Object prevItemId(Object itemId) { - if (ordered) { - return ((Container.Ordered) container).prevItemId(itemId); - } - if (itemId == null) { - return null; - } - return prev.get(itemId); - } - - /** - * Registers a new Property to all Items in the Container. - * - * @param propertyId - * the ID of the new Property. - * @param type - * the Data type of the new Property. - * @param defaultValue - * the value all created Properties are initialized to. - * @return <code>true</code> if the operation succeeded, <code>false</code> - * if not - */ - @Override - public boolean addContainerProperty(Object propertyId, Class<?> type, - Object defaultValue) throws UnsupportedOperationException { - - return container.addContainerProperty(propertyId, type, defaultValue); - } - - /** - * Creates a new Item into the Container, assigns it an automatic ID, and - * adds it to the ordering. - * - * @return the autogenerated ID of the new Item or <code>null</code> if the - * operation failed - * @throws UnsupportedOperationException - * if the addItem is not supported. - */ - @Override - public Object addItem() throws UnsupportedOperationException { - - final Object id = container.addItem(); - if (!ordered && id != null) { - addToOrderWrapper(id); - } - return id; - } - - /** - * Registers a new Item by its ID to the underlying container and to the - * ordering. - * - * @param itemId - * the ID of the Item to be created. - * @return the added Item or <code>null</code> if the operation failed - * @throws UnsupportedOperationException - * if the addItem is not supported. - */ - @Override - public Item addItem(Object itemId) throws UnsupportedOperationException { - final Item item = container.addItem(itemId); - if (!ordered && item != null) { - addToOrderWrapper(itemId); - } - return item; - } - - /** - * Removes all items from the underlying container and from the ordering. - * - * @return <code>true</code> if the operation succeeded, otherwise - * <code>false</code> - * @throws UnsupportedOperationException - * if the removeAllItems is not supported. - */ - @Override - public boolean removeAllItems() throws UnsupportedOperationException { - final boolean success = container.removeAllItems(); - if (!ordered && success) { - first = last = null; - next.clear(); - prev.clear(); - } - return success; - } - - /** - * Removes an Item specified by the itemId from the underlying container and - * from the ordering. - * - * @param itemId - * the ID of the Item to be removed. - * @return <code>true</code> if the operation succeeded, <code>false</code> - * if not - * @throws UnsupportedOperationException - * if the removeItem is not supported. - */ - @Override - public boolean removeItem(Object itemId) - throws UnsupportedOperationException { - - final boolean success = container.removeItem(itemId); - if (!ordered && success) { - removeFromOrderWrapper(itemId); - } - return success; - } - - /** - * Removes the specified Property from the underlying container and from the - * ordering. - * <p> - * Note : The Property will be removed from all the Items in the Container. - * </p> - * - * @param propertyId - * the ID of the Property to remove. - * @return <code>true</code> if the operation succeeded, <code>false</code> - * if not - * @throws UnsupportedOperationException - * if the removeContainerProperty is not supported. - */ - @Override - public boolean removeContainerProperty(Object propertyId) - throws UnsupportedOperationException { - return container.removeContainerProperty(propertyId); - } - - /* - * Does the container contain the specified Item? Don't add a JavaDoc - * comment here, we use the default documentation from implemented - * interface. - */ - @Override - public boolean containsId(Object itemId) { - return container.containsId(itemId); - } - - /* - * Gets the specified Item from the container. Don't add a JavaDoc comment - * here, we use the default documentation from implemented interface. - */ - @Override - public Item getItem(Object itemId) { - return container.getItem(itemId); - } - - /* - * Gets the ID's of all Items stored in the Container Don't add a JavaDoc - * comment here, we use the default documentation from implemented - * interface. - */ - @Override - public Collection<?> getItemIds() { - return container.getItemIds(); - } - - /* - * Gets the Property identified by the given itemId and propertyId from the - * Container Don't add a JavaDoc comment here, we use the default - * documentation from implemented interface. - */ - @Override - public Property getContainerProperty(Object itemId, Object propertyId) { - return container.getContainerProperty(itemId, propertyId); - } - - /* - * Gets the ID's of all Properties stored in the Container Don't add a - * JavaDoc comment here, we use the default documentation from implemented - * interface. - */ - @Override - public Collection<?> getContainerPropertyIds() { - return container.getContainerPropertyIds(); - } - - /* - * Gets the data type of all Properties identified by the given Property ID. - * Don't add a JavaDoc comment here, we use the default documentation from - * implemented interface. - */ - @Override - public Class<?> getType(Object propertyId) { - return container.getType(propertyId); - } - - /* - * Gets the number of Items in the Container. Don't add a JavaDoc comment - * here, we use the default documentation from implemented interface. - */ - @Override - public int size() { - int newSize = container.size(); - assert newSize >= 0; - if (lastKnownSize != -1 && newSize != lastKnownSize - && !(container instanceof Container.ItemSetChangeNotifier)) { - // Update the internal cache when the size of the container changes - // and the container is incapable of sending ItemSetChangeEvents - updateOrderWrapper(); - } - lastKnownSize = newSize; - return newSize; - } - - /* - * Registers a new Item set change listener for this Container. Don't add a - * JavaDoc comment here, we use the default documentation from implemented - * interface. - */ - @Override - public void addItemSetChangeListener( - Container.ItemSetChangeListener listener) { - if (container instanceof Container.ItemSetChangeNotifier) { - ((Container.ItemSetChangeNotifier) container) - .addItemSetChangeListener(new PiggybackListener(listener)); - } - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #addItemSetChangeListener(com.vaadin.data.Container.ItemSetChangeListener)} - **/ - @Override - @Deprecated - public void addListener(Container.ItemSetChangeListener listener) { - addItemSetChangeListener(listener); - } - - /* - * Removes a Item set change listener from the object. Don't add a JavaDoc - * comment here, we use the default documentation from implemented - * interface. - */ - @Override - public void removeItemSetChangeListener( - Container.ItemSetChangeListener listener) { - if (container instanceof Container.ItemSetChangeNotifier) { - ((Container.ItemSetChangeNotifier) container) - .removeItemSetChangeListener(new PiggybackListener(listener)); - } - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #removeItemSetChangeListener(com.vaadin.data.Container.ItemSetChangeListener)} - **/ - @Override - @Deprecated - public void removeListener(Container.ItemSetChangeListener listener) { - removeItemSetChangeListener(listener); - } - - /* - * Registers a new Property set change listener for this Container. Don't - * add a JavaDoc comment here, we use the default documentation from - * implemented interface. - */ - @Override - public void addPropertySetChangeListener( - Container.PropertySetChangeListener listener) { - if (container instanceof Container.PropertySetChangeNotifier) { - ((Container.PropertySetChangeNotifier) container) - .addPropertySetChangeListener(new PiggybackListener( - listener)); - } - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #addPropertySetChangeListener(com.vaadin.data.Container.PropertySetChangeListener)} - **/ - @Override - @Deprecated - public void addListener(Container.PropertySetChangeListener listener) { - addPropertySetChangeListener(listener); - } - - /* - * Removes a Property set change listener from the object. Don't add a - * JavaDoc comment here, we use the default documentation from implemented - * interface. - */ - @Override - public void removePropertySetChangeListener( - Container.PropertySetChangeListener listener) { - if (container instanceof Container.PropertySetChangeNotifier) { - ((Container.PropertySetChangeNotifier) container) - .removePropertySetChangeListener(new PiggybackListener( - listener)); - } - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #removePropertySetChangeListener(com.vaadin.data.Container.PropertySetChangeListener)} - **/ - @Override - @Deprecated - public void removeListener(Container.PropertySetChangeListener listener) { - removePropertySetChangeListener(listener); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Ordered#addItemAfter(java.lang.Object, - * java.lang.Object) - */ - @Override - public Item addItemAfter(Object previousItemId, Object newItemId) - throws UnsupportedOperationException { - - // If the previous item is not in the container, fail - if (previousItemId != null && !containsId(previousItemId)) { - return null; - } - - // Adds the item to container - final Item item = container.addItem(newItemId); - - // Puts the new item to its correct place - if (!ordered && item != null) { - addToOrderWrapper(newItemId, previousItemId); - } - - return item; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Ordered#addItemAfter(java.lang.Object) - */ - @Override - public Object addItemAfter(Object previousItemId) - throws UnsupportedOperationException { - - // If the previous item is not in the container, fail - if (previousItemId != null && !containsId(previousItemId)) { - return null; - } - - // Adds the item to container - final Object id = container.addItem(); - - // Puts the new item to its correct place - if (!ordered && id != null) { - addToOrderWrapper(id, previousItemId); - } - - return id; - } - - /** - * This listener 'piggybacks' on the real listener in order to update the - * wrapper when needed. It proxies equals() and hashCode() to the real - * listener so that the correct listener gets removed. - * - */ - private class PiggybackListener implements - Container.PropertySetChangeListener, - Container.ItemSetChangeListener { - - Object listener; - - public PiggybackListener(Object realListener) { - listener = realListener; - } - - @Override - public void containerItemSetChange(ItemSetChangeEvent event) { - updateOrderWrapper(); - ((Container.ItemSetChangeListener) listener) - .containerItemSetChange(event); - - } - - @Override - public void containerPropertySetChange(PropertySetChangeEvent event) { - updateOrderWrapper(); - ((Container.PropertySetChangeListener) listener) - .containerPropertySetChange(event); - - } - - @Override - public boolean equals(Object obj) { - return obj == listener || (obj != null && obj.equals(listener)); - } - - @Override - public int hashCode() { - return listener.hashCode(); - } - - } - -} diff --git a/server/src/com/vaadin/data/util/DefaultItemSorter.java b/server/src/com/vaadin/data/util/DefaultItemSorter.java deleted file mode 100644 index 9728ccbd51..0000000000 --- a/server/src/com/vaadin/data/util/DefaultItemSorter.java +++ /dev/null @@ -1,222 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Comparator; -import java.util.List; - -import com.vaadin.data.Container; -import com.vaadin.data.Container.Sortable; -import com.vaadin.data.Item; -import com.vaadin.data.Property; - -/** - * Provides a default implementation of an ItemSorter. The - * <code>DefaultItemSorter</code> adheres to the - * {@link Sortable#sort(Object[], boolean[])} rules and sorts the container - * according to the properties given using - * {@link #setSortProperties(Sortable, Object[], boolean[])}. - * <p> - * A Comparator is used for comparing the individual <code>Property</code> - * values. The comparator can be set using the constructor. If no comparator is - * provided a default comparator is used. - * - */ -public class DefaultItemSorter implements ItemSorter { - - private java.lang.Object[] sortPropertyIds; - private boolean[] sortDirections; - private Container container; - private Comparator<Object> propertyValueComparator; - - /** - * Constructs a DefaultItemSorter using the default <code>Comparator</code> - * for comparing <code>Property</code>values. - * - */ - public DefaultItemSorter() { - this(new DefaultPropertyValueComparator()); - } - - /** - * Constructs a DefaultItemSorter which uses the <code>Comparator</code> - * indicated by the <code>propertyValueComparator</code> parameter for - * comparing <code>Property</code>values. - * - * @param propertyValueComparator - * The comparator to use when comparing individual - * <code>Property</code> values - */ - public DefaultItemSorter(Comparator<Object> propertyValueComparator) { - this.propertyValueComparator = propertyValueComparator; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.ItemSorter#compare(java.lang.Object, - * java.lang.Object) - */ - @Override - public int compare(Object o1, Object o2) { - Item item1 = container.getItem(o1); - Item item2 = container.getItem(o2); - - /* - * Items can be null if the container is filtered. Null is considered - * "less" than not-null. - */ - if (item1 == null) { - if (item2 == null) { - return 0; - } else { - return 1; - } - } else if (item2 == null) { - return -1; - } - - for (int i = 0; i < sortPropertyIds.length; i++) { - - int result = compareProperty(sortPropertyIds[i], sortDirections[i], - item1, item2); - - // If order can be decided - if (result != 0) { - return result; - } - - } - - return 0; - } - - /** - * Compares the property indicated by <code>propertyId</code> in the items - * indicated by <code>item1</code> and <code>item2</code> for order. Returns - * a negative integer, zero, or a positive integer as the property value in - * the first item is less than, equal to, or greater than the property value - * in the second item. If the <code>sortDirection</code> is false the - * returned value is negated. - * <p> - * The comparator set for this <code>DefaultItemSorter</code> is used for - * comparing the two property values. - * - * @param propertyId - * The property id for the property that is used for comparison. - * @param sortDirection - * The direction of the sort. A false value negates the result. - * @param item1 - * The first item to compare. - * @param item2 - * The second item to compare. - * @return a negative, zero, or positive integer if the property value in - * the first item is less than, equal to, or greater than the - * property value in the second item. Negated if - * {@code sortDirection} is false. - */ - protected int compareProperty(Object propertyId, boolean sortDirection, - Item item1, Item item2) { - - // Get the properties to compare - final Property<?> property1 = item1.getItemProperty(propertyId); - final Property<?> property2 = item2.getItemProperty(propertyId); - - // Get the values to compare - final Object value1 = (property1 == null) ? null : property1.getValue(); - final Object value2 = (property2 == null) ? null : property2.getValue(); - - // Result of the comparison - int r = 0; - if (sortDirection) { - r = propertyValueComparator.compare(value1, value2); - } else { - r = propertyValueComparator.compare(value2, value1); - } - - return r; - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.ItemSorter#setSortProperties(com.vaadin.data.Container - * .Sortable, java.lang.Object[], boolean[]) - */ - @Override - public void setSortProperties(Container.Sortable container, - Object[] propertyId, boolean[] ascending) { - this.container = container; - - // Removes any non-sortable property ids - final List<Object> ids = new ArrayList<Object>(); - final List<Boolean> orders = new ArrayList<Boolean>(); - final Collection<?> sortable = container - .getSortableContainerPropertyIds(); - for (int i = 0; i < propertyId.length; i++) { - if (sortable.contains(propertyId[i])) { - ids.add(propertyId[i]); - orders.add(Boolean.valueOf(i < ascending.length ? ascending[i] - : true)); - } - } - - sortPropertyIds = ids.toArray(); - sortDirections = new boolean[orders.size()]; - for (int i = 0; i < sortDirections.length; i++) { - sortDirections[i] = (orders.get(i)).booleanValue(); - } - - } - - /** - * Provides a default comparator used for comparing {@link Property} values. - * The <code>DefaultPropertyValueComparator</code> assumes all objects it - * compares can be cast to Comparable. - * - */ - public static class DefaultPropertyValueComparator implements - Comparator<Object>, Serializable { - - @Override - @SuppressWarnings("unchecked") - public int compare(Object o1, Object o2) { - int r = 0; - // Normal non-null comparison - if (o1 != null && o2 != null) { - // Assume the objects can be cast to Comparable, throw - // ClassCastException otherwise. - r = ((Comparable<Object>) o1).compareTo(o2); - } else if (o1 == o2) { - // Objects are equal if both are null - r = 0; - } else { - if (o1 == null) { - r = -1; // null is less than non-null - } else { - r = 1; // non-null is greater than null - } - } - - return r; - } - } - -} diff --git a/server/src/com/vaadin/data/util/FilesystemContainer.java b/server/src/com/vaadin/data/util/FilesystemContainer.java deleted file mode 100644 index 7ceda49918..0000000000 --- a/server/src/com/vaadin/data/util/FilesystemContainer.java +++ /dev/null @@ -1,924 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util; - -import java.io.File; -import java.io.FilenameFilter; -import java.io.IOException; -import java.io.Serializable; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; - -import com.vaadin.data.Container; -import com.vaadin.data.Item; -import com.vaadin.data.Property; -import com.vaadin.server.Resource; -import com.vaadin.util.FileTypeResolver; - -/** - * A hierarchical container wrapper for a filesystem. - * - * @author Vaadin Ltd. - * @since 3.0 - */ -@SuppressWarnings("serial") -public class FilesystemContainer implements Container.Hierarchical { - - /** - * String identifier of a file's "name" property. - */ - public static String PROPERTY_NAME = "Name"; - - /** - * String identifier of a file's "size" property. - */ - public static String PROPERTY_SIZE = "Size"; - - /** - * String identifier of a file's "icon" property. - */ - public static String PROPERTY_ICON = "Icon"; - - /** - * String identifier of a file's "last modified" property. - */ - public static String PROPERTY_LASTMODIFIED = "Last Modified"; - - /** - * List of the string identifiers for the available properties. - */ - public static Collection<String> FILE_PROPERTIES; - - private final static Method FILEITEM_LASTMODIFIED; - - private final static Method FILEITEM_NAME; - - private final static Method FILEITEM_ICON; - - private final static Method FILEITEM_SIZE; - - static { - - FILE_PROPERTIES = new ArrayList<String>(); - FILE_PROPERTIES.add(PROPERTY_NAME); - FILE_PROPERTIES.add(PROPERTY_ICON); - FILE_PROPERTIES.add(PROPERTY_SIZE); - FILE_PROPERTIES.add(PROPERTY_LASTMODIFIED); - FILE_PROPERTIES = Collections.unmodifiableCollection(FILE_PROPERTIES); - try { - FILEITEM_LASTMODIFIED = FileItem.class.getMethod("lastModified", - new Class[] {}); - FILEITEM_NAME = FileItem.class.getMethod("getName", new Class[] {}); - FILEITEM_ICON = FileItem.class.getMethod("getIcon", new Class[] {}); - FILEITEM_SIZE = FileItem.class.getMethod("getSize", new Class[] {}); - } catch (final NoSuchMethodException e) { - throw new RuntimeException( - "Internal error finding methods in FilesystemContainer"); - } - } - - private File[] roots = new File[] {}; - - private FilenameFilter filter = null; - - private boolean recursive = true; - - /** - * Constructs a new <code>FileSystemContainer</code> with the specified file - * as the root of the filesystem. The files are included recursively. - * - * @param root - * the root file for the new file-system container. Null values - * are ignored. - */ - public FilesystemContainer(File root) { - if (root != null) { - roots = new File[] { root }; - } - } - - /** - * Constructs a new <code>FileSystemContainer</code> with the specified file - * as the root of the filesystem. The files are included recursively. - * - * @param root - * the root file for the new file-system container. - * @param recursive - * should the container recursively contain subdirectories. - */ - public FilesystemContainer(File root, boolean recursive) { - this(root); - setRecursive(recursive); - } - - /** - * Constructs a new <code>FileSystemContainer</code> with the specified file - * as the root of the filesystem. - * - * @param root - * the root file for the new file-system container. - * @param extension - * the Filename extension (w/o separator) to limit the files in - * container. - * @param recursive - * should the container recursively contain subdirectories. - */ - public FilesystemContainer(File root, String extension, boolean recursive) { - this(root); - this.setFilter(extension); - setRecursive(recursive); - } - - /** - * Constructs a new <code>FileSystemContainer</code> with the specified root - * and recursivity status. - * - * @param root - * the root file for the new file-system container. - * @param filter - * the Filename filter to limit the files in container. - * @param recursive - * should the container recursively contain subdirectories. - */ - public FilesystemContainer(File root, FilenameFilter filter, - boolean recursive) { - this(root); - this.setFilter(filter); - setRecursive(recursive); - } - - /** - * Adds new root file directory. Adds a file to be included as root file - * directory in the <code>FilesystemContainer</code>. - * - * @param root - * the File to be added as root directory. Null values are - * ignored. - */ - public void addRoot(File root) { - if (root != null) { - final File[] newRoots = new File[roots.length + 1]; - for (int i = 0; i < roots.length; i++) { - newRoots[i] = roots[i]; - } - newRoots[roots.length] = root; - roots = newRoots; - } - } - - /** - * Tests if the specified Item in the container may have children. Since a - * <code>FileSystemContainer</code> contains files and directories, this - * method returns <code>true</code> for directory Items only. - * - * @param itemId - * the id of the item. - * @return <code>true</code> if the specified Item is a directory, - * <code>false</code> otherwise. - */ - @Override - public boolean areChildrenAllowed(Object itemId) { - return itemId instanceof File && ((File) itemId).canRead() - && ((File) itemId).isDirectory(); - } - - /* - * Gets the ID's of all Items who are children of the specified Item. Don't - * add a JavaDoc comment here, we use the default documentation from - * implemented interface. - */ - @Override - public Collection<File> getChildren(Object itemId) { - - if (!(itemId instanceof File)) { - return Collections.unmodifiableCollection(new LinkedList<File>()); - } - File[] f; - if (filter != null) { - f = ((File) itemId).listFiles(filter); - } else { - f = ((File) itemId).listFiles(); - } - - if (f == null) { - return Collections.unmodifiableCollection(new LinkedList<File>()); - } - - final List<File> l = Arrays.asList(f); - Collections.sort(l); - - return Collections.unmodifiableCollection(l); - } - - /* - * Gets the parent item of the specified Item. Don't add a JavaDoc comment - * here, we use the default documentation from implemented interface. - */ - @Override - public Object getParent(Object itemId) { - - if (!(itemId instanceof File)) { - return null; - } - return ((File) itemId).getParentFile(); - } - - /* - * Tests if the specified Item has any children. Don't add a JavaDoc comment - * here, we use the default documentation from implemented interface. - */ - @Override - public boolean hasChildren(Object itemId) { - - if (!(itemId instanceof File)) { - return false; - } - String[] l; - if (filter != null) { - l = ((File) itemId).list(filter); - } else { - l = ((File) itemId).list(); - } - return (l != null) && (l.length > 0); - } - - /* - * Tests if the specified Item is the root of the filesystem. Don't add a - * JavaDoc comment here, we use the default documentation from implemented - * interface. - */ - @Override - public boolean isRoot(Object itemId) { - - if (!(itemId instanceof File)) { - return false; - } - for (int i = 0; i < roots.length; i++) { - if (roots[i].equals(itemId)) { - return true; - } - } - return false; - } - - /* - * Gets the ID's of all root Items in the container. Don't add a JavaDoc - * comment here, we use the default documentation from implemented - * interface. - */ - @Override - public Collection<File> rootItemIds() { - - File[] f; - - // in single root case we use children - if (roots.length == 1) { - if (filter != null) { - f = roots[0].listFiles(filter); - } else { - f = roots[0].listFiles(); - } - } else { - f = roots; - } - - if (f == null) { - return Collections.unmodifiableCollection(new LinkedList<File>()); - } - - final List<File> l = Arrays.asList(f); - Collections.sort(l); - - return Collections.unmodifiableCollection(l); - } - - /** - * Returns <code>false</code> when conversion from files to directories is - * not supported. - * - * @param itemId - * the ID of the item. - * @param areChildrenAllowed - * the boolean value specifying if the Item can have children or - * not. - * @return <code>true</code> if the operaton is successful otherwise - * <code>false</code>. - * @throws UnsupportedOperationException - * if the setChildrenAllowed is not supported. - */ - @Override - public boolean setChildrenAllowed(Object itemId, boolean areChildrenAllowed) - throws UnsupportedOperationException { - - throw new UnsupportedOperationException( - "Conversion file to/from directory is not supported"); - } - - /** - * Returns <code>false</code> when moving files around in the filesystem is - * not supported. - * - * @param itemId - * the ID of the item. - * @param newParentId - * the ID of the Item that's to be the new parent of the Item - * identified with itemId. - * @return <code>true</code> if the operation is successful otherwise - * <code>false</code>. - * @throws UnsupportedOperationException - * if the setParent is not supported. - */ - @Override - public boolean setParent(Object itemId, Object newParentId) - throws UnsupportedOperationException { - - throw new UnsupportedOperationException("File moving is not supported"); - } - - /* - * Tests if the filesystem contains the specified Item. Don't add a JavaDoc - * comment here, we use the default documentation from implemented - * interface. - */ - @Override - public boolean containsId(Object itemId) { - - if (!(itemId instanceof File)) { - return false; - } - boolean val = false; - - // Try to match all roots - for (int i = 0; i < roots.length; i++) { - try { - val |= ((File) itemId).getCanonicalPath().startsWith( - roots[i].getCanonicalPath()); - } catch (final IOException e) { - // Exception ignored - } - - } - if (val && filter != null) { - val &= filter.accept(((File) itemId).getParentFile(), - ((File) itemId).getName()); - } - return val; - } - - /* - * Gets the specified Item from the filesystem. Don't add a JavaDoc comment - * here, we use the default documentation from implemented interface. - */ - @Override - public Item getItem(Object itemId) { - - if (!(itemId instanceof File)) { - return null; - } - return new FileItem((File) itemId); - } - - /** - * Internal recursive method to add the files under the specified directory - * to the collection. - * - * @param col - * the collection where the found items are added - * @param f - * the root file where to start adding files - */ - private void addItemIds(Collection<File> col, File f) { - File[] l; - if (filter != null) { - l = f.listFiles(filter); - } else { - l = f.listFiles(); - } - if (l == null) { - // File.listFiles returns null if File does not exist or if there - // was an IO error (permission denied) - return; - } - final List<File> ll = Arrays.asList(l); - Collections.sort(ll); - - for (final Iterator<File> i = ll.iterator(); i.hasNext();) { - final File lf = i.next(); - col.add(lf); - if (lf.isDirectory()) { - addItemIds(col, lf); - } - } - } - - /* - * Gets the IDs of Items in the filesystem. Don't add a JavaDoc comment - * here, we use the default documentation from implemented interface. - */ - @Override - public Collection<File> getItemIds() { - - if (recursive) { - final Collection<File> col = new ArrayList<File>(); - for (int i = 0; i < roots.length; i++) { - addItemIds(col, roots[i]); - } - return Collections.unmodifiableCollection(col); - } else { - File[] f; - if (roots.length == 1) { - if (filter != null) { - f = roots[0].listFiles(filter); - } else { - f = roots[0].listFiles(); - } - } else { - f = roots; - } - - if (f == null) { - return Collections - .unmodifiableCollection(new LinkedList<File>()); - } - - final List<File> l = Arrays.asList(f); - Collections.sort(l); - return Collections.unmodifiableCollection(l); - } - - } - - /** - * Gets the specified property of the specified file Item. The available - * file properties are "Name", "Size" and "Last Modified". If propertyId is - * not one of those, <code>null</code> is returned. - * - * @param itemId - * the ID of the file whose property is requested. - * @param propertyId - * the property's ID. - * @return the requested property's value, or <code>null</code> - */ - @Override - public Property getContainerProperty(Object itemId, Object propertyId) { - - if (!(itemId instanceof File)) { - return null; - } - - if (propertyId.equals(PROPERTY_NAME)) { - return new MethodProperty<Object>(getType(propertyId), - new FileItem((File) itemId), FILEITEM_NAME, null); - } - - if (propertyId.equals(PROPERTY_ICON)) { - return new MethodProperty<Object>(getType(propertyId), - new FileItem((File) itemId), FILEITEM_ICON, null); - } - - if (propertyId.equals(PROPERTY_SIZE)) { - return new MethodProperty<Object>(getType(propertyId), - new FileItem((File) itemId), FILEITEM_SIZE, null); - } - - if (propertyId.equals(PROPERTY_LASTMODIFIED)) { - return new MethodProperty<Object>(getType(propertyId), - new FileItem((File) itemId), FILEITEM_LASTMODIFIED, null); - } - - return null; - } - - /** - * Gets the collection of available file properties. - * - * @return Unmodifiable collection containing all available file properties. - */ - @Override - public Collection<String> getContainerPropertyIds() { - return FILE_PROPERTIES; - } - - /** - * Gets the specified property's data type. "Name" is a <code>String</code>, - * "Size" is a <code>Long</code>, "Last Modified" is a <code>Date</code>. If - * propertyId is not one of those, <code>null</code> is returned. - * - * @param propertyId - * the ID of the property whose type is requested. - * @return data type of the requested property, or <code>null</code> - */ - @Override - public Class<?> getType(Object propertyId) { - - if (propertyId.equals(PROPERTY_NAME)) { - return String.class; - } - if (propertyId.equals(PROPERTY_ICON)) { - return Resource.class; - } - if (propertyId.equals(PROPERTY_SIZE)) { - return Long.class; - } - if (propertyId.equals(PROPERTY_LASTMODIFIED)) { - return Date.class; - } - return null; - } - - /** - * Internal method to recursively calculate the number of files under a root - * directory. - * - * @param f - * the root to start counting from. - */ - private int getFileCounts(File f) { - File[] l; - if (filter != null) { - l = f.listFiles(filter); - } else { - l = f.listFiles(); - } - - if (l == null) { - return 0; - } - int ret = l.length; - for (int i = 0; i < l.length; i++) { - if (l[i].isDirectory()) { - ret += getFileCounts(l[i]); - } - } - return ret; - } - - /** - * Gets the number of Items in the container. In effect, this is the - * combined amount of files and directories. - * - * @return Number of Items in the container. - */ - @Override - public int size() { - - if (recursive) { - int counts = 0; - for (int i = 0; i < roots.length; i++) { - counts += getFileCounts(roots[i]); - } - return counts; - } else { - File[] f; - if (roots.length == 1) { - if (filter != null) { - f = roots[0].listFiles(filter); - } else { - f = roots[0].listFiles(); - } - } else { - f = roots; - } - - if (f == null) { - return 0; - } - return f.length; - } - } - - /** - * A Item wrapper for files in a filesystem. - * - * @author Vaadin Ltd. - * @since 3.0 - */ - public class FileItem implements Item { - - /** - * The wrapped file. - */ - private final File file; - - /** - * Constructs a FileItem from a existing file. - */ - private FileItem(File file) { - this.file = file; - } - - /* - * Gets the specified property of this file. Don't add a JavaDoc comment - * here, we use the default documentation from implemented interface. - */ - @Override - public Property getItemProperty(Object id) { - return getContainerProperty(file, id); - } - - /* - * Gets the IDs of all properties available for this item Don't add a - * JavaDoc comment here, we use the default documentation from - * implemented interface. - */ - @Override - public Collection<String> getItemPropertyIds() { - return getContainerPropertyIds(); - } - - /** - * Calculates a integer hash-code for the Property that's unique inside - * the Item containing the Property. Two different Properties inside the - * same Item contained in the same list always have different - * hash-codes, though Properties in different Items may have identical - * hash-codes. - * - * @return A locally unique hash-code as integer - */ - @Override - public int hashCode() { - return file.hashCode() ^ FilesystemContainer.this.hashCode(); - } - - /** - * Tests if the given object is the same as the this object. Two - * Properties got from an Item with the same ID are equal. - * - * @param obj - * an object to compare with this object. - * @return <code>true</code> if the given object is the same as this - * object, <code>false</code> if not - */ - @Override - public boolean equals(Object obj) { - if (obj == null || !(obj instanceof FileItem)) { - return false; - } - final FileItem fi = (FileItem) obj; - return fi.getHost() == getHost() && fi.file.equals(file); - } - - /** - * Gets the host of this file. - */ - private FilesystemContainer getHost() { - return FilesystemContainer.this; - } - - /** - * Gets the last modified date of this file. - * - * @return Date - */ - public Date lastModified() { - return new Date(file.lastModified()); - } - - /** - * Gets the name of this file. - * - * @return file name of this file. - */ - public String getName() { - return file.getName(); - } - - /** - * Gets the icon of this file. - * - * @return the icon of this file. - */ - public Resource getIcon() { - return FileTypeResolver.getIcon(file); - } - - /** - * Gets the size of this file. - * - * @return size - */ - public long getSize() { - if (file.isDirectory()) { - return 0; - } - return file.length(); - } - - /** - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - if ("".equals(file.getName())) { - return file.getAbsolutePath(); - } - return file.getName(); - } - - /** - * Filesystem container does not support adding new properties. - * - * @see com.vaadin.data.Item#addItemProperty(Object, Property) - */ - @Override - public boolean addItemProperty(Object id, Property property) - throws UnsupportedOperationException { - throw new UnsupportedOperationException("Filesystem container " - + "does not support adding new properties"); - } - - /** - * Filesystem container does not support removing properties. - * - * @see com.vaadin.data.Item#removeItemProperty(Object) - */ - @Override - public boolean removeItemProperty(Object id) - throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "Filesystem container does not support property removal"); - } - - } - - /** - * Generic file extension filter for displaying only files having certain - * extension. - * - * @author Vaadin Ltd. - * @since 3.0 - */ - public class FileExtensionFilter implements FilenameFilter, Serializable { - - private final String filter; - - /** - * Constructs a new FileExtensionFilter using given extension. - * - * @param fileExtension - * the File extension without the separator (dot). - */ - public FileExtensionFilter(String fileExtension) { - filter = "." + fileExtension; - } - - /** - * Allows only files with the extension and directories. - * - * @see java.io.FilenameFilter#accept(File, String) - */ - @Override - public boolean accept(File dir, String name) { - if (name.endsWith(filter)) { - return true; - } - return new File(dir, name).isDirectory(); - } - - } - - /** - * Returns the file filter used to limit the files in this container. - * - * @return Used filter instance or null if no filter is assigned. - */ - public FilenameFilter getFilter() { - return filter; - } - - /** - * Sets the file filter used to limit the files in this container. - * - * @param filter - * The filter to set. <code>null</code> disables filtering. - */ - public void setFilter(FilenameFilter filter) { - this.filter = filter; - } - - /** - * Sets the file filter used to limit the files in this container. - * - * @param extension - * the Filename extension (w/o separator) to limit the files in - * container. - */ - public void setFilter(String extension) { - filter = new FileExtensionFilter(extension); - } - - /** - * Is this container recursive filesystem. - * - * @return <code>true</code> if container is recursive, <code>false</code> - * otherwise. - */ - public boolean isRecursive() { - return recursive; - } - - /** - * Sets the container recursive property. Set this to false to limit the - * files directly under the root file. - * <p> - * Note : This is meaningful only if the root really is a directory. - * </p> - * - * @param recursive - * the New value for recursive property. - */ - public void setRecursive(boolean recursive) { - this.recursive = recursive; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#addContainerProperty(java.lang.Object, - * java.lang.Class, java.lang.Object) - */ - @Override - public boolean addContainerProperty(Object propertyId, Class<?> type, - Object defaultValue) throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "File system container does not support this operation"); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#addItem() - */ - @Override - public Object addItem() throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "File system container does not support this operation"); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#addItem(java.lang.Object) - */ - @Override - public Item addItem(Object itemId) throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "File system container does not support this operation"); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#removeAllItems() - */ - @Override - public boolean removeAllItems() throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "File system container does not support this operation"); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#removeItem(java.lang.Object) - */ - @Override - public boolean removeItem(Object itemId) - throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "File system container does not support this operation"); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#removeContainerProperty(java.lang.Object ) - */ - @Override - public boolean removeContainerProperty(Object propertyId) - throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "File system container does not support this operation"); - } -} diff --git a/server/src/com/vaadin/data/util/GeneratedPropertyContainer.java b/server/src/com/vaadin/data/util/GeneratedPropertyContainer.java deleted file mode 100644 index 98dd3ed0c1..0000000000 --- a/server/src/com/vaadin/data/util/GeneratedPropertyContainer.java +++ /dev/null @@ -1,769 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; - -import com.google.gwt.thirdparty.guava.common.collect.Sets; -import com.vaadin.data.Container; -import com.vaadin.data.Item; -import com.vaadin.data.Property; -import com.vaadin.data.sort.SortOrder; -import com.vaadin.data.util.filter.UnsupportedFilterException; -import com.vaadin.shared.data.sort.SortDirection; - -/** - * Container wrapper that adds support for generated properties. This container - * only supports adding new generated properties. Adding new normal properties - * should be done for the wrapped container. - * - * <p> - * Removing properties from this container does not remove anything from the - * wrapped container but instead only hides them from the results. These - * properties can be returned to this container by calling - * {@link #addContainerProperty(Object, Class, Object)} with same property id - * which was removed. - * - * <p> - * If wrapped container is Filterable and/or Sortable it should only be handled - * through this container as generated properties need to be handled in a - * specific way when sorting/filtering. - * - * <p> - * Items returned by this container do not support adding or removing - * properties. Generated properties are always read-only. Trying to make them - * editable throws an exception. - * - * @since 7.4 - * @author Vaadin Ltd - */ -public class GeneratedPropertyContainer extends AbstractContainer implements - Container.Indexed, Container.Sortable, Container.Filterable, - Container.PropertySetChangeNotifier, Container.ItemSetChangeNotifier { - - private final Container.Indexed wrappedContainer; - private final Map<Object, PropertyValueGenerator<?>> propertyGenerators; - private final Map<Filter, List<Filter>> activeFilters; - private Sortable sortableContainer = null; - private Filterable filterableContainer = null; - - /* Removed properties which are hidden but not actually removed */ - private final Set<Object> removedProperties = new HashSet<Object>(); - - /** - * Property implementation for generated properties - */ - protected static class GeneratedProperty<T> implements Property<T> { - - private Item item; - private Object itemId; - private Object propertyId; - private PropertyValueGenerator<T> generator; - - public GeneratedProperty(Item item, Object propertyId, Object itemId, - PropertyValueGenerator<T> generator) { - this.item = item; - this.itemId = itemId; - this.propertyId = propertyId; - this.generator = generator; - } - - @Override - public T getValue() { - return generator.getValue(item, itemId, propertyId); - } - - @Override - public void setValue(T newValue) throws ReadOnlyException { - throw new ReadOnlyException("Generated properties are read only"); - } - - @Override - public Class<? extends T> getType() { - return generator.getType(); - } - - @Override - public boolean isReadOnly() { - return true; - } - - @Override - public void setReadOnly(boolean newStatus) { - if (newStatus) { - // No-op - return; - } - throw new UnsupportedOperationException( - "Generated properties are read only"); - } - } - - /** - * Item implementation for generated properties. - */ - protected class GeneratedPropertyItem implements Item { - - private Item wrappedItem; - private Object itemId; - - protected GeneratedPropertyItem(Object itemId, Item item) { - this.itemId = itemId; - wrappedItem = item; - } - - @Override - public Property getItemProperty(Object id) { - if (propertyGenerators.containsKey(id)) { - return createProperty(wrappedItem, id, itemId, - propertyGenerators.get(id)); - } - return wrappedItem.getItemProperty(id); - } - - @Override - public Collection<?> getItemPropertyIds() { - Set<?> wrappedProperties = asSet(wrappedItem.getItemPropertyIds()); - return Sets.union( - Sets.difference(wrappedProperties, removedProperties), - propertyGenerators.keySet()); - } - - @Override - public boolean addItemProperty(Object id, Property property) - throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "GeneratedPropertyItem does not support adding properties"); - } - - @Override - public boolean removeItemProperty(Object id) - throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "GeneratedPropertyItem does not support removing properties"); - } - - /** - * Tests if the given object is the same as the this object. Two Items - * from the same container with the same ID are equal. - * - * @param obj - * an object to compare with this object - * @return <code>true</code> if the given object is the same as this - * object, <code>false</code> if not - */ - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - - if (obj == null - || !obj.getClass().equals(GeneratedPropertyItem.class)) { - return false; - } - final GeneratedPropertyItem li = (GeneratedPropertyItem) obj; - return getContainer() == li.getContainer() - && itemId.equals(li.itemId); - } - - @Override - public int hashCode() { - return itemId.hashCode(); - } - - private GeneratedPropertyContainer getContainer() { - return GeneratedPropertyContainer.this; - } - }; - - /** - * Base implementation for item add or remove events. This is used when an - * event is fired from wrapped container and needs to be reconstructed to - * act like it actually came from this container. - */ - protected abstract class GeneratedItemAddOrRemoveEvent implements - Serializable { - - private Object firstItemId; - private int firstIndex; - private int count; - - protected GeneratedItemAddOrRemoveEvent(Object itemId, int first, - int count) { - firstItemId = itemId; - firstIndex = first; - this.count = count; - } - - public Container getContainer() { - return GeneratedPropertyContainer.this; - } - - public Object getFirstItemId() { - return firstItemId; - } - - public int getFirstIndex() { - return firstIndex; - } - - public int getAffectedItemsCount() { - return count; - } - }; - - protected class GeneratedItemRemoveEvent extends - GeneratedItemAddOrRemoveEvent implements ItemRemoveEvent { - - protected GeneratedItemRemoveEvent(ItemRemoveEvent event) { - super(event.getFirstItemId(), event.getFirstIndex(), event - .getRemovedItemsCount()); - } - - @Override - public int getRemovedItemsCount() { - return super.getAffectedItemsCount(); - } - } - - protected class GeneratedItemAddEvent extends GeneratedItemAddOrRemoveEvent - implements ItemAddEvent { - - protected GeneratedItemAddEvent(ItemAddEvent event) { - super(event.getFirstItemId(), event.getFirstIndex(), event - .getAddedItemsCount()); - } - - @Override - public int getAddedItemsCount() { - return super.getAffectedItemsCount(); - } - - } - - /** - * Constructor for GeneratedPropertyContainer. - * - * @param container - * underlying indexed container - */ - public GeneratedPropertyContainer(Container.Indexed container) { - wrappedContainer = container; - propertyGenerators = new HashMap<Object, PropertyValueGenerator<?>>(); - - if (wrappedContainer instanceof Sortable) { - sortableContainer = (Sortable) wrappedContainer; - } - - if (wrappedContainer instanceof Filterable) { - activeFilters = new HashMap<Filter, List<Filter>>(); - filterableContainer = (Filterable) wrappedContainer; - } else { - activeFilters = null; - } - - // ItemSetChangeEvents - if (wrappedContainer instanceof ItemSetChangeNotifier) { - ((ItemSetChangeNotifier) wrappedContainer) - .addItemSetChangeListener(new ItemSetChangeListener() { - - @Override - public void containerItemSetChange( - ItemSetChangeEvent event) { - if (event instanceof ItemAddEvent) { - final ItemAddEvent addEvent = (ItemAddEvent) event; - fireItemSetChange(new GeneratedItemAddEvent( - addEvent)); - } else if (event instanceof ItemRemoveEvent) { - final ItemRemoveEvent removeEvent = (ItemRemoveEvent) event; - fireItemSetChange(new GeneratedItemRemoveEvent( - removeEvent)); - } else { - fireItemSetChange(); - } - } - }); - } - - // PropertySetChangeEvents - if (wrappedContainer instanceof PropertySetChangeNotifier) { - ((PropertySetChangeNotifier) wrappedContainer) - .addPropertySetChangeListener(new PropertySetChangeListener() { - - @Override - public void containerPropertySetChange( - PropertySetChangeEvent event) { - fireContainerPropertySetChange(); - } - }); - } - } - - /* Functions related to generated properties */ - - /** - * Add a new PropertyValueGenerator with given property id. This will - * override any existing properties with the same property id. Fires a - * PropertySetChangeEvent. - * - * @param propertyId - * property id - * @param generator - * a property value generator - */ - public void addGeneratedProperty(Object propertyId, - PropertyValueGenerator<?> generator) { - propertyGenerators.put(propertyId, generator); - fireContainerPropertySetChange(); - } - - /** - * Removes any possible PropertyValueGenerator with given property id. Fires - * a PropertySetChangeEvent. - * - * @param propertyId - * property id - */ - public void removeGeneratedProperty(Object propertyId) { - if (propertyGenerators.containsKey(propertyId)) { - propertyGenerators.remove(propertyId); - fireContainerPropertySetChange(); - } - } - - private Item createGeneratedPropertyItem(final Object itemId, - final Item item) { - return new GeneratedPropertyItem(itemId, item); - } - - private <T> Property<T> createProperty(final Item item, - final Object propertyId, final Object itemId, - final PropertyValueGenerator<T> generator) { - return new GeneratedProperty<T>(item, propertyId, itemId, generator); - } - - private static <T> LinkedHashSet<T> asSet(Collection<T> collection) { - if (collection instanceof LinkedHashSet) { - return (LinkedHashSet<T>) collection; - } else { - return new LinkedHashSet<T>(collection); - } - } - - /* Listener functionality */ - - @Override - public void addItemSetChangeListener(ItemSetChangeListener listener) { - super.addItemSetChangeListener(listener); - } - - @Override - public void addListener(ItemSetChangeListener listener) { - super.addListener(listener); - } - - @Override - public void removeItemSetChangeListener(ItemSetChangeListener listener) { - super.removeItemSetChangeListener(listener); - } - - @Override - public void removeListener(ItemSetChangeListener listener) { - super.removeListener(listener); - } - - @Override - public void addPropertySetChangeListener(PropertySetChangeListener listener) { - super.addPropertySetChangeListener(listener); - } - - @Override - public void addListener(PropertySetChangeListener listener) { - super.addListener(listener); - } - - @Override - public void removePropertySetChangeListener( - PropertySetChangeListener listener) { - super.removePropertySetChangeListener(listener); - } - - @Override - public void removeListener(PropertySetChangeListener listener) { - super.removeListener(listener); - } - - /* Filtering functionality */ - - @Override - public void addContainerFilter(Filter filter) - throws UnsupportedFilterException { - if (filterableContainer == null) { - throw new UnsupportedOperationException( - "Wrapped container is not filterable"); - } - - List<Filter> addedFilters = new ArrayList<Filter>(); - for (Entry<?, PropertyValueGenerator<?>> entry : propertyGenerators - .entrySet()) { - Object property = entry.getKey(); - if (filter.appliesToProperty(property)) { - // Have generated property modify filter to fit the original - // data in the container. - Filter modifiedFilter = entry.getValue().modifyFilter(filter); - filterableContainer.addContainerFilter(modifiedFilter); - // Keep track of added filters - addedFilters.add(modifiedFilter); - } - } - - if (addedFilters.isEmpty()) { - // No generated property modified this filter, use it as is - addedFilters.add(filter); - filterableContainer.addContainerFilter(filter); - } - // Map filter to actually added filters - activeFilters.put(filter, addedFilters); - } - - @Override - public void removeContainerFilter(Filter filter) { - if (filterableContainer == null) { - throw new UnsupportedOperationException( - "Wrapped container is not filterable"); - } - - if (activeFilters.containsKey(filter)) { - for (Filter f : activeFilters.get(filter)) { - filterableContainer.removeContainerFilter(f); - } - activeFilters.remove(filter); - } - } - - @Override - public void removeAllContainerFilters() { - if (filterableContainer == null) { - throw new UnsupportedOperationException( - "Wrapped container is not filterable"); - } - filterableContainer.removeAllContainerFilters(); - activeFilters.clear(); - } - - @Override - public Collection<Filter> getContainerFilters() { - if (filterableContainer == null) { - throw new UnsupportedOperationException( - "Wrapped container is not filterable"); - } - return Collections.unmodifiableSet(activeFilters.keySet()); - } - - /* Sorting functionality */ - - @Override - public void sort(Object[] propertyId, boolean[] ascending) { - if (sortableContainer == null) { - throw new UnsupportedOperationException( - "Wrapped container is not Sortable"); - } - - if (propertyId.length == 0) { - sortableContainer.sort(propertyId, ascending); - return; - } - - List<Object> actualSortProperties = new ArrayList<Object>(); - List<Boolean> actualSortDirections = new ArrayList<Boolean>(); - - for (int i = 0; i < propertyId.length; ++i) { - Object property = propertyId[i]; - SortDirection direction; - boolean isAscending = i < ascending.length ? ascending[i] : true; - if (isAscending) { - direction = SortDirection.ASCENDING; - } else { - direction = SortDirection.DESCENDING; - } - - if (propertyGenerators.containsKey(property)) { - // Sorting by a generated property. Generated property should - // modify sort orders to work with original properties in the - // container. - for (SortOrder s : propertyGenerators.get(property) - .getSortProperties(new SortOrder(property, direction))) { - actualSortProperties.add(s.getPropertyId()); - actualSortDirections - .add(s.getDirection() == SortDirection.ASCENDING); - } - } else { - actualSortProperties.add(property); - actualSortDirections.add(isAscending); - } - } - - boolean[] actualAscending = new boolean[actualSortDirections.size()]; - for (int i = 0; i < actualAscending.length; ++i) { - actualAscending[i] = actualSortDirections.get(i); - } - - sortableContainer.sort(actualSortProperties.toArray(), actualAscending); - } - - @Override - public Collection<?> getSortableContainerPropertyIds() { - if (sortableContainer == null) { - return Collections.emptySet(); - } - - Set<Object> sortablePropertySet = new HashSet<Object>( - sortableContainer.getSortableContainerPropertyIds()); - for (Entry<?, PropertyValueGenerator<?>> entry : propertyGenerators - .entrySet()) { - Object property = entry.getKey(); - SortOrder order = new SortOrder(property, SortDirection.ASCENDING); - if (entry.getValue().getSortProperties(order).length > 0) { - sortablePropertySet.add(property); - } else { - sortablePropertySet.remove(property); - } - } - - return sortablePropertySet; - } - - /* Item related overrides */ - - @Override - public Item addItemAfter(Object previousItemId, Object newItemId) - throws UnsupportedOperationException { - Item item = wrappedContainer.addItemAfter(previousItemId, newItemId); - if (item == null) { - return null; - } - return createGeneratedPropertyItem(newItemId, item); - } - - @Override - public Item addItem(Object itemId) throws UnsupportedOperationException { - Item item = wrappedContainer.addItem(itemId); - if (item == null) { - return null; - } - return createGeneratedPropertyItem(itemId, item); - } - - @Override - public Item addItemAt(int index, Object newItemId) - throws UnsupportedOperationException { - Item item = wrappedContainer.addItemAt(index, newItemId); - if (item == null) { - return null; - } - return createGeneratedPropertyItem(newItemId, item); - } - - @Override - public Item getItem(Object itemId) { - Item item = wrappedContainer.getItem(itemId); - if (item == null) { - return null; - } - - return createGeneratedPropertyItem(itemId, item); - } - - /* Property related overrides */ - - @Override - public Property<?> getContainerProperty(Object itemId, Object propertyId) { - if (propertyGenerators.keySet().contains(propertyId)) { - return getItem(itemId).getItemProperty(propertyId); - } else if (!removedProperties.contains(propertyId)) { - return wrappedContainer.getContainerProperty(itemId, propertyId); - } - return null; - } - - /** - * Returns a list of propety ids available in this container. This - * collection will contain properties for generated properties. Removed - * properties will not show unless there is a generated property overriding - * those. - */ - @Override - public Collection<?> getContainerPropertyIds() { - Set<?> wrappedProperties = asSet(wrappedContainer - .getContainerPropertyIds()); - return Sets.union( - Sets.difference(wrappedProperties, removedProperties), - propertyGenerators.keySet()); - } - - /** - * Adds a previously removed property back to GeneratedPropertyContainer. - * Adding a property that is not previously removed causes an - * UnsupportedOperationException. - */ - @Override - public boolean addContainerProperty(Object propertyId, Class<?> type, - Object defaultValue) throws UnsupportedOperationException { - if (!removedProperties.contains(propertyId)) { - throw new UnsupportedOperationException( - "GeneratedPropertyContainer does not support adding properties."); - } - removedProperties.remove(propertyId); - fireContainerPropertySetChange(); - return true; - } - - /** - * Marks the given property as hidden. This property from wrapped container - * will be removed from {@link #getContainerPropertyIds()} and is no longer - * be available in Items retrieved from this container. - */ - @Override - public boolean removeContainerProperty(Object propertyId) - throws UnsupportedOperationException { - if (wrappedContainer.getContainerPropertyIds().contains(propertyId) - && removedProperties.add(propertyId)) { - fireContainerPropertySetChange(); - return true; - } - return false; - } - - /* Type related overrides */ - - @Override - public Class<?> getType(Object propertyId) { - if (propertyGenerators.containsKey(propertyId)) { - return propertyGenerators.get(propertyId).getType(); - } else { - return wrappedContainer.getType(propertyId); - } - } - - /* Unmodified functions */ - - @Override - public Object nextItemId(Object itemId) { - return wrappedContainer.nextItemId(itemId); - } - - @Override - public Object prevItemId(Object itemId) { - return wrappedContainer.prevItemId(itemId); - } - - @Override - public Object firstItemId() { - return wrappedContainer.firstItemId(); - } - - @Override - public Object lastItemId() { - return wrappedContainer.lastItemId(); - } - - @Override - public boolean isFirstId(Object itemId) { - return wrappedContainer.isFirstId(itemId); - } - - @Override - public boolean isLastId(Object itemId) { - return wrappedContainer.isLastId(itemId); - } - - @Override - public Object addItemAfter(Object previousItemId) - throws UnsupportedOperationException { - return wrappedContainer.addItemAfter(previousItemId); - } - - @Override - public Collection<?> getItemIds() { - return wrappedContainer.getItemIds(); - } - - @Override - public int size() { - return wrappedContainer.size(); - } - - @Override - public boolean containsId(Object itemId) { - return wrappedContainer.containsId(itemId); - } - - @Override - public Object addItem() throws UnsupportedOperationException { - return wrappedContainer.addItem(); - } - - @Override - public boolean removeItem(Object itemId) - throws UnsupportedOperationException { - return wrappedContainer.removeItem(itemId); - } - - @Override - public boolean removeAllItems() throws UnsupportedOperationException { - return wrappedContainer.removeAllItems(); - } - - @Override - public int indexOfId(Object itemId) { - return wrappedContainer.indexOfId(itemId); - } - - @Override - public Object getIdByIndex(int index) { - return wrappedContainer.getIdByIndex(index); - } - - @Override - public List<?> getItemIds(int startIndex, int numberOfItems) { - return wrappedContainer.getItemIds(startIndex, numberOfItems); - } - - @Override - public Object addItemAt(int index) throws UnsupportedOperationException { - return wrappedContainer.addItemAt(index); - } - - /** - * Returns the original underlying container. - * - * @return the original underlying container - */ - public Container.Indexed getWrappedContainer() { - return wrappedContainer; - } -} diff --git a/server/src/com/vaadin/data/util/HierarchicalContainer.java b/server/src/com/vaadin/data/util/HierarchicalContainer.java deleted file mode 100644 index 2235e77b34..0000000000 --- a/server/src/com/vaadin/data/util/HierarchicalContainer.java +++ /dev/null @@ -1,859 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.LinkedList; -import java.util.Set; -import java.util.logging.Level; -import java.util.logging.Logger; - -import com.vaadin.data.Container; -import com.vaadin.data.Item; - -/** - * A specialized Container whose contents can be accessed like it was a - * tree-like structure. - * - * @author Vaadin Ltd. - * @since 3.0 - */ -@SuppressWarnings("serial") -public class HierarchicalContainer extends IndexedContainer implements - Container.Hierarchical { - - /** - * Set of IDs of those contained Items that can't have children. - */ - private final HashSet<Object> noChildrenAllowed = new HashSet<Object>(); - - /** - * Mapping from Item ID to parent Item ID. - */ - private final HashMap<Object, Object> parent = new HashMap<Object, Object>(); - - /** - * Mapping from Item ID to parent Item ID for items included in the filtered - * container. - */ - private HashMap<Object, Object> filteredParent = null; - - /** - * Mapping from Item ID to a list of child IDs. - */ - private final HashMap<Object, LinkedList<Object>> children = new HashMap<Object, LinkedList<Object>>(); - - /** - * Mapping from Item ID to a list of child IDs when filtered - */ - private HashMap<Object, LinkedList<Object>> filteredChildren = null; - - /** - * List that contains all root elements of the container. - */ - private final LinkedList<Object> roots = new LinkedList<Object>(); - - /** - * List that contains all filtered root elements of the container. - */ - private LinkedList<Object> filteredRoots = null; - - /** - * Determines how filtering of the container is done. - */ - private boolean includeParentsWhenFiltering = true; - - /** - * Counts how many nested contents change disable calls are in progress. - * - * Pending events are only fired when the counter reaches zero again. - */ - private int contentChangedEventsDisabledCount = 0; - - private boolean contentsChangedEventPending; - - /* - * Can the specified Item have any children? Don't add a JavaDoc comment - * here, we use the default documentation from implemented interface. - */ - @Override - public boolean areChildrenAllowed(Object itemId) { - if (noChildrenAllowed.contains(itemId)) { - return false; - } - return containsId(itemId); - } - - /* - * Gets the IDs of the children of the specified Item. Don't add a JavaDoc - * comment here, we use the default documentation from implemented - * interface. - */ - @Override - public Collection<?> getChildren(Object itemId) { - LinkedList<Object> c; - - if (filteredChildren != null) { - c = filteredChildren.get(itemId); - } else { - c = children.get(itemId); - } - - if (c == null) { - return null; - } - return Collections.unmodifiableCollection(c); - } - - /* - * Gets the ID of the parent of the specified Item. Don't add a JavaDoc - * comment here, we use the default documentation from implemented - * interface. - */ - @Override - public Object getParent(Object itemId) { - if (filteredParent != null) { - return filteredParent.get(itemId); - } - return parent.get(itemId); - } - - /* - * Is the Item corresponding to the given ID a leaf node? Don't add a - * JavaDoc comment here, we use the default documentation from implemented - * interface. - */ - @Override - public boolean hasChildren(Object itemId) { - if (filteredChildren != null) { - return filteredChildren.containsKey(itemId); - } else { - return children.containsKey(itemId); - } - } - - /* - * Is the Item corresponding to the given ID a root node? Don't add a - * JavaDoc comment here, we use the default documentation from implemented - * interface. - */ - @Override - public boolean isRoot(Object itemId) { - // If the container is filtered the itemId must be among filteredRoots - // to be a root. - if (filteredRoots != null) { - if (!filteredRoots.contains(itemId)) { - return false; - } - } else { - // Container is not filtered - if (parent.containsKey(itemId)) { - return false; - } - } - - return containsId(itemId); - } - - /* - * Gets the IDs of the root elements in the container. Don't add a JavaDoc - * comment here, we use the default documentation from implemented - * interface. - */ - @Override - public Collection<?> rootItemIds() { - if (filteredRoots != null) { - return Collections.unmodifiableCollection(filteredRoots); - } else { - return Collections.unmodifiableCollection(roots); - } - } - - /** - * <p> - * Sets the given Item's capability to have children. If the Item identified - * with the itemId already has children and the areChildrenAllowed is false - * this method fails and <code>false</code> is returned; the children must - * be first explicitly removed with - * {@link #setParent(Object itemId, Object newParentId)} or - * {@link com.vaadin.data.Container#removeItem(Object itemId)}. - * </p> - * - * @param itemId - * the ID of the Item in the container whose child capability is - * to be set. - * @param childrenAllowed - * the boolean value specifying if the Item can have children or - * not. - * @return <code>true</code> if the operation succeeded, <code>false</code> - * if not - */ - @Override - public boolean setChildrenAllowed(Object itemId, boolean childrenAllowed) { - - // Checks that the item is in the container - if (!containsId(itemId)) { - return false; - } - - // Updates status - if (childrenAllowed) { - noChildrenAllowed.remove(itemId); - } else { - noChildrenAllowed.add(itemId); - } - - return true; - } - - /** - * <p> - * Sets the parent of an Item. The new parent item must exist and be able to - * have children. (<code>canHaveChildren(newParentId) == true</code>). It is - * also possible to detach a node from the hierarchy (and thus make it root) - * by setting the parent <code>null</code>. - * </p> - * - * @param itemId - * the ID of the item to be set as the child of the Item - * identified with newParentId. - * @param newParentId - * the ID of the Item that's to be the new parent of the Item - * identified with itemId. - * @return <code>true</code> if the operation succeeded, <code>false</code> - * if not - */ - @Override - public boolean setParent(Object itemId, Object newParentId) { - - // Checks that the item is in the container - if (!containsId(itemId)) { - return false; - } - - // Gets the old parent - final Object oldParentId = parent.get(itemId); - - // Checks if no change is necessary - if ((newParentId == null && oldParentId == null) - || ((newParentId != null) && newParentId.equals(oldParentId))) { - return true; - } - - // Making root? - if (newParentId == null) { - // The itemId should become a root so we need to - // - Remove it from the old parent's children list - // - Add it as a root - // - Remove it from the item -> parent list (parent is null for - // roots) - - // Removes from old parents children list - final LinkedList<Object> l = children.get(oldParentId); - if (l != null) { - l.remove(itemId); - if (l.isEmpty()) { - children.remove(oldParentId); - } - - } - - // Add to be a root - roots.add(itemId); - - // Updates parent - parent.remove(itemId); - - if (hasFilters()) { - // Refilter the container if setParent is called when filters - // are applied. Changing parent can change what is included in - // the filtered version (if includeParentsWhenFiltering==true). - doFilterContainer(hasFilters()); - } - - fireItemSetChange(); - - return true; - } - - // We get here when the item should not become a root and we need to - // - Verify the new parent exists and can have children - // - Check that the new parent is not a child of the selected itemId - // - Updated the item -> parent mapping to point to the new parent - // - Remove the item from the roots list if it was a root - // - Remove the item from the old parent's children list if it was not a - // root - - // Checks that the new parent exists in container and can have - // children - if (!containsId(newParentId) || noChildrenAllowed.contains(newParentId)) { - return false; - } - - // Checks that setting parent doesn't result to a loop - Object o = newParentId; - while (o != null && !o.equals(itemId)) { - o = parent.get(o); - } - if (o != null) { - return false; - } - - // Updates parent - parent.put(itemId, newParentId); - LinkedList<Object> pcl = children.get(newParentId); - if (pcl == null) { - // Create an empty list for holding children if one were not - // previously created - pcl = new LinkedList<Object>(); - children.put(newParentId, pcl); - } - pcl.add(itemId); - - // Removes from old parent or root - if (oldParentId == null) { - roots.remove(itemId); - } else { - final LinkedList<Object> l = children.get(oldParentId); - if (l != null) { - l.remove(itemId); - if (l.isEmpty()) { - children.remove(oldParentId); - } - } - } - - if (hasFilters()) { - // Refilter the container if setParent is called when filters - // are applied. Changing parent can change what is included in - // the filtered version (if includeParentsWhenFiltering==true). - doFilterContainer(hasFilters()); - } - - fireItemSetChange(); - - return true; - } - - private boolean hasFilters() { - return (filteredRoots != null); - } - - /** - * Moves a node (an Item) in the container immediately after a sibling node. - * The two nodes must have the same parent in the container. - * - * @param itemId - * the identifier of the moved node (Item) - * @param siblingId - * the identifier of the reference node (Item), after which the - * other node will be located - */ - public void moveAfterSibling(Object itemId, Object siblingId) { - Object parent2 = getParent(itemId); - LinkedList<Object> childrenList; - if (parent2 == null) { - childrenList = roots; - } else { - childrenList = children.get(parent2); - } - if (siblingId == null) { - childrenList.remove(itemId); - childrenList.addFirst(itemId); - - } else { - int oldIndex = childrenList.indexOf(itemId); - int indexOfSibling = childrenList.indexOf(siblingId); - if (indexOfSibling != -1 && oldIndex != -1) { - int newIndex; - if (oldIndex > indexOfSibling) { - newIndex = indexOfSibling + 1; - } else { - newIndex = indexOfSibling; - } - childrenList.remove(oldIndex); - childrenList.add(newIndex, itemId); - } else { - throw new IllegalArgumentException( - "Given identifiers no not have the same parent."); - } - } - fireItemSetChange(); - - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.IndexedContainer#addItem() - */ - @Override - public Object addItem() { - disableContentsChangeEvents(); - try { - final Object itemId = super.addItem(); - if (itemId == null) { - return null; - } - - if (!roots.contains(itemId)) { - roots.add(itemId); - if (filteredRoots != null) { - if (passesFilters(itemId)) { - filteredRoots.add(itemId); - } - } - } - return itemId; - } finally { - enableAndFireContentsChangeEvents(); - } - } - - @Override - protected void fireItemSetChange( - com.vaadin.data.Container.ItemSetChangeEvent event) { - if (contentsChangeEventsOn()) { - super.fireItemSetChange(event); - } else { - contentsChangedEventPending = true; - } - } - - private boolean contentsChangeEventsOn() { - return contentChangedEventsDisabledCount == 0; - } - - private void disableContentsChangeEvents() { - contentChangedEventsDisabledCount++; - } - - private void enableAndFireContentsChangeEvents() { - if (contentChangedEventsDisabledCount <= 0) { - getLogger() - .log(Level.WARNING, - "Mismatched calls to disable and enable contents change events in HierarchicalContainer"); - contentChangedEventsDisabledCount = 0; - } else { - contentChangedEventsDisabledCount--; - } - if (contentChangedEventsDisabledCount == 0) { - if (contentsChangedEventPending) { - fireItemSetChange(); - } - contentsChangedEventPending = false; - } - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.IndexedContainer#addItem(java.lang.Object) - */ - @Override - public Item addItem(Object itemId) { - disableContentsChangeEvents(); - try { - final Item item = super.addItem(itemId); - if (item == null) { - return null; - } - - roots.add(itemId); - - if (filteredRoots != null) { - if (passesFilters(itemId)) { - filteredRoots.add(itemId); - } - } - return item; - } finally { - enableAndFireContentsChangeEvents(); - } - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.IndexedContainer#removeAllItems() - */ - @Override - public boolean removeAllItems() { - disableContentsChangeEvents(); - try { - final boolean success = super.removeAllItems(); - - if (success) { - roots.clear(); - parent.clear(); - children.clear(); - noChildrenAllowed.clear(); - if (filteredRoots != null) { - filteredRoots = null; - } - if (filteredChildren != null) { - filteredChildren = null; - } - if (filteredParent != null) { - filteredParent = null; - } - } - return success; - } finally { - enableAndFireContentsChangeEvents(); - } - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.IndexedContainer#removeItem(java.lang.Object ) - */ - @Override - public boolean removeItem(Object itemId) { - disableContentsChangeEvents(); - try { - final boolean success = super.removeItem(itemId); - - if (success) { - // Remove from roots if this was a root - if (roots.remove(itemId)) { - - // If filtering is enabled we might need to remove it from - // the filtered list also - if (filteredRoots != null) { - filteredRoots.remove(itemId); - } - } - - // Clear the children list. Old children will now become root - // nodes - LinkedList<Object> childNodeIds = children.remove(itemId); - if (childNodeIds != null) { - if (filteredChildren != null) { - filteredChildren.remove(itemId); - } - for (Object childId : childNodeIds) { - setParent(childId, null); - } - } - - // Parent of the item that we are removing will contain the item - // id in its children list - final Object parentItemId = parent.get(itemId); - if (parentItemId != null) { - final LinkedList<Object> c = children.get(parentItemId); - if (c != null) { - c.remove(itemId); - - if (c.isEmpty()) { - children.remove(parentItemId); - } - - // Found in the children list so might also be in the - // filteredChildren list - if (filteredChildren != null) { - LinkedList<Object> f = filteredChildren - .get(parentItemId); - if (f != null) { - f.remove(itemId); - if (f.isEmpty()) { - filteredChildren.remove(parentItemId); - } - } - } - } - } - parent.remove(itemId); - if (filteredParent != null) { - // Item id no longer has a parent as the item id is not in - // the container. - filteredParent.remove(itemId); - } - noChildrenAllowed.remove(itemId); - } - - return success; - } finally { - enableAndFireContentsChangeEvents(); - } - } - - /** - * Removes the Item identified by given itemId and all its children. - * - * @see #removeItem(Object) - * @param itemId - * the identifier of the Item to be removed - * @return true if the operation succeeded - */ - public boolean removeItemRecursively(Object itemId) { - disableContentsChangeEvents(); - try { - boolean removeItemRecursively = removeItemRecursively(this, itemId); - return removeItemRecursively; - } finally { - enableAndFireContentsChangeEvents(); - } - } - - /** - * Removes the Item identified by given itemId and all its children from the - * given Container. - * - * @param container - * the container where the item is to be removed - * @param itemId - * the identifier of the Item to be removed - * @return true if the operation succeeded - */ - public static boolean removeItemRecursively( - Container.Hierarchical container, Object itemId) { - boolean success = true; - Collection<?> children2 = container.getChildren(itemId); - if (children2 != null) { - Object[] array = children2.toArray(); - for (int i = 0; i < array.length; i++) { - boolean removeItemRecursively = removeItemRecursively( - container, array[i]); - if (!removeItemRecursively) { - success = false; - } - } - } - // remove the root of subtree if children where succesfully removed - if (success) { - success = container.removeItem(itemId); - } - return success; - - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.IndexedContainer#doSort() - */ - @Override - protected void doSort() { - super.doSort(); - - Collections.sort(roots, getItemSorter()); - for (LinkedList<Object> childList : children.values()) { - Collections.sort(childList, getItemSorter()); - } - } - - /** - * Used to control how filtering works. @see - * {@link #setIncludeParentsWhenFiltering(boolean)} for more information. - * - * @return true if all parents for items that match the filter are included - * when filtering, false if only the matching items are included - */ - public boolean isIncludeParentsWhenFiltering() { - return includeParentsWhenFiltering; - } - - /** - * Controls how the filtering of the container works. Set this to true to - * make filtering include parents for all matched items in addition to the - * items themselves. Setting this to false causes the filtering to only - * include the matching items and make items with excluded parents into root - * items. - * - * @param includeParentsWhenFiltering - * true to include all parents for items that match the filter, - * false to only include the matching items - */ - public void setIncludeParentsWhenFiltering( - boolean includeParentsWhenFiltering) { - this.includeParentsWhenFiltering = includeParentsWhenFiltering; - if (filteredRoots != null) { - // Currently filtered so needs to be re-filtered - doFilterContainer(true); - } - } - - /* - * Overridden to provide filtering for root & children items. - * - * (non-Javadoc) - * - * @see com.vaadin.data.util.IndexedContainer#updateContainerFiltering() - */ - @Override - protected boolean doFilterContainer(boolean hasFilters) { - if (!hasFilters) { - // All filters removed - filteredRoots = null; - filteredChildren = null; - filteredParent = null; - - return super.doFilterContainer(hasFilters); - } - - // Reset data structures - filteredRoots = new LinkedList<Object>(); - filteredChildren = new HashMap<Object, LinkedList<Object>>(); - filteredParent = new HashMap<Object, Object>(); - - if (includeParentsWhenFiltering) { - // Filter so that parents for items that match the filter are also - // included - HashSet<Object> includedItems = new HashSet<Object>(); - for (Object rootId : roots) { - if (filterIncludingParents(rootId, includedItems)) { - filteredRoots.add(rootId); - addFilteredChildrenRecursively(rootId, includedItems); - } - } - // includedItemIds now contains all the item ids that should be - // included. Filter IndexedContainer based on this - filterOverride = includedItems; - super.doFilterContainer(hasFilters); - filterOverride = null; - - return true; - } else { - // Filter by including all items that pass the filter and make items - // with no parent new root items - - // Filter IndexedContainer first so getItemIds return the items that - // match - super.doFilterContainer(hasFilters); - - LinkedHashSet<Object> filteredItemIds = new LinkedHashSet<Object>( - getItemIds()); - - for (Object itemId : filteredItemIds) { - Object itemParent = parent.get(itemId); - if (itemParent == null || !filteredItemIds.contains(itemParent)) { - // Parent is not included or this was a root, in both cases - // this should be a filtered root - filteredRoots.add(itemId); - } else { - // Parent is included. Add this to the children list (create - // it first if necessary) - addFilteredChild(itemParent, itemId); - } - } - - return true; - } - } - - /** - * Adds the given childItemId as a filteredChildren for the parentItemId and - * sets it filteredParent. - * - * @param parentItemId - * @param childItemId - */ - private void addFilteredChild(Object parentItemId, Object childItemId) { - LinkedList<Object> parentToChildrenList = filteredChildren - .get(parentItemId); - if (parentToChildrenList == null) { - parentToChildrenList = new LinkedList<Object>(); - filteredChildren.put(parentItemId, parentToChildrenList); - } - filteredParent.put(childItemId, parentItemId); - parentToChildrenList.add(childItemId); - - } - - /** - * Recursively adds all items in the includedItems list to the - * filteredChildren map in the same order as they are in the children map. - * Starts from parentItemId and recurses down as long as child items that - * should be included are found. - * - * @param parentItemId - * The item id to start recurse from. Not added to a - * filteredChildren list - * @param includedItems - * Set containing the item ids for the items that should be - * included in the filteredChildren map - */ - private void addFilteredChildrenRecursively(Object parentItemId, - HashSet<Object> includedItems) { - LinkedList<Object> childList = children.get(parentItemId); - if (childList == null) { - return; - } - - for (Object childItemId : childList) { - if (includedItems.contains(childItemId)) { - addFilteredChild(parentItemId, childItemId); - addFilteredChildrenRecursively(childItemId, includedItems); - } - } - } - - /** - * Scans the itemId and all its children for which items should be included - * when filtering. All items which passes the filters are included. - * Additionally all items that have a child node that should be included are - * also themselves included. - * - * @param itemId - * @param includedItems - * @return true if the itemId should be included in the filtered container. - */ - private boolean filterIncludingParents(Object itemId, - HashSet<Object> includedItems) { - boolean toBeIncluded = passesFilters(itemId); - - LinkedList<Object> childList = children.get(itemId); - if (childList != null) { - for (Object childItemId : children.get(itemId)) { - toBeIncluded |= filterIncludingParents(childItemId, - includedItems); - } - } - - if (toBeIncluded) { - includedItems.add(itemId); - } - return toBeIncluded; - } - - private Set<Object> filterOverride = null; - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.IndexedContainer#passesFilters(java.lang.Object) - */ - @Override - protected boolean passesFilters(Object itemId) { - if (filterOverride != null) { - return filterOverride.contains(itemId); - } else { - return super.passesFilters(itemId); - } - } - - private static final Logger getLogger() { - return Logger.getLogger(HierarchicalContainer.class.getName()); - } -} diff --git a/server/src/com/vaadin/data/util/HierarchicalContainerOrderedWrapper.java b/server/src/com/vaadin/data/util/HierarchicalContainerOrderedWrapper.java deleted file mode 100644 index 1c7960f954..0000000000 --- a/server/src/com/vaadin/data/util/HierarchicalContainerOrderedWrapper.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import java.util.Collection; - -import com.vaadin.data.Container.Hierarchical; - -/** - * A wrapper class for adding external ordering to containers not implementing - * the {@link com.vaadin.data.Container.Ordered} interface while retaining - * {@link Hierarchical} features. - * - * @see ContainerOrderedWrapper - */ -@SuppressWarnings({ "serial" }) -public class HierarchicalContainerOrderedWrapper extends - ContainerOrderedWrapper implements Hierarchical { - - private Hierarchical hierarchical; - - public HierarchicalContainerOrderedWrapper(Hierarchical toBeWrapped) { - super(toBeWrapped); - hierarchical = toBeWrapped; - } - - @Override - public boolean areChildrenAllowed(Object itemId) { - return hierarchical.areChildrenAllowed(itemId); - } - - @Override - public Collection<?> getChildren(Object itemId) { - return hierarchical.getChildren(itemId); - } - - @Override - public Object getParent(Object itemId) { - return hierarchical.getParent(itemId); - } - - @Override - public boolean hasChildren(Object itemId) { - return hierarchical.hasChildren(itemId); - } - - @Override - public boolean isRoot(Object itemId) { - return hierarchical.isRoot(itemId); - } - - @Override - public Collection<?> rootItemIds() { - return hierarchical.rootItemIds(); - } - - @Override - public boolean setChildrenAllowed(Object itemId, boolean areChildrenAllowed) - throws UnsupportedOperationException { - return hierarchical.setChildrenAllowed(itemId, areChildrenAllowed); - } - - @Override - public boolean setParent(Object itemId, Object newParentId) - throws UnsupportedOperationException { - return hierarchical.setParent(itemId, newParentId); - } - -} diff --git a/server/src/com/vaadin/data/util/IndexedContainer.java b/server/src/com/vaadin/data/util/IndexedContainer.java deleted file mode 100644 index b851baf674..0000000000 --- a/server/src/com/vaadin/data/util/IndexedContainer.java +++ /dev/null @@ -1,1218 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.EventObject; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Hashtable; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.logging.Logger; - -import com.vaadin.data.Container; -import com.vaadin.data.Item; -import com.vaadin.data.Property; -import com.vaadin.data.util.filter.SimpleStringFilter; -import com.vaadin.data.util.filter.UnsupportedFilterException; - -/** - * An implementation of the <code>{@link Container.Indexed}</code> interface - * with all important features.</p> - * - * Features: - * <ul> - * <li> {@link Container.Indexed} - * <li> {@link Container.Ordered} - * <li> {@link Container.Sortable} - * <li> {@link Container.Filterable} - * <li> {@link Cloneable} (deprecated, might be removed in the future) - * <li>Sends all needed events on content changes. - * </ul> - * - * @see com.vaadin.data.Container - * - * @author Vaadin Ltd. - * @since 3.0 - */ - -@SuppressWarnings("serial") -// item type is really IndexedContainerItem, but using Item not to show it in -// public API -public class IndexedContainer extends - AbstractInMemoryContainer<Object, Object, Item> implements - Container.PropertySetChangeNotifier, Property.ValueChangeNotifier, - Container.Sortable, Cloneable, Container.Filterable, - Container.SimpleFilterable { - - /* Internal structure */ - - /** - * Linked list of ordered Property IDs. - */ - private ArrayList<Object> propertyIds = new ArrayList<Object>(); - - /** - * Property ID to type mapping. - */ - private Hashtable<Object, Class<?>> types = new Hashtable<Object, Class<?>>(); - - /** - * Hash of Items, where each Item is implemented as a mapping from Property - * ID to Property value. - */ - private Hashtable<Object, Map<Object, Object>> items = new Hashtable<Object, Map<Object, Object>>(); - - /** - * Set of properties that are read-only. - */ - private HashSet<Property<?>> readOnlyProperties = new HashSet<Property<?>>(); - - /** - * List of all Property value change event listeners listening all the - * properties. - */ - private LinkedList<Property.ValueChangeListener> propertyValueChangeListeners = null; - - /** - * Data structure containing all listeners interested in changes to single - * Properties. The data structure is a hashtable mapping Property IDs to a - * hashtable that maps Item IDs to a linked list of listeners listening - * Property identified by given Property ID and Item ID. - */ - private Hashtable<Object, Map<Object, List<Property.ValueChangeListener>>> singlePropertyValueChangeListeners = null; - - private HashMap<Object, Object> defaultPropertyValues; - - private int nextGeneratedItemId = 1; - - /* Container constructors */ - - public IndexedContainer() { - super(); - } - - public IndexedContainer(Collection<?> itemIds) { - this(); - if (items != null) { - for (final Iterator<?> i = itemIds.iterator(); i.hasNext();) { - Object itemId = i.next(); - internalAddItemAtEnd(itemId, new IndexedContainerItem(itemId), - false); - } - filterAll(); - } - } - - /* Container methods */ - - @Override - protected Item getUnfilteredItem(Object itemId) { - if (itemId != null && items.containsKey(itemId)) { - return new IndexedContainerItem(itemId); - } - return null; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#getContainerPropertyIds() - */ - @Override - public Collection<?> getContainerPropertyIds() { - return Collections.unmodifiableCollection(propertyIds); - } - - /** - * Gets the type of a Property stored in the list. - * - * @param id - * the ID of the Property. - * @return Type of the requested Property - */ - @Override - public Class<?> getType(Object propertyId) { - return types.get(propertyId); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#getContainerProperty(java.lang.Object, - * java.lang.Object) - */ - @Override - public Property getContainerProperty(Object itemId, Object propertyId) { - // map lookup more efficient than propertyIds if there are many - // properties - if (!containsId(itemId) || propertyId == null - || !types.containsKey(propertyId)) { - return null; - } - - return new IndexedContainerProperty(itemId, propertyId); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#addContainerProperty(java.lang.Object, - * java.lang.Class, java.lang.Object) - */ - @Override - public boolean addContainerProperty(Object propertyId, Class<?> type, - Object defaultValue) { - - // Fails, if nulls are given - if (propertyId == null || type == null) { - return false; - } - - // Fails if the Property is already present - if (propertyIds.contains(propertyId)) { - return false; - } - - // Adds the Property to Property list and types - propertyIds.add(propertyId); - types.put(propertyId, type); - - // If default value is given, set it - if (defaultValue != null) { - // for existing rows - for (final Iterator<?> i = getAllItemIds().iterator(); i.hasNext();) { - getItem(i.next()).getItemProperty(propertyId).setValue( - defaultValue); - } - // store for next rows - if (defaultPropertyValues == null) { - defaultPropertyValues = new HashMap<Object, Object>(); - } - defaultPropertyValues.put(propertyId, defaultValue); - } - - // Sends a change event - fireContainerPropertySetChange(); - - return true; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#removeAllItems() - */ - @Override - public boolean removeAllItems() { - int origSize = size(); - Object firstItem = getFirstVisibleItem(); - - internalRemoveAllItems(); - - items.clear(); - - // fire event only if the visible view changed, regardless of whether - // filtered out items were removed or not - if (origSize != 0) { - // Sends a change event - fireItemsRemoved(0, firstItem, origSize); - } - - return true; - } - - /** - * {@inheritDoc} - * <p> - * The item ID is generated from a sequence of Integers. The id of the first - * added item is 1. - */ - @Override - public Object addItem() { - - // Creates a new id - final Object id = generateId(); - - // Adds the Item into container - addItem(id); - - return id; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#addItem(java.lang.Object) - */ - @Override - public Item addItem(Object itemId) { - Item item = internalAddItemAtEnd(itemId, new IndexedContainerItem( - itemId), false); - if (!isFiltered()) { - // always the last item - fireItemAdded(size() - 1, itemId, item); - } else if (passesFilters(itemId) && !containsId(itemId)) { - getFilteredItemIds().add(itemId); - // always the last item - fireItemAdded(size() - 1, itemId, item); - } - return item; - } - - /** - * Helper method to add default values for items if available - * - * @param t - * data table of added item - */ - private void addDefaultValues(Hashtable<Object, Object> t) { - if (defaultPropertyValues != null) { - for (Object key : defaultPropertyValues.keySet()) { - t.put(key, defaultPropertyValues.get(key)); - } - } - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#removeItem(java.lang.Object) - */ - @Override - public boolean removeItem(Object itemId) { - if (itemId == null || items.remove(itemId) == null) { - return false; - } - int origSize = size(); - int position = indexOfId(itemId); - if (internalRemoveItem(itemId)) { - // fire event only if the visible view changed, regardless of - // whether filtered out items were removed or not - if (size() != origSize) { - fireItemRemoved(position, itemId); - } - - return true; - } else { - return false; - } - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#removeContainerProperty(java.lang.Object ) - */ - @Override - public boolean removeContainerProperty(Object propertyId) { - - // Fails if the Property is not present - if (!propertyIds.contains(propertyId)) { - return false; - } - - // Removes the Property to Property list and types - propertyIds.remove(propertyId); - types.remove(propertyId); - if (defaultPropertyValues != null) { - defaultPropertyValues.remove(propertyId); - } - - // If remove the Property from all Items - for (final Iterator<Object> i = getAllItemIds().iterator(); i.hasNext();) { - items.get(i.next()).remove(propertyId); - } - - // Sends a change event - fireContainerPropertySetChange(); - - return true; - } - - /* Container.Ordered methods */ - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Ordered#addItemAfter(java.lang.Object, - * java.lang.Object) - */ - @Override - public Item addItemAfter(Object previousItemId, Object newItemId) { - return internalAddItemAfter(previousItemId, newItemId, - new IndexedContainerItem(newItemId), true); - } - - /** - * {@inheritDoc} - * <p> - * The item ID is generated from a sequence of Integers. The id of the first - * added item is 1. - */ - @Override - public Object addItemAfter(Object previousItemId) { - - // Creates a new id - final Object id = generateId(); - - if (addItemAfter(previousItemId, id) != null) { - return id; - } else { - return null; - } - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Indexed#addItemAt(int, java.lang.Object) - */ - @Override - public Item addItemAt(int index, Object newItemId) { - return internalAddItemAt(index, newItemId, new IndexedContainerItem( - newItemId), true); - } - - /** - * {@inheritDoc} - * <p> - * The item ID is generated from a sequence of Integers. The id of the first - * added item is 1. - */ - @Override - public Object addItemAt(int index) { - - // Creates a new id - final Object id = generateId(); - - // Adds the Item into container - addItemAt(index, id); - - return id; - } - - /** - * Generates an unique identifier for use as an item id. Guarantees that the - * generated id is not currently used as an id. - * - * @return - */ - private Serializable generateId() { - Serializable id; - do { - id = Integer.valueOf(nextGeneratedItemId++); - } while (items.containsKey(id)); - - return id; - } - - @Override - protected void registerNewItem(int index, Object newItemId, Item item) { - Hashtable<Object, Object> t = new Hashtable<Object, Object>(); - items.put(newItemId, t); - addDefaultValues(t); - } - - /* Event notifiers */ - - /** - * An <code>event</code> object specifying the list whose Item set has - * changed. - * - * @author Vaadin Ltd. - * @since 3.0 - */ - public static class ItemSetChangeEvent extends BaseItemSetChangeEvent { - - private final int addedItemIndex; - - private ItemSetChangeEvent(IndexedContainer source, int addedItemIndex) { - super(source); - this.addedItemIndex = addedItemIndex; - } - - /** - * Iff one item is added, gives its index. - * - * @return -1 if either multiple items are changed or some other change - * than add is done. - */ - public int getAddedItemIndex() { - return addedItemIndex; - } - - } - - /** - * An <code>event</code> object specifying the Property in a list whose - * value has changed. - * - * @author Vaadin Ltd. - * @since 3.0 - */ - private static class PropertyValueChangeEvent extends EventObject implements - Property.ValueChangeEvent, Serializable { - - private PropertyValueChangeEvent(Property source) { - super(source); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Property.ValueChangeEvent#getProperty() - */ - @Override - public Property getProperty() { - return (Property) getSource(); - } - - } - - @Override - public void addPropertySetChangeListener( - Container.PropertySetChangeListener listener) { - super.addPropertySetChangeListener(listener); - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #addPropertySetChangeListener(com.vaadin.data.Container.PropertySetChangeListener)} - **/ - @Deprecated - @Override - public void addListener(Container.PropertySetChangeListener listener) { - addPropertySetChangeListener(listener); - } - - @Override - public void removePropertySetChangeListener( - Container.PropertySetChangeListener listener) { - super.removePropertySetChangeListener(listener); - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #removePropertySetChangeListener(com.vaadin.data.Container.PropertySetChangeListener)} - **/ - @Deprecated - @Override - public void removeListener(Container.PropertySetChangeListener listener) { - removePropertySetChangeListener(listener); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Property.ValueChangeNotifier#addListener(com. - * vaadin.data.Property.ValueChangeListener) - */ - @Override - public void addValueChangeListener(Property.ValueChangeListener listener) { - if (propertyValueChangeListeners == null) { - propertyValueChangeListeners = new LinkedList<Property.ValueChangeListener>(); - } - propertyValueChangeListeners.add(listener); - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #addValueChangeListener(com.vaadin.data.Property.ValueChangeListener)} - **/ - @Override - @Deprecated - public void addListener(Property.ValueChangeListener listener) { - addValueChangeListener(listener); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Property.ValueChangeNotifier#removeListener(com - * .vaadin.data.Property.ValueChangeListener) - */ - @Override - public void removeValueChangeListener(Property.ValueChangeListener listener) { - if (propertyValueChangeListeners != null) { - propertyValueChangeListeners.remove(listener); - } - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #removeValueChangeListener(com.vaadin.data.Property.ValueChangeListener)} - **/ - @Override - @Deprecated - public void removeListener(Property.ValueChangeListener listener) { - removeValueChangeListener(listener); - } - - /** - * Sends a Property value change event to all interested listeners. - * - * @param source - * the IndexedContainerProperty object. - */ - private void firePropertyValueChange(IndexedContainerProperty source) { - - // Sends event to listeners listening all value changes - if (propertyValueChangeListeners != null) { - final Object[] l = propertyValueChangeListeners.toArray(); - final Property.ValueChangeEvent event = new IndexedContainer.PropertyValueChangeEvent( - source); - for (int i = 0; i < l.length; i++) { - ((Property.ValueChangeListener) l[i]).valueChange(event); - } - } - - // Sends event to single property value change listeners - if (singlePropertyValueChangeListeners != null) { - final Map<Object, List<Property.ValueChangeListener>> propertySetToListenerListMap = singlePropertyValueChangeListeners - .get(source.propertyId); - if (propertySetToListenerListMap != null) { - final List<Property.ValueChangeListener> listenerList = propertySetToListenerListMap - .get(source.itemId); - if (listenerList != null) { - final Property.ValueChangeEvent event = new IndexedContainer.PropertyValueChangeEvent( - source); - Object[] listeners = listenerList.toArray(); - for (int i = 0; i < listeners.length; i++) { - ((Property.ValueChangeListener) listeners[i]) - .valueChange(event); - } - } - } - } - - } - - @Override - public Collection<?> getListeners(Class<?> eventType) { - if (Property.ValueChangeEvent.class.isAssignableFrom(eventType)) { - if (propertyValueChangeListeners == null) { - return Collections.EMPTY_LIST; - } else { - return Collections - .unmodifiableCollection(propertyValueChangeListeners); - } - } - return super.getListeners(eventType); - } - - @Override - protected void fireItemAdded(int position, Object itemId, Item item) { - if (position >= 0) { - super.fireItemAdded(position, itemId, item); - } - } - - @Override - protected void fireItemSetChange() { - fireItemSetChange(new IndexedContainer.ItemSetChangeEvent(this, -1)); - } - - /** - * Adds new single Property change listener. - * - * @param propertyId - * the ID of the Property to add. - * @param itemId - * the ID of the Item . - * @param listener - * the listener to be added. - */ - private void addSinglePropertyChangeListener(Object propertyId, - Object itemId, Property.ValueChangeListener listener) { - if (listener != null) { - if (singlePropertyValueChangeListeners == null) { - singlePropertyValueChangeListeners = new Hashtable<Object, Map<Object, List<Property.ValueChangeListener>>>(); - } - Map<Object, List<Property.ValueChangeListener>> propertySetToListenerListMap = singlePropertyValueChangeListeners - .get(propertyId); - if (propertySetToListenerListMap == null) { - propertySetToListenerListMap = new Hashtable<Object, List<Property.ValueChangeListener>>(); - singlePropertyValueChangeListeners.put(propertyId, - propertySetToListenerListMap); - } - List<Property.ValueChangeListener> listenerList = propertySetToListenerListMap - .get(itemId); - if (listenerList == null) { - listenerList = new LinkedList<Property.ValueChangeListener>(); - propertySetToListenerListMap.put(itemId, listenerList); - } - listenerList.add(listener); - } - } - - /** - * Removes a previously registered single Property change listener. - * - * @param propertyId - * the ID of the Property to remove. - * @param itemId - * the ID of the Item. - * @param listener - * the listener to be removed. - */ - private void removeSinglePropertyChangeListener(Object propertyId, - Object itemId, Property.ValueChangeListener listener) { - if (listener != null && singlePropertyValueChangeListeners != null) { - final Map<Object, List<Property.ValueChangeListener>> propertySetToListenerListMap = singlePropertyValueChangeListeners - .get(propertyId); - if (propertySetToListenerListMap != null) { - final List<Property.ValueChangeListener> listenerList = propertySetToListenerListMap - .get(itemId); - if (listenerList != null) { - listenerList.remove(listener); - if (listenerList.isEmpty()) { - propertySetToListenerListMap.remove(itemId); - } - } - if (propertySetToListenerListMap.isEmpty()) { - singlePropertyValueChangeListeners.remove(propertyId); - } - } - if (singlePropertyValueChangeListeners.isEmpty()) { - singlePropertyValueChangeListeners = null; - } - } - } - - /* Internal Item and Property implementations */ - - /* - * A class implementing the com.vaadin.data.Item interface to be contained - * in the list. - * - * @author Vaadin Ltd. - * - * - * @since 3.0 - */ - class IndexedContainerItem implements Item { - - /** - * Item ID in the host container for this Item. - */ - private final Object itemId; - - /** - * Constructs a new ListItem instance and connects it to a host - * container. - * - * @param itemId - * the Item ID of the new Item. - */ - private IndexedContainerItem(Object itemId) { - - // Gets the item contents from the host - if (itemId == null) { - throw new NullPointerException(); - } - this.itemId = itemId; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Item#getItemProperty(java.lang.Object) - */ - @Override - public Property getItemProperty(Object id) { - return new IndexedContainerProperty(itemId, id); - } - - @Override - public Collection<?> getItemPropertyIds() { - return Collections.unmodifiableCollection(propertyIds); - } - - /** - * Gets the <code>String</code> representation of the contents of the - * Item. The format of the string is a space separated catenation of the - * <code>String</code> representations of the values of the Properties - * contained by the Item. - * - * @return <code>String</code> representation of the Item contents - */ - @Override - public String toString() { - String retValue = ""; - - for (final Iterator<?> i = propertyIds.iterator(); i.hasNext();) { - final Object propertyId = i.next(); - retValue += getItemProperty(propertyId).getValue(); - if (i.hasNext()) { - retValue += " "; - } - } - - return retValue; - } - - /** - * Calculates a integer hash-code for the Item that's unique inside the - * list. Two Items inside the same list have always different - * hash-codes, though Items in different lists may have identical - * hash-codes. - * - * @return A locally unique hash-code as integer - */ - @Override - public int hashCode() { - return itemId.hashCode(); - } - - /** - * Tests if the given object is the same as the this object. Two Items - * got from a list container with the same ID are equal. - * - * @param obj - * an object to compare with this object - * @return <code>true</code> if the given object is the same as this - * object, <code>false</code> if not - */ - @Override - public boolean equals(Object obj) { - if (obj == null - || !obj.getClass().equals(IndexedContainerItem.class)) { - return false; - } - final IndexedContainerItem li = (IndexedContainerItem) obj; - return getHost() == li.getHost() && itemId.equals(li.itemId); - } - - private IndexedContainer getHost() { - return IndexedContainer.this; - } - - /** - * IndexedContainerItem does not support adding new properties. Add - * properties at container level. See - * {@link IndexedContainer#addContainerProperty(Object, Class, Object)} - * - * @see com.vaadin.data.Item#addProperty(Object, Property) - */ - @Override - public boolean addItemProperty(Object id, Property property) - throws UnsupportedOperationException { - throw new UnsupportedOperationException("Indexed container item " - + "does not support adding new properties"); - } - - /** - * Indexed container does not support removing properties. Remove - * properties at container level. See - * {@link IndexedContainer#removeContainerProperty(Object)} - * - * @see com.vaadin.data.Item#removeProperty(Object) - */ - @Override - public boolean removeItemProperty(Object id) - throws UnsupportedOperationException { - throw new UnsupportedOperationException( - "Indexed container item does not support property removal"); - } - - } - - /** - * A class implementing the {@link Property} interface to be contained in - * the {@link IndexedContainerItem} contained in the - * {@link IndexedContainer}. - * - * @author Vaadin Ltd. - * - * @since 3.0 - */ - private class IndexedContainerProperty<T> implements Property<T>, - Property.ValueChangeNotifier { - - /** - * ID of the Item, where this property resides. - */ - private final Object itemId; - - /** - * Id of the Property. - */ - private final Object propertyId; - - /** - * Constructs a new {@link IndexedContainerProperty} object. - * - * @param itemId - * the ID of the Item to connect the new Property to. - * @param propertyId - * the Property ID of the new Property. - * @param host - * the list that contains the Item to contain the new - * Property. - */ - private IndexedContainerProperty(Object itemId, Object propertyId) { - if (itemId == null || propertyId == null) { - // Null ids are not accepted - throw new NullPointerException( - "Container item or property ids can not be null"); - } - this.propertyId = propertyId; - this.itemId = itemId; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Property#getType() - */ - @Override - public Class<T> getType() { - return (Class<T>) types.get(propertyId); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Property#getValue() - */ - @Override - public T getValue() { - return (T) items.get(itemId).get(propertyId); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Property#isReadOnly() - */ - @Override - public boolean isReadOnly() { - return readOnlyProperties.contains(this); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Property#setReadOnly(boolean) - */ - @Override - public void setReadOnly(boolean newStatus) { - if (newStatus) { - readOnlyProperties.add(this); - } else { - readOnlyProperties.remove(this); - } - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Property#setValue(java.lang.Object) - */ - @Override - public void setValue(Object newValue) throws Property.ReadOnlyException { - // Gets the Property set - final Map<Object, Object> propertySet = items.get(itemId); - - // Support null values on all types - if (newValue == null) { - propertySet.remove(propertyId); - } else if (getType().isAssignableFrom(newValue.getClass())) { - propertySet.put(propertyId, newValue); - } else { - throw new IllegalArgumentException( - "Value is of invalid type, got " - + newValue.getClass().getName() + " but " - + getType().getName() + " was expected"); - } - - // update the container filtering if this property is being filtered - if (isPropertyFiltered(propertyId)) { - filterAll(); - } - - firePropertyValueChange(this); - } - - /** - * Returns a string representation of this object. The returned string - * representation depends on if the legacy Property toString mode is - * enabled or disabled. - * <p> - * If legacy Property toString mode is enabled, returns the value of the - * <code>Property</code> converted to a String. - * </p> - * <p> - * If legacy Property toString mode is disabled, the string - * representation has no special meaning - * </p> - * - * @return A string representation of the value value stored in the - * Property or a string representation of the Property object. - * @deprecated As of 7.0. To get the property value, use - * {@link #getValue()} instead (and possibly toString on - * that) - */ - @Deprecated - @Override - public String toString() { - if (!LegacyPropertyHelper.isLegacyToStringEnabled()) { - return super.toString(); - } else { - return LegacyPropertyHelper.legacyPropertyToString(this); - } - } - - private Logger getLogger() { - return Logger.getLogger(IndexedContainerProperty.class.getName()); - } - - /** - * Calculates a integer hash-code for the Property that's unique inside - * the Item containing the Property. Two different Properties inside the - * same Item contained in the same list always have different - * hash-codes, though Properties in different Items may have identical - * hash-codes. - * - * @return A locally unique hash-code as integer - */ - @Override - public int hashCode() { - return itemId.hashCode() ^ propertyId.hashCode(); - } - - /** - * Tests if the given object is the same as the this object. Two - * Properties got from an Item with the same ID are equal. - * - * @param obj - * an object to compare with this object - * @return <code>true</code> if the given object is the same as this - * object, <code>false</code> if not - */ - @Override - public boolean equals(Object obj) { - if (obj == null - || !obj.getClass().equals(IndexedContainerProperty.class)) { - return false; - } - final IndexedContainerProperty lp = (IndexedContainerProperty) obj; - return lp.getHost() == getHost() - && lp.propertyId.equals(propertyId) - && lp.itemId.equals(itemId); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Property.ValueChangeNotifier#addListener( - * com.vaadin.data.Property.ValueChangeListener) - */ - @Override - public void addValueChangeListener(Property.ValueChangeListener listener) { - addSinglePropertyChangeListener(propertyId, itemId, listener); - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #addValueChangeListener(com.vaadin.data.Property.ValueChangeListener)} - **/ - @Override - @Deprecated - public void addListener(Property.ValueChangeListener listener) { - addValueChangeListener(listener); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Property.ValueChangeNotifier#removeListener - * (com.vaadin.data.Property.ValueChangeListener) - */ - @Override - public void removeValueChangeListener( - Property.ValueChangeListener listener) { - removeSinglePropertyChangeListener(propertyId, itemId, listener); - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #removeValueChangeListener(com.vaadin.data.Property.ValueChangeListener)} - **/ - @Override - @Deprecated - public void removeListener(Property.ValueChangeListener listener) { - removeValueChangeListener(listener); - } - - private IndexedContainer getHost() { - return IndexedContainer.this; - } - - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Sortable#sort(java.lang.Object[], - * boolean[]) - */ - @Override - public void sort(Object[] propertyId, boolean[] ascending) { - sortContainer(propertyId, ascending); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Sortable#getSortableContainerPropertyIds - * () - */ - @Override - public Collection<?> getSortableContainerPropertyIds() { - return getSortablePropertyIds(); - } - - @Override - public ItemSorter getItemSorter() { - return super.getItemSorter(); - } - - @Override - public void setItemSorter(ItemSorter itemSorter) { - super.setItemSorter(itemSorter); - } - - /** - * Supports cloning of the IndexedContainer cleanly. - * - * @throws CloneNotSupportedException - * if an object cannot be cloned. . - * - * @deprecated As of 6.6. Cloning support might be removed from - * IndexedContainer in the future - */ - @Deprecated - @Override - public Object clone() throws CloneNotSupportedException { - - // Creates the clone - final IndexedContainer nc = new IndexedContainer(); - - // Clone the shallow properties - nc.setAllItemIds(getAllItemIds() != null ? (ListSet<Object>) ((ListSet<Object>) getAllItemIds()) - .clone() : null); - nc.setItemSetChangeListeners(getItemSetChangeListeners() != null ? new LinkedList<Container.ItemSetChangeListener>( - getItemSetChangeListeners()) : null); - nc.propertyIds = propertyIds != null ? (ArrayList<Object>) propertyIds - .clone() : null; - nc.setPropertySetChangeListeners(getPropertySetChangeListeners() != null ? new LinkedList<Container.PropertySetChangeListener>( - getPropertySetChangeListeners()) : null); - nc.propertyValueChangeListeners = propertyValueChangeListeners != null ? (LinkedList<Property.ValueChangeListener>) propertyValueChangeListeners - .clone() : null; - nc.readOnlyProperties = readOnlyProperties != null ? (HashSet<Property<?>>) readOnlyProperties - .clone() : null; - nc.singlePropertyValueChangeListeners = singlePropertyValueChangeListeners != null ? (Hashtable<Object, Map<Object, List<Property.ValueChangeListener>>>) singlePropertyValueChangeListeners - .clone() : null; - - nc.types = types != null ? (Hashtable<Object, Class<?>>) types.clone() - : null; - - nc.setFilters((HashSet<Filter>) ((HashSet<Filter>) getFilters()) - .clone()); - - nc.setFilteredItemIds(getFilteredItemIds() == null ? null - : (ListSet<Object>) ((ListSet<Object>) getFilteredItemIds()) - .clone()); - - // Clone property-values - if (items == null) { - nc.items = null; - } else { - nc.items = new Hashtable<Object, Map<Object, Object>>(); - for (final Iterator<?> i = items.keySet().iterator(); i.hasNext();) { - final Object id = i.next(); - final Hashtable<Object, Object> it = (Hashtable<Object, Object>) items - .get(id); - nc.items.put(id, (Map<Object, Object>) it.clone()); - } - } - - return nc; - } - - @Override - public void addContainerFilter(Object propertyId, String filterString, - boolean ignoreCase, boolean onlyMatchPrefix) { - try { - addFilter(new SimpleStringFilter(propertyId, filterString, - ignoreCase, onlyMatchPrefix)); - } catch (UnsupportedFilterException e) { - // the filter instance created here is always valid for in-memory - // containers - } - } - - @Override - public void removeAllContainerFilters() { - removeAllFilters(); - } - - @Override - public void removeContainerFilters(Object propertyId) { - removeFilters(propertyId); - } - - @Override - public void addContainerFilter(Filter filter) - throws UnsupportedFilterException { - addFilter(filter); - } - - @Override - public void removeContainerFilter(Filter filter) { - removeFilter(filter); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.AbstractInMemoryContainer#getContainerFilters() - */ - @Override - public boolean hasContainerFilters() { - return super.hasContainerFilters(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.AbstractInMemoryContainer#getContainerFilters() - */ - @Override - public Collection<Filter> getContainerFilters() { - return super.getContainerFilters(); - } - -} diff --git a/server/src/com/vaadin/data/util/ItemSorter.java b/server/src/com/vaadin/data/util/ItemSorter.java deleted file mode 100644 index 141f84ad43..0000000000 --- a/server/src/com/vaadin/data/util/ItemSorter.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import java.io.Serializable; -import java.util.Comparator; - -import com.vaadin.data.Container; -import com.vaadin.data.Container.Sortable; - -/** - * An item comparator which is compatible with the {@link Sortable} interface. - * The <code>ItemSorter</code> interface can be used in <code>Sortable</code> - * implementations to provide a custom sorting method. - */ -public interface ItemSorter extends Comparator<Object>, Cloneable, Serializable { - - /** - * Sets the parameters for an upcoming sort operation. The parameters - * determine what container to sort and how the <code>ItemSorter</code> - * sorts the container. - * - * @param container - * The container that will be sorted. The container must contain - * the propertyIds given in the <code>propertyId</code> - * parameter. - * @param propertyId - * The property ids used for sorting. The property ids must exist - * in the container and should only be used if they are also - * sortable, i.e include in the collection returned by - * <code>container.getSortableContainerPropertyIds()</code>. See - * {@link Sortable#sort(Object[], boolean[])} for more - * information. - * @param ascending - * Sorting order flags for each property id. See - * {@link Sortable#sort(Object[], boolean[])} for more - * information. - */ - void setSortProperties(Container.Sortable container, Object[] propertyId, - boolean[] ascending); - - /** - * Compares its two arguments for order. Returns a negative integer, zero, - * or a positive integer as the first argument is less than, equal to, or - * greater than the second. - * <p> - * The parameters for the <code>ItemSorter</code> <code>compare()</code> - * method must always be item ids which exist in the container set using - * {@link #setSortProperties(Sortable, Object[], boolean[])}. - * - * @see Comparator#compare(Object, Object) - */ - @Override - int compare(Object itemId1, Object itemId2); - -} diff --git a/server/src/com/vaadin/data/util/LegacyPropertyHelper.java b/server/src/com/vaadin/data/util/LegacyPropertyHelper.java deleted file mode 100644 index 7432d036fb..0000000000 --- a/server/src/com/vaadin/data/util/LegacyPropertyHelper.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import java.io.Serializable; -import java.util.logging.Level; -import java.util.logging.Logger; - -import com.vaadin.data.Property; -import com.vaadin.server.Constants; -import com.vaadin.server.DeploymentConfiguration.LegacyProperyToStringMode; -import com.vaadin.server.VaadinService; - -/** - * Helper class which provides methods for handling Property.toString in a - * Vaadin 6 compatible way - * - * @author Vaadin Ltd - * @since 7.1 - * @deprecated This is only used internally for backwards compatibility - */ -@Deprecated -public class LegacyPropertyHelper implements Serializable { - - /** - * Returns the property value converted to a String. - * - * @param p - * The property - * @return A string representation of the property value, compatible with - * how Property implementations in Vaadin 6 do it - */ - public static String legacyPropertyToString(Property p) { - maybeLogLegacyPropertyToStringWarning(p); - Object value = p.getValue(); - if (value == null) { - return null; - } - return value.toString(); - } - - public static void maybeLogLegacyPropertyToStringWarning(Property p) { - if (!logLegacyToStringWarning()) { - return; - } - - getLogger().log(Level.WARNING, - Constants.WARNING_LEGACY_PROPERTY_TOSTRING, - p.getClass().getName()); - if (getLogger().isLoggable(Level.FINE)) { - getLogger().log(Level.FINE, - "Strack trace for legacy toString to ease debugging", - new Throwable()); - } - } - - /** - * Checks if legacy Property.toString() implementation is enabled. The - * legacy Property.toString() will return the value of the property somehow - * converted to a String. If the legacy mode is disabled, toString() will - * return super.toString(). - * <p> - * The legacy toString mode can be toggled using the - * "legacyPropertyToString" init parameter - * </p> - * - * @return true if legacy Property.toString() mode is enabled, false - * otherwise - */ - public static boolean isLegacyToStringEnabled() { - if (VaadinService.getCurrent() == null) { - // This will happen at least in JUnit tests. We do not what the real - // value should be but it seems more safe to use the legacy mode. - return true; - } - return VaadinService.getCurrent().getDeploymentConfiguration() - .getLegacyPropertyToStringMode().useLegacyMode(); - } - - private static boolean logLegacyToStringWarning() { - if (VaadinService.getCurrent() == null) { - // This will happen at least in JUnit tests. We do not want to spam - // the log with these messages in this case. - return false; - } - return VaadinService.getCurrent().getDeploymentConfiguration() - .getLegacyPropertyToStringMode() == LegacyProperyToStringMode.WARNING; - - } - - private static Logger getLogger() { - return Logger.getLogger(LegacyPropertyHelper.class.getName()); - } - -} diff --git a/server/src/com/vaadin/data/util/ListSet.java b/server/src/com/vaadin/data/util/ListSet.java deleted file mode 100644 index ccc9e0dbfd..0000000000 --- a/server/src/com/vaadin/data/util/ListSet.java +++ /dev/null @@ -1,276 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; - -/** - * ListSet is an internal Vaadin class which implements a combination of a List - * and a Set. The main purpose of this class is to provide a list with a fast - * {@link #contains(Object)} method. Each inserted object must by unique (as - * specified by {@link #equals(Object)}). The {@link #set(int, Object)} method - * allows duplicates because of the way {@link Collections#sort(java.util.List)} - * works. - * - * This class is subject to change and should not be used outside Vaadin core. - */ -public class ListSet<E> extends ArrayList<E> { - private HashSet<E> itemSet = null; - - /** - * Contains a map from an element to the number of duplicates it has. Used - * to temporarily allow duplicates in the list. - */ - private HashMap<E, Integer> duplicates = new HashMap<E, Integer>(); - - public ListSet() { - super(); - itemSet = new HashSet<E>(); - } - - public ListSet(Collection<? extends E> c) { - super(c); - itemSet = new HashSet<E>(c.size()); - itemSet.addAll(c); - } - - public ListSet(int initialCapacity) { - super(initialCapacity); - itemSet = new HashSet<E>(initialCapacity); - } - - // Delegate contains operations to the set - @Override - public boolean contains(Object o) { - return itemSet.contains(o); - } - - @Override - public boolean containsAll(Collection<?> c) { - return itemSet.containsAll(c); - } - - // Methods for updating the set when the list is updated. - @Override - public boolean add(E e) { - if (contains(e)) { - // Duplicates are not allowed - return false; - } - - if (super.add(e)) { - itemSet.add(e); - return true; - } else { - return false; - } - } - - /** - * Works as java.util.ArrayList#add(int, java.lang.Object) but returns - * immediately if the element is already in the ListSet. - */ - @Override - public void add(int index, E element) { - if (contains(element)) { - // Duplicates are not allowed - return; - } - - super.add(index, element); - itemSet.add(element); - } - - @Override - public boolean addAll(Collection<? extends E> c) { - boolean modified = false; - Iterator<? extends E> i = c.iterator(); - while (i.hasNext()) { - E e = i.next(); - if (contains(e)) { - continue; - } - - if (add(e)) { - itemSet.add(e); - modified = true; - } - } - return modified; - } - - @Override - public boolean addAll(int index, Collection<? extends E> c) { - ensureCapacity(size() + c.size()); - - boolean modified = false; - Iterator<? extends E> i = c.iterator(); - while (i.hasNext()) { - E e = i.next(); - if (contains(e)) { - continue; - } - - add(index++, e); - itemSet.add(e); - modified = true; - } - - return modified; - } - - @Override - public void clear() { - super.clear(); - itemSet.clear(); - } - - @Override - public int indexOf(Object o) { - if (!contains(o)) { - return -1; - } - - return super.indexOf(o); - } - - @Override - public int lastIndexOf(Object o) { - if (!contains(o)) { - return -1; - } - - return super.lastIndexOf(o); - } - - @Override - public E remove(int index) { - E e = super.remove(index); - - if (e != null) { - itemSet.remove(e); - } - - return e; - } - - @Override - public boolean remove(Object o) { - if (super.remove(o)) { - itemSet.remove(o); - return true; - } else { - return false; - } - } - - @Override - protected void removeRange(int fromIndex, int toIndex) { - HashSet<E> toRemove = new HashSet<E>(); - for (int idx = fromIndex; idx < toIndex; idx++) { - toRemove.add(get(idx)); - } - super.removeRange(fromIndex, toIndex); - itemSet.removeAll(toRemove); - } - - @Override - public E set(int index, E element) { - if (contains(element)) { - // Element already exist in the list - if (get(index) == element) { - // At the same position, nothing to be done - return element; - } else { - // Adding at another position. We assume this is a sort - // operation and temporarily allow it. - - // We could just remove (null) the old element and keep the list - // unique. This would require finding the index of the old - // element (indexOf(element)) which is not a fast operation in a - // list. So we instead allow duplicates temporarily. - addDuplicate(element); - } - } - - E old = super.set(index, element); - removeFromSet(old); - itemSet.add(element); - - return old; - } - - /** - * Removes "e" from the set if it no longer exists in the list. - * - * @param e - */ - private void removeFromSet(E e) { - Integer dupl = duplicates.get(e); - if (dupl != null) { - // A duplicate was present so we only decrement the duplicate count - // and continue - if (dupl == 1) { - // This is what always should happen. A sort sets the items one - // by one, temporarily breaking the uniqueness requirement. - duplicates.remove(e); - } else { - duplicates.put(e, dupl - 1); - } - } else { - // The "old" value is no longer in the list. - itemSet.remove(e); - } - - } - - /** - * Marks the "element" can be found more than once from the list. Allowed in - * {@link #set(int, Object)} to make sorting work. - * - * @param element - */ - private void addDuplicate(E element) { - Integer nr = duplicates.get(element); - if (nr == null) { - nr = 1; - } else { - nr++; - } - - /* - * Store the number of duplicates of this element so we know later on if - * we should remove an element from the set or if it was a duplicate (in - * removeFromSet) - */ - duplicates.put(element, nr); - - } - - @SuppressWarnings("unchecked") - @Override - public Object clone() { - ListSet<E> v = (ListSet<E>) super.clone(); - v.itemSet = new HashSet<E>(itemSet); - return v; - } - -} diff --git a/server/src/com/vaadin/data/util/MethodProperty.java b/server/src/com/vaadin/data/util/MethodProperty.java deleted file mode 100644 index 83279afa53..0000000000 --- a/server/src/com/vaadin/data/util/MethodProperty.java +++ /dev/null @@ -1,772 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util; - -import static com.vaadin.util.ReflectTools.convertPrimitiveType; - -import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.logging.Level; -import java.util.logging.Logger; - -import com.vaadin.data.Property; -import com.vaadin.shared.util.SharedUtil; -import com.vaadin.util.SerializerHelper; - -/** - * <p> - * Proxy class for creating Properties from pairs of getter and setter methods - * of a Bean property. An instance of this class can be thought as having been - * attached to a field of an object. Accessing the object through the Property - * interface directly manipulates the underlying field. - * </p> - * - * <p> - * It's assumed that the return value returned by the getter method is - * assignable to the type of the property, and the setter method parameter is - * assignable to that value. - * </p> - * - * <p> - * A valid getter method must always be available, but instance of this class - * can be constructed with a <code>null</code> setter method in which case the - * resulting MethodProperty is read-only. - * </p> - * - * <p> - * MethodProperty implements Property.ValueChangeNotifier, but does not - * automatically know whether or not the getter method will actually return a - * new value - value change listeners are always notified when setValue is - * called, without verifying what the getter returns. - * </p> - * - * @author Vaadin Ltd. - * @since 3.0 - */ -@SuppressWarnings("serial") -public class MethodProperty<T> extends AbstractProperty<T> { - - /** - * The object that includes the property the MethodProperty is bound to. - */ - private transient Object instance; - - /** - * Argument arrays for the getter and setter methods. - */ - private transient Object[] setArgs, getArgs; - - /** - * The getter and setter methods. - */ - private transient Method setMethod, getMethod; - - /** - * Index of the new value in the argument list for the setter method. If the - * setter method requires several parameters, this index tells which one is - * the actual value to change. - */ - private int setArgumentIndex; - - /** - * Type of the property. - */ - private transient Class<? extends T> type; - - private static final Object[] DEFAULT_GET_ARGS = new Object[0]; - - private static final Object[] DEFAULT_SET_ARGS = new Object[1]; - - /* Special serialization to handle method references */ - private void writeObject(java.io.ObjectOutputStream out) throws IOException { - out.defaultWriteObject(); - SerializerHelper.writeClass(out, type); - out.writeObject(instance); - out.writeObject(setArgs); - out.writeObject(getArgs); - if (setMethod != null) { - out.writeObject(setMethod.getName()); - SerializerHelper - .writeClassArray(out, setMethod.getParameterTypes()); - } else { - out.writeObject(null); - out.writeObject(null); - } - if (getMethod != null) { - out.writeObject(getMethod.getName()); - SerializerHelper - .writeClassArray(out, getMethod.getParameterTypes()); - } else { - out.writeObject(null); - out.writeObject(null); - } - } - - /* Special serialization to handle method references */ - private void readObject(java.io.ObjectInputStream in) throws IOException, - ClassNotFoundException { - in.defaultReadObject(); - try { - @SuppressWarnings("unchecked") - // business assumption; type parameters not checked at runtime - Class<T> class1 = (Class<T>) SerializerHelper.readClass(in); - type = class1; - instance = in.readObject(); - Object[] setArgs = (Object[]) in.readObject(); - Object[] getArgs = (Object[]) in.readObject(); - setArguments(getArgs, setArgs, setArgumentIndex); - String name = (String) in.readObject(); - Class<?>[] paramTypes = SerializerHelper.readClassArray(in); - if (instance != null && name != null) { - setMethod = instance.getClass().getMethod(name, paramTypes); - } else { - setMethod = null; - } - - name = (String) in.readObject(); - paramTypes = SerializerHelper.readClassArray(in); - if (instance != null && name != null) { - getMethod = instance.getClass().getMethod(name, paramTypes); - } else { - getMethod = null; - } - } catch (SecurityException e) { - getLogger().log(Level.SEVERE, "Internal deserialization error", e); - } catch (NoSuchMethodException e) { - getLogger().log(Level.SEVERE, "Internal deserialization error", e); - } - } - - /** - * <p> - * Creates a new instance of <code>MethodProperty</code> from a named bean - * property. This constructor takes an object and the name of a bean - * property and initializes itself with the accessor methods for the - * property. - * </p> - * <p> - * The getter method of a <code>MethodProperty</code> instantiated with this - * constructor will be called with no arguments, and the setter method with - * only the new value as the sole argument. - * </p> - * - * <p> - * If the setter method is unavailable, the resulting - * <code>MethodProperty</code> will be read-only, otherwise it will be - * read-write. - * </p> - * - * <p> - * Method names are constructed from the bean property by adding - * get/is/are/set prefix and capitalising the first character in the name of - * the given bean property. - * </p> - * - * @param instance - * the object that includes the property. - * @param beanPropertyName - * the name of the property to bind to. - */ - @SuppressWarnings("unchecked") - public MethodProperty(Object instance, String beanPropertyName) { - - final Class<?> beanClass = instance.getClass(); - - // Assure that the first letter is upper cased (it is a common - // mistake to write firstName, not FirstName). - beanPropertyName = SharedUtil.capitalize(beanPropertyName); - - // Find the get method - getMethod = null; - try { - getMethod = initGetterMethod(beanPropertyName, beanClass); - } catch (final java.lang.NoSuchMethodException ignored) { - throw new MethodException(this, "Bean property " + beanPropertyName - + " can not be found"); - } - - // In case the get method is found, resolve the type - Class<?> returnType = getMethod.getReturnType(); - - // Finds the set method - setMethod = null; - try { - setMethod = beanClass.getMethod("set" + beanPropertyName, - new Class[] { returnType }); - } catch (final java.lang.NoSuchMethodException skipped) { - } - - // Gets the return type from get method - if (returnType.isPrimitive()) { - type = (Class<T>) convertPrimitiveType(returnType); - if (type.isPrimitive()) { - throw new MethodException(this, "Bean property " - + beanPropertyName - + " getter return type must not be void"); - } - } else { - type = (Class<T>) returnType; - } - - setArguments(DEFAULT_GET_ARGS, DEFAULT_SET_ARGS, 0); - this.instance = instance; - } - - /** - * <p> - * Creates a new instance of <code>MethodProperty</code> from named getter - * and setter methods. The getter method of a <code>MethodProperty</code> - * instantiated with this constructor will be called with no arguments, and - * the setter method with only the new value as the sole argument. - * </p> - * - * <p> - * If the setter method is <code>null</code>, the resulting - * <code>MethodProperty</code> will be read-only, otherwise it will be - * read-write. - * </p> - * - * @param type - * the type of the property. - * @param instance - * the object that includes the property. - * @param getMethodName - * the name of the getter method. - * @param setMethodName - * the name of the setter method. - * - */ - public MethodProperty(Class<? extends T> type, Object instance, - String getMethodName, String setMethodName) { - this(type, instance, getMethodName, setMethodName, new Object[] {}, - new Object[] { null }, 0); - } - - /** - * <p> - * Creates a new instance of <code>MethodProperty</code> with the getter and - * setter methods. The getter method of a <code>MethodProperty</code> - * instantiated with this constructor will be called with no arguments, and - * the setter method with only the new value as the sole argument. - * </p> - * - * <p> - * If the setter method is <code>null</code>, the resulting - * <code>MethodProperty</code> will be read-only, otherwise it will be - * read-write. - * </p> - * - * @param type - * the type of the property. - * @param instance - * the object that includes the property. - * @param getMethod - * the getter method. - * @param setMethod - * the setter method. - */ - public MethodProperty(Class<? extends T> type, Object instance, - Method getMethod, Method setMethod) { - this(type, instance, getMethod, setMethod, new Object[] {}, - new Object[] { null }, 0); - } - - /** - * <p> - * Creates a new instance of <code>MethodProperty</code> from named getter - * and setter methods and argument lists. The getter method of a - * <code>MethodProperty</code> instantiated with this constructor will be - * called with the getArgs as arguments. The setArgs will be used as the - * arguments for the setter method, though the argument indexed by the - * setArgumentIndex will be replaced with the argument passed to the - * {@link #setValue(Object newValue)} method. - * </p> - * - * <p> - * For example, if the <code>setArgs</code> contains <code>A</code>, - * <code>B</code> and <code>C</code>, and <code>setArgumentIndex = - * 1</code>, the call <code>methodProperty.setValue(X)</code> would result - * in the setter method to be called with the parameter set of - * <code>{A, X, C}</code> - * </p> - * - * @param type - * the type of the property. - * @param instance - * the object that includes the property. - * @param getMethodName - * the name of the getter method. - * @param setMethodName - * the name of the setter method. - * @param getArgs - * the fixed argument list to be passed to the getter method. - * @param setArgs - * the fixed argument list to be passed to the setter method. - * @param setArgumentIndex - * the index of the argument in <code>setArgs</code> to be - * replaced with <code>newValue</code> when - * {@link #setValue(Object newValue)} is called. - */ - @SuppressWarnings("unchecked") - public MethodProperty(Class<? extends T> type, Object instance, - String getMethodName, String setMethodName, Object[] getArgs, - Object[] setArgs, int setArgumentIndex) { - - // Check the setargs and setargs index - if (setMethodName != null && setArgs == null) { - throw new IndexOutOfBoundsException("The setArgs can not be null"); - } - if (setMethodName != null - && (setArgumentIndex < 0 || setArgumentIndex >= setArgs.length)) { - throw new IndexOutOfBoundsException( - "The setArgumentIndex must be >= 0 and < setArgs.length"); - } - - // Set type - this.type = type; - - // Find set and get -methods - final Method[] m = instance.getClass().getMethods(); - - // Finds get method - boolean found = false; - for (int i = 0; i < m.length; i++) { - - // Tests the name of the get Method - if (!m[i].getName().equals(getMethodName)) { - - // name does not match, try next method - continue; - } - - // Tests return type - if (!type.equals(m[i].getReturnType())) { - continue; - } - - // Tests the parameter types - final Class<?>[] c = m[i].getParameterTypes(); - if (c.length != getArgs.length) { - - // not the right amount of parameters, try next method - continue; - } - int j = 0; - while (j < c.length) { - if (getArgs[j] != null - && !c[j].isAssignableFrom(getArgs[j].getClass())) { - - // parameter type does not match, try next method - break; - } - j++; - } - if (j == c.length) { - - // all paramteters matched - if (found == true) { - throw new MethodException(this, - "Could not uniquely identify " + getMethodName - + "-method"); - } else { - found = true; - getMethod = m[i]; - } - } - } - if (found != true) { - throw new MethodException(this, "Could not find " + getMethodName - + "-method"); - } - - // Finds set method - if (setMethodName != null) { - - // Finds setMethod - found = false; - for (int i = 0; i < m.length; i++) { - - // Checks name - if (!m[i].getName().equals(setMethodName)) { - - // name does not match, try next method - continue; - } - - // Checks parameter compatibility - final Class<?>[] c = m[i].getParameterTypes(); - if (c.length != setArgs.length) { - - // not the right amount of parameters, try next method - continue; - } - int j = 0; - while (j < c.length) { - if (setArgs[j] != null - && !c[j].isAssignableFrom(setArgs[j].getClass())) { - - // parameter type does not match, try next method - break; - } else if (j == setArgumentIndex && !c[j].equals(type)) { - - // Property type is not the same as setArg type - break; - } - j++; - } - if (j == c.length) { - - // all parameters match - if (found == true) { - throw new MethodException(this, - "Could not identify unique " + setMethodName - + "-method"); - } else { - found = true; - setMethod = m[i]; - } - } - } - if (found != true) { - throw new MethodException(this, "Could not identify " - + setMethodName + "-method"); - } - } - - // Gets the return type from get method - this.type = (Class<T>) convertPrimitiveType(type); - - setArguments(getArgs, setArgs, setArgumentIndex); - this.instance = instance; - } - - /** - * <p> - * Creates a new instance of <code>MethodProperty</code> from the getter and - * setter methods, and argument lists. - * </p> - * <p> - * This constructor behaves exactly like - * {@link #MethodProperty(Class type, Object instance, String getMethodName, String setMethodName, Object [] getArgs, Object [] setArgs, int setArgumentIndex)} - * except that instead of names of the getter and setter methods this - * constructor is given the actual methods themselves. - * </p> - * - * @param type - * the type of the property. - * @param instance - * the object that includes the property. - * @param getMethod - * the getter method. - * @param setMethod - * the setter method. - * @param getArgs - * the fixed argument list to be passed to the getter method. - * @param setArgs - * the fixed argument list to be passed to the setter method. - * @param setArgumentIndex - * the index of the argument in <code>setArgs</code> to be - * replaced with <code>newValue</code> when - * {@link #setValue(Object newValue)} is called. - */ - @SuppressWarnings("unchecked") - // cannot use "Class<? extends T>" because of automatic primitive type - // conversions - public MethodProperty(Class<?> type, Object instance, Method getMethod, - Method setMethod, Object[] getArgs, Object[] setArgs, - int setArgumentIndex) { - - if (getMethod == null) { - throw new MethodException(this, - "Property GET-method cannot not be null: " + type); - } - - if (setMethod != null) { - if (setArgs == null) { - throw new IndexOutOfBoundsException( - "The setArgs can not be null"); - } - if (setArgumentIndex < 0 || setArgumentIndex >= setArgs.length) { - throw new IndexOutOfBoundsException( - "The setArgumentIndex must be >= 0 and < setArgs.length"); - } - } - - // Gets the return type from get method - Class<? extends T> convertedType = (Class<? extends T>) convertPrimitiveType(type); - - this.getMethod = getMethod; - this.setMethod = setMethod; - setArguments(getArgs, setArgs, setArgumentIndex); - this.instance = instance; - this.type = convertedType; - } - - /** - * Find a getter method for a property (getXyz(), isXyz() or areXyz()). - * - * @param propertyName - * name of the property - * @param beanClass - * class in which to look for the getter methods - * @return Method - * @throws NoSuchMethodException - * if no getter found - */ - static Method initGetterMethod(String propertyName, final Class<?> beanClass) - throws NoSuchMethodException { - propertyName = SharedUtil.capitalize(propertyName); - - Method getMethod = null; - try { - getMethod = beanClass.getMethod("get" + propertyName, - new Class[] {}); - } catch (final java.lang.NoSuchMethodException ignored) { - try { - getMethod = beanClass.getMethod("is" + propertyName, - new Class[] {}); - } catch (final java.lang.NoSuchMethodException ignoredAsWell) { - getMethod = beanClass.getMethod("are" + propertyName, - new Class[] {}); - } - } - return getMethod; - } - - /** - * Returns the type of the Property. The methods <code>getValue</code> and - * <code>setValue</code> must be compatible with this type: one must be able - * to safely cast the value returned from <code>getValue</code> to the given - * type and pass any variable assignable to this type as an argument to - * <code>setValue</code>. - * - * @return type of the Property - */ - @Override - public final Class<? extends T> getType() { - return type; - } - - /** - * Tests if the object is in read-only mode. In read-only mode calls to - * <code>setValue</code> will throw <code>ReadOnlyException</code> and will - * not modify the value of the Property. - * - * @return <code>true</code> if the object is in read-only mode, - * <code>false</code> if it's not - */ - @Override - public boolean isReadOnly() { - return super.isReadOnly() || (setMethod == null); - } - - /** - * Gets the value stored in the Property. The value is resolved by calling - * the specified getter method with the argument specified at instantiation. - * - * @return the value of the Property - */ - @Override - public T getValue() { - try { - if (instance == null) { - return null; - } else { - return (T) getMethod.invoke(instance, getArgs); - } - } catch (final Throwable e) { - throw new MethodException(this, e); - } - } - - /** - * <p> - * Sets the setter method and getter method argument lists. - * </p> - * - * @param getArgs - * the fixed argument list to be passed to the getter method. - * @param setArgs - * the fixed argument list to be passed to the setter method. - * @param setArgumentIndex - * the index of the argument in <code>setArgs</code> to be - * replaced with <code>newValue</code> when - * {@link #setValue(Object newValue)} is called. - */ - public void setArguments(Object[] getArgs, Object[] setArgs, - int setArgumentIndex) { - if (getArgs.length == 0) { - this.getArgs = DEFAULT_GET_ARGS; - } else { - this.getArgs = Arrays.copyOf(getArgs, getArgs.length); - } - if (Arrays.equals(setArgs, DEFAULT_SET_ARGS)) { - this.setArgs = DEFAULT_SET_ARGS; - } else { - this.setArgs = Arrays.copyOf(setArgs, setArgs.length); - } - this.setArgumentIndex = setArgumentIndex; - } - - /** - * Sets the value of the property. - * - * Note that since Vaadin 7, no conversions are performed and the value must - * be of the correct type. - * - * @param newValue - * the New value of the property. - * @throws <code>Property.ReadOnlyException</code> if the object is in - * read-only mode. - * @see #invokeSetMethod(Object) - */ - @Override - public void setValue(T newValue) throws Property.ReadOnlyException { - - // Checks the mode - if (isReadOnly()) { - throw new Property.ReadOnlyException(); - } - - invokeSetMethod(newValue); - fireValueChange(); - } - - /** - * Internal method to actually call the setter method of the wrapped - * property. - * - * @param value - */ - protected void invokeSetMethod(T value) { - - try { - // Construct a temporary argument array only if needed - if (setArgs.length == 1) { - setMethod.invoke(instance, new Object[] { value }); - } else { - - // Sets the value to argument array - final Object[] args = new Object[setArgs.length]; - for (int i = 0; i < setArgs.length; i++) { - args[i] = (i == setArgumentIndex) ? value : setArgs[i]; - } - setMethod.invoke(instance, args); - } - } catch (final InvocationTargetException e) { - final Throwable targetException = e.getTargetException(); - throw new MethodException(this, targetException); - } catch (final Exception e) { - throw new MethodException(this, e); - } - } - - /** - * <code>Exception</code> object that signals that there were problems - * calling or finding the specified getter or setter methods of the - * property. - * - * @author Vaadin Ltd. - * @since 3.0 - */ - @SuppressWarnings("rawtypes") - // Exceptions cannot be parameterized, ever. - public static class MethodException extends RuntimeException { - - /** - * The method property from which the exception originates from - */ - private final Property property; - - /** - * Cause of the method exception - */ - private Throwable cause; - - /** - * Constructs a new <code>MethodException</code> with the specified - * detail message. - * - * @param property - * the property. - * @param msg - * the detail message. - */ - public MethodException(Property property, String msg) { - super(msg); - this.property = property; - } - - /** - * Constructs a new <code>MethodException</code> from another exception. - * - * @param property - * the property. - * @param cause - * the cause of the exception. - */ - public MethodException(Property property, Throwable cause) { - this.property = property; - this.cause = cause; - } - - /** - * @see java.lang.Throwable#getCause() - */ - @Override - public Throwable getCause() { - return cause; - } - - /** - * Gets the method property this exception originates from. - * - * @return MethodProperty or null if not a valid MethodProperty - */ - public MethodProperty getMethodProperty() { - return (property instanceof MethodProperty) ? (MethodProperty) property - : null; - } - - /** - * Gets the method property this exception originates from. - * - * @return Property from which the exception originates - */ - public Property getProperty() { - return property; - } - } - - /** - * Sends a value change event to all registered listeners. - * - * Public for backwards compatibility, visibility may be reduced in future - * versions. - */ - @Override - public void fireValueChange() { - super.fireValueChange(); - } - - private static final Logger getLogger() { - return Logger.getLogger(MethodProperty.class.getName()); - } - -} diff --git a/server/src/com/vaadin/data/util/MethodPropertyDescriptor.java b/server/src/com/vaadin/data/util/MethodPropertyDescriptor.java deleted file mode 100644 index f94ee75ac3..0000000000 --- a/server/src/com/vaadin/data/util/MethodPropertyDescriptor.java +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import java.io.IOException; -import java.lang.reflect.Method; -import java.util.logging.Level; -import java.util.logging.Logger; - -import com.vaadin.data.Property; -import com.vaadin.util.ReflectTools; -import com.vaadin.util.SerializerHelper; - -/** - * Property descriptor that is able to create simple {@link MethodProperty} - * instances for a bean, using given accessors. - * - * @param <BT> - * bean type - * - * @since 6.6 - */ -public class MethodPropertyDescriptor<BT> implements - VaadinPropertyDescriptor<BT> { - - private final String name; - private Class<?> propertyType; - private transient Method readMethod; - private transient Method writeMethod; - - /** - * Creates a property descriptor that can create MethodProperty instances to - * access the underlying bean property. - * - * @param name - * of the property - * @param propertyType - * type (class) of the property - * @param readMethod - * getter {@link Method} for the property - * @param writeMethod - * setter {@link Method} for the property or null if read-only - * property - */ - public MethodPropertyDescriptor(String name, Class<?> propertyType, - Method readMethod, Method writeMethod) { - this.name = name; - this.propertyType = ReflectTools.convertPrimitiveType(propertyType); - this.readMethod = readMethod; - this.writeMethod = writeMethod; - } - - /* Special serialization to handle method references */ - private void writeObject(java.io.ObjectOutputStream out) throws IOException { - out.defaultWriteObject(); - SerializerHelper.writeClass(out, propertyType); - - if (writeMethod != null) { - out.writeObject(writeMethod.getName()); - SerializerHelper.writeClass(out, writeMethod.getDeclaringClass()); - SerializerHelper.writeClassArray(out, - writeMethod.getParameterTypes()); - } else { - out.writeObject(null); - out.writeObject(null); - out.writeObject(null); - } - - if (readMethod != null) { - out.writeObject(readMethod.getName()); - SerializerHelper.writeClass(out, readMethod.getDeclaringClass()); - SerializerHelper.writeClassArray(out, - readMethod.getParameterTypes()); - } else { - out.writeObject(null); - out.writeObject(null); - out.writeObject(null); - } - } - - /* Special serialization to handle method references */ - private void readObject(java.io.ObjectInputStream in) throws IOException, - ClassNotFoundException { - in.defaultReadObject(); - try { - @SuppressWarnings("unchecked") - // business assumption; type parameters not checked at runtime - Class<BT> class1 = (Class<BT>) SerializerHelper.readClass(in); - propertyType = ReflectTools.convertPrimitiveType(class1); - - String name = (String) in.readObject(); - Class<?> writeMethodClass = SerializerHelper.readClass(in); - Class<?>[] paramTypes = SerializerHelper.readClassArray(in); - if (name != null) { - writeMethod = writeMethodClass.getMethod(name, paramTypes); - } else { - writeMethod = null; - } - - name = (String) in.readObject(); - Class<?> readMethodClass = SerializerHelper.readClass(in); - paramTypes = SerializerHelper.readClassArray(in); - if (name != null) { - readMethod = readMethodClass.getMethod(name, paramTypes); - } else { - readMethod = null; - } - } catch (SecurityException e) { - getLogger().log(Level.SEVERE, "Internal deserialization error", e); - } catch (NoSuchMethodException e) { - getLogger().log(Level.SEVERE, "Internal deserialization error", e); - } - } - - @Override - public String getName() { - return name; - } - - @Override - public Class<?> getPropertyType() { - return propertyType; - } - - @Override - public Property<?> createProperty(Object bean) { - return new MethodProperty<Object>(propertyType, bean, readMethod, - writeMethod); - } - - private static final Logger getLogger() { - return Logger.getLogger(MethodPropertyDescriptor.class.getName()); - } -} diff --git a/server/src/com/vaadin/data/util/NestedMethodProperty.java b/server/src/com/vaadin/data/util/NestedMethodProperty.java deleted file mode 100644 index 29fe62f845..0000000000 --- a/server/src/com/vaadin/data/util/NestedMethodProperty.java +++ /dev/null @@ -1,268 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import static com.vaadin.util.ReflectTools.convertPrimitiveType; - -import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import com.vaadin.data.Property; -import com.vaadin.data.util.MethodProperty.MethodException; -import com.vaadin.shared.util.SharedUtil; - -/** - * Nested accessor based property for a bean. - * - * The property is specified in the dotted notation, e.g. "address.street", and - * can contain multiple levels of nesting. - * - * When accessing the property value, all intermediate getters must exist and - * should return non-null values when the property value is accessed. If an - * intermediate getter returns null, a null value will be returned. - * - * @see MethodProperty - * - * @since 6.6 - */ -public class NestedMethodProperty<T> extends AbstractProperty<T> { - - // needed for de-serialization - private String propertyName; - - // chain of getter methods - private transient List<Method> getMethods; - /** - * The setter method. - */ - private transient Method setMethod; - - /** - * Bean instance used as a starting point for accessing the property value. - */ - private Object instance; - - private Class<? extends T> type; - - /* Special serialization to handle method references */ - private void writeObject(java.io.ObjectOutputStream out) throws IOException { - out.defaultWriteObject(); - // getMethods and setMethod are reconstructed on read based on - // propertyName - } - - /* Special serialization to handle method references */ - private void readObject(java.io.ObjectInputStream in) throws IOException, - ClassNotFoundException { - in.defaultReadObject(); - - initialize(instance.getClass(), propertyName); - } - - /** - * Constructs a nested method property for a given object instance. The - * property name is a dot separated string pointing to a nested property, - * e.g. "manager.address.street". - * <p> - * Calling getValue will return null if any intermediate getter returns null - * - * @param instance - * top-level bean to which the property applies - * @param propertyName - * dot separated nested property name - * @throws IllegalArgumentException - * if the property name is invalid - */ - public NestedMethodProperty(Object instance, String propertyName) { - this.instance = instance; - initialize(instance.getClass(), propertyName); - } - - /** - * For internal use to deduce property type etc. without a bean instance. - * Calling {@link #setValue(Object)} or {@link #getValue()} on properties - * constructed this way is not supported. - * - * @param instanceClass - * class of the top-level bean - * @param propertyName - */ - NestedMethodProperty(Class<?> instanceClass, String propertyName) { - instance = null; - initialize(instanceClass, propertyName); - } - - /** - * Initializes most of the internal fields based on the top-level bean - * instance and property name (dot-separated string). - * - * @param beanClass - * class of the top-level bean to which the property applies - * @param propertyName - * dot separated nested property name - * @throws IllegalArgumentException - * if the property name is invalid - */ - private void initialize(Class<?> beanClass, String propertyName) - throws IllegalArgumentException { - - List<Method> getMethods = new ArrayList<Method>(); - - String lastSimplePropertyName = propertyName; - Class<?> lastClass = beanClass; - - // first top-level property, then go deeper in a loop - Class<?> propertyClass = beanClass; - String[] simplePropertyNames = propertyName.split("\\."); - if (propertyName.endsWith(".") || 0 == simplePropertyNames.length) { - throw new IllegalArgumentException("Invalid property name '" - + propertyName + "'"); - } - for (int i = 0; i < simplePropertyNames.length; i++) { - String simplePropertyName = simplePropertyNames[i].trim(); - if (simplePropertyName.length() > 0) { - lastSimplePropertyName = simplePropertyName; - lastClass = propertyClass; - try { - Method getter = MethodProperty.initGetterMethod( - simplePropertyName, propertyClass); - propertyClass = getter.getReturnType(); - getMethods.add(getter); - } catch (final java.lang.NoSuchMethodException e) { - throw new IllegalArgumentException("Bean property '" - + simplePropertyName + "' not found", e); - } - } else { - throw new IllegalArgumentException( - "Empty or invalid bean property identifier in '" - + propertyName + "'"); - } - } - - // In case the get method is found, resolve the type - Method lastGetMethod = getMethods.get(getMethods.size() - 1); - Class<?> type = lastGetMethod.getReturnType(); - - // Finds the set method - Method setMethod = null; - try { - // Assure that the first letter is upper cased (it is a common - // mistake to write firstName, not FirstName). - lastSimplePropertyName = SharedUtil - .capitalize(lastSimplePropertyName); - - setMethod = lastClass.getMethod("set" + lastSimplePropertyName, - new Class[] { type }); - } catch (final NoSuchMethodException skipped) { - } - - this.type = (Class<? extends T>) convertPrimitiveType(type); - this.propertyName = propertyName; - this.getMethods = getMethods; - this.setMethod = setMethod; - } - - @Override - public Class<? extends T> getType() { - return type; - } - - @Override - public boolean isReadOnly() { - return super.isReadOnly() || (null == setMethod); - } - - /** - * Gets the value stored in the Property. The value is resolved by calling - * the specified getter method with the argument specified at instantiation. - * - * @return the value of the Property - */ - @Override - public T getValue() { - try { - Object object = instance; - for (Method m : getMethods) { - object = m.invoke(object); - if (object == null) { - return null; - } - } - return (T) object; - } catch (final Throwable e) { - throw new MethodException(this, e); - } - } - - /** - * Sets the value of the property. The new value must be assignable to the - * type of this property. - * - * @param newValue - * the New value of the property. - * @throws <code>Property.ReadOnlyException</code> if the object is in - * read-only mode. - * @see #invokeSetMethod(Object) - */ - @Override - public void setValue(T newValue) throws ReadOnlyException { - // Checks the mode - if (isReadOnly()) { - throw new Property.ReadOnlyException(); - } - - invokeSetMethod(newValue); - fireValueChange(); - } - - /** - * Internal method to actually call the setter method of the wrapped - * property. - * - * @param value - */ - protected void invokeSetMethod(T value) { - try { - Object object = instance; - for (int i = 0; i < getMethods.size() - 1; i++) { - object = getMethods.get(i).invoke(object); - } - setMethod.invoke(object, new Object[] { value }); - } catch (final InvocationTargetException e) { - throw new MethodException(this, e.getTargetException()); - } catch (final Exception e) { - throw new MethodException(this, e); - } - } - - /** - * Returns an unmodifiable list of getter methods to call in sequence to get - * the property value. - * - * This API may change in future versions. - * - * @return unmodifiable list of getter methods corresponding to each segment - * of the property name - */ - protected List<Method> getGetMethods() { - return Collections.unmodifiableList(getMethods); - } - -} diff --git a/server/src/com/vaadin/data/util/NestedPropertyDescriptor.java b/server/src/com/vaadin/data/util/NestedPropertyDescriptor.java deleted file mode 100644 index ca423b34ed..0000000000 --- a/server/src/com/vaadin/data/util/NestedPropertyDescriptor.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import com.vaadin.data.Property; - -/** - * Property descriptor that is able to create nested property instances for a - * bean. - * - * The property is specified in the dotted notation, e.g. "address.street", and - * can contain multiple levels of nesting. - * - * @param <BT> - * bean type - * - * @since 6.6 - */ -public class NestedPropertyDescriptor<BT> implements - VaadinPropertyDescriptor<BT> { - - private final String name; - private final Class<?> propertyType; - - /** - * Creates a property descriptor that can create MethodProperty instances to - * access the underlying bean property. - * - * @param name - * of the property in a dotted path format, e.g. "address.street" - * @param beanType - * type (class) of the top-level bean - * @throws IllegalArgumentException - * if the property name is invalid - */ - public NestedPropertyDescriptor(String name, Class<BT> beanType) - throws IllegalArgumentException { - this.name = name; - NestedMethodProperty<?> property = new NestedMethodProperty<Object>( - beanType, name); - this.propertyType = property.getType(); - } - - @Override - public String getName() { - return name; - } - - @Override - public Class<?> getPropertyType() { - return propertyType; - } - - @Override - public Property<?> createProperty(BT bean) { - return new NestedMethodProperty<Object>(bean, name); - } - -} diff --git a/server/src/com/vaadin/data/util/ObjectProperty.java b/server/src/com/vaadin/data/util/ObjectProperty.java deleted file mode 100644 index 8a2daa443c..0000000000 --- a/server/src/com/vaadin/data/util/ObjectProperty.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util; - -import com.vaadin.data.Property; - -/** - * A simple data object containing one typed value. This class is a - * straightforward implementation of the the {@link com.vaadin.data.Property} - * interface. - * - * @author Vaadin Ltd. - * @since 3.0 - */ -@SuppressWarnings("serial") -public class ObjectProperty<T> extends AbstractProperty<T> { - - /** - * The value contained by the Property. - */ - private T value; - - /** - * Data type of the Property's value. - */ - private final Class<T> type; - - /** - * Creates a new instance of ObjectProperty with the given value. The type - * of the property is automatically initialized to be the type of the given - * value. - * - * @param value - * the Initial value of the Property. - */ - @SuppressWarnings("unchecked") - // the cast is safe, because an object of type T has class Class<T> - public ObjectProperty(T value) { - this(value, (Class<T>) value.getClass()); - } - - /** - * Creates a new instance of ObjectProperty with the given value and type. - * - * Since Vaadin 7, only values of the correct type are accepted, and no - * automatic conversions are performed. - * - * @param value - * the Initial value of the Property. - * @param type - * the type of the value. The value must be assignable to given - * type. - */ - public ObjectProperty(T value, Class<T> type) { - - // Set the values - this.type = type; - setValue(value); - } - - /** - * Creates a new instance of ObjectProperty with the given value, type and - * read-only mode status. - * - * Since Vaadin 7, only the correct type of values is accepted, see - * {@link #ObjectProperty(Object, Class)}. - * - * @param value - * the Initial value of the property. - * @param type - * the type of the value. <code>value</code> must be assignable - * to this type. - * @param readOnly - * Sets the read-only mode. - */ - public ObjectProperty(T value, Class<T> type, boolean readOnly) { - this(value, type); - setReadOnly(readOnly); - } - - /** - * Returns the type of the ObjectProperty. The methods <code>getValue</code> - * and <code>setValue</code> must be compatible with this type: one must be - * able to safely cast the value returned from <code>getValue</code> to the - * given type and pass any variable assignable to this type as an argument - * to <code>setValue</code>. - * - * @return type of the Property - */ - @Override - public final Class<T> getType() { - return type; - } - - /** - * Gets the value stored in the Property. - * - * @return the value stored in the Property - */ - @Override - public T getValue() { - return value; - } - - /** - * Sets the value of the property. - * - * Note that since Vaadin 7, no conversions are performed and the value must - * be of the correct type. - * - * @param newValue - * the New value of the property. - * @throws <code>Property.ReadOnlyException</code> if the object is in - * read-only mode - */ - @Override - public void setValue(T newValue) throws Property.ReadOnlyException { - - // Checks the mode - if (isReadOnly()) { - throw new Property.ReadOnlyException(); - } - - this.value = newValue; - - fireValueChange(); - } -} diff --git a/server/src/com/vaadin/data/util/PropertyFormatter.java b/server/src/com/vaadin/data/util/PropertyFormatter.java deleted file mode 100644 index e188ce8bc3..0000000000 --- a/server/src/com/vaadin/data/util/PropertyFormatter.java +++ /dev/null @@ -1,257 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import com.vaadin.data.Property; -import com.vaadin.data.util.converter.Converter; - -/** - * Formatting proxy for a {@link Property}. - * - * <p> - * This class can be used to implement formatting for any type of Property - * datasources. The idea is to connect this as proxy between UI component and - * the original datasource. - * </p> - * - * <p> - * For example <code> - * <pre>textfield.setPropertyDataSource(new PropertyFormatter(property) { - public String format(Object value) { - return ((Double) value).toString() + "000000000"; - } - - public Object parse(String formattedValue) throws Exception { - return Double.parseDouble(formattedValue); - } - - });</pre></code> adds formatter for Double-typed property that extends - * standard "1.0" notation with more zeroes. - * </p> - * - * @param T - * type of the underlying property (a PropertyFormatter is always a - * Property<String>) - * - * @deprecated As of 7.0, replaced by {@link Converter} - * @author Vaadin Ltd. - * @since 5.3.0 - */ -@SuppressWarnings("serial") -@Deprecated -public abstract class PropertyFormatter<T> extends AbstractProperty<String> - implements Property.Viewer, Property.ValueChangeListener, - Property.ReadOnlyStatusChangeListener { - - /** Datasource that stores the actual value. */ - Property<T> dataSource; - - /** - * Construct a new {@code PropertyFormatter} that is not connected to any - * data source. Call {@link #setPropertyDataSource(Property)} later on to - * attach it to a property. - * - */ - protected PropertyFormatter() { - } - - /** - * Construct a new formatter that is connected to given data source. Calls - * {@link #format(Object)} which can be a problem if the formatter has not - * yet been initialized. - * - * @param propertyDataSource - * to connect this property to. - */ - public PropertyFormatter(Property<T> propertyDataSource) { - - setPropertyDataSource(propertyDataSource); - } - - /** - * Gets the current data source of the formatter, if any. - * - * @return the current data source as a Property, or <code>null</code> if - * none defined. - */ - @Override - public Property<T> getPropertyDataSource() { - return dataSource; - } - - /** - * Sets the specified Property as the data source for the formatter. - * - * - * <p> - * Remember that new data sources getValue() must return objects that are - * compatible with parse() and format() methods. - * </p> - * - * @param newDataSource - * the new data source Property. - */ - @Override - public void setPropertyDataSource(Property newDataSource) { - - boolean readOnly = false; - String prevValue = null; - - if (dataSource != null) { - if (dataSource instanceof Property.ValueChangeNotifier) { - ((Property.ValueChangeNotifier) dataSource) - .removeListener(this); - } - if (dataSource instanceof Property.ReadOnlyStatusChangeListener) { - ((Property.ReadOnlyStatusChangeNotifier) dataSource) - .removeListener(this); - } - readOnly = isReadOnly(); - prevValue = getValue(); - } - - dataSource = newDataSource; - - if (dataSource != null) { - if (dataSource instanceof Property.ValueChangeNotifier) { - ((Property.ValueChangeNotifier) dataSource).addListener(this); - } - if (dataSource instanceof Property.ReadOnlyStatusChangeListener) { - ((Property.ReadOnlyStatusChangeNotifier) dataSource) - .addListener(this); - } - } - - if (isReadOnly() != readOnly) { - fireReadOnlyStatusChange(); - } - String newVal = getValue(); - if ((prevValue == null && newVal != null) - || (prevValue != null && !prevValue.equals(newVal))) { - fireValueChange(); - } - } - - /* Documented in the interface */ - @Override - public Class<String> getType() { - return String.class; - } - - /** - * Get the formatted value. - * - * @return If the datasource returns null, this is null. Otherwise this is - * String given by format(). - */ - @Override - public String getValue() { - T value = dataSource == null ? null : dataSource.getValue(); - if (value == null) { - return null; - } - return format(value); - } - - /** Reflects the read-only status of the datasource. */ - @Override - public boolean isReadOnly() { - return dataSource == null ? false : dataSource.isReadOnly(); - } - - /** - * This method must be implemented to format the values received from - * DataSource. - * - * @param value - * Value object got from the datasource. This is guaranteed to be - * non-null and of the type compatible with getType() of the - * datasource. - * @return - */ - abstract public String format(T value); - - /** - * Parse string and convert it to format compatible with datasource. - * - * The method is required to assure that parse(format(x)) equals x. - * - * @param formattedValue - * This is guaranteed to be non-null string. - * @return Non-null value compatible with datasource. - * @throws Exception - * Any type of exception can be thrown to indicate that the - * conversion was not succesful. - */ - abstract public T parse(String formattedValue) throws Exception; - - /** - * Sets the Property's read-only mode to the specified status. - * - * @param newStatus - * the new read-only status of the Property. - */ - @Override - public void setReadOnly(boolean newStatus) { - if (dataSource != null) { - dataSource.setReadOnly(newStatus); - } - } - - @Override - public void setValue(String newValue) throws ReadOnlyException { - if (dataSource == null) { - return; - } - if (newValue == null) { - if (dataSource.getValue() != null) { - dataSource.setValue(null); - fireValueChange(); - } - } else { - try { - dataSource.setValue(parse(newValue.toString())); - if (!newValue.equals(getValue())) { - fireValueChange(); - } - } catch (Exception e) { - throw new IllegalArgumentException("Could not parse value", e); - } - } - } - - /** - * Listens for changes in the datasource. - * - * This should not be called directly. - */ - @Override - public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) { - fireValueChange(); - } - - /** - * Listens for changes in the datasource. - * - * This should not be called directly. - */ - @Override - public void readOnlyStatusChange( - com.vaadin.data.Property.ReadOnlyStatusChangeEvent event) { - fireReadOnlyStatusChange(); - } - -} diff --git a/server/src/com/vaadin/data/util/PropertyValueGenerator.java b/server/src/com/vaadin/data/util/PropertyValueGenerator.java deleted file mode 100644 index 453e45b1db..0000000000 --- a/server/src/com/vaadin/data/util/PropertyValueGenerator.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import java.io.Serializable; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.Item; -import com.vaadin.data.Property; -import com.vaadin.data.sort.SortOrder; -import com.vaadin.data.util.filter.UnsupportedFilterException; - -/** - * PropertyValueGenerator for GeneratedPropertyContainer. - * - * @param <T> - * Property data type - * @since 7.4 - * @author Vaadin Ltd - */ -public abstract class PropertyValueGenerator<T> implements Serializable { - - /** - * Returns value for given Item. Used by GeneratedPropertyContainer when - * generating new properties. - * - * @param item - * currently handled item - * @param itemId - * item id for currently handled item - * @param propertyId - * id for this property - * @return generated value - */ - public abstract T getValue(Item item, Object itemId, Object propertyId); - - /** - * Return Property type for this generator. This function is called when - * {@link Property#getType()} is called for generated property. - * - * @return type of generated property - */ - public abstract Class<T> getType(); - - /** - * Translates sorting of the generated property in a specific direction to a - * set of property ids and directions in the underlying container. - * - * SortOrder is similar to (or the same as) the SortOrder already defined - * for Grid. - * - * The default implementation of this method returns an empty array, which - * means that the property will not be included in - * getSortableContainerPropertyIds(). Attempting to sort by that column - * throws UnsupportedOperationException. - * - * Returning null is not allowed. - * - * @param order - * a sort order for this property - * @return an array of sort orders describing how this property is sorted - */ - public SortOrder[] getSortProperties(SortOrder order) { - return new SortOrder[] {}; - } - - /** - * Return an updated filter that should be compatible with the underlying - * container. - * - * This function is called when setting a filter for this generated - * property. Returning null from this function causes - * GeneratedPropertyContainer to discard the filter and not use it. - * - * By default this function throws UnsupportedFilterException. - * - * @param filter - * original filter for this property - * @return modified filter that is compatible with the underlying container - * @throws UnsupportedFilterException - */ - public Filter modifyFilter(Filter filter) throws UnsupportedFilterException { - throw new UnsupportedFilterException("Filter" + filter - + " is not supported"); - } - -} diff --git a/server/src/com/vaadin/data/util/PropertysetItem.java b/server/src/com/vaadin/data/util/PropertysetItem.java deleted file mode 100644 index f463a52f54..0000000000 --- a/server/src/com/vaadin/data/util/PropertysetItem.java +++ /dev/null @@ -1,370 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util; - -import java.util.Collection; -import java.util.Collections; -import java.util.EventObject; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedList; - -import com.vaadin.data.Item; -import com.vaadin.data.Property; - -/** - * Class for handling a set of identified Properties. The elements contained in - * a </code>MapItem</code> can be referenced using locally unique identifiers. - * The class supports listeners who are interested in changes to the Property - * set managed by the class. - * - * @author Vaadin Ltd. - * @since 3.0 - */ -@SuppressWarnings("serial") -public class PropertysetItem implements Item, Item.PropertySetChangeNotifier, - Cloneable { - - /* Private representation of the item */ - - /** - * Mapping from property id to property. - */ - private HashMap<Object, Property<?>> map = new HashMap<Object, Property<?>>(); - - /** - * List of all property ids to maintain the order. - */ - private LinkedList<Object> list = new LinkedList<Object>(); - - /** - * List of property set modification listeners. - */ - private LinkedList<Item.PropertySetChangeListener> propertySetChangeListeners = null; - - /* Item methods */ - - /** - * Gets the Property corresponding to the given Property ID stored in the - * Item. If the Item does not contain the Property, <code>null</code> is - * returned. - * - * @param id - * the identifier of the Property to get. - * @return the Property with the given ID or <code>null</code> - */ - @Override - public Property getItemProperty(Object id) { - return map.get(id); - } - - /** - * Gets the collection of IDs of all Properties stored in the Item. - * - * @return unmodifiable collection containing IDs of the Properties stored - * the Item - */ - @Override - public Collection<?> getItemPropertyIds() { - return Collections.unmodifiableCollection(list); - } - - /* Item.Managed methods */ - - /** - * Removes the Property identified by ID from the Item. This functionality - * is optional. If the method is not implemented, the method always returns - * <code>false</code>. - * - * @param id - * the ID of the Property to be removed. - * @return <code>true</code> if the operation succeeded <code>false</code> - * if not - */ - @Override - public boolean removeItemProperty(Object id) { - - // Cant remove missing properties - if (map.remove(id) == null) { - return false; - } - list.remove(id); - - // Send change events - fireItemPropertySetChange(); - - return true; - } - - /** - * Tries to add a new Property into the Item. - * - * @param id - * the ID of the new Property. - * @param property - * the Property to be added and associated with the id. - * @return <code>true</code> if the operation succeeded, <code>false</code> - * if not - */ - @Override - public boolean addItemProperty(Object id, Property property) { - - // Null ids are not accepted - if (id == null) { - throw new NullPointerException("Item property id can not be null"); - } - - // Cant add a property twice - if (map.containsKey(id)) { - return false; - } - - // Put the property to map - map.put(id, property); - list.add(id); - - // Send event - fireItemPropertySetChange(); - - return true; - } - - /** - * Gets the <code>String</code> representation of the contents of the Item. - * The format of the string is a space separated catenation of the - * <code>String</code> representations of the Properties contained by the - * Item. - * - * @return <code>String</code> representation of the Item contents - */ - @Override - public String toString() { - String retValue = ""; - - for (final Iterator<?> i = getItemPropertyIds().iterator(); i.hasNext();) { - final Object propertyId = i.next(); - retValue += getItemProperty(propertyId).getValue(); - if (i.hasNext()) { - retValue += " "; - } - } - - return retValue; - } - - /* Notifiers */ - - /** - * An <code>event</code> object specifying an Item whose Property set has - * changed. - * - * @author Vaadin Ltd. - * @since 3.0 - */ - private static class PropertySetChangeEvent extends EventObject implements - Item.PropertySetChangeEvent { - - private PropertySetChangeEvent(Item source) { - super(source); - } - - /** - * Gets the Item whose Property set has changed. - * - * @return source object of the event as an <code>Item</code> - */ - @Override - public Item getItem() { - return (Item) getSource(); - } - } - - /** - * Registers a new property set change listener for this Item. - * - * @param listener - * the new Listener to be registered. - */ - @Override - public void addPropertySetChangeListener( - Item.PropertySetChangeListener listener) { - if (propertySetChangeListeners == null) { - propertySetChangeListeners = new LinkedList<PropertySetChangeListener>(); - } - propertySetChangeListeners.add(listener); - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #addPropertySetChangeListener(com.vaadin.data.Item.PropertySetChangeListener)} - **/ - @Override - @Deprecated - public void addListener(Item.PropertySetChangeListener listener) { - addPropertySetChangeListener(listener); - } - - /** - * Removes a previously registered property set change listener. - * - * @param listener - * the Listener to be removed. - */ - @Override - public void removePropertySetChangeListener( - Item.PropertySetChangeListener listener) { - if (propertySetChangeListeners != null) { - propertySetChangeListeners.remove(listener); - } - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #removePropertySetChangeListener(com.vaadin.data.Item.PropertySetChangeListener)} - **/ - @Override - @Deprecated - public void removeListener(Item.PropertySetChangeListener listener) { - removePropertySetChangeListener(listener); - } - - /** - * Sends a Property set change event to all interested listeners. - */ - private void fireItemPropertySetChange() { - if (propertySetChangeListeners != null) { - final Object[] l = propertySetChangeListeners.toArray(); - final Item.PropertySetChangeEvent event = new PropertysetItem.PropertySetChangeEvent( - this); - for (int i = 0; i < l.length; i++) { - ((Item.PropertySetChangeListener) l[i]) - .itemPropertySetChange(event); - } - } - } - - public Collection<?> getListeners(Class<?> eventType) { - if (Item.PropertySetChangeEvent.class.isAssignableFrom(eventType)) { - if (propertySetChangeListeners == null) { - return Collections.EMPTY_LIST; - } else { - return Collections - .unmodifiableCollection(propertySetChangeListeners); - } - } - - return Collections.EMPTY_LIST; - } - - /** - * Creates and returns a copy of this object. - * <p> - * The method <code>clone</code> performs a shallow copy of the - * <code>PropertysetItem</code>. - * </p> - * <p> - * Note : All arrays are considered to implement the interface Cloneable. - * Otherwise, this method creates a new instance of the class of this object - * and initializes all its fields with exactly the contents of the - * corresponding fields of this object, as if by assignment, the contents of - * the fields are not themselves cloned. Thus, this method performs a - * "shallow copy" of this object, not a "deep copy" operation. - * </p> - * - * @throws CloneNotSupportedException - * if the object's class does not support the Cloneable - * interface. - * - * @see java.lang.Object#clone() - */ - @Override - public Object clone() throws CloneNotSupportedException { - - final PropertysetItem npsi = new PropertysetItem(); - - npsi.list = list != null ? (LinkedList<Object>) list.clone() : null; - npsi.propertySetChangeListeners = propertySetChangeListeners != null ? (LinkedList<PropertySetChangeListener>) propertySetChangeListeners - .clone() : null; - npsi.map = (HashMap<Object, Property<?>>) map.clone(); - - return npsi; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - - if (obj == null || !(obj instanceof PropertysetItem)) { - return false; - } - - final PropertysetItem other = (PropertysetItem) obj; - - if (other.list != list) { - if (other.list == null) { - return false; - } - if (!other.list.equals(list)) { - return false; - } - } - if (other.map != map) { - if (other.map == null) { - return false; - } - if (!other.map.equals(map)) { - return false; - } - } - if (other.propertySetChangeListeners != propertySetChangeListeners) { - boolean thisEmpty = (propertySetChangeListeners == null || propertySetChangeListeners - .isEmpty()); - boolean otherEmpty = (other.propertySetChangeListeners == null || other.propertySetChangeListeners - .isEmpty()); - if (thisEmpty && otherEmpty) { - return true; - } - if (otherEmpty) { - return false; - } - if (!other.propertySetChangeListeners - .equals(propertySetChangeListeners)) { - return false; - } - } - - return true; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - - return (list == null ? 0 : list.hashCode()) - ^ (map == null ? 0 : map.hashCode()) - ^ ((propertySetChangeListeners == null || propertySetChangeListeners - .isEmpty()) ? 0 : propertySetChangeListeners.hashCode()); - } -} diff --git a/server/src/com/vaadin/data/util/TextFileProperty.java b/server/src/com/vaadin/data/util/TextFileProperty.java deleted file mode 100644 index 22fd8168a1..0000000000 --- a/server/src/com/vaadin/data/util/TextFileProperty.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.nio.charset.Charset; - -/** - * Property implementation for wrapping a text file. - * - * Supports reading and writing of a File from/to String. - * - * {@link ValueChangeListener}s are supported, but only fire when - * setValue(Object) is explicitly called. {@link ReadOnlyStatusChangeListener}s - * are supported but only fire when setReadOnly(boolean) is explicitly called. - * - */ -@SuppressWarnings("serial") -public class TextFileProperty extends AbstractProperty<String> { - - private File file; - private Charset charset = null; - - /** - * Wrap given file with property interface. - * - * Setting the file to null works, but getValue() will return null. - * - * @param file - * File to be wrapped. - */ - public TextFileProperty(File file) { - this.file = file; - } - - /** - * Wrap the given file with the property interface and specify character - * set. - * - * Setting the file to null works, but getValue() will return null. - * - * @param file - * File to be wrapped. - * @param charset - * Charset to be used for reading and writing the file. - */ - public TextFileProperty(File file, Charset charset) { - this.file = file; - this.charset = charset; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Property#getType() - */ - @Override - public Class<String> getType() { - return String.class; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Property#getValue() - */ - @Override - public String getValue() { - if (file == null) { - return null; - } - try { - FileInputStream fis = new FileInputStream(file); - InputStreamReader isr = charset == null ? new InputStreamReader(fis) - : new InputStreamReader(fis, charset); - BufferedReader r = new BufferedReader(isr); - StringBuilder b = new StringBuilder(); - char buf[] = new char[8 * 1024]; - int len; - while ((len = r.read(buf)) != -1) { - b.append(buf, 0, len); - } - r.close(); - isr.close(); - fis.close(); - return b.toString(); - } catch (FileNotFoundException e) { - return null; - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Property#isReadOnly() - */ - @Override - public boolean isReadOnly() { - return file == null || super.isReadOnly() || !file.canWrite(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Property#setValue(java.lang.Object) - */ - @Override - public void setValue(String newValue) throws ReadOnlyException { - if (isReadOnly()) { - throw new ReadOnlyException(); - } - if (file == null) { - return; - } - - try { - FileOutputStream fos = new FileOutputStream(file); - OutputStreamWriter osw = charset == null ? new OutputStreamWriter( - fos) : new OutputStreamWriter(fos, charset); - BufferedWriter w = new BufferedWriter(osw); - w.append(newValue.toString()); - w.flush(); - w.close(); - osw.close(); - fos.close(); - fireValueChange(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - -} diff --git a/server/src/com/vaadin/data/util/TransactionalPropertyWrapper.java b/server/src/com/vaadin/data/util/TransactionalPropertyWrapper.java deleted file mode 100644 index c3cef6a0ec..0000000000 --- a/server/src/com/vaadin/data/util/TransactionalPropertyWrapper.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import com.vaadin.data.Property; -import com.vaadin.data.Property.ValueChangeEvent; -import com.vaadin.data.Property.ValueChangeNotifier; - -/** - * Wrapper class that helps implement two-phase commit for a non-transactional - * property. - * - * When accessing the property through the wrapper, getting and setting the - * property value take place immediately. However, the wrapper keeps track of - * the old value of the property so that it can be set for the property in case - * of a roll-back. This can result in the underlying property value changing - * multiple times (first based on modifications made by the application, then - * back upon roll-back). - * - * Value change events on the {@link TransactionalPropertyWrapper} are only - * fired at the end of a successful transaction, whereas listeners attached to - * the underlying property may receive multiple value change events. - * - * @see com.vaadin.data.Property.Transactional - * - * @author Vaadin Ltd - * @since 7.0 - * - * @param <T> - */ -public class TransactionalPropertyWrapper<T> extends AbstractProperty<T> - implements ValueChangeNotifier, Property.Transactional<T> { - - private Property<T> wrappedProperty; - private boolean inTransaction = false; - private boolean valueChangePending; - private T valueBeforeTransaction; - private final ValueChangeListener listener = new ValueChangeListener() { - - @Override - public void valueChange(ValueChangeEvent event) { - fireValueChange(); - } - }; - - public TransactionalPropertyWrapper(Property<T> wrappedProperty) { - this.wrappedProperty = wrappedProperty; - if (wrappedProperty instanceof ValueChangeNotifier) { - ((ValueChangeNotifier) wrappedProperty) - .addValueChangeListener(listener); - } - } - - /** - * Removes the ValueChangeListener from wrapped Property that was added by - * TransactionalPropertyWrapper. - * - * @since 7.1.15 - */ - public void detachFromProperty() { - if (wrappedProperty instanceof ValueChangeNotifier) { - ((ValueChangeNotifier) wrappedProperty) - .removeValueChangeListener(listener); - } - } - - @Override - public Class getType() { - return wrappedProperty.getType(); - } - - @Override - public T getValue() { - return wrappedProperty.getValue(); - } - - @Override - public void setValue(T newValue) throws ReadOnlyException { - // Causes a value change to be sent to this listener which in turn fires - // a new value change event for this property - wrappedProperty.setValue(newValue); - } - - @Override - public void startTransaction() { - inTransaction = true; - valueBeforeTransaction = getValue(); - } - - @Override - public void commit() { - endTransaction(); - } - - @Override - public void rollback() { - try { - wrappedProperty.setValue(valueBeforeTransaction); - } finally { - valueChangePending = false; - endTransaction(); - } - } - - protected void endTransaction() { - inTransaction = false; - valueBeforeTransaction = null; - if (valueChangePending) { - fireValueChange(); - } - } - - @Override - protected void fireValueChange() { - if (inTransaction) { - valueChangePending = true; - } else { - super.fireValueChange(); - } - } - - public Property<T> getWrappedProperty() { - return wrappedProperty; - } - - @Override - public boolean isReadOnly() { - return wrappedProperty.isReadOnly(); - } - - @Override - public void setReadOnly(boolean newStatus) { - boolean oldStatus = isReadOnly(); - wrappedProperty.setReadOnly(newStatus); - if (oldStatus != isReadOnly()) { - fireReadOnlyStatusChange(); - } - } - -} diff --git a/server/src/com/vaadin/data/util/VaadinPropertyDescriptor.java b/server/src/com/vaadin/data/util/VaadinPropertyDescriptor.java deleted file mode 100644 index 51084d9115..0000000000 --- a/server/src/com/vaadin/data/util/VaadinPropertyDescriptor.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util; - -import java.io.Serializable; - -import com.vaadin.data.Property; - -/** - * Property descriptor that can create a property instance for a bean. - * - * Used by {@link BeanItem} and {@link AbstractBeanContainer} to keep track of - * the set of properties of items. - * - * @param <BT> - * bean type - * - * @since 6.6 - */ -public interface VaadinPropertyDescriptor<BT> extends Serializable { - /** - * Returns the name of the property. - * - * @return - */ - public String getName(); - - /** - * Returns the type of the property. - * - * @return Class<?> - */ - public Class<?> getPropertyType(); - - /** - * Creates a new {@link Property} instance for this property for a bean. - * - * @param bean - * @return - */ - public Property<?> createProperty(BT bean); -} diff --git a/server/src/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java b/server/src/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java deleted file mode 100644 index 81f1a85f89..0000000000 --- a/server/src/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util.converter; - -import java.text.NumberFormat; -import java.text.ParsePosition; -import java.util.Locale; - -/** - * A converter that converts from the number type T to {@link String} and back. - * Uses the given locale and {@link NumberFormat} for formatting and parsing. - * Automatically trims the input string, removing any leading and trailing white - * space. - * <p> - * Override and overwrite {@link #getFormat(Locale)} to use a different format. - * </p> - * - * @author Vaadin Ltd - * @since 7.1 - */ -public abstract class AbstractStringToNumberConverter<T> implements - Converter<String, T> { - - /** - * Returns the format used by {@link #convertToPresentation(Object, Locale)} - * and {@link #convertToModel(Object, Locale)}. - * - * @param locale - * The locale to use - * @return A NumberFormat instance - * @since 7.1 - */ - protected NumberFormat getFormat(Locale locale) { - if (locale == null) { - locale = Locale.getDefault(); - } - - return NumberFormat.getNumberInstance(locale); - } - - /** - * Convert the value to a Number using the given locale and - * {@link #getFormat(Locale)}. - * - * @param value - * The value to convert - * @param locale - * The locale to use for conversion - * @return The converted value - * @throws ConversionException - * If there was a problem converting the value - * @since 7.1 - */ - protected Number convertToNumber(String value, - Class<? extends Number> targetType, Locale locale) - throws ConversionException { - if (value == null) { - return null; - } - - // Remove leading and trailing white space - value = value.trim(); - - // Parse and detect errors. If the full string was not used, it is - // an error. - ParsePosition parsePosition = new ParsePosition(0); - Number parsedValue = getFormat(locale).parse(value, parsePosition); - if (parsePosition.getIndex() != value.length()) { - throw new ConversionException("Could not convert '" + value - + "' to " + getModelType().getName()); - } - - if (parsedValue == null) { - // Convert "" to null - return null; - } - - return parsedValue; - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang - * .Object, java.util.Locale) - */ - @Override - public String convertToPresentation(T value, - Class<? extends String> targetType, Locale locale) - throws ConversionException { - if (value == null) { - return null; - } - - return getFormat(locale).format(value); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getPresentationType() - */ - @Override - public Class<String> getPresentationType() { - return String.class; - } - -} diff --git a/server/src/com/vaadin/data/util/converter/Converter.java b/server/src/com/vaadin/data/util/converter/Converter.java deleted file mode 100644 index a7188ebb33..0000000000 --- a/server/src/com/vaadin/data/util/converter/Converter.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util.converter; - -import java.io.Serializable; -import java.util.Locale; - -/** - * Interface that implements conversion between a model and a presentation type. - * <p> - * Typically {@link #convertToPresentation(Object, Class, Locale)} and - * {@link #convertToModel(Object, Class, Locale)} should be symmetric so that - * chaining these together returns the original result for all input but this is - * not a requirement. - * </p> - * <p> - * Converters must not have any side effects (never update UI from inside a - * converter). - * </p> - * <p> - * All Converters must be stateless and thread safe. - * </p> - * <p> - * If conversion of a value fails, a {@link ConversionException} is thrown. - * </p> - * - * @param <PRESENTATION> - * The presentation type. Must be compatible with what - * {@link #getPresentationType()} returns. - * @param <MODEL> - * The model type. Must be compatible with what - * {@link #getModelType()} returns. - * @author Vaadin Ltd. - * @since 7.0 - */ -public interface Converter<PRESENTATION, MODEL> extends Serializable { - - /** - * Converts the given value from target type to source type. - * <p> - * A converter can optionally use locale to do the conversion. - * </p> - * A converter should in most cases be symmetric so chaining - * {@link #convertToPresentation(Object, Class, Locale)} and - * {@link #convertToModel(Object, Class, Locale)} should return the original - * value. - * - * @param value - * The value to convert, compatible with the target type. Can be - * null - * @param targetType - * The requested type of the return value - * @param locale - * The locale to use for conversion. Can be null. - * @return The converted value compatible with the source type - * @throws ConversionException - * If the value could not be converted - */ - public MODEL convertToModel(PRESENTATION value, - Class<? extends MODEL> targetType, Locale locale) - throws ConversionException; - - /** - * Converts the given value from source type to target type. - * <p> - * A converter can optionally use locale to do the conversion. - * </p> - * A converter should in most cases be symmetric so chaining - * {@link #convertToPresentation(Object, Class, Locale)} and - * {@link #convertToModel(Object, Class, Locale)} should return the original - * value. - * - * @param value - * The value to convert, compatible with the target type. Can be - * null - * @param targetType - * The requested type of the return value - * @param locale - * The locale to use for conversion. Can be null. - * @return The converted value compatible with the source type - * @throws ConversionException - * If the value could not be converted - */ - public PRESENTATION convertToPresentation(MODEL value, - Class<? extends PRESENTATION> targetType, Locale locale) - throws ConversionException; - - /** - * The source type of the converter. - * - * Values of this type can be passed to - * {@link #convertToPresentation(Object, Class, Locale)}. - * - * @return The source type - */ - public Class<MODEL> getModelType(); - - /** - * The target type of the converter. - * - * Values of this type can be passed to - * {@link #convertToModel(Object, Class, Locale)}. - * - * @return The target type - */ - public Class<PRESENTATION> getPresentationType(); - - /** - * An exception that signals that the value passed to - * {@link Converter#convertToPresentation(Object, Class, Locale)} or - * {@link Converter#convertToModel(Object, Class, Locale)} could not be - * converted. - * - * @author Vaadin Ltd - * @since 7.0 - */ - public static class ConversionException extends RuntimeException { - - /** - * Constructs a new <code>ConversionException</code> without a detail - * message. - */ - public ConversionException() { - } - - /** - * Constructs a new <code>ConversionException</code> with the specified - * detail message. - * - * @param msg - * the detail message - */ - public ConversionException(String msg) { - super(msg); - } - - /** - * Constructs a new {@code ConversionException} with the specified - * cause. - * - * @param cause - * The cause of the the exception - */ - public ConversionException(Throwable cause) { - super(cause); - } - - /** - * Constructs a new <code>ConversionException</code> with the specified - * detail message and cause. - * - * @param message - * the detail message - * @param cause - * The cause of the the exception - */ - public ConversionException(String message, Throwable cause) { - super(message, cause); - } - } - -} diff --git a/server/src/com/vaadin/data/util/converter/ConverterFactory.java b/server/src/com/vaadin/data/util/converter/ConverterFactory.java deleted file mode 100644 index 0280be4a73..0000000000 --- a/server/src/com/vaadin/data/util/converter/ConverterFactory.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util.converter; - -import java.io.Serializable; - -/** - * Factory interface for providing Converters based on a presentation type and a - * model type. - * - * @author Vaadin Ltd. - * @since 7.0 - * - */ -public interface ConverterFactory extends Serializable { - public <PRESENTATION, MODEL> Converter<PRESENTATION, MODEL> createConverter( - Class<PRESENTATION> presentationType, Class<MODEL> modelType); - -} diff --git a/server/src/com/vaadin/data/util/converter/ConverterUtil.java b/server/src/com/vaadin/data/util/converter/ConverterUtil.java deleted file mode 100644 index 91e11a6222..0000000000 --- a/server/src/com/vaadin/data/util/converter/ConverterUtil.java +++ /dev/null @@ -1,261 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.converter; - -import java.io.Serializable; -import java.util.Locale; - -import com.vaadin.server.VaadinSession; - -public class ConverterUtil implements Serializable { - - /** - * Finds a converter that can convert from the given presentation type to - * the given model type and back. Uses the given application to find a - * {@link ConverterFactory} or, if application is null, uses the - * {@link VaadinSession#getCurrent()}. - * - * @param <PRESENTATIONTYPE> - * the presentation type - * @param <MODELTYPE> - * the model type - * @param presentationType - * the presentation type - * @param modelType - * the model type - * @param session - * the session to use to find a ConverterFactory or null to use - * the current session - * @return a Converter capable of converting between the given types or null - * if no converter was found - */ - public static <PRESENTATIONTYPE, MODELTYPE> Converter<PRESENTATIONTYPE, MODELTYPE> getConverter( - Class<PRESENTATIONTYPE> presentationType, - Class<MODELTYPE> modelType, VaadinSession session) { - Converter<PRESENTATIONTYPE, MODELTYPE> converter = null; - if (session == null) { - session = VaadinSession.getCurrent(); - } - - if (session != null) { - ConverterFactory factory = session.getConverterFactory(); - converter = factory.createConverter(presentationType, modelType); - } - return converter; - - } - - /** - * Convert the given value from the data source type to the UI type. - * - * @param modelValue - * the model value to convert - * @param presentationType - * the type of the presentation value - * @param converter - * the converter to use - * @param locale - * the locale to use for conversion - * @param <PRESENTATIONTYPE> - * the presentation type - * @param <MODELTYPE> - * the model type - * - * @return the converted value, compatible with the presentation type, or - * the original value if its type is compatible and no converter is - * set. - * @throws Converter.ConversionException - * if there was a problem converting the value - */ - @SuppressWarnings("unchecked") - public static <PRESENTATIONTYPE, MODELTYPE> PRESENTATIONTYPE convertFromModel( - MODELTYPE modelValue, - Class<? extends PRESENTATIONTYPE> presentationType, - Converter<PRESENTATIONTYPE, MODELTYPE> converter, Locale locale) - throws Converter.ConversionException { - if (converter != null) { - /* - * If there is a converter, always use it. It must convert or throw - * an exception. - */ - PRESENTATIONTYPE presentation = converter.convertToPresentation( - modelValue, presentationType, locale); - if (presentation != null - && !presentationType.isInstance(presentation)) { - throw new Converter.ConversionException( - "Converter returned an object of type " - + presentation.getClass().getName() - + " when expecting " - + presentationType.getName()); - } - - return presentation; - } - if (modelValue == null) { - // Null should always be passed through the converter but if there - // is no converter we can safely return null - return null; - } - - if (presentationType.isAssignableFrom(modelValue.getClass())) { - return (PRESENTATIONTYPE) modelValue; - } else { - throw new Converter.ConversionException( - "Unable to convert value of type " - + modelValue.getClass().getName() - + " to presentation type " - + presentationType - + ". No converter is set and the types are not compatible."); - } - } - - /** - * Convert the given value from the presentation (UI) type to model (data - * source) type. - * - * @param presentationValue - * the presentation value to convert - * @param modelType - * the type of the model - * @param converter - * the converter to use - * @param locale - * the locale to use for conversion - * @param <PRESENTATIONTYPE> - * the presentation type - * @param <MODELTYPE> - * the model type - * - * @return the converted value, compatible with the model type, or the - * original value if its type is compatible and no converter is set. - * @throws Converter.ConversionException - * if there was a problem converting the value - */ - public static <MODELTYPE, PRESENTATIONTYPE> MODELTYPE convertToModel( - PRESENTATIONTYPE presentationValue, Class<MODELTYPE> modelType, - Converter<PRESENTATIONTYPE, MODELTYPE> converter, Locale locale) - throws Converter.ConversionException { - if (converter != null) { - /* - * If there is a converter, always use it. It must convert or throw - * an exception. - */ - MODELTYPE model = converter.convertToModel(presentationValue, - modelType, locale); - if (model != null && !modelType.isInstance(model)) { - throw new Converter.ConversionException( - "Converter returned an object of type " - + model.getClass().getName() - + " when expecting " + modelType.getName()); - } - - return model; - } - - if (presentationValue == null) { - // Null should always be passed through the converter but if there - // is no converter we can safely return null - return null; - } - - if (modelType == null) { - // No model type, return original value - return (MODELTYPE) presentationValue; - } else if (modelType.isAssignableFrom(presentationValue.getClass())) { - // presentation type directly compatible with model type - return modelType.cast(presentationValue); - } else { - throw new Converter.ConversionException( - "Unable to convert value of type " - + presentationValue.getClass().getName() - + " to model type " - + modelType - + ". No converter is set and the types are not compatible."); - } - - } - - /** - * Checks if the given converter can handle conversion between the given - * presentation and model type. Does strict type checking and only returns - * true if the converter claims it can handle exactly the given types. - * - * @see #canConverterPossiblyHandle(Converter, Class, Class) - * - * @param converter - * The converter to check. If this is null the result is always - * false. - * @param presentationType - * The presentation type - * @param modelType - * The model type - * @return true if the converter supports conversion between the given - * presentation and model type, false otherwise - */ - public static boolean canConverterHandle(Converter<?, ?> converter, - Class<?> presentationType, Class<?> modelType) { - if (converter == null) { - return false; - } - - if (modelType != converter.getModelType()) { - return false; - } - if (presentationType != converter.getPresentationType()) { - return false; - } - - return true; - } - - /** - * Checks if it possible that the given converter can handle conversion - * between the given presentation and model type somehow. - * - * @param converter - * The converter to check. If this is null the result is always - * false. - * @param presentationType - * The presentation type - * @param modelType - * The model type - * @return true if the converter possibly support conversion between the - * given presentation and model type, false otherwise - */ - public static boolean canConverterPossiblyHandle(Converter<?, ?> converter, - Class<?> presentationType, Class<?> modelType) { - if (converter == null) { - return false; - } - Class<?> converterModelType = converter.getModelType(); - - if (!modelType.isAssignableFrom(converterModelType) - && !converterModelType.isAssignableFrom(modelType)) { - // model types are not compatible in any way - return false; - } - - Class<?> converterPresentationType = converter.getPresentationType(); - if (!presentationType.isAssignableFrom(converterPresentationType) - && !converterPresentationType - .isAssignableFrom(presentationType)) { - // presentation types are not compatible in any way - return false; - } - - return true; - } -} diff --git a/server/src/com/vaadin/data/util/converter/DateToLongConverter.java b/server/src/com/vaadin/data/util/converter/DateToLongConverter.java deleted file mode 100644 index fffe80352e..0000000000 --- a/server/src/com/vaadin/data/util/converter/DateToLongConverter.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util.converter; - -import java.util.Date; -import java.util.Locale; - -/** - * A converter that converts from {@link Long} to {@link Date} and back. - * - * @author Vaadin Ltd - * @since 7.0 - */ -public class DateToLongConverter implements Converter<Date, Long> { - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.lang.Class, java.util.Locale) - */ - @Override - public Long convertToModel(Date value, Class<? extends Long> targetType, - Locale locale) { - if (value == null) { - return null; - } - - return value.getTime(); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang - * .Object, java.lang.Class, java.util.Locale) - */ - @Override - public Date convertToPresentation(Long value, - Class<? extends Date> targetType, Locale locale) { - if (targetType != getPresentationType()) { - throw new ConversionException("Converter only supports " - + getPresentationType().getName() + " (targetType was " - + targetType.getName() + ")"); - } - if (value == null) { - return null; - } - - return new Date(value); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getModelType() - */ - @Override - public Class<Long> getModelType() { - return Long.class; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getPresentationType() - */ - @Override - public Class<Date> getPresentationType() { - return Date.class; - } - -} diff --git a/server/src/com/vaadin/data/util/converter/DateToSqlDateConverter.java b/server/src/com/vaadin/data/util/converter/DateToSqlDateConverter.java deleted file mode 100644 index 6a7e8327a1..0000000000 --- a/server/src/com/vaadin/data/util/converter/DateToSqlDateConverter.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -/** - * - */ -package com.vaadin.data.util.converter; - -import java.util.Date; -import java.util.Locale; - -/** - * Converter for handling conversion between {@link java.util.Date} and - * {@link java.sql.Date}. This is used when a PopupDateField or InlineDateField - * is connected to a java.sql.Date property, typically through a JPAContainer or - * SQLContainer. Note that information (time information) is lost when - * converting from {@link java.util.Date} to {@link java.sql.Date}. - * - * @since 7.1 - * @author Vaadin Ltd - */ -public class DateToSqlDateConverter implements Converter<Date, java.sql.Date> { - - @Override - public java.sql.Date convertToModel(Date value, - Class<? extends java.sql.Date> targetType, Locale locale) - throws ConversionException { - if (targetType != getModelType()) { - throw new ConversionException("Converter only supports " - + getModelType().getName() + " (targetType was " - + targetType.getName() + ")"); - } - - if (value == null) { - return null; - } - - return new java.sql.Date(value.getTime()); - } - - @Override - public Date convertToPresentation(java.sql.Date value, - Class<? extends Date> targetType, Locale locale) - throws ConversionException { - if (targetType != getPresentationType()) { - throw new ConversionException("Converter only supports " - + getPresentationType().getName() + " (targetType was " - + targetType.getName() + ")"); - } - - if (value == null) { - return null; - } - - return new Date(value.getTime()); - } - - @Override - public Class<java.sql.Date> getModelType() { - return java.sql.Date.class; - } - - @Override - public Class<Date> getPresentationType() { - return Date.class; - } - -} diff --git a/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java b/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java deleted file mode 100644 index 3a1f1a4252..0000000000 --- a/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util.converter; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.Date; -import java.util.logging.Logger; - -import com.vaadin.server.VaadinSession; - -/** - * Default implementation of {@link ConverterFactory}. Provides converters for - * standard types like {@link String}, {@link Double} and {@link Date}. </p> - * <p> - * Custom converters can be provided by extending this class and using - * {@link VaadinSession#setConverterFactory(ConverterFactory)}. - * </p> - * - * @author Vaadin Ltd - * @since 7.0 - */ -public class DefaultConverterFactory implements ConverterFactory { - - private final static Logger log = Logger - .getLogger(DefaultConverterFactory.class.getName()); - - @Override - public <PRESENTATION, MODEL> Converter<PRESENTATION, MODEL> createConverter( - Class<PRESENTATION> presentationType, Class<MODEL> modelType) { - Converter<PRESENTATION, MODEL> converter = findConverter( - presentationType, modelType); - if (converter != null) { - log.finest(getClass().getName() + " created a " - + converter.getClass()); - return converter; - } - - // Try to find a reverse converter - Converter<MODEL, PRESENTATION> reverseConverter = findConverter( - modelType, presentationType); - if (reverseConverter != null) { - log.finest(getClass().getName() + " created a reverse " - + reverseConverter.getClass()); - return new ReverseConverter<PRESENTATION, MODEL>(reverseConverter); - } - - log.finest(getClass().getName() + " could not find a converter for " - + presentationType.getName() + " to " + modelType.getName() - + " conversion"); - return null; - - } - - protected <PRESENTATION, MODEL> Converter<PRESENTATION, MODEL> findConverter( - Class<PRESENTATION> presentationType, Class<MODEL> modelType) { - if (presentationType == String.class) { - // TextField converters and more - Converter<PRESENTATION, MODEL> converter = (Converter<PRESENTATION, MODEL>) createStringConverter(modelType); - if (converter != null) { - return converter; - } - } else if (presentationType == Date.class) { - // DateField converters and more - Converter<PRESENTATION, MODEL> converter = (Converter<PRESENTATION, MODEL>) createDateConverter(modelType); - if (converter != null) { - return converter; - } - } - - return null; - - } - - protected Converter<Date, ?> createDateConverter(Class<?> sourceType) { - if (Long.class.isAssignableFrom(sourceType)) { - return new DateToLongConverter(); - } else if (java.sql.Date.class.isAssignableFrom(sourceType)) { - return new DateToSqlDateConverter(); - } else { - return null; - } - } - - protected Converter<String, ?> createStringConverter(Class<?> sourceType) { - if (Double.class.isAssignableFrom(sourceType)) { - return new StringToDoubleConverter(); - } else if (Float.class.isAssignableFrom(sourceType)) { - return new StringToFloatConverter(); - } else if (Integer.class.isAssignableFrom(sourceType)) { - return new StringToIntegerConverter(); - } else if (Long.class.isAssignableFrom(sourceType)) { - return new StringToLongConverter(); - } else if (BigDecimal.class.isAssignableFrom(sourceType)) { - return new StringToBigDecimalConverter(); - } else if (Boolean.class.isAssignableFrom(sourceType)) { - return new StringToBooleanConverter(); - } else if (Date.class.isAssignableFrom(sourceType)) { - return new StringToDateConverter(); - } else if (Enum.class.isAssignableFrom(sourceType)) { - return new StringToEnumConverter(); - } else if (BigInteger.class.isAssignableFrom(sourceType)) { - return new StringToBigIntegerConverter(); - } else if (Short.class.isAssignableFrom(sourceType)) { - return new StringToShortConverter(); - } else if (Byte.class.isAssignableFrom(sourceType)) { - return new StringToByteConverter(); - } else { - return null; - } - } - -} diff --git a/server/src/com/vaadin/data/util/converter/ReverseConverter.java b/server/src/com/vaadin/data/util/converter/ReverseConverter.java deleted file mode 100644 index 2495dc631a..0000000000 --- a/server/src/com/vaadin/data/util/converter/ReverseConverter.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util.converter; - -import java.util.Locale; - -/** - * A converter that wraps another {@link Converter} and reverses source and - * target types. - * - * @param <MODEL> - * The source type - * @param <PRESENTATION> - * The target type - * - * @author Vaadin Ltd - * @since 7.0 - */ -public class ReverseConverter<PRESENTATION, MODEL> implements - Converter<PRESENTATION, MODEL> { - - private Converter<MODEL, PRESENTATION> realConverter; - - /** - * Creates a converter from source to target based on a converter that - * converts from target to source. - * - * @param converter - * The converter to use in a reverse fashion - */ - public ReverseConverter(Converter<MODEL, PRESENTATION> converter) { - this.realConverter = converter; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#convertToModel(java - * .lang.Object, java.util.Locale) - */ - @Override - public MODEL convertToModel(PRESENTATION value, - Class<? extends MODEL> targetType, Locale locale) - throws com.vaadin.data.util.converter.Converter.ConversionException { - return realConverter.convertToPresentation(value, targetType, locale); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang - * .Object, java.util.Locale) - */ - @Override - public PRESENTATION convertToPresentation(MODEL value, - Class<? extends PRESENTATION> targetType, Locale locale) - throws com.vaadin.data.util.converter.Converter.ConversionException { - return realConverter.convertToModel(value, targetType, locale); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getSourceType() - */ - @Override - public Class<MODEL> getModelType() { - return realConverter.getPresentationType(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getTargetType() - */ - @Override - public Class<PRESENTATION> getPresentationType() { - return realConverter.getModelType(); - } - -} diff --git a/server/src/com/vaadin/data/util/converter/StringToBigDecimalConverter.java b/server/src/com/vaadin/data/util/converter/StringToBigDecimalConverter.java deleted file mode 100644 index 549f156f61..0000000000 --- a/server/src/com/vaadin/data/util/converter/StringToBigDecimalConverter.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.converter; - -import java.math.BigDecimal; -import java.text.DecimalFormat; -import java.text.NumberFormat; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link BigDecimal} and back. - * Uses the given locale and a {@link NumberFormat} instance for formatting and - * parsing. - * <p> - * Leading and trailing white spaces are ignored when converting from a String. - * </p> - * <p> - * Override and overwrite {@link #getFormat(Locale)} to use a different format. - * </p> - * - * @author Vaadin Ltd - * @since 7.2 - */ -public class StringToBigDecimalConverter extends - AbstractStringToNumberConverter<BigDecimal> { - @Override - protected NumberFormat getFormat(Locale locale) { - NumberFormat numberFormat = super.getFormat(locale); - if (numberFormat instanceof DecimalFormat) { - ((DecimalFormat) numberFormat).setParseBigDecimal(true); - } - - return numberFormat; - } - - @Override - public BigDecimal convertToModel(String value, - Class<? extends BigDecimal> targetType, Locale locale) - throws com.vaadin.data.util.converter.Converter.ConversionException { - return (BigDecimal) convertToNumber(value, BigDecimal.class, locale); - } - - @Override - public Class<BigDecimal> getModelType() { - return BigDecimal.class; - } -} diff --git a/server/src/com/vaadin/data/util/converter/StringToBigIntegerConverter.java b/server/src/com/vaadin/data/util/converter/StringToBigIntegerConverter.java deleted file mode 100644 index adaa8c6111..0000000000 --- a/server/src/com/vaadin/data/util/converter/StringToBigIntegerConverter.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.converter; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.text.DecimalFormat; -import java.text.NumberFormat; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link BigInteger} and back. - * Uses the given locale and a {@link NumberFormat} instance for formatting and - * parsing. - * <p> - * Leading and trailing white spaces are ignored when converting from a String. - * </p> - * <p> - * Override and overwrite {@link #getFormat(Locale)} to use a different format. - * </p> - * - * @author Vaadin Ltd - * @since 7.4 - */ -public class StringToBigIntegerConverter extends - AbstractStringToNumberConverter<BigInteger> { - - @Override - protected NumberFormat getFormat(Locale locale) { - NumberFormat numberFormat = super.getFormat(locale); - if (numberFormat instanceof DecimalFormat) { - ((DecimalFormat) numberFormat).setParseBigDecimal(true); - } - - return numberFormat; - } - - @Override - public BigInteger convertToModel(String value, - Class<? extends BigInteger> targetType, Locale locale) - throws com.vaadin.data.util.converter.Converter.ConversionException { - - BigDecimal bigDecimalValue = (BigDecimal) convertToNumber(value, - BigDecimal.class, locale); - - return (bigDecimalValue != null) ? bigDecimalValue.toBigInteger() - : null; - } - - @Override - public Class<BigInteger> getModelType() { - return BigInteger.class; - } -} diff --git a/server/src/com/vaadin/data/util/converter/StringToBooleanConverter.java b/server/src/com/vaadin/data/util/converter/StringToBooleanConverter.java deleted file mode 100644 index f965cfcc6a..0000000000 --- a/server/src/com/vaadin/data/util/converter/StringToBooleanConverter.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util.converter; - -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link Boolean} and back. The String representation is given by - * {@link Boolean#toString()} or provided in constructor {@link #StringToBooleanConverter(String, String)}. - * <p> Leading and trailing white spaces are ignored when converting from a String. </p> - * <p> For language-dependent representation, subclasses should overwrite {@link #getFalseString(Locale)} and {@link #getTrueString(Locale)}</p> - * - * @author Vaadin Ltd - * @since 7.0 - */ -public class StringToBooleanConverter implements Converter<String, Boolean> { - - private final String trueString; - - private final String falseString; - - /** - * Creates converter with default string representations - "true" and "false" - * - */ - public StringToBooleanConverter() { - this(Boolean.TRUE.toString(), Boolean.FALSE.toString()); - } - - /** - * Creates converter with custom string representation. - * - * @since 7.5.4 - * @param falseString string representation for <code>false</code> - * @param trueString string representation for <code>true</code> - */ - public StringToBooleanConverter(String trueString, String falseString) { - this.trueString = trueString; - this.falseString = falseString; - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.lang.Class, java.util.Locale) - */ - @Override - public Boolean convertToModel(String value, - Class<? extends Boolean> targetType, Locale locale) - throws ConversionException { - if (value == null || value.isEmpty()) { - return null; - } - - // Remove leading and trailing white space - value = value.trim(); - - if (getTrueString().equals(value)) { - return true; - } else if (getFalseString().equals(value)) { - return false; - } else { - throw new ConversionException("Cannot convert " + value + " to " - + getModelType().getName()); - } - } - - /** - * Gets the string representation for true. Default is "true", if not set in constructor. - * - * @return the string representation for true - */ - protected String getTrueString() { - return trueString; - } - - /** - * Gets the string representation for false. Default is "false", if not set in constructor. - * - * @return the string representation for false - */ - protected String getFalseString() { - return falseString; - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang - * .Object, java.lang.Class, java.util.Locale) - */ - @Override - public String convertToPresentation(Boolean value, - Class<? extends String> targetType, Locale locale) - throws ConversionException { - if (value == null) { - return null; - } - if (value) { - return getTrueString(locale); - } else { - return getFalseString(locale); - } - } - - /** - * Gets the locale-depended string representation for false. - * Default is locale-independent value provided by {@link #getFalseString()} - * - * @since 7.5.4 - * @param locale to be used - * @return the string representation for false - */ - protected String getFalseString(Locale locale) { - return getFalseString(); - } - - /** - * Gets the locale-depended string representation for true. - * Default is locale-independent value provided by {@link #getTrueString()} - * - * @since 7.5.4 - * @param locale to be used - * @return the string representation for true - */ - protected String getTrueString(Locale locale) { - return getTrueString(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getModelType() - */ - @Override - public Class<Boolean> getModelType() { - return Boolean.class; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getPresentationType() - */ - @Override - public Class<String> getPresentationType() { - return String.class; - } - -} diff --git a/server/src/com/vaadin/data/util/converter/StringToByteConverter.java b/server/src/com/vaadin/data/util/converter/StringToByteConverter.java deleted file mode 100644 index 719fced0ed..0000000000 --- a/server/src/com/vaadin/data/util/converter/StringToByteConverter.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util.converter; - -import java.text.NumberFormat; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link Byte} and back. Uses - * the given locale and a {@link NumberFormat} instance for formatting and - * parsing. - * <p> - * Override and overwrite {@link #getFormat(Locale)} to use a different format. - * </p> - * - * @author Vaadin Ltd - * @since 7.4 - */ -public class StringToByteConverter extends - AbstractStringToNumberConverter<Byte> { - - /** - * Returns the format used by - * {@link #convertToPresentation(Byte, Class, Locale)} and - * {@link #convertToModel(String, Class, Locale)} - * - * @param locale - * The locale to use - * @return A NumberFormat instance - */ - @Override - protected NumberFormat getFormat(Locale locale) { - if (locale == null) { - locale = Locale.getDefault(); - } - return NumberFormat.getIntegerInstance(locale); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.lang.Class, java.util.Locale) - */ - @Override - public Byte convertToModel(String value, Class<? extends Byte> targetType, - Locale locale) throws ConversionException { - Number n = convertToNumber(value, targetType, locale); - - if (n == null) { - return null; - } - - byte byteValue = n.byteValue(); - if (byteValue == n.longValue()) { - return byteValue; - } - - throw new ConversionException("Could not convert '" + value + "' to " - + Byte.class.getName() + ": value out of range"); - - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getModelType() - */ - @Override - public Class<Byte> getModelType() { - return Byte.class; - } - -} diff --git a/server/src/com/vaadin/data/util/converter/StringToCollectionConverter.java b/server/src/com/vaadin/data/util/converter/StringToCollectionConverter.java deleted file mode 100644 index 6930b6da06..0000000000 --- a/server/src/com/vaadin/data/util/converter/StringToCollectionConverter.java +++ /dev/null @@ -1,243 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.converter; - -import java.io.Serializable; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link Collection} of tokens - * and back. - * <p> - * Allows to break a string into tokens using delimiter. Each token can be - * converted to its own model using provided converter. - * <p> - * Default constructor uses <code>", "</code> as delimiter string and - * {@link String} for token types. Other constructors allow to configure - * delimiter and token types. - * - * @since 7.5.0 - * - * @author Vaadin Ltd - */ -public class StringToCollectionConverter implements - Converter<String, Collection> { - - private final String delimiter; - private final Converter<String, ?> tokenConverter; - private final Class<?> tokenType; - private final CollectionFactory factory; - - /** - * Creates converter with <code>", "</code> as delimiter and {@link String} - * as token model type in collection. - */ - public StringToCollectionConverter() { - this(", ", null, String.class); - } - - /** - * Creates converter with given {@code delimiter} and {@link String} as - * token model type in collection. - * - * @param delimiter - * custom delimiter - */ - public StringToCollectionConverter(String delimiter) { - this(delimiter, null, String.class); - } - - /** - * Creates converter with given {@code tokenConverter} for convert tokens - * and expected {@code tokenType}. - * <p> - * If {@code tokenConverter} is null then no conversation is done and - * {@link String} is used as token type in resulting model collection. - * - * @param tokenConverter - * converter for token - * @param tokenType - * expected token model type - */ - public StringToCollectionConverter(Converter<String, ?> tokenConverter, - Class<?> tokenType) { - this(", ", tokenConverter, tokenType); - } - - /** - * Creates converter with given {@code tokenConverter} for convert tokens - * and expected {@code tokenType}. - * <p> - * If {@code tokenConverter} is null then no conversation is done and - * {@link String} is used as token type in resulting model collection. - * - * @param tokenConverter - * converter for token - * @param tokenType - * expected token model type - * @param delimiter - * delimiter in presentation string - */ - public StringToCollectionConverter(String delimiter, - Converter<String, ?> tokenConverter, Class<?> tokenClass) { - this(delimiter, tokenConverter, tokenClass, - new DefaultCollectionFactory()); - } - - /** - * Creates converter with given {@code tokenConverter} for convert tokens - * and expected {@code tokenType}. - * <p> - * If {@code tokenConverter} is null then no conversation is done and - * {@link String} is used as token type in resulting model collection. - * - * @param tokenConverter - * converter for token - * @param tokenType - * expected token model type - * @param delimiter - * delimiter in presentation string - * @param factory - * factory to create resulting collection - */ - public StringToCollectionConverter(String delimiter, - Converter<String, ?> tokenConverter, Class<?> tokenClass, - CollectionFactory factory) { - if (delimiter == null || delimiter.isEmpty()) { - throw new IllegalArgumentException( - "Delimiter should be non-empty string"); - } - this.delimiter = delimiter; - this.tokenConverter = tokenConverter; - tokenType = tokenClass; - this.factory = factory; - } - - @Override - public Class<Collection> getModelType() { - return Collection.class; - } - - @Override - public Class<String> getPresentationType() { - return String.class; - } - - @Override - public Collection convertToModel(String value, - Class<? extends Collection> targetType, Locale locale) - throws Converter.ConversionException { - if (value == null) { - return null; - } - - int index = value.indexOf(delimiter); - int previous = 0; - Collection result = factory.createCollection(targetType); - Converter converter = tokenConverter; - while (index != -1) { - collectToken(value.substring(previous, index), result, converter, - locale); - previous = index + delimiter.length(); - index = value.indexOf(delimiter, previous); - } - collectToken(value.substring(previous), result, converter, locale); - return result; - } - - @Override - public String convertToPresentation(Collection value, - Class<? extends String> targetType, Locale locale) - throws Converter.ConversionException { - if (value == null) { - return null; - } - StringBuilder builder = new StringBuilder(); - Converter converter = tokenConverter; - for (Iterator<?> iterator = value.iterator(); iterator.hasNext();) { - if (converter == null) { - builder.append(iterator.next()); - } else { - builder.append(converter.convertToPresentation(iterator.next(), - targetType, locale)); - } - builder.append(delimiter); - } - if (builder.length() > 0) { - return builder.substring(0, builder.length() - delimiter.length()); - } else { - return builder.toString(); - } - } - - private void collectToken(String token, Collection collection, - Converter converter, Locale locale) { - if (converter == null) { - collection.add(token); - } else { - collection.add(converter.convertToModel(token, tokenType, locale)); - } - } - - /** - * Default collection factory implementation. - * - * @author Vaadin Ltd - */ - public static class DefaultCollectionFactory implements CollectionFactory { - - @Override - public Collection<?> createCollection(Class<? extends Collection> type) { - if (type.isAssignableFrom(ArrayList.class)) { - return new ArrayList(); - } else if (type.isAssignableFrom(HashSet.class)) { - return new HashSet(); - } else if (!type.isInterface() - && !Modifier.isAbstract(type.getModifiers())) { - try { - return type.newInstance(); - } catch (InstantiationException ignore) { - } catch (IllegalAccessException ignore) { - } - } - return new ArrayList(); - } - - } - - /** - * Collection factory. Defines a strategy to create collection by collection - * class. - * - * @author Vaadin Ltd - */ - public interface CollectionFactory extends Serializable { - - /** - * Create collection by its {@code type}. - * - * @param type - * collection type - * @return instantiated collection with given {@code type} - */ - Collection<?> createCollection(Class<? extends Collection> type); - } -} diff --git a/server/src/com/vaadin/data/util/converter/StringToDateConverter.java b/server/src/com/vaadin/data/util/converter/StringToDateConverter.java deleted file mode 100644 index 8f0db24caf..0000000000 --- a/server/src/com/vaadin/data/util/converter/StringToDateConverter.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util.converter; - -import java.text.DateFormat; -import java.text.ParsePosition; -import java.util.Date; -import java.util.Locale; - -/** - * A converter that converts from {@link Date} to {@link String} and back. Uses - * the given locale and {@link DateFormat} for formatting and parsing. - * <p> - * Leading and trailing white spaces are ignored when converting from a String. - * </p> - * <p> - * Override and overwrite {@link #getFormat(Locale)} to use a different format. - * </p> - * - * @author Vaadin Ltd - * @since 7.0 - */ -public class StringToDateConverter implements Converter<String, Date> { - - /** - * Returns the format used by - * {@link #convertToPresentation(Date, Class,Locale)} and - * {@link #convertToModel(String, Class, Locale)}. - * - * @param locale - * The locale to use - * @return A DateFormat instance - */ - protected DateFormat getFormat(Locale locale) { - if (locale == null) { - locale = Locale.getDefault(); - } - - DateFormat f = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, - DateFormat.MEDIUM, locale); - f.setLenient(false); - return f; - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.lang.Class, java.util.Locale) - */ - @Override - public Date convertToModel(String value, Class<? extends Date> targetType, - Locale locale) - throws com.vaadin.data.util.converter.Converter.ConversionException { - if (targetType != getModelType()) { - throw new ConversionException("Converter only supports " - + getModelType().getName() + " (targetType was " - + targetType.getName() + ")"); - } - - if (value == null) { - return null; - } - - // Remove leading and trailing white space - value = value.trim(); - - ParsePosition parsePosition = new ParsePosition(0); - Date parsedValue = getFormat(locale).parse(value, parsePosition); - if (parsePosition.getIndex() != value.length()) { - throw new ConversionException("Could not convert '" + value - + "' to " + getModelType().getName()); - } - - return parsedValue; - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang - * .Object, java.lang.Class, java.util.Locale) - */ - @Override - public String convertToPresentation(Date value, - Class<? extends String> targetType, Locale locale) - throws com.vaadin.data.util.converter.Converter.ConversionException { - if (value == null) { - return null; - } - - return getFormat(locale).format(value); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getModelType() - */ - @Override - public Class<Date> getModelType() { - return Date.class; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getPresentationType() - */ - @Override - public Class<String> getPresentationType() { - return String.class; - } - -} diff --git a/server/src/com/vaadin/data/util/converter/StringToDoubleConverter.java b/server/src/com/vaadin/data/util/converter/StringToDoubleConverter.java deleted file mode 100644 index f514eac648..0000000000 --- a/server/src/com/vaadin/data/util/converter/StringToDoubleConverter.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util.converter; - -import java.text.NumberFormat; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link Double} and back. - * Uses the given locale and a {@link NumberFormat} instance for formatting and - * parsing. - * <p> - * Leading and trailing white spaces are ignored when converting from a String. - * </p> - * <p> - * Override and overwrite {@link #getFormat(Locale)} to use a different format. - * </p> - * - * @author Vaadin Ltd - * @since 7.0 - */ -public class StringToDoubleConverter extends - AbstractStringToNumberConverter<Double> { - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.util.Locale) - */ - @Override - public Double convertToModel(String value, - Class<? extends Double> targetType, Locale locale) - throws ConversionException { - Number n = convertToNumber(value, targetType, locale); - return n == null ? null : n.doubleValue(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getModelType() - */ - @Override - public Class<Double> getModelType() { - return Double.class; - } - -} diff --git a/server/src/com/vaadin/data/util/converter/StringToEnumConverter.java b/server/src/com/vaadin/data/util/converter/StringToEnumConverter.java deleted file mode 100644 index 7b2f43f972..0000000000 --- a/server/src/com/vaadin/data/util/converter/StringToEnumConverter.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.converter; - -import java.util.EnumSet; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to an {@link Enum} and back. - * <p> - * Designed to provide nice human readable strings for {@link Enum} classes - * conforming to one of these patterns: - * <ul> - * <li>The constants are named SOME_UPPERCASE_WORDS and there's no toString - * implementation.</li> - * <li>toString() always returns the same human readable string that is not the - * same as its name() value. Each constant in the enum type returns a distinct - * toString() value.</li> - * </ul> - * Will not necessarily work correctly for other cases. - * </p> - * - * @author Vaadin Ltd - * @since 7.4 - */ -public class StringToEnumConverter implements Converter<String, Enum> { - - @Override - public Enum convertToModel(String value, Class<? extends Enum> targetType, - Locale locale) throws ConversionException { - if (value == null || value.trim().equals("")) { - return null; - } - - return stringToEnum(value, targetType, locale); - } - - /** - * Converts the given string to the given enum type using the given locale - * <p> - * Compatible with {@link #enumToString(Enum, Locale)} - * - * @param value - * The string value to convert - * @param enumType - * The type of enum to create - * @param locale - * The locale to use for conversion. If null, the JVM default - * locale will be used - * @return The enum which matches the given string - * @throws ConversionException - * if the conversion fails - */ - public static <T extends Enum<T>> T stringToEnum(String value, - Class<T> enumType, Locale locale) throws ConversionException { - if (locale == null) { - locale = Locale.getDefault(); - } - - if (!enumType.isEnum()) { - throw new ConversionException(enumType.getName() - + " is not an enum type"); - } - - // First test for the human-readable value since that's the more likely - // input - String upperCaseValue = value.toUpperCase(locale); - T match = null; - for (T e : EnumSet.allOf(enumType)) { - String upperCase = enumToString(e, locale).toUpperCase(locale); - if (upperCase.equals(upperCaseValue)) { - if (match != null) { - throw new ConversionException("Both " + match.name() - + " and " + e.name() - + " are matching the input string " + value); - } - match = e; - } - } - - if (match != null) { - return match; - } - - // Then fall back to using a strict match based on name() - try { - return Enum.valueOf(enumType, upperCaseValue); - } catch (Exception ee) { - throw new ConversionException(ee); - } - } - - /** - * Converts the given enum to a human readable string using the given locale - * <p> - * Compatible with {@link #stringToEnum(String, Class, Locale)} - * - * @param value - * The enum value to convert - * @param locale - * The locale to use for conversion. If null, the JVM default - * locale will be used - * @return A human readable string based on the enum - * @throws ConversionException - * if the conversion fails - */ - public static String enumToString(Enum<?> value, Locale locale) { - if (locale == null) { - locale = Locale.getDefault(); - } - - String enumString = value.toString(); - if (enumString.equals(value.name())) { - // FOO -> Foo - // FOO_BAR -> Foo bar - // _FOO -> _foo - String result = enumString.substring(0, 1).toUpperCase(locale); - result += enumString.substring(1).toLowerCase(locale) - .replace('_', ' '); - return result; - } else { - return enumString; - } - } - - @Override - public String convertToPresentation(Enum value, - Class<? extends String> targetType, Locale locale) - throws ConversionException { - if (value == null) { - return null; - } - - return enumToString(value, locale); - } - - @Override - public Class<Enum> getModelType() { - return Enum.class; - } - - @Override - public Class<String> getPresentationType() { - return String.class; - } - -} diff --git a/server/src/com/vaadin/data/util/converter/StringToFloatConverter.java b/server/src/com/vaadin/data/util/converter/StringToFloatConverter.java deleted file mode 100644 index f4d7f97853..0000000000 --- a/server/src/com/vaadin/data/util/converter/StringToFloatConverter.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util.converter; - -import java.text.NumberFormat; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link Float} and back. Uses - * the given locale and a {@link NumberFormat} instance for formatting and - * parsing. - * <p> - * Leading and trailing white spaces are ignored when converting from a String. - * </p> - * <p> - * Override and overwrite {@link #getFormat(Locale)} to use a different format. - * </p> - * - * @author Vaadin Ltd - * @since 7.0 - */ -public class StringToFloatConverter extends - AbstractStringToNumberConverter<Float> { - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.util.Locale) - */ - @Override - public Float convertToModel(String value, - Class<? extends Float> targetType, Locale locale) - throws ConversionException { - Number n = convertToNumber(value, targetType, locale); - return n == null ? null : n.floatValue(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getModelType() - */ - @Override - public Class<Float> getModelType() { - return Float.class; - } - -} diff --git a/server/src/com/vaadin/data/util/converter/StringToIntegerConverter.java b/server/src/com/vaadin/data/util/converter/StringToIntegerConverter.java deleted file mode 100644 index 59a46babe1..0000000000 --- a/server/src/com/vaadin/data/util/converter/StringToIntegerConverter.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util.converter; - -import java.text.NumberFormat; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link Integer} and back. - * Uses the given locale and a {@link NumberFormat} instance for formatting and - * parsing. - * <p> - * Override and overwrite {@link #getFormat(Locale)} to use a different format. - * </p> - * - * @author Vaadin Ltd - * @since 7.0 - */ -public class StringToIntegerConverter extends - AbstractStringToNumberConverter<Integer> { - - /** - * Returns the format used by - * {@link #convertToPresentation(Integer, Class, Locale)} and - * {@link #convertToModel(String, Class, Locale)} - * - * @param locale - * The locale to use - * @return A NumberFormat instance - */ - @Override - protected NumberFormat getFormat(Locale locale) { - if (locale == null) { - locale = Locale.getDefault(); - } - return NumberFormat.getIntegerInstance(locale); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.lang.Class, java.util.Locale) - */ - @Override - public Integer convertToModel(String value, - Class<? extends Integer> targetType, Locale locale) - throws ConversionException { - Number n = convertToNumber(value, targetType, locale); - - if (n == null) { - return null; - } - - int intValue = n.intValue(); - if (intValue == n.longValue()) { - // If the value of n is outside the range of long, the return value - // of longValue() is either Long.MIN_VALUE or Long.MAX_VALUE. The - // above comparison promotes int to long and thus does not need to - // consider wrap-around. - return intValue; - } - - throw new ConversionException("Could not convert '" + value + "' to " - + Integer.class.getName() + ": value out of range"); - - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getModelType() - */ - @Override - public Class<Integer> getModelType() { - return Integer.class; - } - -} diff --git a/server/src/com/vaadin/data/util/converter/StringToLongConverter.java b/server/src/com/vaadin/data/util/converter/StringToLongConverter.java deleted file mode 100644 index cd7cf49042..0000000000 --- a/server/src/com/vaadin/data/util/converter/StringToLongConverter.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util.converter; - -import java.text.NumberFormat; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link Long} and back. Uses - * the given locale and a {@link NumberFormat} instance for formatting and - * parsing. - * <p> - * Override and overwrite {@link #getFormat(Locale)} to use a different format. - * </p> - * - * @author Vaadin Ltd - * @since 7.2 - */ -public class StringToLongConverter extends - AbstractStringToNumberConverter<Long> { - - /** - * Returns the format used by - * {@link #convertToPresentation(Long, Class, Locale)} and - * {@link #convertToModel(String, Class, Locale)} - * - * @param locale - * The locale to use - * @return A NumberFormat instance - */ - @Override - protected NumberFormat getFormat(Locale locale) { - if (locale == null) { - locale = Locale.getDefault(); - } - return NumberFormat.getIntegerInstance(locale); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.lang.Class, java.util.Locale) - */ - @Override - public Long convertToModel(String value, Class<? extends Long> targetType, - Locale locale) throws ConversionException { - Number n = convertToNumber(value, targetType, locale); - return n == null ? null : n.longValue(); - - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getModelType() - */ - @Override - public Class<Long> getModelType() { - return Long.class; - } - -} diff --git a/server/src/com/vaadin/data/util/converter/StringToShortConverter.java b/server/src/com/vaadin/data/util/converter/StringToShortConverter.java deleted file mode 100644 index 554b958ee0..0000000000 --- a/server/src/com/vaadin/data/util/converter/StringToShortConverter.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.data.util.converter; - -import java.text.NumberFormat; -import java.util.Locale; - -/** - * A converter that converts from {@link String} to {@link Short} and back. Uses - * the given locale and a {@link NumberFormat} instance for formatting and - * parsing. - * <p> - * Override and overwrite {@link #getFormat(Locale)} to use a different format. - * </p> - * - * @author Vaadin Ltd - * @since 7.4 - */ -public class StringToShortConverter extends - AbstractStringToNumberConverter<Short> { - - /** - * Returns the format used by - * {@link #convertToPresentation(Short, Class, Locale)} and - * {@link #convertToModel(String, Class, Locale)} - * - * @param locale - * The locale to use - * @return A NumberFormat instance - */ - @Override - protected NumberFormat getFormat(Locale locale) { - if (locale == null) { - locale = Locale.getDefault(); - } - return NumberFormat.getIntegerInstance(locale); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.lang.Class, java.util.Locale) - */ - @Override - public Short convertToModel(String value, - Class<? extends Short> targetType, Locale locale) - throws ConversionException { - Number n = convertToNumber(value, targetType, locale); - - if (n == null) { - return null; - } - - short shortValue = n.shortValue(); - if (shortValue == n.longValue()) { - return shortValue; - } - - throw new ConversionException("Could not convert '" + value + "' to " - + Short.class.getName() + ": value out of range"); - - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.util.converter.Converter#getModelType() - */ - @Override - public Class<Short> getModelType() { - return Short.class; - } - -} diff --git a/server/src/com/vaadin/data/util/filter/AbstractJunctionFilter.java b/server/src/com/vaadin/data/util/filter/AbstractJunctionFilter.java deleted file mode 100644 index 28e6cda34e..0000000000 --- a/server/src/com/vaadin/data/util/filter/AbstractJunctionFilter.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.filter; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; - -import com.vaadin.data.Container.Filter; - -/** - * Abstract base class for filters that are composed of multiple sub-filters. - * - * The method {@link #appliesToProperty(Object)} is provided to help - * implementing {@link Filter} for in-memory filters. - * - * @since 6.6 - */ -public abstract class AbstractJunctionFilter implements Filter { - - protected final Collection<Filter> filters; - - public AbstractJunctionFilter(Filter... filters) { - this.filters = Collections.unmodifiableCollection(Arrays - .asList(filters)); - } - - /** - * Returns an unmodifiable collection of the sub-filters of this composite - * filter. - * - * @return - */ - public Collection<Filter> getFilters() { - return filters; - } - - /** - * Returns true if a change in the named property may affect the filtering - * result. If some of the sub-filters are not in-memory filters, true is - * returned. - * - * By default, all sub-filters are iterated to check if any of them applies. - * If there are no sub-filters, false is returned - override in subclasses - * to change this behavior. - */ - @Override - public boolean appliesToProperty(Object propertyId) { - for (Filter filter : getFilters()) { - if (filter.appliesToProperty(propertyId)) { - return true; - } - } - return false; - } - - @Override - public boolean equals(Object obj) { - if (obj == null || !getClass().equals(obj.getClass())) { - return false; - } - AbstractJunctionFilter other = (AbstractJunctionFilter) obj; - // contents comparison with equals() - return Arrays.equals(filters.toArray(), other.filters.toArray()); - } - - @Override - public int hashCode() { - int hash = getFilters().size(); - for (Filter filter : filters) { - hash = (hash << 1) ^ filter.hashCode(); - } - return hash; - } -} diff --git a/server/src/com/vaadin/data/util/filter/And.java b/server/src/com/vaadin/data/util/filter/And.java deleted file mode 100644 index 938aedae95..0000000000 --- a/server/src/com/vaadin/data/util/filter/And.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.filter; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.Item; - -/** - * A compound {@link Filter} that accepts an item if all of its filters accept - * the item. - * - * If no filters are given, the filter should accept all items. - * - * This filter also directly supports in-memory filtering when all sub-filters - * do so. - * - * @see Or - * - * @since 6.6 - */ -public final class And extends AbstractJunctionFilter { - - /** - * - * @param filters - * filters of which the And filter will be composed - */ - public And(Filter... filters) { - super(filters); - } - - @Override - public boolean passesFilter(Object itemId, Item item) - throws UnsupportedFilterException { - for (Filter filter : getFilters()) { - if (!filter.passesFilter(itemId, item)) { - return false; - } - } - return true; - } - -} diff --git a/server/src/com/vaadin/data/util/filter/Between.java b/server/src/com/vaadin/data/util/filter/Between.java deleted file mode 100644 index c50488c521..0000000000 --- a/server/src/com/vaadin/data/util/filter/Between.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.filter; - -import java.util.Arrays; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.Item; -import com.vaadin.shared.util.SharedUtil; - -public class Between implements Filter { - - private final Object propertyId; - private final Comparable<?> startValue; - private final Comparable<?> endValue; - - public Between(Object propertyId, Comparable<?> startValue, - Comparable<?> endValue) { - this.propertyId = propertyId; - this.startValue = startValue; - this.endValue = endValue; - } - - public Object getPropertyId() { - return propertyId; - } - - public Comparable<?> getStartValue() { - return startValue; - } - - public Comparable<?> getEndValue() { - return endValue; - } - - @Override - public boolean passesFilter(Object itemId, Item item) - throws UnsupportedOperationException { - Object value = item.getItemProperty(getPropertyId()).getValue(); - if (value instanceof Comparable) { - Comparable comparable = (Comparable) value; - return isAfterStartValue(comparable) - && isBeforeEndValue(comparable); - } else if (value == null) { - return getStartValue() == null && getEndValue() == null; - } - return false; - } - - @Override - public boolean appliesToProperty(Object propertyId) { - return getPropertyId() != null && getPropertyId().equals(propertyId); - } - - @Override - public int hashCode() { - return Arrays.hashCode(new Object[] { getPropertyId(), getStartValue(), - getEndValue() }); - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - - // Only objects of the same class can be equal - if (!getClass().equals(obj.getClass())) { - return false; - } - final Between o = (Between) obj; - - // Checks the properties one by one - boolean propertyIdEqual = SharedUtil.equals(getPropertyId(), - o.getPropertyId()); - boolean startValueEqual = SharedUtil.equals(getStartValue(), - o.getStartValue()); - boolean endValueEqual = SharedUtil.equals(getEndValue(), - o.getEndValue()); - return propertyIdEqual && startValueEqual && endValueEqual; - - } - - private boolean isAfterStartValue(Comparable comparable) { - return getStartValue() == null - || comparable.compareTo(getStartValue()) >= 0; - } - - private boolean isBeforeEndValue(Comparable comparable) { - return getEndValue() == null - || comparable.compareTo(getEndValue()) <= 0; - } -} diff --git a/server/src/com/vaadin/data/util/filter/Compare.java b/server/src/com/vaadin/data/util/filter/Compare.java deleted file mode 100644 index 1fcbe85580..0000000000 --- a/server/src/com/vaadin/data/util/filter/Compare.java +++ /dev/null @@ -1,365 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.filter; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.Item; -import com.vaadin.data.Property; - -/** - * Simple container filter comparing an item property value against a given - * constant value. Use the nested classes {@link Equal}, {@link Greater}, - * {@link Less}, {@link GreaterOrEqual} and {@link LessOrEqual} instead of this - * class directly. - * - * This filter also directly supports in-memory filtering. - * - * The reference and actual values must implement {@link Comparable} and the - * class of the actual property value must be assignable from the class of the - * reference value. - * - * @since 6.6 - */ -public abstract class Compare implements Filter { - - public enum Operation { - EQUAL, GREATER, LESS, GREATER_OR_EQUAL, LESS_OR_EQUAL - } - - private final Object propertyId; - private final Operation operation; - private final Object value; - - /** - * A {@link Compare} filter that accepts items for which the identified - * property value is equal to <code>value</code>. - * - * For in-memory filters, {@link Comparable#compareTo(Object)} or, if not - * Comparable, {@link #equals(Object)} is used for the comparison. For other - * containers, the comparison implementation is container dependent and may - * use e.g. database comparison operations. - * - * @since 6.6 - */ - public static final class Equal extends Compare { - /** - * Construct a filter that accepts items for which the identified - * property value is equal to <code>value</code>. - * - * For in-memory filters, equals() is used for the comparison. For other - * containers, the comparison implementation is container dependent and - * may use e.g. database comparison operations. - * - * @param propertyId - * the identifier of the property whose value to compare - * against value, not null - * @param value - * the value to compare against - null values may or may not - * be supported depending on the container - */ - public Equal(Object propertyId, Object value) { - super(propertyId, value, Operation.EQUAL); - } - } - - /** - * A {@link Compare} filter that accepts items for which the identified - * property value is greater than <code>value</code>. - * - * For in-memory filters, the values must implement {@link Comparable} and - * {@link Comparable#compareTo(Object)} is used for the comparison. For - * other containers, the comparison implementation is container dependent - * and may use e.g. database comparison operations. - * - * @since 6.6 - */ - public static final class Greater extends Compare { - /** - * Construct a filter that accepts items for which the identified - * property value is greater than <code>value</code>. - * - * For in-memory filters, the values must implement {@link Comparable} - * and {@link Comparable#compareTo(Object)} is used for the comparison. - * For other containers, the comparison implementation is container - * dependent and may use e.g. database comparison operations. - * - * @param propertyId - * the identifier of the property whose value to compare - * against value, not null - * @param value - * the value to compare against - null values may or may not - * be supported depending on the container - */ - public Greater(Object propertyId, Object value) { - super(propertyId, value, Operation.GREATER); - } - } - - /** - * A {@link Compare} filter that accepts items for which the identified - * property value is less than <code>value</code>. - * - * For in-memory filters, the values must implement {@link Comparable} and - * {@link Comparable#compareTo(Object)} is used for the comparison. For - * other containers, the comparison implementation is container dependent - * and may use e.g. database comparison operations. - * - * @since 6.6 - */ - public static final class Less extends Compare { - /** - * Construct a filter that accepts items for which the identified - * property value is less than <code>value</code>. - * - * For in-memory filters, the values must implement {@link Comparable} - * and {@link Comparable#compareTo(Object)} is used for the comparison. - * For other containers, the comparison implementation is container - * dependent and may use e.g. database comparison operations. - * - * @param propertyId - * the identifier of the property whose value to compare - * against value, not null - * @param value - * the value to compare against - null values may or may not - * be supported depending on the container - */ - public Less(Object propertyId, Object value) { - super(propertyId, value, Operation.LESS); - } - } - - /** - * A {@link Compare} filter that accepts items for which the identified - * property value is greater than or equal to <code>value</code>. - * - * For in-memory filters, the values must implement {@link Comparable} and - * {@link Comparable#compareTo(Object)} is used for the comparison. For - * other containers, the comparison implementation is container dependent - * and may use e.g. database comparison operations. - * - * @since 6.6 - */ - public static final class GreaterOrEqual extends Compare { - /** - * Construct a filter that accepts items for which the identified - * property value is greater than or equal to <code>value</code>. - * - * For in-memory filters, the values must implement {@link Comparable} - * and {@link Comparable#compareTo(Object)} is used for the comparison. - * For other containers, the comparison implementation is container - * dependent and may use e.g. database comparison operations. - * - * @param propertyId - * the identifier of the property whose value to compare - * against value, not null - * @param value - * the value to compare against - null values may or may not - * be supported depending on the container - */ - public GreaterOrEqual(Object propertyId, Object value) { - super(propertyId, value, Operation.GREATER_OR_EQUAL); - } - } - - /** - * A {@link Compare} filter that accepts items for which the identified - * property value is less than or equal to <code>value</code>. - * - * For in-memory filters, the values must implement {@link Comparable} and - * {@link Comparable#compareTo(Object)} is used for the comparison. For - * other containers, the comparison implementation is container dependent - * and may use e.g. database comparison operations. - * - * @since 6.6 - */ - public static final class LessOrEqual extends Compare { - /** - * Construct a filter that accepts items for which the identified - * property value is less than or equal to <code>value</code>. - * - * For in-memory filters, the values must implement {@link Comparable} - * and {@link Comparable#compareTo(Object)} is used for the comparison. - * For other containers, the comparison implementation is container - * dependent and may use e.g. database comparison operations. - * - * @param propertyId - * the identifier of the property whose value to compare - * against value, not null - * @param value - * the value to compare against - null values may or may not - * be supported depending on the container - */ - public LessOrEqual(Object propertyId, Object value) { - super(propertyId, value, Operation.LESS_OR_EQUAL); - } - } - - /** - * Constructor for a {@link Compare} filter that compares the value of an - * item property with the given constant <code>value</code>. - * - * This constructor is intended to be used by the nested static classes only - * ({@link Equal}, {@link Greater}, {@link Less}, {@link GreaterOrEqual}, - * {@link LessOrEqual}). - * - * For in-memory filtering, comparisons except EQUAL require that the values - * implement {@link Comparable} and {@link Comparable#compareTo(Object)} is - * used for the comparison. The equality comparison is performed using - * {@link Object#equals(Object)}. - * - * For other containers, the comparison implementation is container - * dependent and may use e.g. database comparison operations. Therefore, the - * behavior of comparisons might differ in some cases between in-memory and - * other containers. - * - * @param propertyId - * the identifier of the property whose value to compare against - * value, not null - * @param value - * the value to compare against - null values may or may not be - * supported depending on the container - * @param operation - * the comparison {@link Operation} to use - */ - Compare(Object propertyId, Object value, Operation operation) { - this.propertyId = propertyId; - this.value = value; - this.operation = operation; - } - - @Override - public boolean passesFilter(Object itemId, Item item) { - final Property<?> p = item.getItemProperty(getPropertyId()); - if (null == p) { - return false; - } - Object value = p.getValue(); - switch (getOperation()) { - case EQUAL: - return compareEquals(value); - case GREATER: - return compareValue(value) > 0; - case LESS: - return compareValue(value) < 0; - case GREATER_OR_EQUAL: - return compareValue(value) >= 0; - case LESS_OR_EQUAL: - return compareValue(value) <= 0; - } - // all cases should have been processed above - return false; - } - - /** - * Checks if the this value equals the given value. Favors Comparable over - * equals to better support e.g. BigDecimal where equals is stricter than - * compareTo. - * - * @param otherValue - * The value to compare to - * @return true if the values are equal, false otherwise - */ - private boolean compareEquals(Object otherValue) { - if (value == null || otherValue == null) { - return (otherValue == value); - } else if (value == otherValue) { - return true; - } else if (value instanceof Comparable - && otherValue.getClass() - .isAssignableFrom(getValue().getClass())) { - return ((Comparable) value).compareTo(otherValue) == 0; - } else { - return value.equals(otherValue); - } - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - protected int compareValue(Object value1) { - if (null == value) { - return null == value1 ? 0 : -1; - } else if (null == value1) { - return 1; - } else if (getValue() instanceof Comparable - && value1.getClass().isAssignableFrom(getValue().getClass())) { - return -((Comparable) getValue()).compareTo(value1); - } - throw new IllegalArgumentException("Could not compare the arguments: " - + value1 + ", " + getValue()); - } - - @Override - public boolean appliesToProperty(Object propertyId) { - return getPropertyId().equals(propertyId); - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - - // Only objects of the same class can be equal - if (!getClass().equals(obj.getClass())) { - return false; - } - final Compare o = (Compare) obj; - - // Checks the properties one by one - if (getPropertyId() != o.getPropertyId() && null != o.getPropertyId() - && !o.getPropertyId().equals(getPropertyId())) { - return false; - } - if (getOperation() != o.getOperation()) { - return false; - } - return (null == getValue()) ? null == o.getValue() : getValue().equals( - o.getValue()); - } - - @Override - public int hashCode() { - return (null != getPropertyId() ? getPropertyId().hashCode() : 0) - ^ (null != getValue() ? getValue().hashCode() : 0); - } - - /** - * Returns the property id of the property to compare against the fixed - * value. - * - * @return property id (not null) - */ - public Object getPropertyId() { - return propertyId; - } - - /** - * Returns the comparison operation. - * - * @return {@link Operation} - */ - public Operation getOperation() { - return operation; - } - - /** - * Returns the value to compare the property against. - * - * @return comparison reference value - */ - public Object getValue() { - return value; - } -} diff --git a/server/src/com/vaadin/data/util/filter/IsNull.java b/server/src/com/vaadin/data/util/filter/IsNull.java deleted file mode 100644 index 9bcfe40c03..0000000000 --- a/server/src/com/vaadin/data/util/filter/IsNull.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.filter; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.Item; -import com.vaadin.data.Property; - -/** - * Simple container filter checking whether an item property value is null. - * - * This filter also directly supports in-memory filtering. - * - * @since 6.6 - */ -public final class IsNull implements Filter { - - private final Object propertyId; - - /** - * Constructor for a filter that compares the value of an item property with - * null. - * - * For in-memory filtering, a simple == check is performed. For other - * containers, the comparison implementation is container dependent but - * should correspond to the in-memory null check. - * - * @param propertyId - * the identifier (not null) of the property whose value to check - */ - public IsNull(Object propertyId) { - this.propertyId = propertyId; - } - - @Override - public boolean passesFilter(Object itemId, Item item) - throws UnsupportedOperationException { - final Property<?> p = item.getItemProperty(getPropertyId()); - if (null == p) { - return false; - } - return null == p.getValue(); - } - - @Override - public boolean appliesToProperty(Object propertyId) { - return getPropertyId().equals(propertyId); - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - - // Only objects of the same class can be equal - if (!getClass().equals(obj.getClass())) { - return false; - } - final IsNull o = (IsNull) obj; - - // Checks the properties one by one - return (null != getPropertyId()) ? getPropertyId().equals( - o.getPropertyId()) : null == o.getPropertyId(); - } - - @Override - public int hashCode() { - return (null != getPropertyId() ? getPropertyId().hashCode() : 0); - } - - /** - * Returns the property id of the property tested by the filter, not null - * for valid filters. - * - * @return property id (not null) - */ - public Object getPropertyId() { - return propertyId; - } - -} diff --git a/server/src/com/vaadin/data/util/filter/Like.java b/server/src/com/vaadin/data/util/filter/Like.java deleted file mode 100644 index 9b7b2af292..0000000000 --- a/server/src/com/vaadin/data/util/filter/Like.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.filter; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.Item; - -public class Like implements Filter { - private final Object propertyId; - private final String value; - private boolean caseSensitive; - - public Like(Object propertyId, String value) { - this(propertyId, value, true); - } - - public Like(Object propertyId, String value, boolean caseSensitive) { - this.propertyId = propertyId; - this.value = value; - setCaseSensitive(caseSensitive); - } - - public Object getPropertyId() { - return propertyId; - } - - public String getValue() { - return value; - } - - public void setCaseSensitive(boolean caseSensitive) { - this.caseSensitive = caseSensitive; - } - - public boolean isCaseSensitive() { - return caseSensitive; - } - - @Override - public boolean passesFilter(Object itemId, Item item) - throws UnsupportedOperationException { - if (!item.getItemProperty(getPropertyId()).getType() - .isAssignableFrom(String.class)) { - // We can only handle strings - return false; - } - String colValue = (String) item.getItemProperty(getPropertyId()) - .getValue(); - - // Fix issue #10167 - avoid NPE and drop null property values - if (colValue == null) { - return false; - } - - String pattern = getValue().replace("%", ".*"); - if (isCaseSensitive()) { - return colValue.matches(pattern); - } - return colValue.toUpperCase().matches(pattern.toUpperCase()); - } - - @Override - public boolean appliesToProperty(Object propertyId) { - return getPropertyId() != null && getPropertyId().equals(propertyId); - } - - @Override - public int hashCode() { - return getPropertyId().hashCode() + getValue().hashCode(); - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - - // Only objects of the same class can be equal - if (!getClass().equals(obj.getClass())) { - return false; - } - final Like o = (Like) obj; - - // Checks the properties one by one - boolean propertyIdEqual = (null != getPropertyId()) ? getPropertyId() - .equals(o.getPropertyId()) : null == o.getPropertyId(); - boolean valueEqual = (null != getValue()) ? getValue().equals( - o.getValue()) : null == o.getValue(); - return propertyIdEqual && valueEqual; - } -} diff --git a/server/src/com/vaadin/data/util/filter/Not.java b/server/src/com/vaadin/data/util/filter/Not.java deleted file mode 100644 index a677e7f752..0000000000 --- a/server/src/com/vaadin/data/util/filter/Not.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.filter; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.Item; - -/** - * Negating filter that accepts the items rejected by another filter. - * - * This filter directly supports in-memory filtering when the negated filter - * does so. - * - * @since 6.6 - */ -public final class Not implements Filter { - private final Filter filter; - - /** - * Constructs a filter that negates a filter. - * - * @param filter - * {@link Filter} to negate, not-null - */ - public Not(Filter filter) { - this.filter = filter; - } - - /** - * Returns the negated filter. - * - * @return Filter - */ - public Filter getFilter() { - return filter; - } - - @Override - public boolean passesFilter(Object itemId, Item item) - throws UnsupportedOperationException { - return !filter.passesFilter(itemId, item); - } - - /** - * Returns true if a change in the named property may affect the filtering - * result. Return value is the same as {@link #appliesToProperty(Object)} - * for the negated filter. - * - * @return boolean - */ - @Override - public boolean appliesToProperty(Object propertyId) { - return filter.appliesToProperty(propertyId); - } - - @Override - public boolean equals(Object obj) { - if (obj == null || !getClass().equals(obj.getClass())) { - return false; - } - return filter.equals(((Not) obj).getFilter()); - } - - @Override - public int hashCode() { - return filter.hashCode(); - } - -} diff --git a/server/src/com/vaadin/data/util/filter/Or.java b/server/src/com/vaadin/data/util/filter/Or.java deleted file mode 100644 index 69d20513a2..0000000000 --- a/server/src/com/vaadin/data/util/filter/Or.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.filter; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.Item; - -/** - * A compound {@link Filter} that accepts an item if any of its filters accept - * the item. - * - * If no filters are given, the filter should reject all items. - * - * This filter also directly supports in-memory filtering when all sub-filters - * do so. - * - * @see And - * - * @since 6.6 - */ -public final class Or extends AbstractJunctionFilter { - - /** - * - * @param filters - * filters of which the Or filter will be composed - */ - public Or(Filter... filters) { - super(filters); - } - - @Override - public boolean passesFilter(Object itemId, Item item) - throws UnsupportedFilterException { - for (Filter filter : getFilters()) { - if (filter.passesFilter(itemId, item)) { - return true; - } - } - return false; - } - - /** - * Returns true if a change in the named property may affect the filtering - * result. If some of the sub-filters are not in-memory filters, true is - * returned. - * - * By default, all sub-filters are iterated to check if any of them applies. - * If there are no sub-filters, true is returned as an empty Or rejects all - * items. - */ - @Override - public boolean appliesToProperty(Object propertyId) { - if (getFilters().isEmpty()) { - // empty Or filters out everything - return true; - } else { - return super.appliesToProperty(propertyId); - } - } - -} diff --git a/server/src/com/vaadin/data/util/filter/SimpleStringFilter.java b/server/src/com/vaadin/data/util/filter/SimpleStringFilter.java deleted file mode 100644 index ea5e93277e..0000000000 --- a/server/src/com/vaadin/data/util/filter/SimpleStringFilter.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.filter; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.Item; -import com.vaadin.data.Property; - -/** - * Simple string filter for matching items that start with or contain a - * specified string. The matching can be case-sensitive or case-insensitive. - * - * This filter also directly supports in-memory filtering. When performing - * in-memory filtering, values of other types are converted using toString(), - * but other (lazy container) implementations do not need to perform such - * conversions and might not support values of different types. - * - * Note that this filter is modeled after the pre-6.6 filtering mechanisms, and - * might not be very efficient e.g. for database filtering. - * - * TODO this might still change - * - * @since 6.6 - */ -public final class SimpleStringFilter implements Filter { - - final Object propertyId; - final String filterString; - final boolean ignoreCase; - final boolean onlyMatchPrefix; - - public SimpleStringFilter(Object propertyId, String filterString, - boolean ignoreCase, boolean onlyMatchPrefix) { - this.propertyId = propertyId; - this.filterString = ignoreCase ? filterString.toLowerCase() - : filterString; - this.ignoreCase = ignoreCase; - this.onlyMatchPrefix = onlyMatchPrefix; - } - - @Override - public boolean passesFilter(Object itemId, Item item) { - final Property<?> p = item.getItemProperty(propertyId); - if (p == null) { - return false; - } - Object propertyValue = p.getValue(); - if (propertyValue == null) { - return false; - } - final String value = ignoreCase ? propertyValue.toString() - .toLowerCase() : propertyValue.toString(); - if (onlyMatchPrefix) { - if (!value.startsWith(filterString)) { - return false; - } - } else { - if (!value.contains(filterString)) { - return false; - } - } - return true; - } - - @Override - public boolean appliesToProperty(Object propertyId) { - return this.propertyId.equals(propertyId); - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - - // Only ones of the objects of the same class can be equal - if (!(obj instanceof SimpleStringFilter)) { - return false; - } - final SimpleStringFilter o = (SimpleStringFilter) obj; - - // Checks the properties one by one - if (propertyId != o.propertyId && o.propertyId != null - && !o.propertyId.equals(propertyId)) { - return false; - } - if (filterString != o.filterString && o.filterString != null - && !o.filterString.equals(filterString)) { - return false; - } - if (ignoreCase != o.ignoreCase) { - return false; - } - if (onlyMatchPrefix != o.onlyMatchPrefix) { - return false; - } - - return true; - } - - @Override - public int hashCode() { - return (propertyId != null ? propertyId.hashCode() : 0) - ^ (filterString != null ? filterString.hashCode() : 0); - } - - /** - * Returns the property identifier to which this filter applies. - * - * @return property id - */ - public Object getPropertyId() { - return propertyId; - } - - /** - * Returns the filter string. - * - * Note: this method is intended only for implementations of lazy string - * filters and may change in the future. - * - * @return filter string given to the constructor - */ - public String getFilterString() { - return filterString; - } - - /** - * Returns whether the filter is case-insensitive or case-sensitive. - * - * Note: this method is intended only for implementations of lazy string - * filters and may change in the future. - * - * @return true if performing case-insensitive filtering, false for - * case-sensitive - */ - public boolean isIgnoreCase() { - return ignoreCase; - } - - /** - * Returns true if the filter only applies to the beginning of the value - * string, false for any location in the value. - * - * Note: this method is intended only for implementations of lazy string - * filters and may change in the future. - * - * @return true if checking for matches at the beginning of the value only, - * false if matching any part of value - */ - public boolean isOnlyMatchPrefix() { - return onlyMatchPrefix; - } -} diff --git a/server/src/com/vaadin/data/util/filter/UnsupportedFilterException.java b/server/src/com/vaadin/data/util/filter/UnsupportedFilterException.java deleted file mode 100644 index 42a7784da8..0000000000 --- a/server/src/com/vaadin/data/util/filter/UnsupportedFilterException.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.filter; - -import java.io.Serializable; - -/** - * Exception for cases where a container does not support a specific type of - * filters. - * - * If possible, this should be thrown already when adding a filter to a - * container. If a problem is not detected at that point, an - * {@link UnsupportedOperationException} can be throws when attempting to - * perform filtering. - * - * @since 6.6 - */ -public class UnsupportedFilterException extends RuntimeException implements - Serializable { - public UnsupportedFilterException() { - } - - public UnsupportedFilterException(String message) { - super(message); - } - - public UnsupportedFilterException(Exception cause) { - super(cause); - } - - public UnsupportedFilterException(String message, Exception cause) { - super(message, cause); - } -} diff --git a/server/src/com/vaadin/data/util/package.html b/server/src/com/vaadin/data/util/package.html deleted file mode 100644 index 07e3acde9e..0000000000 --- a/server/src/com/vaadin/data/util/package.html +++ /dev/null @@ -1,18 +0,0 @@ -<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> -<html> -<head> - -</head> - -<body bgcolor="white"> - -<p>Provides implementations of Property, Item and Container -interfaces, and utilities for the data layer.</p> - -<p>Various Property, Item and Container implementations are provided -in this package. Each implementation can have its own sets of -constraints on the data it encapsulates and on how the implementation -can be used. See the class javadocs for more information.</p> - -</body> -</html> diff --git a/server/src/com/vaadin/data/util/sqlcontainer/CacheFlushNotifier.java b/server/src/com/vaadin/data/util/sqlcontainer/CacheFlushNotifier.java deleted file mode 100644 index 12d806a8d5..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/CacheFlushNotifier.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer; - -import java.io.Serializable; -import java.lang.ref.ReferenceQueue; -import java.lang.ref.WeakReference; -import java.util.ArrayList; -import java.util.List; - -import com.vaadin.data.util.sqlcontainer.query.FreeformQuery; -import com.vaadin.data.util.sqlcontainer.query.QueryDelegate; -import com.vaadin.data.util.sqlcontainer.query.TableQuery; - -/** - * CacheFlushNotifier is a simple static notification mechanism to inform other - * SQLContainers that the contents of their caches may have become stale. - */ -class CacheFlushNotifier implements Serializable { - /* - * SQLContainer instance reference list and dead reference queue. Used for - * the cache flush notification feature. - */ - private static List<WeakReference<SQLContainer>> allInstances = new ArrayList<WeakReference<SQLContainer>>(); - private static ReferenceQueue<SQLContainer> deadInstances = new ReferenceQueue<SQLContainer>(); - - /** - * Adds the given SQLContainer to the cache flush notification receiver list - * - * @param c - * Container to add - */ - public static void addInstance(SQLContainer c) { - removeDeadReferences(); - if (c != null) { - allInstances.add(new WeakReference<SQLContainer>(c, deadInstances)); - } - } - - /** - * Removes dead references from instance list - */ - private static void removeDeadReferences() { - java.lang.ref.Reference<? extends SQLContainer> dead = deadInstances - .poll(); - while (dead != null) { - allInstances.remove(dead); - dead = deadInstances.poll(); - } - } - - /** - * Iterates through the instances and notifies containers which are - * connected to the same table or are using the same query string. - * - * @param c - * SQLContainer that issued the cache flush notification - */ - public static void notifyOfCacheFlush(SQLContainer c) { - removeDeadReferences(); - for (WeakReference<SQLContainer> wr : allInstances) { - if (wr.get() != null) { - SQLContainer wrc = wr.get(); - if (wrc == null) { - continue; - } - /* - * If the reference points to the container sending the - * notification, do nothing. - */ - if (wrc.equals(c)) { - continue; - } - /* Compare QueryDelegate types and tableName/queryString */ - QueryDelegate wrQd = wrc.getQueryDelegate(); - QueryDelegate qd = c.getQueryDelegate(); - if (wrQd instanceof TableQuery - && qd instanceof TableQuery - && ((TableQuery) wrQd).getTableName().equals( - ((TableQuery) qd).getTableName())) { - wrc.refresh(); - } else if (wrQd instanceof FreeformQuery - && qd instanceof FreeformQuery - && ((FreeformQuery) wrQd).getQueryString().equals( - ((FreeformQuery) qd).getQueryString())) { - wrc.refresh(); - } - } - } - } -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/CacheMap.java b/server/src/com/vaadin/data/util/sqlcontainer/CacheMap.java deleted file mode 100644 index cdeff873d0..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/CacheMap.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer; - -import java.util.LinkedHashMap; -import java.util.Map; - -/** - * CacheMap extends LinkedHashMap, adding the possibility to adjust maximum - * number of items. In SQLContainer this is used for RowItem -cache. Cache size - * will be two times the page length parameter of the container. - */ -class CacheMap<K, V> extends LinkedHashMap<K, V> { - private static final long serialVersionUID = 679999766473555231L; - private int cacheLimit = SQLContainer.CACHE_RATIO - * SQLContainer.DEFAULT_PAGE_LENGTH; - - @Override - protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { - return size() > cacheLimit; - } - - void setCacheLimit(int limit) { - cacheLimit = limit > 0 ? limit : SQLContainer.DEFAULT_PAGE_LENGTH; - } - - int getCacheLimit() { - return cacheLimit; - } -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/ColumnProperty.java b/server/src/com/vaadin/data/util/sqlcontainer/ColumnProperty.java deleted file mode 100644 index e89b8c02a8..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/ColumnProperty.java +++ /dev/null @@ -1,388 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer; - -import java.sql.Date; -import java.sql.Time; -import java.sql.Timestamp; -import java.util.logging.Logger; - -import com.vaadin.data.Property; -import com.vaadin.data.util.LegacyPropertyHelper; -import com.vaadin.data.util.converter.Converter.ConversionException; - -/** - * ColumnProperty represents the value of one column in a RowItem. In addition - * to the value, ColumnProperty also contains some basic column attributes such - * as nullability status, read-only status and data type. - * - * Note that depending on the QueryDelegate in use this does not necessarily map - * into an actual column in a database table. - */ -final public class ColumnProperty implements Property { - private static final long serialVersionUID = -3694463129581802457L; - - private RowItem owner; - - private String propertyId; - - private boolean readOnly; - private boolean allowReadOnlyChange = true; - private boolean nullable = true; - - private Object value; - private Object changedValue; - private Class<?> type; - - private boolean modified; - - private boolean versionColumn; - private boolean primaryKey = false; - - /** - * Prevent instantiation without required parameters. - */ - @SuppressWarnings("unused") - private ColumnProperty() { - } - - /** - * Deprecated constructor for ColumnProperty. If this is used the primary - * keys are not identified correctly in some cases for some databases (i.e. - * Oracle). See http://dev.vaadin.com/ticket/9145. - * - * @param propertyId - * @param readOnly - * @param allowReadOnlyChange - * @param nullable - * @param value - * @param type - * - * @deprecated As of 7.0. Use - * {@link #ColumnProperty(String, boolean, boolean, boolean, boolean, Object, Class) - * instead - */ - @Deprecated - public ColumnProperty(String propertyId, boolean readOnly, - boolean allowReadOnlyChange, boolean nullable, Object value, - Class<?> type) { - this(propertyId, readOnly, allowReadOnlyChange, nullable, false, value, - type); - } - - /** - * Creates a new ColumnProperty instance. - * - * @param propertyId - * The ID of this property. - * @param readOnly - * Whether this property is read-only. - * @param allowReadOnlyChange - * Whether the read-only status of this property can be changed. - * @param nullable - * Whether this property accepts null values. - * @param primaryKey - * Whether this property corresponds to a database primary key. - * @param value - * The value of this property. - * @param type - * The type of this property. - */ - public ColumnProperty(String propertyId, boolean readOnly, - boolean allowReadOnlyChange, boolean nullable, boolean primaryKey, - Object value, Class<?> type) { - - if (propertyId == null) { - throw new IllegalArgumentException("Properties must be named."); - } - if (type == null) { - throw new IllegalArgumentException("Property type must be set."); - } - this.propertyId = propertyId; - this.type = type; - this.value = value; - - this.allowReadOnlyChange = allowReadOnlyChange; - this.nullable = nullable; - this.readOnly = readOnly; - this.primaryKey = primaryKey; - } - - /** - * Returns the current value for this property. To get the previous value - * (if one exists) for a modified property use {@link #getOldValue()}. - * - * @return - */ - @Override - public Object getValue() { - if (isModified()) { - return changedValue; - } - return value; - } - - /** - * Returns the original non-modified value of this property if it has been - * modified. - * - * @return The original value if <code>isModified()</code> is true, - * <code>getValue()</code> otherwise. - */ - public Object getOldValue() { - return value; - } - - @Override - public void setValue(Object newValue) throws ReadOnlyException, - ConversionException { - if (newValue == null && !nullable) { - throw new NotNullableException( - "Null values are not allowed for this property."); - } - if (readOnly) { - throw new ReadOnlyException( - "Cannot set value for read-only property."); - } - - /* Check if this property is a date property. */ - boolean isDateProperty = Time.class.equals(getType()) - || Date.class.equals(getType()) - || Timestamp.class.equals(getType()); - - if (newValue != null) { - /* Handle SQL dates, times and Timestamps given as java.util.Date */ - if (isDateProperty) { - /* - * Try to get the millisecond value from the new value of this - * property. Possible type to convert from is java.util.Date. - */ - long millis = 0; - if (newValue instanceof java.util.Date) { - millis = ((java.util.Date) newValue).getTime(); - /* - * Create the new object based on the millisecond value, - * according to the type of this property. - */ - if (Time.class.equals(getType())) { - newValue = new Time(millis); - } else if (Date.class.equals(getType())) { - newValue = new Date(millis); - } else if (Timestamp.class.equals(getType())) { - newValue = new Timestamp(millis); - } - } - } - - if (!getType().isAssignableFrom(newValue.getClass())) { - throw new IllegalArgumentException( - "Illegal value type for ColumnProperty"); - } - - /* - * If the value to be set is the same that has already been set, do - * not set it again. - */ - if (isValueAlreadySet(newValue)) { - return; - } - } - - /* Set the new value and notify container of the change. */ - changedValue = newValue; - modified = true; - owner.getContainer().itemChangeNotification(owner); - } - - private boolean isValueAlreadySet(Object newValue) { - Object referenceValue = isModified() ? changedValue : value; - - return (isNullable() && newValue == null && referenceValue == null) - || newValue.equals(referenceValue); - } - - @Override - public Class<?> getType() { - return type; - } - - @Override - public boolean isReadOnly() { - return readOnly; - } - - /** - * Returns whether the read-only status of this property can be changed - * using {@link #setReadOnly(boolean)}. - * <p> - * Used to prevent setting to read/write mode a property that is not allowed - * to be written by the underlying database. Also used for values like - * VERSION and AUTO_INCREMENT fields that might be set to read-only by the - * container but the database still allows writes. - * - * @return true if the read-only status can be changed, false otherwise. - */ - public boolean isReadOnlyChangeAllowed() { - return allowReadOnlyChange; - } - - @Override - public void setReadOnly(boolean newStatus) { - if (allowReadOnlyChange) { - readOnly = newStatus; - } - } - - public boolean isPrimaryKey() { - return primaryKey; - } - - public String getPropertyId() { - return propertyId; - } - - /** - * Returns a string representation of this object. The returned string - * representation depends on if the legacy Property toString mode is enabled - * or disabled. - * <p> - * If legacy Property toString mode is enabled, returns the value of this - * <code>Property</code> converted to a String. - * </p> - * <p> - * If legacy Property toString mode is disabled, the string representation - * has no special meaning - * </p> - * - * @see LegacyPropertyHelper#isLegacyToStringEnabled() - * - * @return A string representation of the value value stored in the Property - * or a string representation of the Property object. - * @deprecated As of 7.0. To get the property value, use {@link #getValue()} - * instead (and possibly toString on that) - */ - @Deprecated - @Override - public String toString() { - if (!LegacyPropertyHelper.isLegacyToStringEnabled()) { - return super.toString(); - } else { - return LegacyPropertyHelper.legacyPropertyToString(this); - } - } - - private static Logger getLogger() { - return Logger.getLogger(ColumnProperty.class.getName()); - } - - public void setOwner(RowItem owner) { - if (owner == null) { - throw new IllegalArgumentException("Owner can not be set to null."); - } - if (this.owner != null) { - throw new IllegalStateException( - "ColumnProperties can only be bound once."); - } - this.owner = owner; - } - - public boolean isModified() { - return modified; - } - - public boolean isVersionColumn() { - return versionColumn; - } - - public void setVersionColumn(boolean versionColumn) { - this.versionColumn = versionColumn; - } - - public boolean isNullable() { - return nullable; - } - - /** - * Return whether the value of this property should be persisted to the - * database. - * - * @return true if the value should be written to the database, false - * otherwise. - */ - public boolean isPersistent() { - if (isVersionColumn()) { - return false; - } else if (isReadOnlyChangeAllowed() && !isReadOnly()) { - return true; - } else { - return false; - } - } - - /** - * Returns whether or not this property is used as a row identifier. - * - * @return true if the property is a row identifier, false otherwise. - */ - public boolean isRowIdentifier() { - return isPrimaryKey() || isVersionColumn(); - } - - /** - * An exception that signals that a <code>null</code> value was passed to - * the <code>setValue</code> method, but the value of this property can not - * be set to <code>null</code>. - */ - @SuppressWarnings("serial") - public class NotNullableException extends RuntimeException { - - /** - * Constructs a new <code>NotNullableException</code> without a detail - * message. - */ - public NotNullableException() { - } - - /** - * Constructs a new <code>NotNullableException</code> with the specified - * detail message. - * - * @param msg - * the detail message - */ - public NotNullableException(String msg) { - super(msg); - } - - /** - * Constructs a new <code>NotNullableException</code> from another - * exception. - * - * @param cause - * The cause of the failure - */ - public NotNullableException(Throwable cause) { - super(cause); - } - } - - public void commit() { - if (isModified()) { - modified = false; - value = changedValue; - } - } -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/OptimisticLockException.java b/server/src/com/vaadin/data/util/sqlcontainer/OptimisticLockException.java deleted file mode 100644 index ed8eb3c0ff..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/OptimisticLockException.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer; - -import com.vaadin.data.util.sqlcontainer.query.TableQuery; - -/** - * An OptimisticLockException is thrown when trying to update or delete a row - * that has been changed since last read from the database. - * - * OptimisticLockException is a runtime exception because optimistic locking is - * turned off by default, and as such will never be thrown in a default - * configuration. In order to turn on optimistic locking, you need to specify - * the version column in your TableQuery instance. - * - * @see TableQuery#setVersionColumn(String) - * - * @author Jonatan Kronqvist / Vaadin Ltd - */ -public class OptimisticLockException extends RuntimeException { - - private final RowId rowId; - - public OptimisticLockException(RowId rowId) { - super(); - this.rowId = rowId; - } - - public OptimisticLockException(String msg, RowId rowId) { - super(msg); - this.rowId = rowId; - } - - public RowId getRowId() { - return rowId; - } -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/ReadOnlyRowId.java b/server/src/com/vaadin/data/util/sqlcontainer/ReadOnlyRowId.java deleted file mode 100644 index c845cadc7a..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/ReadOnlyRowId.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer; - -public class ReadOnlyRowId extends RowId { - private static final long serialVersionUID = -2626764781642012467L; - private final Integer rowNum; - - public ReadOnlyRowId(int rowNum) { - super(); - this.rowNum = rowNum; - } - - @Override - public int hashCode() { - return getRowNum(); - } - - @Override - public boolean equals(Object obj) { - if (obj == null || !(ReadOnlyRowId.class.equals(obj.getClass()))) { - return false; - } - return getRowNum() == (((ReadOnlyRowId) obj).getRowNum()); - } - - public int getRowNum() { - return rowNum; - } - - @Override - public String toString() { - return String.valueOf(getRowNum()); - } -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/Reference.java b/server/src/com/vaadin/data/util/sqlcontainer/Reference.java deleted file mode 100644 index e82f63ad2f..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/Reference.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer; - -import java.io.Serializable; - -/** - * The reference class represents a simple [usually foreign key] reference to - * another SQLContainer. Actual foreign key reference in the database is not - * required, but it is recommended to make sure that certain constraints are - * followed. - */ -@SuppressWarnings("serial") -class Reference implements Serializable { - - /** - * The SQLContainer that this reference points to. - */ - private SQLContainer referencedContainer; - - /** - * The column ID/name in the referencing SQLContainer that contains the key - * used for the reference. - */ - private String referencingColumn; - - /** - * The column ID/name in the referenced SQLContainer that contains the key - * used for the reference. - */ - private String referencedColumn; - - /** - * Constructs a new reference to be used within the SQLContainer to - * reference another SQLContainer. - */ - Reference(SQLContainer referencedContainer, String referencingColumn, - String referencedColumn) { - this.referencedContainer = referencedContainer; - this.referencingColumn = referencingColumn; - this.referencedColumn = referencedColumn; - } - - SQLContainer getReferencedContainer() { - return referencedContainer; - } - - String getReferencingColumn() { - return referencingColumn; - } - - String getReferencedColumn() { - return referencedColumn; - } -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/RowId.java b/server/src/com/vaadin/data/util/sqlcontainer/RowId.java deleted file mode 100644 index 79c16b0f60..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/RowId.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer; - -import java.io.Serializable; -import java.util.Arrays; - -/** - * RowId represents identifiers of a single database result set row. - * - * The data structure of a RowId is an Object array which contains the values of - * the primary key columns of the identified row. This allows easy equals() - * -comparison of RowItems. - */ -public class RowId implements Serializable { - private static final long serialVersionUID = -3161778404698901258L; - protected Object[] id; - - /** - * Prevent instantiation without required parameters. - */ - protected RowId() { - } - - public RowId(Object... id) { - if (id == null) { - throw new IllegalArgumentException("id parameter must not be null!"); - } - this.id = id; - } - - public Object[] getId() { - return id; - } - - @Override - public int hashCode() { - return Arrays.hashCode(getId()); - } - - @Override - public boolean equals(Object obj) { - if (obj == null || !(RowId.class.equals(obj.getClass()))) { - return false; - } - return Arrays.equals(getId(), ((RowId) obj).getId()); - } - - @Override - public String toString() { - if (getId() == null) { - return ""; - } - StringBuilder builder = new StringBuilder(); - for (Object id : getId()) { - builder.append(id); - builder.append('/'); - } - if (builder.length() > 0) { - return builder.substring(0, builder.length() - 1); - } - return builder.toString(); - } -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/RowItem.java b/server/src/com/vaadin/data/util/sqlcontainer/RowItem.java deleted file mode 100644 index ddb5de1d31..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/RowItem.java +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; - -import com.vaadin.data.Item; -import com.vaadin.data.Property; - -/** - * RowItem represents one row of a result set obtained from a QueryDelegate. - * - * Note that depending on the QueryDelegate in use this does not necessarily map - * into an actual row in a database table. - */ -public final class RowItem implements Item { - private static final long serialVersionUID = -6228966439127951408L; - private SQLContainer container; - private RowId id; - private Collection<ColumnProperty> properties; - - /** - * Prevent instantiation without required parameters. - */ - @SuppressWarnings("unused") - private RowItem() { - } - - public RowItem(SQLContainer container, RowId id, - Collection<ColumnProperty> properties) { - if (container == null) { - throw new IllegalArgumentException("Container cannot be null."); - } - if (id == null) { - throw new IllegalArgumentException("Row ID cannot be null."); - } - this.container = container; - this.properties = properties; - /* Set this RowItem as owner to the properties */ - if (properties != null) { - for (ColumnProperty p : properties) { - p.setOwner(this); - } - } - this.id = id; - } - - @Override - public Property getItemProperty(Object id) { - if (id instanceof String && id != null) { - for (ColumnProperty cp : properties) { - if (id.equals(cp.getPropertyId())) { - return cp; - } - } - } - return null; - } - - @Override - public Collection<?> getItemPropertyIds() { - Collection<String> ids = new ArrayList<String>(properties.size()); - for (ColumnProperty cp : properties) { - ids.add(cp.getPropertyId()); - } - return Collections.unmodifiableCollection(ids); - } - - /** - * Adding properties is not supported. Properties are generated by - * SQLContainer. - */ - @Override - public boolean addItemProperty(Object id, Property property) - throws UnsupportedOperationException { - throw new UnsupportedOperationException(); - } - - /** - * Removing properties is not supported. Properties are generated by - * SQLContainer. - */ - @Override - public boolean removeItemProperty(Object id) - throws UnsupportedOperationException { - throw new UnsupportedOperationException(); - } - - public RowId getId() { - return id; - } - - public SQLContainer getContainer() { - return container; - } - - public boolean isModified() { - if (properties != null) { - for (ColumnProperty p : properties) { - if (p.isModified()) { - return true; - } - } - } - return false; - } - - @Override - public String toString() { - StringBuffer s = new StringBuffer(); - s.append("ID:"); - s.append(getId().toString()); - for (Object propId : getItemPropertyIds()) { - s.append("|"); - s.append(propId.toString()); - s.append(":"); - Object value = getItemProperty(propId).getValue(); - s.append((null != value) ? value.toString() : null); - } - return s.toString(); - } - - public void commit() { - if (properties != null) { - for (ColumnProperty p : properties) { - p.commit(); - } - } - } -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/SQLContainer.java b/server/src/com/vaadin/data/util/sqlcontainer/SQLContainer.java deleted file mode 100644 index 86e9069e90..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/SQLContainer.java +++ /dev/null @@ -1,1872 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer; - -import java.io.IOException; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.ConcurrentModificationException; -import java.util.Date; -import java.util.EventObject; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; - -import com.vaadin.data.Container; -import com.vaadin.data.ContainerHelpers; -import com.vaadin.data.Item; -import com.vaadin.data.Property; -import com.vaadin.data.util.filter.Compare.Equal; -import com.vaadin.data.util.filter.Like; -import com.vaadin.data.util.filter.UnsupportedFilterException; -import com.vaadin.data.util.sqlcontainer.query.OrderBy; -import com.vaadin.data.util.sqlcontainer.query.QueryDelegate; -import com.vaadin.data.util.sqlcontainer.query.QueryDelegate.RowIdChangeListener; -import com.vaadin.data.util.sqlcontainer.query.TableQuery; -import com.vaadin.data.util.sqlcontainer.query.generator.MSSQLGenerator; -import com.vaadin.data.util.sqlcontainer.query.generator.OracleGenerator; - -public class SQLContainer implements Container, Container.Filterable, - Container.Indexed, Container.Sortable, Container.ItemSetChangeNotifier { - - /** Query delegate */ - private QueryDelegate queryDelegate; - /** Auto commit mode, default = false */ - private boolean autoCommit = false; - - /** Page length = number of items contained in one page */ - private int pageLength = DEFAULT_PAGE_LENGTH; - public static final int DEFAULT_PAGE_LENGTH = 100; - - /** Number of items to cache = CACHE_RATIO x pageLength */ - public static final int CACHE_RATIO = 2; - - /** Amount of cache to overlap with previous page */ - private int cacheOverlap = pageLength; - - /** Item and index caches */ - private final Map<Integer, RowId> itemIndexes = new HashMap<Integer, RowId>(); - private final CacheMap<RowId, RowItem> cachedItems = new CacheMap<RowId, RowItem>(); - - /** Container properties = column names, data types and statuses */ - private final List<String> propertyIds = new ArrayList<String>(); - private final Map<String, Class<?>> propertyTypes = new HashMap<String, Class<?>>(); - private final Map<String, Boolean> propertyReadOnly = new HashMap<String, Boolean>(); - private final Map<String, Boolean> propertyPersistable = new HashMap<String, Boolean>(); - private final Map<String, Boolean> propertyNullable = new HashMap<String, Boolean>(); - private final Map<String, Boolean> propertyPrimaryKey = new HashMap<String, Boolean>(); - - /** Filters (WHERE) and sorters (ORDER BY) */ - private final List<Filter> filters = new ArrayList<Filter>(); - private final List<OrderBy> sorters = new ArrayList<OrderBy>(); - - /** - * Total number of items available in the data source using the current - * query, filters and sorters. - */ - private int size; - - /** - * Size updating logic. Do not update size from data source if it has been - * updated in the last sizeValidMilliSeconds milliseconds. - */ - private final int sizeValidMilliSeconds = 10000; - private boolean sizeDirty = true; - private Date sizeUpdated = new Date(); - - /** Starting row number of the currently fetched page */ - private int currentOffset; - - /** ItemSetChangeListeners */ - private LinkedList<Container.ItemSetChangeListener> itemSetChangeListeners; - - /** Temporary storage for modified items and items to be removed and added */ - private final Map<RowId, RowItem> removedItems = new HashMap<RowId, RowItem>(); - private final List<RowItem> addedItems = new ArrayList<RowItem>(); - private final List<RowItem> modifiedItems = new ArrayList<RowItem>(); - - /** List of references to other SQLContainers */ - private final Map<SQLContainer, Reference> references = new HashMap<SQLContainer, Reference>(); - - /** Cache flush notification system enabled. Disabled by default. */ - private boolean notificationsEnabled; - - /** - * Prevent instantiation without a QueryDelegate. - */ - @SuppressWarnings("unused") - private SQLContainer() { - } - - /** - * Creates and initializes SQLContainer using the given QueryDelegate - * - * @param delegate - * QueryDelegate implementation - * @throws SQLException - */ - public SQLContainer(QueryDelegate delegate) throws SQLException { - if (delegate == null) { - throw new IllegalArgumentException( - "QueryDelegate must not be null."); - } - queryDelegate = delegate; - getPropertyIds(); - cachedItems.setCacheLimit(CACHE_RATIO * getPageLength() + cacheOverlap); - } - - /**************************************/ - /** Methods from interface Container **/ - /**************************************/ - - /** - * Note! If auto commit mode is enabled, this method will still return the - * temporary row ID assigned for the item. Implement - * QueryDelegate.RowIdChangeListener to receive the actual Row ID value - * after the addition has been committed. - * - * {@inheritDoc} - */ - - @Override - public Object addItem() throws UnsupportedOperationException { - Object emptyKey[] = new Object[queryDelegate.getPrimaryKeyColumns() - .size()]; - RowId itemId = new TemporaryRowId(emptyKey); - // Create new empty column properties for the row item. - List<ColumnProperty> itemProperties = new ArrayList<ColumnProperty>(); - for (String propertyId : propertyIds) { - /* Default settings for new item properties. */ - ColumnProperty cp = new ColumnProperty(propertyId, - propertyReadOnly.get(propertyId), - propertyPersistable.get(propertyId), - propertyNullable.get(propertyId), - propertyPrimaryKey.get(propertyId), null, - getType(propertyId)); - - itemProperties.add(cp); - } - RowItem newRowItem = new RowItem(this, itemId, itemProperties); - - if (autoCommit) { - /* Add and commit instantly */ - try { - if (queryDelegate instanceof TableQuery) { - itemId = ((TableQuery) queryDelegate) - .storeRowImmediately(newRowItem); - } else { - queryDelegate.beginTransaction(); - queryDelegate.storeRow(newRowItem); - queryDelegate.commit(); - } - refresh(); - if (notificationsEnabled) { - CacheFlushNotifier.notifyOfCacheFlush(this); - } - getLogger().log(Level.FINER, "Row added to DB..."); - return itemId; - } catch (SQLException e) { - getLogger().log(Level.WARNING, - "Failed to add row to DB. Rolling back.", e); - try { - queryDelegate.rollback(); - } catch (SQLException ee) { - getLogger().log(Level.SEVERE, - "Failed to roll back row addition", e); - } - return null; - } - } else { - addedItems.add(newRowItem); - fireContentsChange(); - return itemId; - } - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#containsId(java.lang.Object) - */ - - @Override - public boolean containsId(Object itemId) { - if (itemId == null) { - return false; - } - - if (cachedItems.containsKey(itemId)) { - return true; - } else { - for (RowItem item : addedItems) { - if (item.getId().equals(itemId)) { - return itemPassesFilters(item); - } - } - } - if (removedItems.containsKey(itemId)) { - return false; - } - - if (itemId instanceof ReadOnlyRowId) { - int rowNum = ((ReadOnlyRowId) itemId).getRowNum(); - return rowNum >= 0 && rowNum < size; - } - - if (itemId instanceof RowId && !(itemId instanceof TemporaryRowId)) { - try { - return queryDelegate.containsRowWithKey(((RowId) itemId) - .getId()); - } catch (Exception e) { - /* Query failed, just return false. */ - getLogger().log(Level.WARNING, "containsId query failed", e); - } - } - return false; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#getContainerProperty(java.lang.Object, - * java.lang.Object) - */ - - @Override - public Property getContainerProperty(Object itemId, Object propertyId) { - Item item = getItem(itemId); - if (item == null) { - return null; - } - return item.getItemProperty(propertyId); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#getContainerPropertyIds() - */ - - @Override - public Collection<?> getContainerPropertyIds() { - return Collections.unmodifiableCollection(propertyIds); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#getItem(java.lang.Object) - */ - - @Override - public Item getItem(Object itemId) { - if (!cachedItems.containsKey(itemId)) { - int index = indexOfId(itemId); - if (index >= size) { - // The index is in the added items - int offset = index - size; - RowItem item = addedItems.get(offset); - if (itemPassesFilters(item)) { - return item; - } else { - return null; - } - } else { - // load the item into cache - updateOffsetAndCache(index); - } - } - return cachedItems.get(itemId); - } - - /** - * Bypasses in-memory filtering to return items that are cached in memory. - * <em>NOTE</em>: This does not bypass database-level filtering. - * - * @param itemId - * the id of the item to retrieve. - * @return the item represented by itemId. - */ - public Item getItemUnfiltered(Object itemId) { - if (!cachedItems.containsKey(itemId)) { - for (RowItem item : addedItems) { - if (item.getId().equals(itemId)) { - return item; - } - } - } - return cachedItems.get(itemId); - } - - /** - * NOTE! Do not use this method if in any way avoidable. This method doesn't - * (and cannot) use lazy loading, which means that all rows in the database - * will be loaded into memory. - * - * {@inheritDoc} - */ - - @Override - public Collection<?> getItemIds() { - updateCount(); - ArrayList<RowId> ids = new ArrayList<RowId>(); - ResultSet rs = null; - try { - // Load ALL rows :( - queryDelegate.beginTransaction(); - rs = queryDelegate.getResults(0, 0); - List<String> pKeys = queryDelegate.getPrimaryKeyColumns(); - while (rs.next()) { - RowId id = null; - if (pKeys.isEmpty()) { - /* Create a read only itemId */ - id = new ReadOnlyRowId(rs.getRow()); - } else { - /* Generate itemId for the row based on primary key(s) */ - Object[] itemId = new Object[pKeys.size()]; - for (int i = 0; i < pKeys.size(); i++) { - itemId[i] = rs.getObject(pKeys.get(i)); - } - id = new RowId(itemId); - } - if (id != null && !removedItems.containsKey(id)) { - ids.add(id); - } - } - rs.getStatement().close(); - rs.close(); - queryDelegate.commit(); - } catch (SQLException e) { - getLogger().log(Level.WARNING, - "getItemIds() failed, rolling back.", e); - try { - queryDelegate.rollback(); - } catch (SQLException e1) { - getLogger().log(Level.SEVERE, "Failed to roll back state", e1); - } - try { - rs.getStatement().close(); - rs.close(); - } catch (SQLException e1) { - getLogger().log(Level.WARNING, "Closing session failed", e1); - } - throw new RuntimeException("Failed to fetch item indexes.", e); - } - for (RowItem item : getFilteredAddedItems()) { - ids.add(item.getId()); - } - return Collections.unmodifiableCollection(ids); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#getType(java.lang.Object) - */ - - @Override - public Class<?> getType(Object propertyId) { - if (!propertyIds.contains(propertyId)) { - return null; - } - return propertyTypes.get(propertyId); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#size() - */ - - @Override - public int size() { - updateCount(); - return size + sizeOfAddedItems() - removedItems.size(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#removeItem(java.lang.Object) - */ - - @Override - public boolean removeItem(Object itemId) - throws UnsupportedOperationException { - if (!containsId(itemId)) { - return false; - } - for (RowItem item : addedItems) { - if (item.getId().equals(itemId)) { - addedItems.remove(item); - fireContentsChange(); - return true; - } - } - - if (autoCommit) { - /* Remove and commit instantly. */ - Item i = getItem(itemId); - if (i == null) { - return false; - } - try { - queryDelegate.beginTransaction(); - boolean success = queryDelegate.removeRow((RowItem) i); - queryDelegate.commit(); - refresh(); - if (notificationsEnabled) { - CacheFlushNotifier.notifyOfCacheFlush(this); - } - if (success) { - getLogger().log(Level.FINER, "Row removed from DB..."); - } - return success; - } catch (SQLException e) { - getLogger().log(Level.WARNING, - "Failed to remove row, rolling back", e); - try { - queryDelegate.rollback(); - } catch (SQLException ee) { - /* Nothing can be done here */ - getLogger().log(Level.SEVERE, - "Failed to rollback row removal", ee); - } - return false; - } catch (OptimisticLockException e) { - getLogger().log(Level.WARNING, - "Failed to remove row, rolling back", e); - try { - queryDelegate.rollback(); - } catch (SQLException ee) { - /* Nothing can be done here */ - getLogger().log(Level.SEVERE, - "Failed to rollback row removal", ee); - } - throw e; - } - } else { - removedItems.put((RowId) itemId, (RowItem) getItem(itemId)); - cachedItems.remove(itemId); - refresh(); - return true; - } - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#removeAllItems() - */ - - @Override - public boolean removeAllItems() throws UnsupportedOperationException { - if (autoCommit) { - /* Remove and commit instantly. */ - try { - queryDelegate.beginTransaction(); - boolean success = true; - for (Object id : getItemIds()) { - if (!queryDelegate.removeRow((RowItem) getItem(id))) { - success = false; - } - } - if (success) { - queryDelegate.commit(); - getLogger().log(Level.FINER, "All rows removed from DB..."); - refresh(); - if (notificationsEnabled) { - CacheFlushNotifier.notifyOfCacheFlush(this); - } - } else { - queryDelegate.rollback(); - } - return success; - } catch (SQLException e) { - getLogger().log(Level.WARNING, - "removeAllItems() failed, rolling back", e); - try { - queryDelegate.rollback(); - } catch (SQLException ee) { - /* Nothing can be done here */ - getLogger().log(Level.SEVERE, "Failed to roll back", ee); - } - return false; - } catch (OptimisticLockException e) { - getLogger().log(Level.WARNING, - "removeAllItems() failed, rolling back", e); - try { - queryDelegate.rollback(); - } catch (SQLException ee) { - /* Nothing can be done here */ - getLogger().log(Level.SEVERE, "Failed to roll back", ee); - } - throw e; - } - } else { - for (Object id : getItemIds()) { - removedItems.put((RowId) id, (RowItem) getItem(id)); - cachedItems.remove(id); - } - refresh(); - return true; - } - } - - /*************************************************/ - /** Methods from interface Container.Filterable **/ - /*************************************************/ - - /** - * {@inheritDoc} - */ - - @Override - public void addContainerFilter(Filter filter) - throws UnsupportedFilterException { - // filter.setCaseSensitive(!ignoreCase); - - filters.add(filter); - refresh(); - } - - /** - * {@inheritDoc} - */ - - @Override - public void removeContainerFilter(Filter filter) { - filters.remove(filter); - refresh(); - } - - /** - * {@inheritDoc} - */ - public void addContainerFilter(Object propertyId, String filterString, - boolean ignoreCase, boolean onlyMatchPrefix) { - if (propertyId == null || !propertyIds.contains(propertyId)) { - return; - } - - /* Generate Filter -object */ - String likeStr = onlyMatchPrefix ? filterString + "%" : "%" - + filterString + "%"; - Like like = new Like(propertyId.toString(), likeStr); - like.setCaseSensitive(!ignoreCase); - filters.add(like); - refresh(); - } - - /** - * {@inheritDoc} - */ - public void removeContainerFilters(Object propertyId) { - ArrayList<Filter> toRemove = new ArrayList<Filter>(); - for (Filter f : filters) { - if (f.appliesToProperty(propertyId)) { - toRemove.add(f); - } - } - filters.removeAll(toRemove); - refresh(); - } - - /** - * {@inheritDoc} - */ - @Override - public void removeAllContainerFilters() { - filters.clear(); - refresh(); - } - - /** - * Returns true if any filters have been applied to the container. - * - * @return true if the container has filters applied, false otherwise - * @since 7.1 - */ - public boolean hasContainerFilters() { - return !getContainerFilters().isEmpty(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Filterable#getContainerFilters() - */ - @Override - public Collection<Filter> getContainerFilters() { - return Collections.unmodifiableCollection(filters); - } - - /**********************************************/ - /** Methods from interface Container.Indexed **/ - /**********************************************/ - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Indexed#indexOfId(java.lang.Object) - */ - - @Override - public int indexOfId(Object itemId) { - // First check if the id is in the added items - for (int ix = 0; ix < addedItems.size(); ix++) { - RowItem item = addedItems.get(ix); - if (item.getId().equals(itemId)) { - if (itemPassesFilters(item)) { - updateCount(); - return size + ix; - } else { - return -1; - } - } - } - - if (!containsId(itemId)) { - return -1; - } - if (cachedItems.isEmpty()) { - getPage(); - } - // this protects against infinite looping - int counter = 0; - int oldIndex; - while (counter < size) { - if (itemIndexes.containsValue(itemId)) { - for (Integer idx : itemIndexes.keySet()) { - if (itemIndexes.get(idx).equals(itemId)) { - return idx; - } - } - } - oldIndex = currentOffset; - // load in the next page. - int nextIndex = currentOffset + pageLength * CACHE_RATIO - + cacheOverlap; - if (nextIndex >= size) { - // Container wrapped around, start from index 0. - nextIndex = 0; - } - updateOffsetAndCache(nextIndex); - - // Update counter - if (currentOffset > oldIndex) { - counter += currentOffset - oldIndex; - } else { - counter += size - oldIndex; - } - } - // safeguard in case item not found - return -1; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Indexed#getIdByIndex(int) - */ - - @Override - public Object getIdByIndex(int index) { - if (index < 0) { - throw new IndexOutOfBoundsException("Index is negative! index=" - + index); - } - // make sure the size field is valid - updateCount(); - if (index < size) { - if (itemIndexes.keySet().contains(index)) { - return itemIndexes.get(index); - } - updateOffsetAndCache(index); - return itemIndexes.get(index); - } else { - // The index is in the added items - int offset = index - size; - // TODO this is very inefficient if looping - should improve - // getItemIds(int, int) - return getFilteredAddedItems().get(offset).getId(); - } - } - - @Override - public List<Object> getItemIds(int startIndex, int numberOfIds) { - // TODO create a better implementation - return (List<Object>) ContainerHelpers.getItemIdsUsingGetIdByIndex( - startIndex, numberOfIds, this); - } - - /**********************************************/ - /** Methods from interface Container.Ordered **/ - /**********************************************/ - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Ordered#nextItemId(java.lang.Object) - */ - - @Override - public Object nextItemId(Object itemId) { - int index = indexOfId(itemId) + 1; - try { - return getIdByIndex(index); - } catch (IndexOutOfBoundsException e) { - return null; - } - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Ordered#prevItemId(java.lang.Object) - */ - - @Override - public Object prevItemId(Object itemId) { - int prevIndex = indexOfId(itemId) - 1; - try { - return getIdByIndex(prevIndex); - } catch (IndexOutOfBoundsException e) { - return null; - } - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Ordered#firstItemId() - */ - - @Override - public Object firstItemId() { - updateCount(); - if (size == 0) { - if (addedItems.isEmpty()) { - return null; - } else { - int ix = -1; - do { - ix++; - } while (!itemPassesFilters(addedItems.get(ix)) - && ix < addedItems.size()); - if (ix < addedItems.size()) { - return addedItems.get(ix).getId(); - } - } - } - if (!itemIndexes.containsKey(0)) { - updateOffsetAndCache(0); - } - return itemIndexes.get(0); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Ordered#lastItemId() - */ - - @Override - public Object lastItemId() { - if (addedItems.isEmpty()) { - int lastIx = size() - 1; - if (!itemIndexes.containsKey(lastIx)) { - updateOffsetAndCache(size - 1); - } - return itemIndexes.get(lastIx); - } else { - int ix = addedItems.size(); - do { - ix--; - } while (!itemPassesFilters(addedItems.get(ix)) && ix >= 0); - if (ix >= 0) { - return addedItems.get(ix).getId(); - } else { - return null; - } - } - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Ordered#isFirstId(java.lang.Object) - */ - - @Override - public boolean isFirstId(Object itemId) { - return firstItemId().equals(itemId); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Ordered#isLastId(java.lang.Object) - */ - - @Override - public boolean isLastId(Object itemId) { - return lastItemId().equals(itemId); - } - - /***********************************************/ - /** Methods from interface Container.Sortable **/ - /***********************************************/ - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Sortable#sort(java.lang.Object[], - * boolean[]) - */ - - @Override - public void sort(Object[] propertyId, boolean[] ascending) { - sorters.clear(); - if (propertyId == null || propertyId.length == 0) { - refresh(); - return; - } - /* Generate OrderBy -objects */ - boolean asc = true; - for (int i = 0; i < propertyId.length; i++) { - /* Check that the property id is valid */ - if (propertyId[i] instanceof String - && propertyIds.contains(propertyId[i])) { - try { - asc = ascending[i]; - } catch (Exception e) { - getLogger().log(Level.WARNING, "", e); - } - sorters.add(new OrderBy((String) propertyId[i], asc)); - } - } - refresh(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Sortable#getSortableContainerPropertyIds() - */ - - @Override - public Collection<?> getSortableContainerPropertyIds() { - return getContainerPropertyIds(); - } - - /**************************************/ - /** Methods specific to SQLContainer **/ - /**************************************/ - - /** - * Refreshes the container - clears all caches and resets size and offset. - * Does NOT remove sorting or filtering rules! - */ - public void refresh() { - refresh(true); - } - - /** - * Refreshes the container. If <code>setSizeDirty</code> is - * <code>false</code>, assumes that the current size is up to date. This is - * used in {@link #updateCount()} to refresh the contents when we know the - * size was just updated. - * - * @param setSizeDirty - */ - private void refresh(boolean setSizeDirty) { - if (setSizeDirty) { - sizeDirty = true; - } - currentOffset = 0; - cachedItems.clear(); - itemIndexes.clear(); - fireContentsChange(); - } - - /** - * Returns modify state of the container. - * - * @return true if contents of this container have been modified - */ - public boolean isModified() { - return !removedItems.isEmpty() || !addedItems.isEmpty() - || !modifiedItems.isEmpty(); - } - - /** - * Set auto commit mode enabled or disabled. Auto commit mode means that all - * changes made to items of this container will be immediately written to - * the underlying data source. - * - * @param autoCommitEnabled - * true to enable auto commit mode - */ - public void setAutoCommit(boolean autoCommitEnabled) { - autoCommit = autoCommitEnabled; - } - - /** - * Returns status of the auto commit mode. - * - * @return true if auto commit mode is enabled - */ - public boolean isAutoCommit() { - return autoCommit; - } - - /** - * Returns the currently set page length. - * - * @return current page length - */ - public int getPageLength() { - return pageLength; - } - - /** - * Sets the page length used in lazy fetching of items from the data source. - * Also resets the cache size to match the new page length. - * - * As a side effect the container will be refreshed. - * - * @param pageLength - * new page length - */ - public void setPageLength(int pageLength) { - setPageLengthInternal(pageLength); - refresh(); - } - - /** - * Sets the page length internally, without refreshing the container. - * - * @param pageLength - * the new page length - */ - private void setPageLengthInternal(int pageLength) { - this.pageLength = pageLength > 0 ? pageLength : DEFAULT_PAGE_LENGTH; - cacheOverlap = getPageLength(); - cachedItems.setCacheLimit(CACHE_RATIO * getPageLength() + cacheOverlap); - } - - /** - * Adds the given OrderBy to this container and refreshes the container - * contents with the new sorting rules. - * - * Note that orderBy.getColumn() must return a column name that exists in - * this container. - * - * @param orderBy - * OrderBy to be added to the container sorting rules - */ - public void addOrderBy(OrderBy orderBy) { - if (orderBy == null) { - return; - } - if (!propertyIds.contains(orderBy.getColumn())) { - throw new IllegalArgumentException( - "The column given for sorting does not exist in this container."); - } - sorters.add(orderBy); - refresh(); - } - - /** - * Commits all the changes, additions and removals made to the items of this - * container. - * - * @throws UnsupportedOperationException - * @throws SQLException - */ - public void commit() throws UnsupportedOperationException, SQLException { - try { - getLogger().log(Level.FINER, - "Commiting changes through delegate..."); - queryDelegate.beginTransaction(); - /* Perform buffered deletions */ - for (RowItem item : removedItems.values()) { - try { - if (!queryDelegate.removeRow(item)) { - throw new SQLException( - "Removal failed for row with ID: " - + item.getId()); - } - } catch (IllegalArgumentException e) { - throw new SQLException("Removal failed for row with ID: " - + item.getId(), e); - } - } - /* Perform buffered modifications */ - for (RowItem item : modifiedItems) { - if (!removedItems.containsKey(item.getId())) { - if (queryDelegate.storeRow(item) > 0) { - /* - * Also reset the modified state in the item in case it - * is reused e.g. in a form. - */ - item.commit(); - } else { - queryDelegate.rollback(); - refresh(); - throw new ConcurrentModificationException( - "Item with the ID '" + item.getId() - + "' has been externally modified."); - } - } - } - /* Perform buffered additions */ - for (RowItem item : addedItems) { - queryDelegate.storeRow(item); - } - queryDelegate.commit(); - removedItems.clear(); - addedItems.clear(); - modifiedItems.clear(); - refresh(); - if (notificationsEnabled) { - CacheFlushNotifier.notifyOfCacheFlush(this); - } - } catch (SQLException e) { - queryDelegate.rollback(); - throw e; - } catch (OptimisticLockException e) { - queryDelegate.rollback(); - throw e; - } - } - - /** - * Rolls back all the changes, additions and removals made to the items of - * this container. - * - * @throws UnsupportedOperationException - * @throws SQLException - */ - public void rollback() throws UnsupportedOperationException, SQLException { - getLogger().log(Level.FINE, "Rolling back changes..."); - removedItems.clear(); - addedItems.clear(); - modifiedItems.clear(); - refresh(); - } - - /** - * Notifies this container that a property in the given item has been - * modified. The change will be buffered or made instantaneously depending - * on auto commit mode. - * - * @param changedItem - * item that has a modified property - */ - void itemChangeNotification(RowItem changedItem) { - if (autoCommit) { - try { - queryDelegate.beginTransaction(); - if (queryDelegate.storeRow(changedItem) == 0) { - queryDelegate.rollback(); - refresh(); - throw new ConcurrentModificationException( - "Item with the ID '" + changedItem.getId() - + "' has been externally modified."); - } - queryDelegate.commit(); - if (notificationsEnabled) { - CacheFlushNotifier.notifyOfCacheFlush(this); - } - getLogger().log(Level.FINER, "Row updated to DB..."); - } catch (SQLException e) { - getLogger().log(Level.WARNING, - "itemChangeNotification failed, rolling back...", e); - try { - queryDelegate.rollback(); - } catch (SQLException ee) { - /* Nothing can be done here */ - getLogger().log(Level.SEVERE, "Rollback failed", e); - } - throw new RuntimeException(e); - } - } else { - if (!(changedItem.getId() instanceof TemporaryRowId) - && !modifiedItems.contains(changedItem)) { - modifiedItems.add(changedItem); - } - } - } - - /** - * Determines a new offset for updating the row cache. The offset is - * calculated from the given index, and will be fixed to match the start of - * a page, based on the value of pageLength. - * - * @param index - * Index of the item that was requested, but not found in cache - */ - private void updateOffsetAndCache(int index) { - - int oldOffset = currentOffset; - - currentOffset = (index / pageLength) * pageLength - cacheOverlap; - - if (currentOffset < 0) { - currentOffset = 0; - } - - if (oldOffset == currentOffset && !cachedItems.isEmpty()) { - return; - } - - getPage(); - } - - /** - * Fetches new count of rows from the data source, if needed. - */ - private void updateCount() { - if (!sizeDirty - && new Date().getTime() < sizeUpdated.getTime() - + sizeValidMilliSeconds) { - return; - } - try { - try { - queryDelegate.setFilters(filters); - } catch (UnsupportedOperationException e) { - getLogger().log(Level.FINE, - "The query delegate doesn't support filtering", e); - } - try { - queryDelegate.setOrderBy(sorters); - } catch (UnsupportedOperationException e) { - getLogger().log(Level.FINE, - "The query delegate doesn't support sorting", e); - } - int newSize = queryDelegate.getCount(); - sizeUpdated = new Date(); - sizeDirty = false; - if (newSize != size) { - size = newSize; - // Size is up to date so don't set it back to dirty in refresh() - refresh(false); - } - getLogger().log(Level.FINER, - "Updated row count. New count is: {0}", size); - } catch (SQLException e) { - throw new RuntimeException("Failed to update item set size.", e); - } - } - - /** - * Fetches property id's (column names and their types) from the data - * source. - * - * @throws SQLException - */ - private void getPropertyIds() throws SQLException { - propertyIds.clear(); - propertyTypes.clear(); - queryDelegate.setFilters(null); - queryDelegate.setOrderBy(null); - ResultSet rs = null; - ResultSetMetaData rsmd = null; - try { - queryDelegate.beginTransaction(); - rs = queryDelegate.getResults(0, 1); - rsmd = rs.getMetaData(); - boolean resultExists = rs.next(); - Class<?> type = null; - for (int i = 1; i <= rsmd.getColumnCount(); i++) { - if (!isColumnIdentifierValid(rsmd.getColumnLabel(i))) { - continue; - } - String colName = rsmd.getColumnLabel(i); - /* - * Make sure not to add the same colName twice. This can easily - * happen if the SQL query joins many tables with an ID column. - */ - if (!propertyIds.contains(colName)) { - propertyIds.add(colName); - } - /* Try to determine the column's JDBC class by all means. */ - if (resultExists && rs.getObject(i) != null) { - type = rs.getObject(i).getClass(); - } else { - try { - type = Class.forName(rsmd.getColumnClassName(i)); - } catch (Exception e) { - getLogger().log(Level.WARNING, "Class not found", e); - /* On failure revert to Object and hope for the best. */ - type = Object.class; - } - } - /* - * Determine read only and nullability status of the column. A - * column is read only if it is reported as either read only or - * auto increment by the database, and also it is set as the - * version column in a TableQuery delegate. - */ - boolean readOnly = rsmd.isAutoIncrement(i) - || rsmd.isReadOnly(i); - - boolean persistable = !rsmd.isReadOnly(i); - - if (queryDelegate instanceof TableQuery) { - if (rsmd.getColumnLabel(i).equals( - ((TableQuery) queryDelegate).getVersionColumn())) { - readOnly = true; - } - } - - propertyReadOnly.put(colName, readOnly); - propertyPersistable.put(colName, persistable); - propertyNullable.put(colName, - rsmd.isNullable(i) == ResultSetMetaData.columnNullable); - propertyPrimaryKey.put(colName, queryDelegate - .getPrimaryKeyColumns() - .contains(rsmd.getColumnLabel(i))); - propertyTypes.put(colName, type); - } - rs.getStatement().close(); - rs.close(); - queryDelegate.commit(); - getLogger().log(Level.FINER, "Property IDs fetched."); - } catch (SQLException e) { - getLogger().log(Level.WARNING, - "Failed to fetch property ids, rolling back", e); - try { - queryDelegate.rollback(); - } catch (SQLException e1) { - getLogger().log(Level.SEVERE, "Failed to roll back", e1); - } - try { - if (rs != null) { - if (rs.getStatement() != null) { - rs.getStatement().close(); - } - rs.close(); - } - } catch (SQLException e1) { - getLogger().log(Level.WARNING, "Failed to close session", e1); - } - throw e; - } - } - - /** - * Fetches a page from the data source based on the values of pageLength and - * currentOffset. Also updates the set of primary keys, used in - * identification of RowItems. - */ - private void getPage() { - updateCount(); - ResultSet rs = null; - ResultSetMetaData rsmd = null; - cachedItems.clear(); - itemIndexes.clear(); - try { - try { - queryDelegate.setOrderBy(sorters); - } catch (UnsupportedOperationException e) { - /* The query delegate doesn't support sorting. */ - /* No need to do anything. */ - getLogger().log(Level.FINE, - "The query delegate doesn't support sorting", e); - } - queryDelegate.beginTransaction(); - int fetchedRows = pageLength * CACHE_RATIO + cacheOverlap; - rs = queryDelegate.getResults(currentOffset, fetchedRows); - rsmd = rs.getMetaData(); - List<String> pKeys = queryDelegate.getPrimaryKeyColumns(); - // } - /* Create new items and column properties */ - ColumnProperty cp = null; - int rowCount = currentOffset; - if (!queryDelegate.implementationRespectsPagingLimits()) { - rowCount = currentOffset = 0; - setPageLengthInternal(size); - } - while (rs.next()) { - List<ColumnProperty> itemProperties = new ArrayList<ColumnProperty>(); - /* Generate row itemId based on primary key(s) */ - Object[] itemId = new Object[pKeys.size()]; - for (int i = 0; i < pKeys.size(); i++) { - itemId[i] = rs.getObject(pKeys.get(i)); - } - RowId id = null; - if (pKeys.isEmpty()) { - id = new ReadOnlyRowId(rs.getRow()); - } else { - id = new RowId(itemId); - } - List<String> propertiesToAdd = new ArrayList<String>( - propertyIds); - if (!removedItems.containsKey(id)) { - for (int i = 1; i <= rsmd.getColumnCount(); i++) { - if (!isColumnIdentifierValid(rsmd.getColumnLabel(i))) { - continue; - } - String colName = rsmd.getColumnLabel(i); - Object value = rs.getObject(i); - Class<?> type = value != null ? value.getClass() - : Object.class; - if (value == null) { - for (String propName : propertyTypes.keySet()) { - if (propName.equals(rsmd.getColumnLabel(i))) { - type = propertyTypes.get(propName); - break; - } - } - } - /* - * In case there are more than one column with the same - * name, add only the first one. This can easily happen - * if you join many tables where each table has an ID - * column. - */ - if (propertiesToAdd.contains(colName)) { - - cp = new ColumnProperty(colName, - propertyReadOnly.get(colName), - propertyPersistable.get(colName), - propertyNullable.get(colName), - propertyPrimaryKey.get(colName), value, - type); - itemProperties.add(cp); - propertiesToAdd.remove(colName); - } - } - /* Cache item */ - itemIndexes.put(rowCount, id); - - // if an item with the id is contained in the modified - // cache, then use this record and add it to the cached - // items. Otherwise create a new item - int modifiedIndex = indexInModifiedCache(id); - if (modifiedIndex != -1) { - cachedItems.put(id, modifiedItems.get(modifiedIndex)); - } else { - cachedItems.put(id, new RowItem(this, id, - itemProperties)); - } - - rowCount++; - } - } - rs.getStatement().close(); - rs.close(); - queryDelegate.commit(); - getLogger().log(Level.FINER, "Fetched {0} rows starting from {1}", - new Object[] { fetchedRows, currentOffset }); - } catch (SQLException e) { - getLogger().log(Level.WARNING, - "Failed to fetch rows, rolling back", e); - try { - queryDelegate.rollback(); - } catch (SQLException e1) { - getLogger().log(Level.SEVERE, "Failed to roll back", e1); - } - try { - if (rs != null) { - if (rs.getStatement() != null) { - rs.getStatement().close(); - rs.close(); - } - } - } catch (SQLException e1) { - getLogger().log(Level.WARNING, "Failed to close session", e1); - } - throw new RuntimeException("Failed to fetch page.", e); - } - } - - /** - * Returns the index of the item with the given itemId for the modified - * cache. - * - * @param itemId - * @return the index of the item with the itemId in the modified cache. Or - * -1 if not found. - */ - private int indexInModifiedCache(Object itemId) { - for (int ix = 0; ix < modifiedItems.size(); ix++) { - RowItem item = modifiedItems.get(ix); - if (item.getId().equals(itemId)) { - return ix; - } - } - return -1; - } - - private int sizeOfAddedItems() { - return getFilteredAddedItems().size(); - } - - private List<RowItem> getFilteredAddedItems() { - ArrayList<RowItem> filtered = new ArrayList<RowItem>(addedItems); - if (filters != null && !filters.isEmpty()) { - for (RowItem item : addedItems) { - if (!itemPassesFilters(item)) { - filtered.remove(item); - } - } - } - return filtered; - } - - private boolean itemPassesFilters(RowItem item) { - for (Filter filter : filters) { - if (!filter.passesFilter(item.getId(), item)) { - return false; - } - } - return true; - } - - /** - * Checks is the given column identifier valid to be used with SQLContainer. - * Currently the only non-valid identifier is "rownum" when MSSQL or Oracle - * is used. This is due to the way the SELECT queries are constructed in - * order to implement paging in these databases. - * - * @param identifier - * Column identifier - * @return true if the identifier is valid - */ - private boolean isColumnIdentifierValid(String identifier) { - if (identifier.equalsIgnoreCase("rownum") - && queryDelegate instanceof TableQuery) { - TableQuery tq = (TableQuery) queryDelegate; - if (tq.getSqlGenerator() instanceof MSSQLGenerator - || tq.getSqlGenerator() instanceof OracleGenerator) { - return false; - } - } - return true; - } - - /** - * Returns the QueryDelegate set for this SQLContainer. - * - * @return current querydelegate - */ - protected QueryDelegate getQueryDelegate() { - return queryDelegate; - } - - /************************************/ - /** UNSUPPORTED CONTAINER FEATURES **/ - /************************************/ - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#addContainerProperty(java.lang.Object, - * java.lang.Class, java.lang.Object) - */ - - @Override - public boolean addContainerProperty(Object propertyId, Class<?> type, - Object defaultValue) throws UnsupportedOperationException { - throw new UnsupportedOperationException(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#removeContainerProperty(java.lang.Object) - */ - - @Override - public boolean removeContainerProperty(Object propertyId) - throws UnsupportedOperationException { - throw new UnsupportedOperationException(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#addItem(java.lang.Object) - */ - - @Override - public Item addItem(Object itemId) throws UnsupportedOperationException { - throw new UnsupportedOperationException(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Ordered#addItemAfter(java.lang.Object, - * java.lang.Object) - */ - - @Override - public Item addItemAfter(Object previousItemId, Object newItemId) - throws UnsupportedOperationException { - throw new UnsupportedOperationException(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Indexed#addItemAt(int, java.lang.Object) - */ - - @Override - public Item addItemAt(int index, Object newItemId) - throws UnsupportedOperationException { - throw new UnsupportedOperationException(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Indexed#addItemAt(int) - */ - - @Override - public Object addItemAt(int index) throws UnsupportedOperationException { - throw new UnsupportedOperationException(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Ordered#addItemAfter(java.lang.Object) - */ - - @Override - public Object addItemAfter(Object previousItemId) - throws UnsupportedOperationException { - throw new UnsupportedOperationException(); - } - - /******************************************/ - /** ITEMSETCHANGENOTIFIER IMPLEMENTATION **/ - /******************************************/ - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.Container.ItemSetChangeNotifier#addListener(com.vaadin - * .data.Container.ItemSetChangeListener) - */ - - @Override - public void addItemSetChangeListener( - Container.ItemSetChangeListener listener) { - if (itemSetChangeListeners == null) { - itemSetChangeListeners = new LinkedList<Container.ItemSetChangeListener>(); - } - itemSetChangeListeners.add(listener); - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #addItemSetChangeListener(com.vaadin.data.Container.ItemSetChangeListener)} - **/ - @Override - @Deprecated - public void addListener(Container.ItemSetChangeListener listener) { - addItemSetChangeListener(listener); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.Container.ItemSetChangeNotifier#removeListener(com.vaadin - * .data.Container.ItemSetChangeListener) - */ - - @Override - public void removeItemSetChangeListener( - Container.ItemSetChangeListener listener) { - if (itemSetChangeListeners != null) { - itemSetChangeListeners.remove(listener); - } - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #removeItemSetChangeListener(com.vaadin.data.Container.ItemSetChangeListener)} - **/ - @Override - @Deprecated - public void removeListener(Container.ItemSetChangeListener listener) { - removeItemSetChangeListener(listener); - } - - protected void fireContentsChange() { - if (itemSetChangeListeners != null) { - final Object[] l = itemSetChangeListeners.toArray(); - final Container.ItemSetChangeEvent event = new SQLContainer.ItemSetChangeEvent( - this); - for (int i = 0; i < l.length; i++) { - ((Container.ItemSetChangeListener) l[i]) - .containerItemSetChange(event); - } - } - } - - /** - * Simple ItemSetChangeEvent implementation. - */ - @SuppressWarnings("serial") - public static class ItemSetChangeEvent extends EventObject implements - Container.ItemSetChangeEvent { - - private ItemSetChangeEvent(SQLContainer source) { - super(source); - } - - @Override - public Container getContainer() { - return (Container) getSource(); - } - } - - /**************************************************/ - /** ROWIDCHANGELISTENER PASSING TO QUERYDELEGATE **/ - /**************************************************/ - - /** - * Adds a RowIdChangeListener to the QueryDelegate - * - * @param listener - */ - public void addRowIdChangeListener(RowIdChangeListener listener) { - if (queryDelegate instanceof QueryDelegate.RowIdChangeNotifier) { - ((QueryDelegate.RowIdChangeNotifier) queryDelegate) - .addListener(listener); - } - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #addRowIdChangeListener(RowIdChangeListener)} - **/ - @Deprecated - public void addListener(RowIdChangeListener listener) { - addRowIdChangeListener(listener); - } - - /** - * Removes a RowIdChangeListener from the QueryDelegate - * - * @param listener - */ - public void removeRowIdChangeListener(RowIdChangeListener listener) { - if (queryDelegate instanceof QueryDelegate.RowIdChangeNotifier) { - ((QueryDelegate.RowIdChangeNotifier) queryDelegate) - .removeListener(listener); - } - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #removeRowIdChangeListener(RowIdChangeListener)} - **/ - @Deprecated - public void removeListener(RowIdChangeListener listener) { - removeRowIdChangeListener(listener); - } - - /** - * Calling this will enable this SQLContainer to send and receive cache - * flush notifications for its lifetime. - */ - public void enableCacheFlushNotifications() { - if (!notificationsEnabled) { - notificationsEnabled = true; - CacheFlushNotifier.addInstance(this); - } - } - - /******************************************/ - /** Referencing mechanism implementation **/ - /******************************************/ - - /** - * Adds a new reference to the given SQLContainer. In addition to the - * container you must provide the column (property) names used for the - * reference in both this and the referenced SQLContainer. - * - * Note that multiple references pointing to the same SQLContainer are not - * supported. - * - * @param refdCont - * Target SQLContainer of the new reference - * @param refingCol - * Column (property) name in this container storing the (foreign - * key) reference - * @param refdCol - * Column (property) name in the referenced container storing the - * referenced key - */ - public void addReference(SQLContainer refdCont, String refingCol, - String refdCol) { - if (refdCont == null) { - throw new IllegalArgumentException( - "Referenced SQLContainer can not be null."); - } - if (!getContainerPropertyIds().contains(refingCol)) { - throw new IllegalArgumentException( - "Given referencing column name is invalid." - + " Please ensure that this container" - + " contains a property ID named: " + refingCol); - } - if (!refdCont.getContainerPropertyIds().contains(refdCol)) { - throw new IllegalArgumentException( - "Given referenced column name is invalid." - + " Please ensure that the referenced container" - + " contains a property ID named: " + refdCol); - } - if (references.keySet().contains(refdCont)) { - throw new IllegalArgumentException( - "An SQLContainer instance can only be referenced once."); - } - references.put(refdCont, new Reference(refdCont, refingCol, refdCol)); - } - - /** - * Removes the reference pointing to the given SQLContainer. - * - * @param refdCont - * Target SQLContainer of the reference - * @return true if successful, false if the reference did not exist - */ - public boolean removeReference(SQLContainer refdCont) { - if (refdCont == null) { - throw new IllegalArgumentException( - "Referenced SQLContainer can not be null."); - } - return references.remove(refdCont) == null ? false : true; - } - - /** - * Sets the referenced item. The referencing column of the item in this - * container is updated accordingly. - * - * @param itemId - * Item Id of the reference source (from this container) - * @param refdItemId - * Item Id of the reference target (from referenced container) - * @param refdCont - * Target SQLContainer of the reference - * @return true if the referenced item was successfully set, false on - * failure - */ - public boolean setReferencedItem(Object itemId, Object refdItemId, - SQLContainer refdCont) { - if (refdCont == null) { - throw new IllegalArgumentException( - "Referenced SQLContainer can not be null."); - } - Reference r = references.get(refdCont); - if (r == null) { - throw new IllegalArgumentException( - "Reference to the given SQLContainer not defined."); - } - try { - getContainerProperty(itemId, r.getReferencingColumn()).setValue( - refdCont.getContainerProperty(refdItemId, - r.getReferencedColumn())); - return true; - } catch (Exception e) { - getLogger() - .log(Level.WARNING, "Setting referenced item failed.", e); - return false; - } - } - - /** - * Fetches the Item Id of the referenced item from the target SQLContainer. - * - * @param itemId - * Item Id of the reference source (from this container) - * @param refdCont - * Target SQLContainer of the reference - * @return Item Id of the referenced item, or null if not found - */ - public Object getReferencedItemId(Object itemId, SQLContainer refdCont) { - if (refdCont == null) { - throw new IllegalArgumentException( - "Referenced SQLContainer can not be null."); - } - Reference r = references.get(refdCont); - if (r == null) { - throw new IllegalArgumentException( - "Reference to the given SQLContainer not defined."); - } - Object refKey = getContainerProperty(itemId, r.getReferencingColumn()) - .getValue(); - - refdCont.removeAllContainerFilters(); - refdCont.addContainerFilter(new Equal(r.getReferencedColumn(), refKey)); - Object toReturn = refdCont.firstItemId(); - refdCont.removeAllContainerFilters(); - return toReturn; - } - - /** - * Fetches the referenced item from the target SQLContainer. - * - * @param itemId - * Item Id of the reference source (from this container) - * @param refdCont - * Target SQLContainer of the reference - * @return The referenced item, or null if not found - */ - public Item getReferencedItem(Object itemId, SQLContainer refdCont) { - return refdCont.getItem(getReferencedItemId(itemId, refdCont)); - } - - private void writeObject(java.io.ObjectOutputStream out) throws IOException { - out.defaultWriteObject(); - } - - private void readObject(java.io.ObjectInputStream in) throws IOException, - ClassNotFoundException { - in.defaultReadObject(); - if (notificationsEnabled) { - /* - * Register instance with CacheFlushNotifier after de-serialization - * if notifications are enabled - */ - CacheFlushNotifier.addInstance(this); - } - } - - private static final Logger getLogger() { - return Logger.getLogger(SQLContainer.class.getName()); - } - -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/SQLUtil.java b/server/src/com/vaadin/data/util/sqlcontainer/SQLUtil.java deleted file mode 100644 index e5282c867a..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/SQLUtil.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer; - -import java.io.Serializable; - -public class SQLUtil implements Serializable { - /** - * Escapes different special characters in strings that are passed to SQL. - * Replaces the following: - * - * <list> <li>' is replaced with ''</li> <li>\x00 is removed</li> <li>\ is - * replaced with \\</li> <li>" is replaced with \"</li> <li> - * \x1a is removed</li> </list> - * - * Also note! The escaping done here may or may not be enough to prevent any - * and all SQL injections so it is recommended to check user input before - * giving it to the SQLContainer/TableQuery. - * - * @param constant - * @return \\\'\' - */ - public static String escapeSQL(String constant) { - if (constant == null) { - return null; - } - String fixedConstant = constant; - fixedConstant = fixedConstant.replaceAll("\\\\x00", ""); - fixedConstant = fixedConstant.replaceAll("\\\\x1a", ""); - fixedConstant = fixedConstant.replaceAll("'", "''"); - fixedConstant = fixedConstant.replaceAll("\\\\", "\\\\\\\\"); - fixedConstant = fixedConstant.replaceAll("\\\"", "\\\\\""); - return fixedConstant; - } -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/TemporaryRowId.java b/server/src/com/vaadin/data/util/sqlcontainer/TemporaryRowId.java deleted file mode 100644 index ca2f25963e..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/TemporaryRowId.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer; - -public class TemporaryRowId extends RowId { - private static final long serialVersionUID = -641983830469018329L; - - public TemporaryRowId(Object... id) { - super(id); - } - - @Override - public int hashCode() { - return id.hashCode(); - } - - @Override - public boolean equals(Object obj) { - if (obj == null || !(TemporaryRowId.class.equals(obj.getClass()))) { - return false; - } - Object[] compId = ((TemporaryRowId) obj).getId(); - return id.equals(compId); - } - - @Override - public String toString() { - return "Temporary row id"; - } - -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/connection/J2EEConnectionPool.java b/server/src/com/vaadin/data/util/sqlcontainer/connection/J2EEConnectionPool.java deleted file mode 100644 index acad5beed9..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/connection/J2EEConnectionPool.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.connection; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.logging.Level; -import java.util.logging.Logger; - -import javax.naming.InitialContext; -import javax.naming.NamingException; -import javax.sql.DataSource; - -public class J2EEConnectionPool implements JDBCConnectionPool { - - private String dataSourceJndiName; - - private DataSource dataSource = null; - - public J2EEConnectionPool(DataSource dataSource) { - this.dataSource = dataSource; - } - - public J2EEConnectionPool(String dataSourceJndiName) { - this.dataSourceJndiName = dataSourceJndiName; - } - - @Override - public Connection reserveConnection() throws SQLException { - Connection conn = getDataSource().getConnection(); - conn.setAutoCommit(false); - - return conn; - } - - private DataSource getDataSource() throws SQLException { - if (dataSource == null) { - dataSource = lookupDataSource(); - } - return dataSource; - } - - private DataSource lookupDataSource() throws SQLException { - try { - InitialContext ic = new InitialContext(); - return (DataSource) ic.lookup(dataSourceJndiName); - } catch (NamingException e) { - throw new SQLException( - "NamingException - Cannot connect to the database. Cause: " - + e.getMessage()); - } - } - - @Override - public void releaseConnection(Connection conn) { - if (conn != null) { - try { - conn.close(); - } catch (SQLException e) { - Logger.getLogger(J2EEConnectionPool.class.getName()).log( - Level.FINE, "Could not release SQL connection", e); - } - } - } - - @Override - public void destroy() { - dataSource = null; - } - -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/connection/JDBCConnectionPool.java b/server/src/com/vaadin/data/util/sqlcontainer/connection/JDBCConnectionPool.java deleted file mode 100644 index 11669075a3..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/connection/JDBCConnectionPool.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.connection; - -import java.io.Serializable; -import java.sql.Connection; -import java.sql.SQLException; - -/** - * Interface for implementing connection pools to be used with SQLContainer. - */ -public interface JDBCConnectionPool extends Serializable { - /** - * Retrieves a connection. - * - * @return a usable connection to the database - * @throws SQLException - */ - public Connection reserveConnection() throws SQLException; - - /** - * Releases a connection that was retrieved earlier. - * - * Note that depending on implementation, the transaction possibly open in - * the connection may or may not be rolled back. - * - * @param conn - * Connection to be released - */ - public void releaseConnection(Connection conn); - - /** - * Destroys the connection pool: close() is called an all the connections in - * the pool, whether available or reserved. - * - * This method was added to fix PostgreSQL -related issues with connections - * that were left hanging 'idle'. - */ - public void destroy(); -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/connection/SimpleJDBCConnectionPool.java b/server/src/com/vaadin/data/util/sqlcontainer/connection/SimpleJDBCConnectionPool.java deleted file mode 100644 index 57ea188cb4..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/connection/SimpleJDBCConnectionPool.java +++ /dev/null @@ -1,180 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.connection; - -import java.io.IOException; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.HashSet; -import java.util.Set; - -/** - * Simple implementation of the JDBCConnectionPool interface. Handles loading - * the JDBC driver, setting up the connections and ensuring they are still - * usable upon release. - */ -@SuppressWarnings("serial") -public class SimpleJDBCConnectionPool implements JDBCConnectionPool { - - private int initialConnections = 5; - private int maxConnections = 20; - - private String driverName; - private String connectionUri; - private String userName; - private String password; - - private transient Set<Connection> availableConnections; - private transient Set<Connection> reservedConnections; - - private boolean initialized; - - public SimpleJDBCConnectionPool(String driverName, String connectionUri, - String userName, String password) throws SQLException { - if (driverName == null) { - throw new IllegalArgumentException( - "JDBC driver class name must be given."); - } - if (connectionUri == null) { - throw new IllegalArgumentException( - "Database connection URI must be given."); - } - if (userName == null) { - throw new IllegalArgumentException( - "Database username must be given."); - } - if (password == null) { - throw new IllegalArgumentException( - "Database password must be given."); - } - this.driverName = driverName; - this.connectionUri = connectionUri; - this.userName = userName; - this.password = password; - - /* Initialize JDBC driver */ - try { - Class.forName(driverName).newInstance(); - } catch (Exception ex) { - throw new RuntimeException("Specified JDBC Driver: " + driverName - + " - initialization failed.", ex); - } - } - - public SimpleJDBCConnectionPool(String driverName, String connectionUri, - String userName, String password, int initialConnections, - int maxConnections) throws SQLException { - this(driverName, connectionUri, userName, password); - this.initialConnections = initialConnections; - this.maxConnections = maxConnections; - } - - private void initializeConnections() throws SQLException { - availableConnections = new HashSet<Connection>(initialConnections); - reservedConnections = new HashSet<Connection>(initialConnections); - for (int i = 0; i < initialConnections; i++) { - availableConnections.add(createConnection()); - } - initialized = true; - } - - @Override - public synchronized Connection reserveConnection() throws SQLException { - if (!initialized) { - initializeConnections(); - } - if (availableConnections.isEmpty()) { - if (reservedConnections.size() < maxConnections) { - availableConnections.add(createConnection()); - } else { - throw new SQLException("Connection limit has been reached."); - } - } - - Connection c = availableConnections.iterator().next(); - availableConnections.remove(c); - reservedConnections.add(c); - - return c; - } - - @Override - public synchronized void releaseConnection(Connection conn) { - if (conn == null || !initialized) { - return; - } - /* Try to roll back if necessary */ - try { - if (!conn.getAutoCommit()) { - conn.rollback(); - } - } catch (SQLException e) { - /* Roll back failed, close and discard connection */ - try { - conn.close(); - } catch (SQLException e1) { - /* Nothing needs to be done */ - } - reservedConnections.remove(conn); - return; - } - reservedConnections.remove(conn); - availableConnections.add(conn); - } - - private Connection createConnection() throws SQLException { - Connection c = DriverManager.getConnection(connectionUri, userName, - password); - c.setAutoCommit(false); - if (driverName.toLowerCase().contains("mysql")) { - try { - Statement s = c.createStatement(); - s.execute("SET SESSION sql_mode = 'ANSI'"); - s.close(); - } catch (Exception e) { - // Failed to set ansi mode; continue - } - } - return c; - } - - @Override - public void destroy() { - for (Connection c : availableConnections) { - try { - c.close(); - } catch (SQLException e) { - // No need to do anything - } - } - for (Connection c : reservedConnections) { - try { - c.close(); - } catch (SQLException e) { - // No need to do anything - } - } - - } - - private void writeObject(java.io.ObjectOutputStream out) throws IOException { - initialized = false; - out.defaultWriteObject(); - } - -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/AbstractTransactionalQuery.java b/server/src/com/vaadin/data/util/sqlcontainer/query/AbstractTransactionalQuery.java deleted file mode 100644 index e7fd9f4aa4..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/AbstractTransactionalQuery.java +++ /dev/null @@ -1,185 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query; - -import java.io.Serializable; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; - -import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool; - -/** - * Common base class for database query classes that handle connections and - * transactions. - * - * @author Vaadin Ltd - * @since 6.8.9 - */ -public abstract class AbstractTransactionalQuery implements Serializable { - - private JDBCConnectionPool connectionPool; - private transient Connection activeConnection; - - AbstractTransactionalQuery() { - } - - AbstractTransactionalQuery(JDBCConnectionPool connectionPool) { - this.connectionPool = connectionPool; - } - - /** - * Reserves a connection with auto-commit off if no transaction is in - * progress. - * - * @throws IllegalStateException - * if a transaction is already open - * @throws SQLException - * if a connection could not be obtained or configured - */ - public void beginTransaction() throws UnsupportedOperationException, - SQLException { - if (isInTransaction()) { - throw new IllegalStateException("A transaction is already active!"); - } - activeConnection = connectionPool.reserveConnection(); - activeConnection.setAutoCommit(false); - } - - /** - * Commits (if not in auto-commit mode) and releases the active connection. - * - * @throws SQLException - * if not in a transaction managed by this query - */ - public void commit() throws UnsupportedOperationException, SQLException { - if (!isInTransaction()) { - throw new SQLException("No active transaction"); - } - if (!activeConnection.getAutoCommit()) { - activeConnection.commit(); - } - connectionPool.releaseConnection(activeConnection); - activeConnection = null; - } - - /** - * Rolls back and releases the active connection. - * - * @throws SQLException - * if not in a transaction managed by this query - */ - public void rollback() throws UnsupportedOperationException, SQLException { - if (!isInTransaction()) { - throw new SQLException("No active transaction"); - } - activeConnection.rollback(); - connectionPool.releaseConnection(activeConnection); - activeConnection = null; - } - - /** - * Check that a transaction is active. - * - * @throws SQLException - * if no active transaction - */ - protected void ensureTransaction() throws SQLException { - if (!isInTransaction()) { - throw new SQLException("No active transaction!"); - } - } - - /** - * Closes a statement and a resultset, then releases the connection if it is - * not part of an active transaction. A failure in closing one of the - * parameters does not prevent closing the rest. - * - * If the statement is a {@link PreparedStatement}, its parameters are - * cleared prior to closing the statement. - * - * Although JDBC specification does state that closing a statement closes - * its result set and closing a connection closes statements and result - * sets, this method does try to close the result set and statement - * explicitly whenever not null. This can guard against bugs in certain JDBC - * drivers and reduce leaks in case e.g. closing the result set succeeds but - * closing the statement or connection fails. - * - * @param conn - * the connection to release - * @param statement - * the statement to close, may be null to skip closing - * @param rs - * the result set to close, may be null to skip closing - * @throws SQLException - * if closing the result set or the statement fails - */ - protected void releaseConnection(Connection conn, Statement statement, - ResultSet rs) throws SQLException { - try { - try { - if (null != rs) { - rs.close(); - } - } finally { - if (null != statement) { - if (statement instanceof PreparedStatement) { - try { - ((PreparedStatement) statement).clearParameters(); - } catch (Exception e) { - // will be closed below anyway - } - } - statement.close(); - } - } - } finally { - releaseConnection(conn); - } - } - - /** - * Returns the currently active connection, reserves and returns a new - * connection if no active connection. - * - * @return previously active or newly reserved connection - * @throws SQLException - */ - protected Connection getConnection() throws SQLException { - if (activeConnection != null) { - return activeConnection; - } - return connectionPool.reserveConnection(); - } - - protected boolean isInTransaction() { - return activeConnection != null; - } - - /** - * Releases the connection if it is not part of an active transaction. - * - * @param conn - * the connection to release - */ - private void releaseConnection(Connection conn) { - if (conn != activeConnection && conn != null) { - connectionPool.releaseConnection(conn); - } - } -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/FreeformQuery.java b/server/src/com/vaadin/data/util/sqlcontainer/query/FreeformQuery.java deleted file mode 100644 index 6b800cb965..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/FreeformQuery.java +++ /dev/null @@ -1,495 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query; - -import java.io.IOException; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.sqlcontainer.RowItem; -import com.vaadin.data.util.sqlcontainer.SQLContainer; -import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool; -import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper; -import com.vaadin.data.util.sqlcontainer.query.generator.filter.QueryBuilder; - -@SuppressWarnings("serial") -public class FreeformQuery extends AbstractTransactionalQuery implements - QueryDelegate { - - FreeformQueryDelegate delegate = null; - private String queryString; - private List<String> primaryKeyColumns; - - /** - * Prevent no-parameters instantiation of FreeformQuery - */ - @SuppressWarnings("unused") - private FreeformQuery() { - } - - /** - * Creates a new freeform query delegate to be used with the - * {@link SQLContainer}. - * - * @param queryString - * The actual query to perform. - * @param primaryKeyColumns - * The primary key columns. Read-only mode is forced if this - * parameter is null or empty. - * @param connectionPool - * the JDBCConnectionPool to use to open connections to the SQL - * database. - * @deprecated As of 6.7, @see - * {@link FreeformQuery#FreeformQuery(String, JDBCConnectionPool, String...)} - */ - @Deprecated - public FreeformQuery(String queryString, List<String> primaryKeyColumns, - JDBCConnectionPool connectionPool) { - super(connectionPool); - if (primaryKeyColumns == null) { - primaryKeyColumns = new ArrayList<String>(); - } - if (primaryKeyColumns.contains("")) { - throw new IllegalArgumentException( - "The primary key columns contain an empty string!"); - } else if (queryString == null || "".equals(queryString)) { - throw new IllegalArgumentException( - "The query string may not be empty or null!"); - } else if (connectionPool == null) { - throw new IllegalArgumentException( - "The connectionPool may not be null!"); - } - this.queryString = queryString; - this.primaryKeyColumns = Collections - .unmodifiableList(primaryKeyColumns); - } - - /** - * Creates a new freeform query delegate to be used with the - * {@link SQLContainer}. - * - * @param queryString - * The actual query to perform. - * @param connectionPool - * the JDBCConnectionPool to use to open connections to the SQL - * database. - * @param primaryKeyColumns - * The primary key columns. Read-only mode is forced if none are - * provided. (optional) - */ - public FreeformQuery(String queryString, JDBCConnectionPool connectionPool, - String... primaryKeyColumns) { - this(queryString, Arrays.asList(primaryKeyColumns), connectionPool); - } - - /** - * This implementation of getCount() actually fetches all records from the - * database, which might be a performance issue. Override this method with a - * SELECT COUNT(*) ... query if this is too slow for your needs. - * - * {@inheritDoc} - */ - @Override - public int getCount() throws SQLException { - // First try the delegate - int count = countByDelegate(); - if (count < 0) { - // Couldn't use the delegate, use the bad way. - Statement statement = null; - ResultSet rs = null; - Connection conn = getConnection(); - try { - statement = conn.createStatement( - ResultSet.TYPE_SCROLL_INSENSITIVE, - ResultSet.CONCUR_READ_ONLY); - - rs = statement.executeQuery(queryString); - if (rs.last()) { - count = rs.getRow(); - } else { - count = 0; - } - } finally { - releaseConnection(conn, statement, rs); - } - } - return count; - } - - @SuppressWarnings("deprecation") - private int countByDelegate() throws SQLException { - int count = -1; - if (delegate == null) { - return count; - } - /* First try using prepared statement */ - if (delegate instanceof FreeformStatementDelegate) { - try { - StatementHelper sh = ((FreeformStatementDelegate) delegate) - .getCountStatement(); - PreparedStatement pstmt = null; - ResultSet rs = null; - Connection c = getConnection(); - try { - pstmt = c.prepareStatement(sh.getQueryString()); - sh.setParameterValuesToStatement(pstmt); - rs = pstmt.executeQuery(); - if (rs.next()) { - count = rs.getInt(1); - } else { - // The result can be empty when using group by and there - // are no matches (#18043) - count = 0; - } - } finally { - releaseConnection(c, pstmt, rs); - } - return count; - } catch (UnsupportedOperationException e) { - // Count statement generation not supported - } - } - /* Try using regular statement */ - try { - String countQuery = delegate.getCountQuery(); - if (countQuery != null) { - Statement statement = null; - ResultSet rs = null; - Connection conn = getConnection(); - try { - statement = conn.createStatement(); - rs = statement.executeQuery(countQuery); - if (rs.next()) { - count = rs.getInt(1); - } else { - // The result can be empty when using group by and there - // are no matches (#18043) - count = 0; - } - return count; - } finally { - releaseConnection(conn, statement, rs); - } - } - } catch (UnsupportedOperationException e) { - // Count query generation not supported - } - return count; - } - - /** - * Fetches the results for the query. This implementation always fetches the - * entire record set, ignoring the offset and page length parameters. In - * order to support lazy loading of records, you must supply a - * FreeformQueryDelegate that implements the - * FreeformQueryDelegate.getQueryString(int,int) method. - * - * @throws SQLException - * - * @see FreeformQueryDelegate#getQueryString(int, int) - */ - @Override - @SuppressWarnings({ "deprecation", "finally" }) - public ResultSet getResults(int offset, int pagelength) throws SQLException { - ensureTransaction(); - String query = queryString; - if (delegate != null) { - /* First try using prepared statement */ - if (delegate instanceof FreeformStatementDelegate) { - try { - StatementHelper sh = ((FreeformStatementDelegate) delegate) - .getQueryStatement(offset, pagelength); - PreparedStatement pstmt = getConnection().prepareStatement( - sh.getQueryString()); - sh.setParameterValuesToStatement(pstmt); - return pstmt.executeQuery(); - } catch (UnsupportedOperationException e) { - // Statement generation not supported, continue... - } - } - try { - query = delegate.getQueryString(offset, pagelength); - } catch (UnsupportedOperationException e) { - // This is fine, we'll just use the default queryString. - } - } - Statement statement = getConnection().createStatement(); - ResultSet rs; - try { - rs = statement.executeQuery(query); - } catch (SQLException e) { - try { - statement.close(); - } finally { - // throw the original exception even if closing the statement - // fails - throw e; - } - } - return rs; - } - - @Override - @SuppressWarnings("deprecation") - public boolean implementationRespectsPagingLimits() { - if (delegate == null) { - return false; - } - /* First try using prepared statement */ - if (delegate instanceof FreeformStatementDelegate) { - try { - StatementHelper sh = ((FreeformStatementDelegate) delegate) - .getCountStatement(); - if (sh != null && sh.getQueryString() != null - && sh.getQueryString().length() > 0) { - return true; - } - } catch (UnsupportedOperationException e) { - // Statement generation not supported, continue... - } - } - try { - String queryString = delegate.getQueryString(0, 50); - return queryString != null && queryString.length() > 0; - } catch (UnsupportedOperationException e) { - return false; - } - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.sqlcontainer.query.QueryDelegate#setFilters(java - * .util.List) - */ - @Override - public void setFilters(List<Filter> filters) - throws UnsupportedOperationException { - if (delegate != null) { - delegate.setFilters(filters); - } else if (filters != null) { - throw new UnsupportedOperationException( - "FreeFormQueryDelegate not set!"); - } - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.sqlcontainer.query.QueryDelegate#setOrderBy(java - * .util.List) - */ - @Override - public void setOrderBy(List<OrderBy> orderBys) - throws UnsupportedOperationException { - if (delegate != null) { - delegate.setOrderBy(orderBys); - } else if (orderBys != null) { - throw new UnsupportedOperationException( - "FreeFormQueryDelegate not set!"); - } - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.sqlcontainer.query.QueryDelegate#storeRow(com.vaadin - * .data.util.sqlcontainer.RowItem) - */ - @Override - public int storeRow(RowItem row) throws SQLException { - if (!isInTransaction()) { - throw new IllegalStateException("No transaction is active!"); - } else if (primaryKeyColumns.isEmpty()) { - throw new UnsupportedOperationException( - "Cannot store items fetched with a read-only freeform query!"); - } - if (delegate != null) { - return delegate.storeRow(getConnection(), row); - } else { - throw new UnsupportedOperationException( - "FreeFormQueryDelegate not set!"); - } - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.sqlcontainer.query.QueryDelegate#removeRow(com.vaadin - * .data.util.sqlcontainer.RowItem) - */ - @Override - public boolean removeRow(RowItem row) throws SQLException { - if (!isInTransaction()) { - throw new IllegalStateException("No transaction is active!"); - } else if (primaryKeyColumns.isEmpty()) { - throw new UnsupportedOperationException( - "Cannot remove items fetched with a read-only freeform query!"); - } - if (delegate != null) { - return delegate.removeRow(getConnection(), row); - } else { - throw new UnsupportedOperationException( - "FreeFormQueryDelegate not set!"); - } - } - - @Override - public synchronized void beginTransaction() - throws UnsupportedOperationException, SQLException { - super.beginTransaction(); - } - - @Override - public synchronized void commit() throws UnsupportedOperationException, - SQLException { - super.commit(); - } - - @Override - public synchronized void rollback() throws UnsupportedOperationException, - SQLException { - super.rollback(); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.data.util.sqlcontainer.query.QueryDelegate#getPrimaryKeyColumns - * () - */ - @Override - public List<String> getPrimaryKeyColumns() { - return primaryKeyColumns; - } - - public String getQueryString() { - return queryString; - } - - public FreeformQueryDelegate getDelegate() { - return delegate; - } - - public void setDelegate(FreeformQueryDelegate delegate) { - this.delegate = delegate; - } - - /** - * This implementation of the containsRowWithKey method rewrites existing - * WHERE clauses in the query string. The logic is, however, not very - * complex and some times can do the Wrong Thing<sup>TM</sup>. For the - * situations where this logic is not enough, you can implement the - * getContainsRowQueryString method in FreeformQueryDelegate and this will - * be used instead of the logic. - * - * @see FreeformQueryDelegate#getContainsRowQueryString(Object...) - * - */ - @Override - @SuppressWarnings("deprecation") - public boolean containsRowWithKey(Object... keys) throws SQLException { - String query = null; - boolean contains = false; - if (delegate != null) { - if (delegate instanceof FreeformStatementDelegate) { - try { - StatementHelper sh = ((FreeformStatementDelegate) delegate) - .getContainsRowQueryStatement(keys); - - PreparedStatement pstmt = null; - ResultSet rs = null; - Connection c = getConnection(); - try { - pstmt = c.prepareStatement(sh.getQueryString()); - sh.setParameterValuesToStatement(pstmt); - rs = pstmt.executeQuery(); - contains = rs.next(); - return contains; - } finally { - releaseConnection(c, pstmt, rs); - } - } catch (UnsupportedOperationException e) { - // Statement generation not supported, continue... - } - } - try { - query = delegate.getContainsRowQueryString(keys); - } catch (UnsupportedOperationException e) { - query = modifyWhereClause(keys); - } - } else { - query = modifyWhereClause(keys); - } - Statement statement = null; - ResultSet rs = null; - Connection conn = getConnection(); - try { - statement = conn.createStatement(); - rs = statement.executeQuery(query); - contains = rs.next(); - } finally { - releaseConnection(conn, statement, rs); - } - return contains; - } - - private String modifyWhereClause(Object... keys) { - // Build the where rules for the provided keys - StringBuffer where = new StringBuffer(); - for (int ix = 0; ix < primaryKeyColumns.size(); ix++) { - where.append(QueryBuilder.quote(primaryKeyColumns.get(ix))); - if (keys[ix] == null) { - where.append(" IS NULL"); - } else { - where.append(" = '").append(keys[ix]).append("'"); - } - if (ix < primaryKeyColumns.size() - 1) { - where.append(" AND "); - } - } - // Is there already a WHERE clause in the query string? - int index = queryString.toLowerCase().indexOf("where "); - if (index > -1) { - // Rewrite the where clause - return queryString.substring(0, index) + "WHERE " + where + " AND " - + queryString.substring(index + 6); - } - // Append a where clause - return queryString + " WHERE " + where; - } - - private void writeObject(java.io.ObjectOutputStream out) throws IOException { - try { - rollback(); - } catch (SQLException ignored) { - } - out.defaultWriteObject(); - } -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/FreeformQueryDelegate.java b/server/src/com/vaadin/data/util/sqlcontainer/query/FreeformQueryDelegate.java deleted file mode 100644 index b6fa56055d..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/FreeformQueryDelegate.java +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query; - -import java.io.Serializable; -import java.sql.Connection; -import java.sql.SQLException; -import java.util.List; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.sqlcontainer.RowItem; - -public interface FreeformQueryDelegate extends Serializable { - /** - * Should return the SQL query string to be performed. This method is - * responsible for gluing together the select query from the filters and the - * order by conditions if these are supported. - * - * @param offset - * the first record (row) to fetch. - * @param pagelength - * the number of records (rows) to fetch. 0 means all records - * starting from offset. - * @deprecated As of 6.7. Implement {@link FreeformStatementDelegate} - * instead of {@link FreeformQueryDelegate} - */ - @Deprecated - public String getQueryString(int offset, int limit) - throws UnsupportedOperationException; - - /** - * Generates and executes a query to determine the current row count from - * the DB. Row count will be fetched using filters that are currently set to - * the QueryDelegate. - * - * @return row count - * @throws SQLException - * @deprecated As of 6.7. Implement {@link FreeformStatementDelegate} - * instead of {@link FreeformQueryDelegate} - */ - @Deprecated - public String getCountQuery() throws UnsupportedOperationException; - - /** - * Sets the filters to apply when performing the SQL query. These are - * translated into a WHERE clause. Default filtering mode will be used. - * - * @param filters - * The filters to apply. - * @throws UnsupportedOperationException - * if the implementation doesn't support filtering. - */ - public void setFilters(List<Filter> filters) - throws UnsupportedOperationException; - - /** - * Sets the order in which to retrieve rows from the database. The result - * can be ordered by zero or more columns and each column can be in - * ascending or descending order. These are translated into an ORDER BY - * clause in the SQL query. - * - * @param orderBys - * A list of the OrderBy conditions. - * @throws UnsupportedOperationException - * if the implementation doesn't support ordering. - */ - public void setOrderBy(List<OrderBy> orderBys) - throws UnsupportedOperationException; - - /** - * Stores a row in the database. The implementation of this interface - * decides how to identify whether to store a new row or update an existing - * one. - * - * @param conn - * the JDBC connection to use - * @param row - * RowItem to be stored or updated. - * @throws UnsupportedOperationException - * if the implementation is read only. - * @throws SQLException - */ - public int storeRow(Connection conn, RowItem row) - throws UnsupportedOperationException, SQLException; - - /** - * Removes the given RowItem from the database. - * - * @param conn - * the JDBC connection to use - * @param row - * RowItem to be removed - * @return true on success - * @throws UnsupportedOperationException - * @throws SQLException - */ - public boolean removeRow(Connection conn, RowItem row) - throws UnsupportedOperationException, SQLException; - - /** - * Generates an SQL Query string that allows the user of the FreeformQuery - * class to customize the query string used by the - * FreeformQuery.containsRowWithKeys() method. This is useful for cases when - * the logic in the containsRowWithKeys method is not enough to support more - * complex free form queries. - * - * @param keys - * the values of the primary keys - * @throws UnsupportedOperationException - * to use the default logic in FreeformQuery - * @deprecated As of 6.7. Implement {@link FreeformStatementDelegate} - * instead of {@link FreeformQueryDelegate} - */ - @Deprecated - public String getContainsRowQueryString(Object... keys) - throws UnsupportedOperationException; -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/FreeformStatementDelegate.java b/server/src/com/vaadin/data/util/sqlcontainer/query/FreeformStatementDelegate.java deleted file mode 100644 index 9df72f5b98..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/FreeformStatementDelegate.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query; - -import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper; - -/** - * FreeformStatementDelegate is an extension to FreeformQueryDelegate that - * provides definitions for methods that produce StatementHelper objects instead - * of basic query strings. This allows the FreeformQuery query delegate to use - * PreparedStatements instead of regular Statement when accessing the database. - * - * Due to the injection protection and other benefits of prepared statements, it - * is advisable to implement this interface instead of the FreeformQueryDelegate - * whenever possible. - */ -public interface FreeformStatementDelegate extends FreeformQueryDelegate { - /** - * Should return a new instance of StatementHelper that contains the query - * string and parameter values required to create a PreparedStatement. This - * method is responsible for gluing together the select query from the - * filters and the order by conditions if these are supported. - * - * @param offset - * the first record (row) to fetch. - * @param pagelength - * the number of records (rows) to fetch. 0 means all records - * starting from offset. - */ - public StatementHelper getQueryStatement(int offset, int limit) - throws UnsupportedOperationException; - - /** - * Should return a new instance of StatementHelper that contains the query - * string and parameter values required to create a PreparedStatement that - * will fetch the row count from the DB. Row count should be fetched using - * filters that are currently set to the QueryDelegate. - */ - public StatementHelper getCountStatement() - throws UnsupportedOperationException; - - /** - * Should return a new instance of StatementHelper that contains the query - * string and parameter values required to create a PreparedStatement used - * by the FreeformQuery.containsRowWithKeys() method. This is useful for - * cases when the default logic in said method is not enough to support more - * complex free form queries. - * - * @param keys - * the values of the primary keys - * @throws UnsupportedOperationException - * to use the default logic in FreeformQuery - */ - public StatementHelper getContainsRowQueryStatement(Object... keys) - throws UnsupportedOperationException; -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/OrderBy.java b/server/src/com/vaadin/data/util/sqlcontainer/query/OrderBy.java deleted file mode 100644 index 2bafa5dc0c..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/OrderBy.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query; - -import java.io.Serializable; - -/** - * OrderBy represents a sorting rule to be applied to a query made by the - * SQLContainer's QueryDelegate. - * - * The sorting rule is simple and contains only the affected column's name and - * the direction of the sort. - */ -public class OrderBy implements Serializable { - private String column; - private boolean isAscending; - - /** - * Prevent instantiation without required parameters. - */ - @SuppressWarnings("unused") - private OrderBy() { - } - - public OrderBy(String column, boolean isAscending) { - setColumn(column); - setAscending(isAscending); - } - - public void setColumn(String column) { - this.column = column; - } - - public String getColumn() { - return column; - } - - public void setAscending(boolean isAscending) { - this.isAscending = isAscending; - } - - public boolean isAscending() { - return isAscending; - } -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/QueryDelegate.java b/server/src/com/vaadin/data/util/sqlcontainer/query/QueryDelegate.java deleted file mode 100644 index 413dd55ab9..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/QueryDelegate.java +++ /dev/null @@ -1,239 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query; - -import java.io.Serializable; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.List; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.sqlcontainer.RowId; -import com.vaadin.data.util.sqlcontainer.RowItem; - -public interface QueryDelegate extends Serializable { - /** - * Generates and executes a query to determine the current row count from - * the DB. Row count will be fetched using filters that are currently set to - * the QueryDelegate. - * - * @return row count - * @throws SQLException - */ - public int getCount() throws SQLException; - - /** - * Executes a paged SQL query and returns the ResultSet. The query is - * defined through implementations of this QueryDelegate interface. - * - * @param offset - * the first item of the page to load - * @param pagelength - * the length of the page to load - * @return a ResultSet containing the rows of the page - * @throws SQLException - * if the database access fails. - */ - public ResultSet getResults(int offset, int pagelength) throws SQLException; - - /** - * Allows the SQLContainer implementation to check whether the QueryDelegate - * implementation implements paging in the getResults method. - * - * @see QueryDelegate#getResults(int, int) - * - * @return true if the delegate implements paging - */ - public boolean implementationRespectsPagingLimits(); - - /** - * Sets the filters to apply when performing the SQL query. These are - * translated into a WHERE clause. Default filtering mode will be used. - * - * @param filters - * The filters to apply. - * @throws UnsupportedOperationException - * if the implementation doesn't support filtering. - */ - public void setFilters(List<Filter> filters) - throws UnsupportedOperationException; - - /** - * Sets the order in which to retrieve rows from the database. The result - * can be ordered by zero or more columns and each column can be in - * ascending or descending order. These are translated into an ORDER BY - * clause in the SQL query. - * - * @param orderBys - * A list of the OrderBy conditions. - * @throws UnsupportedOperationException - * if the implementation doesn't support ordering. - */ - public void setOrderBy(List<OrderBy> orderBys) - throws UnsupportedOperationException; - - /** - * Stores a row in the database. The implementation of this interface - * decides how to identify whether to store a new row or update an existing - * one. - * - * @param columnToValueMap - * A map containing the values for all columns to be stored or - * updated. - * @return the number of affected rows in the database table - * @throws UnsupportedOperationException - * if the implementation is read only. - */ - public int storeRow(RowItem row) throws UnsupportedOperationException, - SQLException; - - /** - * Removes the given RowItem from the database. - * - * @param row - * RowItem to be removed - * @return true on success - * @throws UnsupportedOperationException - * @throws SQLException - */ - public boolean removeRow(RowItem row) throws UnsupportedOperationException, - SQLException; - - /** - * Starts a new database transaction. Used when storing multiple changes. - * - * Note that if a transaction is already open, it will be rolled back when a - * new transaction is started. - * - * @throws SQLException - * if the database access fails. - */ - public void beginTransaction() throws SQLException; - - /** - * Commits a transaction. If a transaction is not open nothing should - * happen. - * - * @throws SQLException - * if the database access fails. - */ - public void commit() throws SQLException; - - /** - * Rolls a transaction back. If a transaction is not open nothing should - * happen. - * - * @throws SQLException - * if the database access fails. - */ - public void rollback() throws SQLException; - - /** - * Returns a list of primary key column names. The list is either fetched - * from the database (TableQuery) or given as an argument depending on - * implementation. - * - * @return - */ - public List<String> getPrimaryKeyColumns(); - - /** - * Performs a query to find out whether the SQL table contains a row with - * the given set of primary keys. - * - * @param keys - * the primary keys - * @return true if the SQL table contains a row with the provided keys - * @throws SQLException - */ - public boolean containsRowWithKey(Object... keys) throws SQLException; - - /************************/ - /** ROWID CHANGE EVENT **/ - /************************/ - - /** - * An <code>Event</code> object specifying the old and new RowId of an added - * item after the addition has been successfully committed. - */ - public interface RowIdChangeEvent extends Serializable { - /** - * Gets the old (temporary) RowId of the added row that raised this - * event. - * - * @return old RowId - */ - public RowId getOldRowId(); - - /** - * Gets the new, possibly database assigned RowId of the added row that - * raised this event. - * - * @return new RowId - */ - public RowId getNewRowId(); - } - - /** RowId change listener interface. */ - public interface RowIdChangeListener extends Serializable { - /** - * Lets the listener know that a RowId has been changed. - * - * @param event - */ - public void rowIdChange(QueryDelegate.RowIdChangeEvent event); - } - - /** - * The interface for adding and removing <code>RowIdChangeEvent</code> - * listeners. By implementing this interface a class explicitly announces - * that it will generate a <code>RowIdChangeEvent</code> when it performs a - * database commit that may change the RowId. - */ - public interface RowIdChangeNotifier extends Serializable { - /** - * Adds a RowIdChangeListener for the object. - * - * @param listener - * listener to be added - */ - public void addRowIdChangeListener( - QueryDelegate.RowIdChangeListener listener); - - /** - * @deprecated As of 7.0, replaced by - * {@link #addRowIdChangeListener(RowIdChangeListener)} - **/ - @Deprecated - public void addListener(QueryDelegate.RowIdChangeListener listener); - - /** - * Removes the specified RowIdChangeListener from the object. - * - * @param listener - * listener to be removed - */ - public void removeRowIdChangeListener( - QueryDelegate.RowIdChangeListener listener); - - /** - * @deprecated As of 7.0, replaced by - * {@link #removeRowIdChangeListener(RowIdChangeListener)} - **/ - @Deprecated - public void removeListener(QueryDelegate.RowIdChangeListener listener); - } -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/TableQuery.java b/server/src/com/vaadin/data/util/sqlcontainer/query/TableQuery.java deleted file mode 100644 index 9a41766a31..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/TableQuery.java +++ /dev/null @@ -1,868 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query; - -import java.io.IOException; -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.EventObject; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.filter.Compare.Equal; -import com.vaadin.data.util.sqlcontainer.ColumnProperty; -import com.vaadin.data.util.sqlcontainer.OptimisticLockException; -import com.vaadin.data.util.sqlcontainer.RowId; -import com.vaadin.data.util.sqlcontainer.RowItem; -import com.vaadin.data.util.sqlcontainer.SQLUtil; -import com.vaadin.data.util.sqlcontainer.TemporaryRowId; -import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool; -import com.vaadin.data.util.sqlcontainer.query.generator.DefaultSQLGenerator; -import com.vaadin.data.util.sqlcontainer.query.generator.MSSQLGenerator; -import com.vaadin.data.util.sqlcontainer.query.generator.SQLGenerator; -import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper; - -@SuppressWarnings("serial") -public class TableQuery extends AbstractTransactionalQuery implements - QueryDelegate, QueryDelegate.RowIdChangeNotifier { - - /** - * Table name (without catalog or schema information). - */ - private String tableName; - private String catalogName; - private String schemaName; - /** - * Cached concatenated version of the table name. - */ - private String fullTableName; - /** - * Primary key column name(s) in the table. - */ - private List<String> primaryKeyColumns; - /** - * Version column name in the table. - */ - private String versionColumn; - - /** Currently set Filters and OrderBys */ - private List<Filter> filters; - private List<OrderBy> orderBys; - - /** SQLGenerator instance to use for generating queries */ - private SQLGenerator sqlGenerator; - - /** Row ID change listeners */ - private LinkedList<RowIdChangeListener> rowIdChangeListeners; - /** Row ID change events, stored until commit() is called */ - private final List<RowIdChangeEvent> bufferedEvents = new ArrayList<RowIdChangeEvent>(); - - /** Set to true to output generated SQL Queries to System.out */ - private final boolean debug = false; - - /** - * Creates a new TableQuery using the given connection pool, SQL generator - * and table name to fetch the data from. All parameters must be non-null. - * - * The table name must be a simple name with no catalog or schema - * information. If those are needed, use - * {@link #TableQuery(String, String, String, JDBCConnectionPool, SQLGenerator)} - * . - * - * @param tableName - * Name of the database table to connect to - * @param connectionPool - * Connection pool for accessing the database - * @param sqlGenerator - * SQL query generator implementation - */ - public TableQuery(String tableName, JDBCConnectionPool connectionPool, - SQLGenerator sqlGenerator) { - this(null, null, tableName, connectionPool, sqlGenerator); - } - - /** - * Creates a new TableQuery using the given connection pool, SQL generator - * and table name to fetch the data from. Catalog and schema names can be - * null, all other parameters must be non-null. - * - * @param catalogName - * Name of the database catalog (can be null) - * @param schemaName - * Name of the database schema (can be null) - * @param tableName - * Name of the database table to connect to - * @param connectionPool - * Connection pool for accessing the database - * @param sqlGenerator - * SQL query generator implementation - * @since 7.1 - */ - public TableQuery(String catalogName, String schemaName, String tableName, - JDBCConnectionPool connectionPool, SQLGenerator sqlGenerator) { - this(catalogName, schemaName, tableName, connectionPool, sqlGenerator, - true); - } - - /** - * Creates a new TableQuery using the given connection pool and table name - * to fetch the data from. All parameters must be non-null. The default SQL - * generator will be used for queries. - * - * The table name must be a simple name with no catalog or schema - * information. If those are needed, use - * {@link #TableQuery(String, String, String, JDBCConnectionPool, SQLGenerator)} - * . - * - * @param tableName - * Name of the database table to connect to - * @param connectionPool - * Connection pool for accessing the database - */ - public TableQuery(String tableName, JDBCConnectionPool connectionPool) { - this(tableName, connectionPool, new DefaultSQLGenerator()); - } - - /** - * Creates a new TableQuery using the given connection pool, SQL generator - * and table name to fetch the data from. Catalog and schema names can be - * null, all other parameters must be non-null. - * - * @param catalogName - * Name of the database catalog (can be null) - * @param schemaName - * Name of the database schema (can be null) - * @param tableName - * Name of the database table to connect to - * @param connectionPool - * Connection pool for accessing the database - * @param sqlGenerator - * SQL query generator implementation - * @param escapeNames - * true to escape special characters in catalog, schema and table - * names, false to use the names as-is - * @since 7.1 - */ - protected TableQuery(String catalogName, String schemaName, - String tableName, JDBCConnectionPool connectionPool, - SQLGenerator sqlGenerator, boolean escapeNames) { - super(connectionPool); - if (tableName == null || tableName.trim().length() < 1 - || connectionPool == null || sqlGenerator == null) { - throw new IllegalArgumentException( - "Table name, connection pool and SQL generator parameters must be non-null and non-empty."); - } - if (escapeNames) { - this.catalogName = SQLUtil.escapeSQL(catalogName); - this.schemaName = SQLUtil.escapeSQL(schemaName); - this.tableName = SQLUtil.escapeSQL(tableName); - } else { - this.catalogName = catalogName; - this.schemaName = schemaName; - this.tableName = tableName; - } - this.sqlGenerator = sqlGenerator; - fetchMetaData(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.addon.sqlcontainer.query.QueryDelegate#getCount() - */ - @Override - public int getCount() throws SQLException { - getLogger().log(Level.FINE, "Fetching count..."); - StatementHelper sh = sqlGenerator.generateSelectQuery( - getFullTableName(), filters, null, 0, 0, "COUNT(*)"); - boolean shouldCloseTransaction = false; - if (!isInTransaction()) { - shouldCloseTransaction = true; - beginTransaction(); - } - ResultSet r = null; - int count = -1; - try { - r = executeQuery(sh); - r.next(); - count = r.getInt(1); - } finally { - try { - if (r != null) { - // Do not release connection, it is done in commit() - releaseConnection(null, r.getStatement(), r); - } - } finally { - if (shouldCloseTransaction) { - commit(); - } - } - } - return count; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.addon.sqlcontainer.query.QueryDelegate#getResults(int, - * int) - */ - @Override - public ResultSet getResults(int offset, int pagelength) throws SQLException { - StatementHelper sh; - /* - * If no ordering is explicitly set, results will be ordered by the - * first primary key column. - */ - if (orderBys == null || orderBys.isEmpty()) { - List<OrderBy> ob = new ArrayList<OrderBy>(); - for (int i = 0; i < primaryKeyColumns.size(); i++) { - ob.add(new OrderBy(primaryKeyColumns.get(i), true)); - } - sh = sqlGenerator.generateSelectQuery(getFullTableName(), filters, - ob, offset, pagelength, null); - } else { - sh = sqlGenerator.generateSelectQuery(getFullTableName(), filters, - orderBys, offset, pagelength, null); - } - return executeQuery(sh); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.addon.sqlcontainer.query.QueryDelegate# - * implementationRespectsPagingLimits() - */ - @Override - public boolean implementationRespectsPagingLimits() { - return true; - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.addon.sqlcontainer.query.QueryDelegate#storeRow(com.vaadin - * .addon.sqlcontainer.RowItem) - */ - @Override - public int storeRow(RowItem row) throws UnsupportedOperationException, - SQLException { - if (row == null) { - throw new IllegalArgumentException("Row argument must be non-null."); - } - StatementHelper sh; - int result = 0; - if (row.getId() instanceof TemporaryRowId) { - setVersionColumnFlagInProperty(row); - sh = sqlGenerator.generateInsertQuery(getFullTableName(), row); - result = executeUpdateReturnKeys(sh, row); - } else { - setVersionColumnFlagInProperty(row); - sh = sqlGenerator.generateUpdateQuery(getFullTableName(), row); - result = executeUpdate(sh); - } - if (versionColumn != null && result == 0) { - throw new OptimisticLockException( - "Someone else changed the row that was being updated.", - row.getId()); - } - return result; - } - - private void setVersionColumnFlagInProperty(RowItem row) { - ColumnProperty versionProperty = (ColumnProperty) row - .getItemProperty(versionColumn); - if (versionProperty != null) { - versionProperty.setVersionColumn(true); - } - } - - /** - * Inserts the given row in the database table immediately. Begins and - * commits the transaction needed. This method was added specifically to - * solve the problem of returning the final RowId immediately on the - * SQLContainer.addItem() call when auto commit mode is enabled in the - * SQLContainer. - * - * @param row - * RowItem to add to the database - * @return Final RowId of the added row - * @throws SQLException - */ - public RowId storeRowImmediately(RowItem row) throws SQLException { - beginTransaction(); - /* Set version column, if one is provided */ - setVersionColumnFlagInProperty(row); - /* Generate query */ - StatementHelper sh = sqlGenerator.generateInsertQuery( - getFullTableName(), row); - Connection connection = null; - PreparedStatement pstmt = null; - ResultSet generatedKeys = null; - connection = getConnection(); - try { - pstmt = connection.prepareStatement(sh.getQueryString(), - primaryKeyColumns.toArray(new String[0])); - sh.setParameterValuesToStatement(pstmt); - getLogger().log(Level.FINE, "DB -> {0}", sh.getQueryString()); - int result = pstmt.executeUpdate(); - RowId newId = null; - if (result > 0) { - /* - * If affected rows exist, we'll get the new RowId, commit the - * transaction and return the new RowId. - */ - generatedKeys = pstmt.getGeneratedKeys(); - newId = getNewRowId(row, generatedKeys); - } - // transaction has to be closed in any case - commit(); - return newId; - } finally { - releaseConnection(connection, pstmt, generatedKeys); - } - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.addon.sqlcontainer.query.QueryDelegate#setFilters(java.util - * .List) - */ - @Override - public void setFilters(List<Filter> filters) - throws UnsupportedOperationException { - if (filters == null) { - this.filters = null; - return; - } - this.filters = Collections.unmodifiableList(filters); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.addon.sqlcontainer.query.QueryDelegate#setOrderBy(java.util - * .List) - */ - @Override - public void setOrderBy(List<OrderBy> orderBys) - throws UnsupportedOperationException { - if (orderBys == null) { - this.orderBys = null; - return; - } - this.orderBys = Collections.unmodifiableList(orderBys); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.addon.sqlcontainer.query.QueryDelegate#beginTransaction() - */ - @Override - public void beginTransaction() throws UnsupportedOperationException, - SQLException { - getLogger().log(Level.FINE, "DB -> begin transaction"); - super.beginTransaction(); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.addon.sqlcontainer.query.QueryDelegate#commit() - */ - @Override - public void commit() throws UnsupportedOperationException, SQLException { - getLogger().log(Level.FINE, "DB -> commit"); - super.commit(); - - /* Handle firing row ID change events */ - RowIdChangeEvent[] unFiredEvents = bufferedEvents - .toArray(new RowIdChangeEvent[] {}); - bufferedEvents.clear(); - if (rowIdChangeListeners != null && !rowIdChangeListeners.isEmpty()) { - for (RowIdChangeListener r : rowIdChangeListeners) { - for (RowIdChangeEvent e : unFiredEvents) { - r.rowIdChange(e); - } - } - } - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.addon.sqlcontainer.query.QueryDelegate#rollback() - */ - @Override - public void rollback() throws UnsupportedOperationException, SQLException { - getLogger().log(Level.FINE, "DB -> rollback"); - super.rollback(); - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.addon.sqlcontainer.query.QueryDelegate#getPrimaryKeyColumns() - */ - @Override - public List<String> getPrimaryKeyColumns() { - return Collections.unmodifiableList(primaryKeyColumns); - } - - public String getVersionColumn() { - return versionColumn; - } - - public void setVersionColumn(String column) { - versionColumn = column; - } - - /** - * Returns the table name for the query without catalog and schema - * information. - * - * @return table name, not null - */ - public String getTableName() { - return tableName; - } - - /** - * Returns the catalog name for the query. - * - * @return catalog name, can be null - * @since 7.1 - */ - public String getCatalogName() { - return catalogName; - } - - /** - * Returns the catalog name for the query. - * - * @return catalog name, can be null - * @since 7.1 - */ - public String getSchemaName() { - return schemaName; - } - - /** - * Returns the complete table name obtained by concatenation of the catalog - * and schema names (if any) and the table name. - * - * This method can be overridden if customization is needed. - * - * @return table name in the form it should be used in query and update - * statements - * @since 7.1 - */ - protected String getFullTableName() { - if (fullTableName == null) { - StringBuilder sb = new StringBuilder(); - if (catalogName != null) { - sb.append(catalogName).append("."); - } - if (schemaName != null) { - sb.append(schemaName).append("."); - } - sb.append(tableName); - fullTableName = sb.toString(); - } - return fullTableName; - } - - public SQLGenerator getSqlGenerator() { - return sqlGenerator; - } - - /** - * Executes the given query string using either the active connection if a - * transaction is already open, or a new connection from this query's - * connection pool. - * - * @param sh - * an instance of StatementHelper, containing the query string - * and parameter values. - * @return ResultSet of the query - * @throws SQLException - */ - private ResultSet executeQuery(StatementHelper sh) throws SQLException { - ensureTransaction(); - Connection connection = getConnection(); - PreparedStatement pstmt = null; - try { - pstmt = connection.prepareStatement(sh.getQueryString()); - sh.setParameterValuesToStatement(pstmt); - getLogger().log(Level.FINE, "DB -> {0}", sh.getQueryString()); - return pstmt.executeQuery(); - } catch (SQLException e) { - releaseConnection(null, pstmt, null); - throw e; - } - } - - /** - * Executes the given update query string using either the active connection - * if a transaction is already open, or a new connection from this query's - * connection pool. - * - * @param sh - * an instance of StatementHelper, containing the query string - * and parameter values. - * @return Number of affected rows - * @throws SQLException - */ - private int executeUpdate(StatementHelper sh) throws SQLException { - PreparedStatement pstmt = null; - Connection connection = null; - try { - connection = getConnection(); - pstmt = connection.prepareStatement(sh.getQueryString()); - sh.setParameterValuesToStatement(pstmt); - getLogger().log(Level.FINE, "DB -> {0}", sh.getQueryString()); - int retval = pstmt.executeUpdate(); - return retval; - } finally { - releaseConnection(connection, pstmt, null); - } - } - - /** - * Executes the given update query string using either the active connection - * if a transaction is already open, or a new connection from this query's - * connection pool. - * - * Additionally adds a new RowIdChangeEvent to the event buffer. - * - * @param sh - * an instance of StatementHelper, containing the query string - * and parameter values. - * @param row - * the row item to update - * @return Number of affected rows - * @throws SQLException - */ - private int executeUpdateReturnKeys(StatementHelper sh, RowItem row) - throws SQLException { - PreparedStatement pstmt = null; - ResultSet genKeys = null; - Connection connection = null; - try { - connection = getConnection(); - pstmt = connection.prepareStatement(sh.getQueryString(), - primaryKeyColumns.toArray(new String[0])); - sh.setParameterValuesToStatement(pstmt); - getLogger().log(Level.FINE, "DB -> {0}", sh.getQueryString()); - int result = pstmt.executeUpdate(); - genKeys = pstmt.getGeneratedKeys(); - RowId newId = getNewRowId(row, genKeys); - bufferedEvents.add(new RowIdChangeEvent(row.getId(), newId)); - return result; - } finally { - releaseConnection(connection, pstmt, genKeys); - } - } - - /** - * Fetches name(s) of primary key column(s) from DB metadata. - * - * Also tries to get the escape string to be used in search strings. - */ - private void fetchMetaData() { - Connection connection = null; - ResultSet rs = null; - ResultSet tables = null; - try { - connection = getConnection(); - DatabaseMetaData dbmd = connection.getMetaData(); - if (dbmd != null) { - tables = dbmd.getTables(catalogName, schemaName, tableName, - null); - if (!tables.next()) { - String catalog = (catalogName != null) ? catalogName - .toUpperCase() : null; - String schema = (schemaName != null) ? schemaName - .toUpperCase() : null; - tables = dbmd.getTables(catalog, schema, - tableName.toUpperCase(), null); - if (!tables.next()) { - throw new IllegalArgumentException( - "Table with the name \"" - + getFullTableName() - + "\" was not found. Check your database contents."); - } else { - catalogName = catalog; - schemaName = schema; - tableName = tableName.toUpperCase(); - } - } - tables.close(); - rs = dbmd.getPrimaryKeys(catalogName, schemaName, tableName); - List<String> names = new ArrayList<String>(); - while (rs.next()) { - names.add(rs.getString("COLUMN_NAME")); - } - rs.close(); - if (!names.isEmpty()) { - primaryKeyColumns = names; - } - if (primaryKeyColumns == null || primaryKeyColumns.isEmpty()) { - throw new IllegalArgumentException( - "Primary key constraints have not been defined for the table \"" - + getFullTableName() - + "\". Use FreeFormQuery to access this table."); - } - for (String colName : primaryKeyColumns) { - if (colName.equalsIgnoreCase("rownum")) { - if (getSqlGenerator() instanceof MSSQLGenerator - || getSqlGenerator() instanceof MSSQLGenerator) { - throw new IllegalArgumentException( - "When using Oracle or MSSQL, a primary key column" - + " named \'rownum\' is not allowed!"); - } - } - } - } - } catch (SQLException e) { - throw new RuntimeException(e); - } finally { - try { - releaseConnection(connection, null, rs); - } catch (SQLException ignore) { - } finally { - try { - if (tables != null) { - tables.close(); - } - } catch (SQLException ignore) { - } - } - } - } - - private RowId getNewRowId(RowItem row, ResultSet genKeys) { - try { - /* Fetch primary key values and generate a map out of them. */ - Map<String, Object> values = new HashMap<String, Object>(); - ResultSetMetaData rsmd = genKeys.getMetaData(); - int colCount = rsmd.getColumnCount(); - if (genKeys.next()) { - for (int i = 1; i <= colCount; i++) { - values.put(rsmd.getColumnName(i), genKeys.getObject(i)); - } - } - /* Generate new RowId */ - List<Object> newRowId = new ArrayList<Object>(); - if (values.size() == 1) { - if (primaryKeyColumns.size() == 1) { - newRowId.add(values.get(values.keySet().iterator().next())); - } else { - for (String s : primaryKeyColumns) { - if (!((ColumnProperty) row.getItemProperty(s)) - .isReadOnlyChangeAllowed()) { - newRowId.add(values.get(values.keySet().iterator() - .next())); - } else { - newRowId.add(values.get(s)); - } - } - } - } else { - for (String s : primaryKeyColumns) { - newRowId.add(values.get(s)); - } - } - return new RowId(newRowId.toArray()); - } catch (Exception e) { - getLogger() - .log(Level.FINE, - "Failed to fetch key values on insert: {0}", - e.getMessage()); - return null; - } - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.addon.sqlcontainer.query.QueryDelegate#removeRow(com.vaadin - * .addon.sqlcontainer.RowItem) - */ - @Override - public boolean removeRow(RowItem row) throws UnsupportedOperationException, - SQLException { - if (getLogger().isLoggable(Level.FINE)) { - getLogger().log(Level.FINE, "Removing row with id: {0}", - row.getId().getId()[0]); - } - if (executeUpdate(sqlGenerator.generateDeleteQuery(getFullTableName(), - primaryKeyColumns, versionColumn, row)) == 1) { - return true; - } - if (versionColumn != null) { - throw new OptimisticLockException( - "Someone else changed the row that was being deleted.", - row.getId()); - } - return false; - } - - /* - * (non-Javadoc) - * - * @see - * com.vaadin.addon.sqlcontainer.query.QueryDelegate#containsRowWithKey( - * java.lang.Object[]) - */ - @Override - public boolean containsRowWithKey(Object... keys) throws SQLException { - ArrayList<Filter> filtersAndKeys = new ArrayList<Filter>(); - if (filters != null) { - filtersAndKeys.addAll(filters); - } - int ix = 0; - for (String colName : primaryKeyColumns) { - filtersAndKeys.add(new Equal(colName, keys[ix])); - ix++; - } - StatementHelper sh = sqlGenerator.generateSelectQuery( - getFullTableName(), filtersAndKeys, orderBys, 0, 0, "*"); - - boolean shouldCloseTransaction = false; - if (!isInTransaction()) { - shouldCloseTransaction = true; - beginTransaction(); - } - ResultSet rs = null; - try { - rs = executeQuery(sh); - boolean contains = rs.next(); - return contains; - } finally { - try { - if (rs != null) { - // Do not release connection, it is done in commit() - releaseConnection(null, rs.getStatement(), rs); - } - } finally { - if (shouldCloseTransaction) { - commit(); - } - } - } - } - - /** - * Custom writeObject to call rollback() if object is serialized. - */ - private void writeObject(java.io.ObjectOutputStream out) throws IOException { - try { - rollback(); - } catch (SQLException ignored) { - } - out.defaultWriteObject(); - } - - /** - * Simple RowIdChangeEvent implementation. - */ - public static class RowIdChangeEvent extends EventObject implements - QueryDelegate.RowIdChangeEvent { - private final RowId oldId; - private final RowId newId; - - private RowIdChangeEvent(RowId oldId, RowId newId) { - super(oldId); - this.oldId = oldId; - this.newId = newId; - } - - @Override - public RowId getNewRowId() { - return newId; - } - - @Override - public RowId getOldRowId() { - return oldId; - } - } - - /** - * Adds RowIdChangeListener to this query - */ - @Override - public void addRowIdChangeListener(RowIdChangeListener listener) { - if (rowIdChangeListeners == null) { - rowIdChangeListeners = new LinkedList<QueryDelegate.RowIdChangeListener>(); - } - rowIdChangeListeners.add(listener); - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #addRowIdChangeListener(com.vaadin.data.util.sqlcontainer.query.QueryDelegate.RowIdChangeListener)} - **/ - @Override - @Deprecated - public void addListener(RowIdChangeListener listener) { - addRowIdChangeListener(listener); - } - - /** - * Removes the given RowIdChangeListener from this query - */ - @Override - public void removeRowIdChangeListener(RowIdChangeListener listener) { - if (rowIdChangeListeners != null) { - rowIdChangeListeners.remove(listener); - } - } - - /** - * @deprecated As of 7.0, replaced by - * {@link #removeRowIdChangeListener(com.vaadin.data.util.sqlcontainer.query.QueryDelegate.RowIdChangeListener)} - **/ - @Override - @Deprecated - public void removeListener(RowIdChangeListener listener) { - removeRowIdChangeListener(listener); - } - - private static final Logger getLogger() { - return Logger.getLogger(TableQuery.class.getName()); - } -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/DefaultSQLGenerator.java b/server/src/com/vaadin/data/util/sqlcontainer/query/generator/DefaultSQLGenerator.java deleted file mode 100644 index 2fc7ebd544..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/DefaultSQLGenerator.java +++ /dev/null @@ -1,391 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query.generator; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.sqlcontainer.ColumnProperty; -import com.vaadin.data.util.sqlcontainer.RowItem; -import com.vaadin.data.util.sqlcontainer.SQLUtil; -import com.vaadin.data.util.sqlcontainer.TemporaryRowId; -import com.vaadin.data.util.sqlcontainer.query.OrderBy; -import com.vaadin.data.util.sqlcontainer.query.generator.filter.QueryBuilder; -import com.vaadin.data.util.sqlcontainer.query.generator.filter.StringDecorator; - -/** - * Generates generic SQL that is supported by HSQLDB, MySQL and PostgreSQL. - * - * @author Jonatan Kronqvist / Vaadin Ltd - */ -@SuppressWarnings("serial") -public class DefaultSQLGenerator implements SQLGenerator { - - private Class<? extends StatementHelper> statementHelperClass = null; - - public DefaultSQLGenerator() { - - } - - /** - * Create a new DefaultSqlGenerator instance that uses the given - * implementation of {@link StatementHelper} - * - * @param statementHelper - */ - public DefaultSQLGenerator( - Class<? extends StatementHelper> statementHelperClazz) { - this(); - statementHelperClass = statementHelperClazz; - } - - /** - * Construct a DefaultSQLGenerator with the specified identifiers for start - * and end of quoted strings. The identifiers may be different depending on - * the database engine and it's settings. - * - * @param quoteStart - * the identifier (character) denoting the start of a quoted - * string - * @param quoteEnd - * the identifier (character) denoting the end of a quoted string - */ - public DefaultSQLGenerator(String quoteStart, String quoteEnd) { - QueryBuilder.setStringDecorator(new StringDecorator(quoteStart, - quoteEnd)); - } - - /** - * Same as {@link #DefaultSQLGenerator(String, String)} but with support for - * custom {@link StatementHelper} implementation. - * - * @param quoteStart - * @param quoteEnd - * @param statementHelperClazz - */ - public DefaultSQLGenerator(String quoteStart, String quoteEnd, - Class<? extends StatementHelper> statementHelperClazz) { - this(quoteStart, quoteEnd); - statementHelperClass = statementHelperClazz; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.addon.sqlcontainer.query.generator.SQLGenerator# - * generateSelectQuery(java.lang.String, java.util.List, java.util.List, - * int, int, java.lang.String) - */ - @Override - public StatementHelper generateSelectQuery(String tableName, - List<Filter> filters, List<OrderBy> orderBys, int offset, - int pagelength, String toSelect) { - if (tableName == null || tableName.trim().equals("")) { - throw new IllegalArgumentException("Table name must be given."); - } - toSelect = toSelect == null ? "*" : toSelect; - StatementHelper sh = getStatementHelper(); - StringBuffer query = new StringBuffer(); - query.append("SELECT " + toSelect + " FROM ").append( - SQLUtil.escapeSQL(tableName)); - if (filters != null) { - query.append(QueryBuilder.getWhereStringForFilters(filters, sh)); - } - if (orderBys != null) { - for (OrderBy o : orderBys) { - generateOrderBy(query, o, orderBys.indexOf(o) == 0); - } - } - if (pagelength != 0) { - generateLimits(query, offset, pagelength); - } - sh.setQueryString(query.toString()); - return sh; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.addon.sqlcontainer.query.generator.SQLGenerator# - * generateUpdateQuery(java.lang.String, - * com.vaadin.addon.sqlcontainer.RowItem) - */ - @Override - public StatementHelper generateUpdateQuery(String tableName, RowItem item) { - if (tableName == null || tableName.trim().equals("")) { - throw new IllegalArgumentException("Table name must be given."); - } - if (item == null) { - throw new IllegalArgumentException("Updated item must be given."); - } - StatementHelper sh = getStatementHelper(); - StringBuffer query = new StringBuffer(); - query.append("UPDATE ").append(tableName).append(" SET"); - - /* Generate column<->value and rowidentifiers map */ - Map<String, Object> columnToValueMap = generateColumnToValueMap(item); - Map<String, Object> rowIdentifiers = generateRowIdentifiers(item); - /* Generate columns and values to update */ - boolean first = true; - for (String column : columnToValueMap.keySet()) { - if (first) { - query.append(" " + QueryBuilder.quote(column) + " = ?"); - } else { - query.append(", " + QueryBuilder.quote(column) + " = ?"); - } - sh.addParameterValue(columnToValueMap.get(column), item - .getItemProperty(column).getType()); - first = false; - } - /* Generate identifiers for the row to be updated */ - first = true; - for (String column : rowIdentifiers.keySet()) { - if (first) { - query.append(" WHERE " + QueryBuilder.quote(column) + " = ?"); - } else { - query.append(" AND " + QueryBuilder.quote(column) + " = ?"); - } - sh.addParameterValue(rowIdentifiers.get(column), item - .getItemProperty(column).getType()); - first = false; - } - sh.setQueryString(query.toString()); - return sh; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.addon.sqlcontainer.query.generator.SQLGenerator# - * generateInsertQuery(java.lang.String, - * com.vaadin.addon.sqlcontainer.RowItem) - */ - @Override - public StatementHelper generateInsertQuery(String tableName, RowItem item) { - if (tableName == null || tableName.trim().equals("")) { - throw new IllegalArgumentException("Table name must be given."); - } - if (item == null) { - throw new IllegalArgumentException("New item must be given."); - } - if (!(item.getId() instanceof TemporaryRowId)) { - throw new IllegalArgumentException( - "Cannot generate an insert query for item already in database."); - } - StatementHelper sh = getStatementHelper(); - StringBuffer query = new StringBuffer(); - query.append("INSERT INTO ").append(tableName).append(" ("); - - /* Generate column<->value map */ - Map<String, Object> columnToValueMap = generateColumnToValueMap(item); - /* Generate column names for insert query */ - boolean first = true; - for (String column : columnToValueMap.keySet()) { - if (!first) { - query.append(", "); - } - query.append(QueryBuilder.quote(column)); - first = false; - } - - /* Generate values for insert query */ - query.append(") VALUES ("); - first = true; - for (String column : columnToValueMap.keySet()) { - if (!first) { - query.append(", "); - } - query.append("?"); - sh.addParameterValue(columnToValueMap.get(column), item - .getItemProperty(column).getType()); - first = false; - } - query.append(")"); - sh.setQueryString(query.toString()); - return sh; - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.addon.sqlcontainer.query.generator.SQLGenerator# - * generateDeleteQuery(java.lang.String, - * com.vaadin.addon.sqlcontainer.RowItem) - */ - @Override - public StatementHelper generateDeleteQuery(String tableName, - List<String> primaryKeyColumns, String versionColumn, RowItem item) { - if (tableName == null || tableName.trim().equals("")) { - throw new IllegalArgumentException("Table name must be given."); - } - if (item == null) { - throw new IllegalArgumentException( - "Item to be deleted must be given."); - } - if (primaryKeyColumns == null || primaryKeyColumns.isEmpty()) { - throw new IllegalArgumentException( - "Valid keyColumnNames must be provided."); - } - StatementHelper sh = getStatementHelper(); - StringBuffer query = new StringBuffer(); - query.append("DELETE FROM ").append(tableName).append(" WHERE "); - int count = 1; - for (String keyColName : primaryKeyColumns) { - if ((this instanceof MSSQLGenerator || this instanceof OracleGenerator) - && keyColName.equalsIgnoreCase("rownum")) { - count++; - continue; - } - if (count > 1) { - query.append(" AND "); - } - if (item.getItemProperty(keyColName).getValue() != null) { - query.append(QueryBuilder.quote(keyColName) + " = ?"); - sh.addParameterValue(item.getItemProperty(keyColName) - .getValue(), item.getItemProperty(keyColName).getType()); - } - count++; - } - if (versionColumn != null) { - if (!item.getItemPropertyIds().contains(versionColumn)) { - throw new IllegalArgumentException(String.format( - "Table '%s' does not contain version column '%s'.", - tableName, versionColumn)); - } - - query.append(String.format(" AND %s = ?", - QueryBuilder.quote(versionColumn))); - sh.addParameterValue( - item.getItemProperty(versionColumn).getValue(), item - .getItemProperty(versionColumn).getType()); - } - - sh.setQueryString(query.toString()); - return sh; - } - - /** - * Generates sorting rules as an ORDER BY -clause - * - * @param sb - * StringBuffer to which the clause is appended. - * @param o - * OrderBy object to be added into the sb. - * @param firstOrderBy - * If true, this is the first OrderBy. - * @return - */ - protected StringBuffer generateOrderBy(StringBuffer sb, OrderBy o, - boolean firstOrderBy) { - if (firstOrderBy) { - sb.append(" ORDER BY "); - } else { - sb.append(", "); - } - sb.append(QueryBuilder.quote(o.getColumn())); - if (o.isAscending()) { - sb.append(" ASC"); - } else { - sb.append(" DESC"); - } - return sb; - } - - /** - * Generates the LIMIT and OFFSET clause. - * - * @param sb - * StringBuffer to which the clause is appended. - * @param offset - * Value for offset. - * @param pagelength - * Value for pagelength. - * @return StringBuffer with LIMIT and OFFSET clause added. - */ - protected StringBuffer generateLimits(StringBuffer sb, int offset, - int pagelength) { - sb.append(" LIMIT ").append(pagelength).append(" OFFSET ") - .append(offset); - return sb; - } - - protected Map<String, Object> generateColumnToValueMap(RowItem item) { - Map<String, Object> columnToValueMap = new HashMap<String, Object>(); - for (Object id : item.getItemPropertyIds()) { - ColumnProperty cp = (ColumnProperty) item.getItemProperty(id); - /* Prevent "rownum" usage as a column name if MSSQL or ORACLE */ - if ((this instanceof MSSQLGenerator || this instanceof OracleGenerator) - && cp.getPropertyId().equalsIgnoreCase("rownum")) { - continue; - } - if (cp.isPersistent()) { - columnToValueMap.put(cp.getPropertyId(), cp.getValue()); - } - } - return columnToValueMap; - } - - protected Map<String, Object> generateRowIdentifiers(RowItem item) { - Map<String, Object> rowIdentifiers = new HashMap<String, Object>(); - for (Object id : item.getItemPropertyIds()) { - ColumnProperty cp = (ColumnProperty) item.getItemProperty(id); - /* Prevent "rownum" usage as a column name if MSSQL or ORACLE */ - if ((this instanceof MSSQLGenerator || this instanceof OracleGenerator) - && cp.getPropertyId().equalsIgnoreCase("rownum")) { - continue; - } - - if (cp.isRowIdentifier()) { - Object value; - if (cp.isPrimaryKey()) { - // If the value of a primary key has changed, its old value - // should be used to identify the row (#9145) - value = cp.getOldValue(); - } else { - value = cp.getValue(); - } - rowIdentifiers.put(cp.getPropertyId(), value); - } - } - return rowIdentifiers; - } - - /** - * Returns the statement helper for the generator. Override this to handle - * platform specific data types. - * - * @see http://dev.vaadin.com/ticket/9148 - * @return a new instance of the statement helper - */ - protected StatementHelper getStatementHelper() { - if (statementHelperClass == null) { - return new StatementHelper(); - } - - try { - return statementHelperClass.newInstance(); - } catch (InstantiationException e) { - throw new RuntimeException( - "Unable to instantiate custom StatementHelper", e); - } catch (IllegalAccessException e) { - throw new RuntimeException( - "Unable to instantiate custom StatementHelper", e); - } - } - -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/MSSQLGenerator.java b/server/src/com/vaadin/data/util/sqlcontainer/query/generator/MSSQLGenerator.java deleted file mode 100644 index 5a1f2003cd..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/MSSQLGenerator.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query.generator; - -import java.util.List; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.sqlcontainer.query.OrderBy; -import com.vaadin.data.util.sqlcontainer.query.generator.filter.QueryBuilder; - -@SuppressWarnings("serial") -public class MSSQLGenerator extends DefaultSQLGenerator { - - public MSSQLGenerator() { - - } - - /** - * Construct a MSSQLGenerator with the specified identifiers for start and - * end of quoted strings. The identifiers may be different depending on the - * database engine and it's settings. - * - * @param quoteStart - * the identifier (character) denoting the start of a quoted - * string - * @param quoteEnd - * the identifier (character) denoting the end of a quoted string - */ - public MSSQLGenerator(String quoteStart, String quoteEnd) { - super(quoteStart, quoteEnd); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.addon.sqlcontainer.query.generator.DefaultSQLGenerator# - * generateSelectQuery(java.lang.String, java.util.List, - * com.vaadin.addon.sqlcontainer.query.FilteringMode, java.util.List, int, - * int, java.lang.String) - */ - @Override - public StatementHelper generateSelectQuery(String tableName, - List<Filter> filters, List<OrderBy> orderBys, int offset, - int pagelength, String toSelect) { - if (tableName == null || tableName.trim().equals("")) { - throw new IllegalArgumentException("Table name must be given."); - } - /* Adjust offset and page length parameters to match "row numbers" */ - offset = pagelength > 1 ? ++offset : offset; - pagelength = pagelength > 1 ? --pagelength : pagelength; - toSelect = toSelect == null ? "*" : toSelect; - StatementHelper sh = getStatementHelper(); - StringBuffer query = new StringBuffer(); - - /* Row count request is handled here */ - if ("COUNT(*)".equalsIgnoreCase(toSelect)) { - query.append(String.format( - "SELECT COUNT(*) AS %s FROM (SELECT * FROM %s", - QueryBuilder.quote("rowcount"), tableName)); - if (filters != null && !filters.isEmpty()) { - query.append(QueryBuilder.getWhereStringForFilters(filters, sh)); - } - query.append(") AS t"); - sh.setQueryString(query.toString()); - return sh; - } - - /* SELECT without row number constraints */ - if (offset == 0 && pagelength == 0) { - query.append("SELECT ").append(toSelect).append(" FROM ") - .append(tableName); - if (filters != null) { - query.append(QueryBuilder.getWhereStringForFilters(filters, sh)); - } - if (orderBys != null) { - for (OrderBy o : orderBys) { - generateOrderBy(query, o, orderBys.indexOf(o) == 0); - } - } - sh.setQueryString(query.toString()); - return sh; - } - - /* Remaining SELECT cases are handled here */ - query.append("SELECT * FROM (SELECT row_number() OVER ("); - if (orderBys != null) { - for (OrderBy o : orderBys) { - generateOrderBy(query, o, orderBys.indexOf(o) == 0); - } - } - query.append(") AS rownum, " + toSelect + " FROM ").append(tableName); - if (filters != null) { - query.append(QueryBuilder.getWhereStringForFilters(filters, sh)); - } - query.append(") AS a WHERE a.rownum BETWEEN ").append(offset) - .append(" AND ").append(Integer.toString(offset + pagelength)); - sh.setQueryString(query.toString()); - return sh; - } -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/OracleGenerator.java b/server/src/com/vaadin/data/util/sqlcontainer/query/generator/OracleGenerator.java deleted file mode 100644 index 86508d37c4..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/OracleGenerator.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query.generator; - -import java.util.List; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.sqlcontainer.query.OrderBy; -import com.vaadin.data.util.sqlcontainer.query.generator.filter.QueryBuilder; - -@SuppressWarnings("serial") -public class OracleGenerator extends DefaultSQLGenerator { - - public OracleGenerator() { - - } - - public OracleGenerator(Class<? extends StatementHelper> statementHelperClazz) { - super(statementHelperClazz); - } - - /** - * Construct an OracleSQLGenerator with the specified identifiers for start - * and end of quoted strings. The identifiers may be different depending on - * the database engine and it's settings. - * - * @param quoteStart - * the identifier (character) denoting the start of a quoted - * string - * @param quoteEnd - * the identifier (character) denoting the end of a quoted string - */ - public OracleGenerator(String quoteStart, String quoteEnd) { - super(quoteStart, quoteEnd); - } - - public OracleGenerator(String quoteStart, String quoteEnd, - Class<? extends StatementHelper> statementHelperClazz) { - super(quoteStart, quoteEnd, statementHelperClazz); - } - - /* - * (non-Javadoc) - * - * @see com.vaadin.addon.sqlcontainer.query.generator.DefaultSQLGenerator# - * generateSelectQuery(java.lang.String, java.util.List, - * com.vaadin.addon.sqlcontainer.query.FilteringMode, java.util.List, int, - * int, java.lang.String) - */ - @Override - public StatementHelper generateSelectQuery(String tableName, - List<Filter> filters, List<OrderBy> orderBys, int offset, - int pagelength, String toSelect) { - if (tableName == null || tableName.trim().equals("")) { - throw new IllegalArgumentException("Table name must be given."); - } - /* Adjust offset and page length parameters to match "row numbers" */ - offset = pagelength > 1 ? ++offset : offset; - pagelength = pagelength > 1 ? --pagelength : pagelength; - toSelect = toSelect == null ? "*" : toSelect; - StatementHelper sh = getStatementHelper(); - StringBuffer query = new StringBuffer(); - - /* Row count request is handled here */ - if ("COUNT(*)".equalsIgnoreCase(toSelect)) { - query.append(String.format( - "SELECT COUNT(*) AS %s FROM (SELECT * FROM %s", - QueryBuilder.quote("rowcount"), tableName)); - if (filters != null && !filters.isEmpty()) { - query.append(QueryBuilder.getWhereStringForFilters(filters, sh)); - } - query.append(")"); - sh.setQueryString(query.toString()); - return sh; - } - - /* SELECT without row number constraints */ - if (offset == 0 && pagelength == 0) { - query.append("SELECT ").append(toSelect).append(" FROM ") - .append(tableName); - if (filters != null) { - query.append(QueryBuilder.getWhereStringForFilters(filters, sh)); - } - if (orderBys != null) { - for (OrderBy o : orderBys) { - generateOrderBy(query, o, orderBys.indexOf(o) == 0); - } - } - sh.setQueryString(query.toString()); - return sh; - } - - /* Remaining SELECT cases are handled here */ - query.append(String - .format("SELECT * FROM (SELECT x.*, ROWNUM AS %s FROM (SELECT %s FROM %s", - QueryBuilder.quote("rownum"), toSelect, tableName)); - if (filters != null) { - query.append(QueryBuilder.getWhereStringForFilters(filters, sh)); - } - if (orderBys != null) { - for (OrderBy o : orderBys) { - generateOrderBy(query, o, orderBys.indexOf(o) == 0); - } - } - query.append(String.format(") x) WHERE %s BETWEEN %d AND %d", - QueryBuilder.quote("rownum"), offset, offset + pagelength)); - sh.setQueryString(query.toString()); - return sh; - } - -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/SQLGenerator.java b/server/src/com/vaadin/data/util/sqlcontainer/query/generator/SQLGenerator.java deleted file mode 100644 index 53ff924a36..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/SQLGenerator.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query.generator; - -import java.io.Serializable; -import java.util.List; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.sqlcontainer.RowItem; -import com.vaadin.data.util.sqlcontainer.query.OrderBy; - -/** - * The SQLGenerator interface is meant to be implemented for each different SQL - * syntax that is to be supported. By default there are implementations for - * HSQLDB, MySQL, PostgreSQL, MSSQL and Oracle syntaxes. - * - * @author Jonatan Kronqvist / Vaadin Ltd - */ -public interface SQLGenerator extends Serializable { - /** - * Generates a SELECT query with the provided parameters. Uses default - * filtering mode (INCLUSIVE). - * - * @param tableName - * Name of the table queried - * @param filters - * The filters, converted into a WHERE clause - * @param orderBys - * The the ordering conditions, converted into an ORDER BY clause - * @param offset - * The offset of the first row to be included - * @param pagelength - * The number of rows to be returned when the query executes - * @param toSelect - * String containing what to select, e.g. "*", "COUNT(*)" - * @return StatementHelper instance containing the query string for a - * PreparedStatement and the values required for the parameters - */ - public StatementHelper generateSelectQuery(String tableName, - List<Filter> filters, List<OrderBy> orderBys, int offset, - int pagelength, String toSelect); - - /** - * Generates an UPDATE query with the provided parameters. - * - * @param tableName - * Name of the table queried - * @param item - * RowItem containing the updated values update. - * @return StatementHelper instance containing the query string for a - * PreparedStatement and the values required for the parameters - */ - public StatementHelper generateUpdateQuery(String tableName, RowItem item); - - /** - * Generates an INSERT query for inserting a new row with the provided - * values. - * - * @param tableName - * Name of the table queried - * @param item - * New RowItem to be inserted into the database. - * @return StatementHelper instance containing the query string for a - * PreparedStatement and the values required for the parameters - */ - public StatementHelper generateInsertQuery(String tableName, RowItem item); - - /** - * Generates a DELETE query for deleting data related to the given RowItem - * from the database. - * - * @param tableName - * Name of the table queried - * @param primaryKeyColumns - * the names of the columns holding the primary key. Usually just - * one column, but might be several. - * @param versionColumn - * the column containing the version number of the row, null if - * versioning (optimistic locking) not enabled. - * @param item - * Item to be deleted from the database - * @return StatementHelper instance containing the query string for a - * PreparedStatement and the values required for the parameters - */ - public StatementHelper generateDeleteQuery(String tableName, - List<String> primaryKeyColumns, String versionColumn, RowItem item); -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/StatementHelper.java b/server/src/com/vaadin/data/util/sqlcontainer/query/generator/StatementHelper.java deleted file mode 100644 index 5a9fcb17c0..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/StatementHelper.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query.generator; - -import java.io.Serializable; -import java.math.BigDecimal; -import java.sql.Date; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.sql.Time; -import java.sql.Timestamp; -import java.sql.Types; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * StatementHelper is a simple helper class that assists TableQuery and the - * query generators in filling a PreparedStatement. The actual statement is - * generated by the query generator methods, but the resulting statement and all - * the parameter values are stored in an instance of StatementHelper. - * - * This class will also fill the values with correct setters into the - * PreparedStatement on request. - */ -public class StatementHelper implements Serializable { - - private String queryString; - - private List<Object> parameters = new ArrayList<Object>(); - private Map<Integer, Class<?>> dataTypes = new HashMap<Integer, Class<?>>(); - - public StatementHelper() { - } - - public void setQueryString(String queryString) { - this.queryString = queryString; - } - - public String getQueryString() { - return queryString; - } - - public void addParameterValue(Object parameter) { - if (parameter != null) { - parameters.add(parameter); - dataTypes.put(parameters.size() - 1, parameter.getClass()); - } else { - throw new IllegalArgumentException( - "You cannot add null parameters using addParamaters(Object). " - + "Use addParameters(Object,Class) instead"); - } - } - - public void addParameterValue(Object parameter, Class<?> type) { - parameters.add(parameter); - dataTypes.put(parameters.size() - 1, type); - } - - public void setParameterValuesToStatement(PreparedStatement pstmt) - throws SQLException { - for (int i = 0; i < parameters.size(); i++) { - if (parameters.get(i) == null) { - handleNullValue(i, pstmt); - } else { - pstmt.setObject(i + 1, parameters.get(i)); - } - } - - /* - * The following list contains the data types supported by - * PreparedStatement but not supported by SQLContainer: - * - * [The list is provided as PreparedStatement method signatures] - * - * setNCharacterStream(int parameterIndex, Reader value) - * - * setNClob(int parameterIndex, NClob value) - * - * setNString(int parameterIndex, String value) - * - * setRef(int parameterIndex, Ref x) - * - * setRowId(int parameterIndex, RowId x) - * - * setSQLXML(int parameterIndex, SQLXML xmlObject) - * - * setBytes(int parameterIndex, byte[] x) - * - * setCharacterStream(int parameterIndex, Reader reader) - * - * setClob(int parameterIndex, Clob x) - * - * setURL(int parameterIndex, URL x) - * - * setArray(int parameterIndex, Array x) - * - * setAsciiStream(int parameterIndex, InputStream x) - * - * setBinaryStream(int parameterIndex, InputStream x) - * - * setBlob(int parameterIndex, Blob x) - */ - } - - private void handleNullValue(int i, PreparedStatement pstmt) - throws SQLException { - Class<?> dataType = dataTypes.get(i); - int index = i + 1; - if (BigDecimal.class.equals(dataType)) { - pstmt.setBigDecimal(index, null); - } else if (Boolean.class.equals(dataType)) { - pstmt.setNull(index, Types.BOOLEAN); - } else if (Byte.class.equals(dataType)) { - pstmt.setNull(index, Types.SMALLINT); - } else if (Date.class.equals(dataType)) { - pstmt.setDate(index, null); - } else if (Double.class.equals(dataType)) { - pstmt.setNull(index, Types.DOUBLE); - } else if (Float.class.equals(dataType)) { - pstmt.setNull(index, Types.FLOAT); - } else if (Integer.class.equals(dataType)) { - pstmt.setNull(index, Types.INTEGER); - } else if (Long.class.equals(dataType)) { - pstmt.setNull(index, Types.BIGINT); - } else if (Short.class.equals(dataType)) { - pstmt.setNull(index, Types.SMALLINT); - } else if (String.class.equals(dataType)) { - pstmt.setString(index, null); - } else if (Time.class.equals(dataType)) { - pstmt.setTime(index, null); - } else if (Timestamp.class.equals(dataType)) { - pstmt.setTimestamp(index, null); - } else if (byte[].class.equals(dataType)) { - pstmt.setBytes(index, null); - } else { - - if (handleUnrecognizedTypeNullValue(i, pstmt, dataTypes)) { - return; - } - - throw new SQLException("Data type for parameter " + i - + " not supported by SQLContainer: " + dataType.getName()); - } - } - - /** - * Handle unrecognized null values. Override this to handle null values for - * platform specific data types that are not handled by the default - * implementation of the {@link StatementHelper}. - * - * @param i - * @param pstmt - * @param dataTypes2 - * - * @return true if handled, false otherwise - * - * @see {@link http://dev.vaadin.com/ticket/9148} - */ - protected boolean handleUnrecognizedTypeNullValue(int i, - PreparedStatement pstmt, Map<Integer, Class<?>> dataTypes) - throws SQLException { - return false; - } -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/AndTranslator.java b/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/AndTranslator.java deleted file mode 100644 index a3d9b90705..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/AndTranslator.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query.generator.filter; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.filter.And; -import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper; - -public class AndTranslator implements FilterTranslator { - - @Override - public boolean translatesFilter(Filter filter) { - return filter instanceof And; - } - - @Override - public String getWhereStringForFilter(Filter filter, StatementHelper sh) { - return QueryBuilder.group(QueryBuilder.getJoinedFilterString( - ((And) filter).getFilters(), "AND", sh)); - } - -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/BetweenTranslator.java b/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/BetweenTranslator.java deleted file mode 100644 index 13d3553742..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/BetweenTranslator.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query.generator.filter; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.filter.Between; -import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper; - -public class BetweenTranslator implements FilterTranslator { - - @Override - public boolean translatesFilter(Filter filter) { - return filter instanceof Between; - } - - @Override - public String getWhereStringForFilter(Filter filter, StatementHelper sh) { - Between between = (Between) filter; - sh.addParameterValue(between.getStartValue()); - sh.addParameterValue(between.getEndValue()); - return QueryBuilder.quote(between.getPropertyId()) + " BETWEEN ? AND ?"; - } - -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/CompareTranslator.java b/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/CompareTranslator.java deleted file mode 100644 index d8d5cc61fb..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/CompareTranslator.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query.generator.filter; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.filter.Compare; -import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper; - -public class CompareTranslator implements FilterTranslator { - - @Override - public boolean translatesFilter(Filter filter) { - return filter instanceof Compare; - } - - @Override - public String getWhereStringForFilter(Filter filter, StatementHelper sh) { - Compare compare = (Compare) filter; - sh.addParameterValue(compare.getValue()); - String prop = QueryBuilder.quote(compare.getPropertyId()); - switch (compare.getOperation()) { - case EQUAL: - return prop + " = ?"; - case GREATER: - return prop + " > ?"; - case GREATER_OR_EQUAL: - return prop + " >= ?"; - case LESS: - return prop + " < ?"; - case LESS_OR_EQUAL: - return prop + " <= ?"; - default: - return ""; - } - } - -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/FilterTranslator.java b/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/FilterTranslator.java deleted file mode 100644 index 0ece263ef4..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/FilterTranslator.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query.generator.filter; - -import java.io.Serializable; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper; - -public interface FilterTranslator extends Serializable { - public boolean translatesFilter(Filter filter); - - public String getWhereStringForFilter(Filter filter, StatementHelper sh); - -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/IsNullTranslator.java b/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/IsNullTranslator.java deleted file mode 100644 index 764a04eece..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/IsNullTranslator.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query.generator.filter; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.filter.IsNull; -import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper; - -public class IsNullTranslator implements FilterTranslator { - - @Override - public boolean translatesFilter(Filter filter) { - return filter instanceof IsNull; - } - - @Override - public String getWhereStringForFilter(Filter filter, StatementHelper sh) { - IsNull in = (IsNull) filter; - return QueryBuilder.quote(in.getPropertyId()) + " IS NULL"; - } -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/LikeTranslator.java b/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/LikeTranslator.java deleted file mode 100644 index 07e544d40b..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/LikeTranslator.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query.generator.filter; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.filter.Like; -import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper; - -public class LikeTranslator implements FilterTranslator { - - @Override - public boolean translatesFilter(Filter filter) { - return filter instanceof Like; - } - - @Override - public String getWhereStringForFilter(Filter filter, StatementHelper sh) { - Like like = (Like) filter; - if (like.isCaseSensitive()) { - sh.addParameterValue(like.getValue()); - return QueryBuilder.quote(like.getPropertyId()) + " LIKE ?"; - } else { - sh.addParameterValue(like.getValue().toUpperCase()); - return "UPPER(" + QueryBuilder.quote(like.getPropertyId()) - + ") LIKE ?"; - } - } - -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/NotTranslator.java b/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/NotTranslator.java deleted file mode 100644 index 1bbc2a8e7e..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/NotTranslator.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query.generator.filter; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.filter.IsNull; -import com.vaadin.data.util.filter.Not; -import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper; - -public class NotTranslator implements FilterTranslator { - - @Override - public boolean translatesFilter(Filter filter) { - return filter instanceof Not; - } - - @Override - public String getWhereStringForFilter(Filter filter, StatementHelper sh) { - Not not = (Not) filter; - if (not.getFilter() instanceof IsNull) { - IsNull in = (IsNull) not.getFilter(); - return QueryBuilder.quote(in.getPropertyId()) + " IS NOT NULL"; - } - return "NOT " - + QueryBuilder.getWhereStringForFilter(not.getFilter(), sh); - } - -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/OrTranslator.java b/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/OrTranslator.java deleted file mode 100644 index 00b4d5421b..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/OrTranslator.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query.generator.filter; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.filter.Or; -import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper; - -public class OrTranslator implements FilterTranslator { - - @Override - public boolean translatesFilter(Filter filter) { - return filter instanceof Or; - } - - @Override - public String getWhereStringForFilter(Filter filter, StatementHelper sh) { - return QueryBuilder.group(QueryBuilder.getJoinedFilterString( - ((Or) filter).getFilters(), "OR", sh)); - } - -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/QueryBuilder.java b/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/QueryBuilder.java deleted file mode 100644 index b277551209..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/QueryBuilder.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query.generator.filter; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper; - -public class QueryBuilder implements Serializable { - - private static ArrayList<FilterTranslator> filterTranslators = new ArrayList<FilterTranslator>(); - private static StringDecorator stringDecorator = new StringDecorator("\"", - "\""); - - static { - /* Register all default filter translators */ - addFilterTranslator(new AndTranslator()); - addFilterTranslator(new OrTranslator()); - addFilterTranslator(new LikeTranslator()); - addFilterTranslator(new BetweenTranslator()); - addFilterTranslator(new CompareTranslator()); - addFilterTranslator(new NotTranslator()); - addFilterTranslator(new IsNullTranslator()); - addFilterTranslator(new SimpleStringTranslator()); - } - - public synchronized static void addFilterTranslator( - FilterTranslator translator) { - filterTranslators.add(translator); - } - - /** - * Allows specification of a custom ColumnQuoter instance that handles - * quoting of column names for the current DB dialect. - * - * @param decorator - * the ColumnQuoter instance to use. - */ - public static void setStringDecorator(StringDecorator decorator) { - stringDecorator = decorator; - } - - public static String quote(Object str) { - return stringDecorator.quote(str); - } - - public static String group(String str) { - return stringDecorator.group(str); - } - - /** - * Constructs and returns a string representing the filter that can be used - * in a WHERE clause. - * - * @param filter - * the filter to translate - * @param sh - * the statement helper to update with the value(s) of the filter - * @return a string representing the filter. - */ - public synchronized static String getWhereStringForFilter(Filter filter, - StatementHelper sh) { - for (FilterTranslator ft : filterTranslators) { - if (ft.translatesFilter(filter)) { - return ft.getWhereStringForFilter(filter, sh); - } - } - return ""; - } - - public static String getJoinedFilterString(Collection<Filter> filters, - String joinString, StatementHelper sh) { - StringBuilder result = new StringBuilder(); - for (Filter f : filters) { - result.append(getWhereStringForFilter(f, sh)); - result.append(" ").append(joinString).append(" "); - } - // Remove the last instance of joinString - result.delete(result.length() - joinString.length() - 2, - result.length()); - return result.toString(); - } - - public static String getWhereStringForFilters(List<Filter> filters, - StatementHelper sh) { - if (filters == null || filters.isEmpty()) { - return ""; - } - StringBuilder where = new StringBuilder(" WHERE "); - where.append(getJoinedFilterString(filters, "AND", sh)); - return where.toString(); - } -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/SimpleStringTranslator.java b/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/SimpleStringTranslator.java deleted file mode 100644 index 9bfda5fb34..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/SimpleStringTranslator.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query.generator.filter; - -import com.vaadin.data.Container.Filter; -import com.vaadin.data.util.filter.Like; -import com.vaadin.data.util.filter.SimpleStringFilter; -import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper; - -public class SimpleStringTranslator implements FilterTranslator { - - @Override - public boolean translatesFilter(Filter filter) { - return filter instanceof SimpleStringFilter; - } - - @Override - public String getWhereStringForFilter(Filter filter, StatementHelper sh) { - SimpleStringFilter ssf = (SimpleStringFilter) filter; - // Create a Like filter based on the SimpleStringFilter and execute the - // LikeTranslator - String likeStr = ssf.isOnlyMatchPrefix() ? ssf.getFilterString() + "%" - : "%" + ssf.getFilterString() + "%"; - Like like = new Like(ssf.getPropertyId().toString(), likeStr); - like.setCaseSensitive(!ssf.isIgnoreCase()); - return new LikeTranslator().getWhereStringForFilter(like, sh); - } - -} diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/StringDecorator.java b/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/StringDecorator.java deleted file mode 100644 index 0132260a0c..0000000000 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/generator/filter/StringDecorator.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2000-2014 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package com.vaadin.data.util.sqlcontainer.query.generator.filter; - -import java.io.Serializable; - -/** - * The StringDecorator knows how to produce a quoted string using the specified - * quote start and quote end characters. It also handles grouping of a string - * (surrounding it in parenthesis). - * - * Extend this class if you need to support special characters for grouping - * (parenthesis). - * - * @author Vaadin Ltd - */ -public class StringDecorator implements Serializable { - - private final String quoteStart; - private final String quoteEnd; - - /** - * Constructs a StringDecorator that uses the quoteStart and quoteEnd - * characters to create quoted strings. - * - * @param quoteStart - * the character denoting the start of a quote. - * @param quoteEnd - * the character denoting the end of a quote. - */ - public StringDecorator(String quoteStart, String quoteEnd) { - this.quoteStart = quoteStart; - this.quoteEnd = quoteEnd; - } - - /** - * Surround a string with quote characters. - * - * @param str - * the string to quote - * @return the quoted string - */ - public String quote(Object str) { - return quoteStart + str + quoteEnd; - } - - /** - * Groups a string by surrounding it in parenthesis - * - * @param str - * the string to group - * @return the grouped string - */ - public String group(String str) { - return "(" + str + ")"; - } -} |