Package org.jspresso.framework.model.component.basic

Source Code of org.jspresso.framework.model.component.basic.AbstractComponentFactory

/*
* Copyright (c) 2005-2011 Vincent Vandenschrick. All rights reserved.
*
*  This file is part of the Jspresso framework.
*
*  Jspresso is free software: you can redistribute it and/or modify
*  it under the terms of the GNU Lesser General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  Jspresso is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU Lesser General Public License for more details.
*
*  You should have received a copy of the GNU Lesser General Public License
*  along with Jspresso.  If not, see <http://www.gnu.org/licenses/>.
*/
package org.jspresso.framework.model.component.basic;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.hibernate.collection.PersistentCollection;
import org.jspresso.framework.model.component.IComponent;
import org.jspresso.framework.model.component.IComponentFactory;
import org.jspresso.framework.model.descriptor.ICollectionPropertyDescriptor;
import org.jspresso.framework.util.accessor.IAccessor;
import org.jspresso.framework.util.accessor.IAccessorFactory;
import org.jspresso.framework.util.bean.BeanComparator;
import org.jspresso.framework.util.collection.ESort;

/**
* Base class for component factories.
*
* @version $LastChangedRevision$
* @author Vincent Vandenschrick
*/
public abstract class AbstractComponentFactory implements IComponentFactory {

  private IAccessorFactory accessorFactory;

  /**
   * Gets the accessorFactory.
   *
   * @return the accessorFactory.
   */
  @Override
  public IAccessorFactory getAccessorFactory() {
    return accessorFactory;
  }

  /**
   * Sets the accessorFactory used by this entity factory.
   *
   * @param accessorFactory
   *          the accessorFactory to set.
   */
  public void setAccessorFactory(IAccessorFactory accessorFactory) {
    this.accessorFactory = accessorFactory;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  @SuppressWarnings("unchecked")
  public void sortCollectionProperty(IComponent component, String propertyName) {
    Collection<Object> propertyValue = (Collection<Object>) component
        .straightGetProperty(propertyName);
    boolean wasClean = false;
    if (propertyValue instanceof PersistentCollection
        && !((PersistentCollection) propertyValue).isDirty()) {
      wasClean = true;
    }
    ICollectionPropertyDescriptor<?> propertyDescriptor = (ICollectionPropertyDescriptor<?>) getComponentDescriptor(
        component.getComponentContract()).getPropertyDescriptor(propertyName);
    if (propertyValue != null
        && !propertyValue.isEmpty()
        && !List.class.isAssignableFrom(propertyDescriptor
            .getCollectionDescriptor().getCollectionInterface())) {
      Map<String, ESort> orderingProperties = propertyDescriptor
          .getOrderingProperties();
      if (orderingProperties != null && !orderingProperties.isEmpty()) {
        List<IAccessor> orderingAccessors = new ArrayList<IAccessor>();
        List<ESort> orderingDirections = new ArrayList<ESort>();
        Class<?> collectionElementContract = propertyDescriptor
            .getCollectionDescriptor().getElementDescriptor()
            .getComponentContract();
        for (Map.Entry<String, ESort> orderingProperty : orderingProperties
            .entrySet()) {
          orderingAccessors.add(accessorFactory.createPropertyAccessor(
              orderingProperty.getKey(), collectionElementContract));
          orderingDirections.add(orderingProperty.getValue());
        }
        BeanComparator comparator = new BeanComparator(orderingAccessors,
            orderingDirections);
        List<Object> collectionOrigin = new ArrayList<Object>(propertyValue);
        List<Object> collectionCopy = new ArrayList<Object>(propertyValue);
        Collections.sort(collectionCopy, comparator);
        if (!collectionCopy.equals(collectionOrigin)) {
          Collection<Object> collectionProperty = propertyValue;
          collectionProperty.clear();
          collectionProperty.addAll(collectionCopy);
          if (wasClean) {
            ((PersistentCollection) collectionProperty).clearDirty();
          }
        }
      }
    }
  }
}
TOP

Related Classes of org.jspresso.framework.model.component.basic.AbstractComponentFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.