de.tsl2.nano.bean.def.BeanAssigner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tsl2.nano.descriptor Show documentation
Show all versions of tsl2.nano.descriptor Show documentation
TSL2 Framework Descriptor (currency-handling, generic formatter, descriptors for beans, collections, actions and values)
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