Author: fdesbois Date: 2010-06-04 00:13:22 +0200 (Fri, 04 Jun 2010) New Revision: 1995 Url: http://nuiton.org/repositories/revision/topia/1995 Log: Missing new classes from previous commit Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/EntityFilter.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFilter.java Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/EntityFilter.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/EntityFilter.java (rev 0) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/EntityFilter.java 2010-06-03 22:13:22 UTC (rev 1995) @@ -0,0 +1,146 @@ +package org.nuiton.topia.framework; + +import java.beans.PropertyChangeListener; + +/** + * Created: 3 juin 2010 + * + * @author fdesbois <fdesbois@codelutin.com> + * @version $Id$ + */ +public interface EntityFilter { + + String PROPERTY_START_INDEX = "startIndex"; + + String PROPERTY_END_INDEX = "endIndex"; + + String PROPERTY_ORDER_BY = "orderBy"; + + String PROPERTY_REFERENCE_ID = "referenceId"; + + String PROPERTY_REFERENCE_PROPERTY = "referenceProperty"; + + /** + * Get the value of startIndex + * + * @return the value of startIndex + */ + Integer getStartIndex(); + + /** + * Set the value of startIndex + * + * @param startIndex new value of startIndex + */ + void setStartIndex(Integer startIndex); + + /** + * Get the value of orderBy + * + * @return the value of orderBy + */ + String getOrderBy(); + + /** + * Set the value of orderBy + * + * @param orderBy new value of orderBy + */ + void setOrderBy(String orderBy); + + /** + * Get the value of endIndex + * + * @return the value of endIndex + */ + Integer getEndIndex(); + + /** + * Set the value of endIndex + * + * @param endIndex new value of endIndex + */ + void setEndIndex(Integer endIndex); + + /** + * Get the value of referenceId + * + * @return the value of referenceId + */ + String getReferenceId(); + + /** + * Set the value of referenceId + * + * @param referenceId + */ + void setReferenceId(String referenceId); + + /** + * Set the value of referenceId from {@code entity} + * + * @param entity + */ + void setReference(Object entity) throws IllegalArgumentException; + + /** + * Used to check if the filter contains a reference. + * + * @return true if the filter contains a reference + */ + boolean hasReference(); + + /** + * Use to check if {@code reference} class is supported by the current + * filter reference. The reference can be not {@code mandatory}. Exceptions + * are thrown if the check failed. If you prefer to have a boolean instead + * of exceptions, you can use {@link #isClassReference(Class)}. + * + * @param reference Class reference to check + * @param mandatory If the existence of the reference is mandatory + * @throws IllegalArgumentException for errors on check + * @see #hasReference() + * @see #isClassReference(Class) + */ + void checkReference(Class<?> reference, boolean mandatory) + throws IllegalArgumentException; + + /** + * Test if the {@code entityClass} is corresponding to the current reference + * in the filter. Will return false if no reference is set in the filter. + * + * @param entityClass Class reference to test + * @return true if the classReference is corresponding, false otherwise + * @see #checkReference(Class, boolean) + */ + boolean isClassReference(Class<?> entityClass); + + /** + * Get the value of referenceProperty + * + * @return the value of referenceProperty + */ + String getReferenceProperty(); + + /** + * Set the value of referenceProperty + * + * @param referenceProperty + */ + void setReferenceProperty(String referenceProperty); + + /** + * Add PropertyChangeListener. + * + * @param listener + */ + void addPropertyChangeListener(PropertyChangeListener listener); + + /** + * Remove PropertyChangeListener. + * + * @param listener + */ + void removePropertyChangeListener(PropertyChangeListener listener); + +} \ No newline at end of file Property changes on: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/EntityFilter.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFilter.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFilter.java (rev 0) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFilter.java 2010-06-03 22:13:22 UTC (rev 1995) @@ -0,0 +1,208 @@ +package org.nuiton.topia.framework; + +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.topia.TopiaNotFoundException; +import org.nuiton.topia.persistence.TopiaEntity; +import org.nuiton.topia.persistence.TopiaId; + +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; +import java.util.ArrayList; +import java.util.List; + +/** + * Filter + * <p/> + * Created: 23 avr. 2010 + * + * @author fdesbois + * @since 2.0 + */ +public class TopiaFilter implements EntityFilter { + + private static final Log log = LogFactory.getLog(TopiaFilter.class); + + protected Integer startIndex; + + protected Integer endIndex; + + protected String orderBy; + + protected String referenceId; + + protected String referenceProperty; + + private PropertyChangeSupport propertyChangeSupport = + new PropertyChangeSupport(this); + + @Override + public Integer getStartIndex() { + return startIndex; + } + + @Override + public void setStartIndex(Integer startIndex) { + Integer oldStartIndex = this.startIndex; + this.startIndex = startIndex; + propertyChangeSupport.firePropertyChange(PROPERTY_START_INDEX, + oldStartIndex, startIndex); + } + + @Override + public String getOrderBy() { + return orderBy; + } + + @Override + public void setOrderBy(String orderBy) { + String oldOrderBy = this.orderBy; + this.orderBy = orderBy; + propertyChangeSupport.firePropertyChange(PROPERTY_ORDER_BY, + oldOrderBy, orderBy); + } + + @Override + public Integer getEndIndex() { + return endIndex; + } + + @Override + public void setEndIndex(Integer endIndex) { + Integer oldEndIndex = this.endIndex; + this.endIndex = endIndex; + propertyChangeSupport.firePropertyChange(PROPERTY_END_INDEX, + oldEndIndex, endIndex); + } + + @Override + public String getReferenceId() { + return referenceId; + } + + @Override + public void setReferenceId(String referenceId) { + String oldReferenceId = this.referenceId; + this.referenceId = referenceId; + propertyChangeSupport.firePropertyChange(PROPERTY_REFERENCE_ID, + oldReferenceId, referenceId); + } + + @Override + public void setReference(Object entity) throws IllegalArgumentException { + + if (! (entity instanceof TopiaEntity)) { + throw new IllegalArgumentException("Can't set reference of type '" + + entity.getClass().getName() + "' need a TopiaEntity"); + } + + setReferenceId(((TopiaEntity)entity).getTopiaId()); + } + + @Override + public boolean hasReference() { + return StringUtils.isNotEmpty(referenceId); + } + + @Override + public String getReferenceProperty() { + return referenceProperty; + } + + @Override + public void setReferenceProperty(String referenceProperty) { + String oldReferenceProperty = this.referenceProperty; + this.referenceProperty = referenceProperty; + propertyChangeSupport.firePropertyChange(PROPERTY_REFERENCE_PROPERTY, + oldReferenceProperty, referenceProperty); + } + + @Override + public void addPropertyChangeListener(PropertyChangeListener listener) { + propertyChangeSupport.addPropertyChangeListener(listener); + } + + @Override + public void removePropertyChangeListener(PropertyChangeListener listener) { + propertyChangeSupport.removePropertyChangeListener(listener); + } + + /** + * Test if the {@code entityClass} is corresponding to the current reference + * in the filter. Will return false if no reference is set in the filter. + * + * @param entityClass Class reference to test + * @return true if the classReference is corresponding, false otherwise + * @see #checkReference(Class, boolean) + */ + @Override + public boolean isClassReference(Class<?> entityClass) { + + boolean result = false; + + if (hasReference()) { + try { + Class<?> referenceClass = TopiaId.getClassName(referenceId); + if (referenceClass.isAssignableFrom(entityClass)) { + result = true; + } + } catch (TopiaNotFoundException eee) { + if (log.isWarnEnabled()) { + log.warn("ReferenceId '" + referenceId + "' is not a" + + " compatible topiaId : " + eee.getMessage()); + } + } + } + return result; + } + + /** + * Use to check if {@code reference} class is supported by the current + * filter reference. The reference can be not {@code mandatory}. Exceptions + * are thrown if the check failed. If you prefer to have a boolean instead + * of exceptions, you can use {@link #isClassReference(Class)}. + * + * @param reference Class reference to check + * @param mandatory If the existence of the reference is mandatory + * @throws IllegalArgumentException for errors on check + * @see #hasReference() + * @see #isClassReference(Class) + */ + @Override + public void checkReference(Class<?> reference, boolean mandatory) + throws IllegalArgumentException { + + if (log.isTraceEnabled()) { + log.trace("referenceClass to check : " + reference.getName()); + log.trace("mandatory : " + mandatory); + log.trace("filter hasReference : " + hasReference()); + log.trace("filter isClassReference : " + isClassReference(reference)); + } + + if (mandatory && !hasReference()) { + throw new IllegalArgumentException("The filter reference" + + " of type '" + reference.getSimpleName() + "' is mandatory !"); + } + + if (hasReference() && + !isClassReference(reference)) { + throw new IllegalArgumentException("Reference filtered need to be" + + " a '" + reference.getSimpleName() + "' (referenceId = " + + referenceId + ")"); + } + + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder("TopiaFilter{"). + append("referenceProperty = '").append(referenceProperty). + append("', startIndex = ").append(startIndex). + append(", endIndex = ").append(endIndex). + append(", orderBy = '").append(orderBy). + append("' , referenceId = '").append(referenceId). + append("'}"); + return builder.toString(); + } +} \ No newline at end of file Property changes on: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFilter.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL