Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package de.tsl2.nano.bean.def;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.UUID;
import org.apache.commons.logging.Log;
import de.tsl2.nano.bean.BeanContainer;
import de.tsl2.nano.bean.BeanUtil;
import de.tsl2.nano.core.ManagedException;
import de.tsl2.nano.core.cls.BeanAttribute;
import de.tsl2.nano.core.log.LogFactory;
import de.tsl2.nano.core.util.ListSet;
import de.tsl2.nano.core.util.StringUtil;
/**
* simple bean assigner usable for two-list-selection. on construction, the elements contained in source and dest item
* collection will be removed in the source item collection. the source item collection will be additionally wrapped
* into a {@link ListSet} to provide both interfaces - but losing the direct instance connection. the dest item list
* wont be wrapped to obtain the original collection instance.
*
* be careful to implement the {@link Object#hashCode()} and {@link Object#equals(Object)} methods, because the moved
* elements will be removed from the other list.
*
* if you work on a special gui implementation like richfaces, you have to wrap your dest-item collection into a new
* instance of {@link ListSet} before calling the BeanAssigner constructor. giving the dest item collection to the gui
* framework (through your managed bean) you have to add all dest items to your source item collection - otherwise they
* wont be visible.
*
* @author Thomas Schneider
* @version $Revision$
*/
public class BeanAssigner {
protected Collection sourceItems;
protected Collection destItems;
private String idAttributeName;
protected static final Log LOG = LogFactory.getLog(BeanAssigner.class);
/**
* constructor
*
* @param sourceItems source item list
* @param destItems (optional) destination item list
* @param idAttributeName (optional) attribute name containing the real id of the items to be filtered from
* sourceItems
*/
public BeanAssigner(Collection sourceItems, Collection destItems, String idAttributeName) {
LOG.debug("sourceItems: " + StringUtil.toFormattedString(sourceItems, 200));
LOG.debug("destItems: " + StringUtil.toFormattedString(destItems, 200));
this.sourceItems = new ListSet(sourceItems);
if (this.sourceItems.size() < sourceItems.size()) {
LOG.warn("sourceItems in hashSet removed! please check your hashCode() and equals() methods!");
}
this.destItems = destItems != null ? destItems : new ListSet();
this.idAttributeName = idAttributeName;
//filter currently assigned items from all items
if (idAttributeName != null && sourceItems.size() > 0) {
final Class beanType = (Class) sourceItems.iterator().next().getClass();
final Collection toRemove = new LinkedList();
for (final T d : destItems) {
for (final T s : sourceItems) {
final BeanAttribute attr = BeanAttribute.getBeanAttribute(beanType, idAttributeName);
final T sValue = (T) attr.getValue(s);
final T dValue = (T) attr.getValue(d);
if (sValue == dValue || (sValue != null && BeanUtil.equals(sValue, dValue))) {
toRemove.add(s);
}
}
}
LOG.info("removing already assigned objects from available objects: " + StringUtil.toFormattedString(toRemove,
200));
this.sourceItems.removeAll(toRemove);
} else {
if (destItems != null) {
this.sourceItems.removeAll(destItems);
}
}
}
/**
* override this method, if you work on entity beans with generic ids. you should assign the parent object.
*
* @param selectedSourceItems items to move to the destination list.
*/
public void moveToDest(Collection selectedSourceItems) {
assert sourceItems.containsAll(selectedSourceItems);
if (!(sourceItems.removeAll(selectedSourceItems))) {
throw new ManagedException("tsl2nano.unexpectederror", null);
}
destItems.addAll(selectedSourceItems);
}
/**
* override this method, if you work on entity beans with generic ids. you should remove the ids.
*
* @param destItem items to move to the source list.
*/
public void moveToSource(Collection selectedDestItems) {
assert destItems.containsAll(selectedDestItems);
if (!(destItems.removeAll(selectedDestItems))) {
throw new ManagedException("tsl2nano.unexpectederror", null);
}
sourceItems.addAll(selectedDestItems);
}
/**
* moveAllToSource
*/
public void moveAllToSource() {
moveToSource(new ArrayList(destItems));
}
/**
* moveAllToDest
*/
public void moveAllToDest() {
moveToDest(new ArrayList(sourceItems));
}
/**
* @return Returns the sourceItems. please read the description of {@link BeanAssigner}.
*/
public Collection getSourceItems() {
return sourceItems;
}
/**
* @return Returns the destItems. please read the description of {@link BeanAssigner}.
*/
public Collection getDestItems() {
return destItems;
}
/**
* @param sourceItems The sourceItems to set.
*/
public void setSourceItems(Collection sourceItems) {
//do nothing
//this.sourceItems = sourceItems;
}
/**
* @param destItems The destItems to set.
*/
public void setDestItems(Collection destItems) {
//do nothing
// this.destItems = destItems;
}
/**
* @return Returns the idAttributeName.
*/
public String getIdAttributeName() {
return idAttributeName;
}
/**
* wraps the a simple selection type into an assigned object type. through a 'findAll' of the source type a list
* will be evaluated. for each item of that list, a new dest object (with uuid on idAttributeName) will be created.
* on the dest object type, an attribute of type 'sourceType' will be searched, and the setter will be called to
* assign the simple source object.
*
* WARNING: The {@link BeanContainer} must be initialized - to create and find bean instances.
*
* @param source type
* @param wrapping type
* @param sourceType source type
* @param destType wrapping type
* @param idAttributeName id attribute
* @return wrapped collection
*/
public static Collection getWrappedCollection(Class sourceType,
Class destType,
String idAttributeName,
int maxCount) {
final Collection allItems = BeanContainer.instance().getBeans(sourceType, 0, maxCount);
return getWrappedCollection(allItems, destType, idAttributeName, maxCount);
}
/**
* wraps the a simple selection type into an assigned object type. through a 'findAll' of the source type a list
* will be evaluated. for each item of that list, a new dest object (with uuid on idAttributeName) will be created.
* on the dest object type, an attribute of type 'sourceType' will be searched, and the setter will be called to
* assign the simple source object.
*
* WARNING: The {@link BeanContainer} must be initialized - to create bean instances.
*
* @param source type
* @param wrapping type
* @param sourceType source type
* @param destType wrapping type
* @param idAttributeName id attribute
* @return wrapped collection
*/
public static Collection getWrappedCollection(Collection availableItems,
Class destType,
String idAttributeName,
int maxCount) {
//availableItems must have at least one item - otherwise the assigner is senseless!
final Class sourceType = (Class) availableItems.iterator().next().getClass();
final Collection allWrappedItems = new LinkedList();
final BeanAttribute srcAttribute = BeanAttribute.getBeanAttribute(destType,
BeanAttribute.getAttributeName(sourceType));
final BeanAttribute idAttribute = BeanAttribute.getBeanAttribute(destType, idAttributeName);
for (final SOURCE g : availableItems) {
final DEST bg = BeanContainer.isInitialized() ? BeanContainer.instance().createBean(destType)
: BeanContainer.createBeanInstance(destType);
idAttribute.setValue(bg, UUID.randomUUID().toString());
srcAttribute.setValue(bg, g);
allWrappedItems.add(bg);
}
return allWrappedItems;
}
/**
* createAssigner, uses {@link #getWrappedCollection(Class, Class, String, int)} to evaluate the available item
* list. please read the description of {@link BeanAssigner}.
*
* @param
* @param type list type
* @param destItems (optional) destination item list
* @param idAttributeName (optional) attribute name containing the real id of the items to be filtered from
* sourceItems
* @param wrappedBeanAttributeName attribute name of wrapped bean
* @param maxCount maximum count of available items to load
* @return new filled BeanAssigner instance
*/
public static BeanAssigner createAssigner(Collection availableItems,
Class type,
Collection dest,
String idAttributeName,
String wrappedBeanAttributeName,
int maxCount) {
return new BeanAssigner(getWrappedCollection(availableItems, type, idAttributeName, maxCount),
dest,
wrappedBeanAttributeName);
}
/**
* createAssigner, uses {@link #getWrappedCollection(Class, Class, String, int)} to evaluate the available item
* list.please read the description of {@link BeanAssigner}.
*
* @param
* @param type list type
* @param destItems (optional) destination item list
* @param idAttributeName (optional) attribute name containing the real id of the items to be filtered from
* sourceItems
* @param wrappedBeanAttributeName attribute name of wrapped bean
* @param maxCount maximum count of available items to load
* @return new filled BeanAssigner instance
*/
public static BeanAssigner createAssigner(Class type,
Collection dest,
String idAttributeName,
String wrappedBeanAttributeName,
int maxCount) {
return new BeanAssigner(getWrappedCollection(type, type, idAttributeName, maxCount),
dest,
wrappedBeanAttributeName);
}
}