0

set parameters (inherits from superClass) in different object

asked 2014-04-01 10:47:57 +0800

pasqualeleone gravatar image pasqualeleone flag of Italy
81 2

updated 2014-04-01 10:56:53 +0800

Hi guys,

in the project that I'm developing, I have a problem that I can't solve. I will give the class hierarchy and a simple example that should explain the use case. Even though I work on two different instances of objects (tvm.arrFlightOperatorViewVMPagingDAO and tvm.depFlightOperatorViewVMPagingDAO) of the class ListVMPagingDAO that inherits from GenericDaoModel, when imposed a @ save on a parameter (value = "@ bind (tvm.arrFlightOperatorViewVMPagingDAO.pageSize)") this is set in the object relative (tvm.arrFlightOperatorViewVMPagingDAO), but is replicated in the other object (tvm.depFlightOperatorViewVMPagingDAO), and obviously this is undesirable behavior.

public abstract class GenericDAOModel<T> extends ListModelSet<T> {

private static final long serialVersionUID = 1L;
protected List <HibernateOrderCriteria> orderCriteriaList = new ArrayList <HibernateOrderCriteria>();
protected List <HibernateAliasCriteria> aliasCriteriaList = new ArrayList <HibernateAliasCriteria>();
protected HashMap <String,Criterion> filterCriteriaHash = new HashMap <String,Criterion>();
@SuppressWarnings("rawtypes")
protected GenericHibernateDAO DAO;
protected Class<T> persistentClass;


/*constructor*/
@SuppressWarnings("unchecked")
public GenericDAOModel(DAOFactoryInterface DAOFactory) throws Exception{
    super();
    persistentClass = (Class<T>) ((ParameterizedType) getClass()
            .getGenericSuperclass()).getActualTypeArguments()[0];
    DAO = DAOFactory.getDAOInstance(persistentClass);
}

/*get and set method*/
public int getPageSize() {
    return DAO.getPageSize();
}
public void setPageSize(int pageSize) {
    DAO.setPageSize(pageSize);
}
@SuppressWarnings("rawtypes")
public GenericHibernateDAO getDAO() {
    return DAO;
}

public Class<T> getPersistentClass() {
    return persistentClass;
}

/*method*/
public void resetDAO() throws Exception{
    DAO.clearAllList();
}

public boolean addPersistentAliasCriteria(String fieldName, String alias){
    return addPersistentAliasCriteria(new HibernateAliasCriteria(fieldName, alias));
}
public boolean addPersistentAliasCriteria(HibernateAliasCriteria aliasCriteria){
    removePersistentAliasCriteria(aliasCriteria);
    return aliasCriteriaList.add(aliasCriteria);
}
public void removePersistentAliasCriteria(String alias){
    removePersistentAliasCriteria(new HibernateAliasCriteria("", alias));
}
public void removePersistentAliasCriteria(HibernateAliasCriteria aliasCriteria){
    for(HibernateAliasCriteria alias : aliasCriteriaList){
        if(alias.equals(aliasCriteria)){
            aliasCriteriaList.remove(alias);        
        }
    }
}
public void removeAllPersistentAliasCriteria(){
    aliasCriteriaList.clear();
}   
public boolean addPersistentOrderCriteria(String fieldName, boolean ascOrder){
    return addPersistentOrderCriteria(new HibernateOrderCriteria(fieldName, ascOrder));
}
public boolean addPersistentOrderCriteria(HibernateOrderCriteria orderCriteria){
    removePersistentOrderCriteria(orderCriteria);
    return orderCriteriaList.add(orderCriteria);
}
public void removePersistentOrderCriteria(String fieldName){
    removePersistentOrderCriteria(new HibernateOrderCriteria(fieldName, false));
}
public void removePersistentOrderCriteria(HibernateOrderCriteria orderCriteria){
    for(HibernateOrderCriteria order : orderCriteriaList){
        if(order.equals(orderCriteria)){
            orderCriteriaList.remove(order);        
        }
    }
}
public void removeAllPersistentOrderCriteria(){
    orderCriteriaList.clear();
}

public Criterion addPersistentFilterCriteria(String uniqueCriteriaName, Criterion criteria){
    return filterCriteriaHash.put(uniqueCriteriaName.toUpperCase(), criteria);
}
public Criterion removePersistentFilterCriteria(String uniqueCriteriaName){
    return filterCriteriaHash.remove(uniqueCriteriaName.toUpperCase());     
}
public void removeAllPersistentFilterCriteria(){
    filterCriteriaHash.clear();
}
    }

and this are two SubClass: 1)

public abstract class ComboAutocompleteModel<T> extends GenericDAOModel<T> implements ListSubModel<T> {
private static final long serialVersionUID = 2583L;
public static final int CASE_SENSITIVE_MATCH = 1;
public static final int CASE_UNSENSITIVE_MATCH = 0;
public static final int MATCH_MODE_START = 0;
public static final int MATCH_MODE_ANYWHERE = 1;
public static final int DEFAULT_N_ROWS = 250;
private String searchField;
private int matchType;
private int matchMode;
private int subModelRows;
public ComboAutocompleteModel( String field, DAOFactoryInterface DAOFactory) throws Exception{
super(DAOFactory);
this.searchField = field;
this.subModelRows = DEFAULT_N_ROWS;
this.setMultiple(false);
}
/*-----------------METODI----------------*/
@SuppressWarnings("unchecked")
@Override
public ListModel<T> getSubModel(Object value, int nRows){
String key = value == null ? "" : value.toString();
int rows = nRows<0 ? subModelRows : nRows;
try {
    return new ListModelSet<T>( getMatched( key, rows ) );
} catch (Throwable e) {
    Log.getLogger().info("Something wrong on getSubModel: " + e.getMessage());
    Log.getLogger().error(e,e);
}
return null;
}
@SuppressWarnings("unchecked")
private Collection<? extends T> getMatched( String key, int rows ) throws Throwable {
try{
    if( !key.equals("") ){
        this.resetDAO();
        MatchMode mm = this.getMatchMode() == 0 ? MatchMode.START : MatchMode.ANYWHERE;
this.getDAO().addCriterion( Restrictions.ilike(this.searchField, key, mm)       );
this.getDAO().setCriteriaMaxResult( rows );// GenericHibernateDAO.DEF_PAGE_SIZE
        this.getDAO().addOrderCriteria( this.searchField, true);
        for(HibernateAliasCriteria alias : aliasCriteriaList){
            if(!getDAO().getAliasList().contains(alias))                    
                getDAO().addAlias(alias);
        }
        for(Criterion filter : filterCriteriaHash.values()){
            getDAO().addCriterion(filter);
        }
        for(HibernateOrderCriteria order : orderCriteriaList){
            if(!getDAO().getOrderCriteriaList().contains(order))
                getDAO().addOrderCriteria(order);
        }
        if(!getDAO().getOrderCriteriaList().contains(new HibernateOrderCriteria(this.searchField, true)))
            getDAO().addOrderCriteria(this.searchField, true);
        List<T> temp = (List<T>)getDAO().findByCriteria( GenericHibernateDAO.NO_PAGING );
        super.addAll(temp);
    }
}
catch (NullPointerException e){
    Log.getLogger().info("Something wrong on getMatched function: " + e.getMessage());
    Log.getLogger().error(e,e);
}

List<T> matchedData = new LinkedList<T>();
Iterator<T> iter = this._set.iterator();
T next;
int cnt = 0;
while( iter.hasNext() ){
    next = iter.next();
    if( compare( next, key ) ){
        matchedData.add( next );
        cnt++;
        if(cnt == rows)
            break;
    }
}

return matchedData;
}
private boolean compare(T next, String key) throws Throwable {
String methodName = "get" + String.valueOf(this.searchField.toCharArray()[0]).toUpperCase() + searchField.substring(1);
boolean comparation = false;
try{
    String ret = (String) next.getClass().getMethod(methodName).invoke(next);
    String comp1 = this.getMatchType() == CASE_SENSITIVE_MATCH ? ret : ret.toLowerCase();
    String comp2 = this.getMatchType() == CASE_SENSITIVE_MATCH ? key : key.toLowerCase();
    comparation = this.getMatchMode() == MATCH_MODE_START ? comp1.startsWith(comp2) : comp1.contains(comp2);
}
catch(Exception e){
    Log.getLogger().info("Something wrong on 'compare' function: " + e.getMessage());
    Log.getLogger().error(e,e); 
}

return comparation;}// metodi setSelectedObj e getSelectedObj sono stati introdotti per retrocompatibilità
@Deprecated public void setSelectedObj( T obj ){ super.clear(); super.add(obj);      super.addToSelection(obj); }
@Deprecated 
public T getSelectedObj(){
if( ( !this.isMultiple() ) && ( !_selection.isEmpty() ) )
    return _selection.iterator().next();
else
    return null;
}
/*-----------------METODI GETTER E SETTER----------------*/
}

2)

public abstract class ListVMPagingDAO<T> extends GenericDAOModel<T> {

private static final long serialVersionUID = 3852L;
private String searchParameter;
private boolean searchActive;
private int activePage;
private boolean preserveSelection = true;
private ComboAutocompleteModel <T> synchComboModel;
public ListVMPagingDAO(DAOFactoryInterface DAOFactory) throws Throwable {
    this(false, DAOFactory);
}
public ListVMPagingDAO (boolean multipleSelection, DAOFactoryInterface DAOFactory) throws Exception {
    super(DAOFactory);
    searchParameter = "";
    activePage = 0;
    searchActive = false;
    this.setMultiple(multipleSelection);
}
public ListVMPagingDAO(boolean multipleSelection, HibernateOrderCriteria defOrderCriteria, DAOFactoryInterface DAOFactory) throws Throwable {
    this(multipleSelection, DAOFactory);
    orderCriteriaList.add(defOrderCriteria);
}
public ListVMPagingDAO(boolean multipleSelection, String fieldName, boolean ascOrder, DAOFactoryInterface DAOFactory) throws Throwable {
    this(multipleSelection, DAOFactory);
    orderCriteriaList.add(new HibernateOrderCriteria(fieldName, ascOrder));
}
@SuppressWarnings("unchecked")
public List<T> loadObjList() throws Throwable{
    try{
        for(HibernateAliasCriteria alias : aliasCriteriaList){
            if(!getDAO().getAliasList().contains(alias))                    
                getDAO().addAlias(alias);
        }
        for(Criterion filter : filterCriteriaHash.values()){
            getDAO().addCriterion(filter);
        }
        for(HibernateOrderCriteria order : orderCriteriaList){
            if(!getDAO().getOrderCriteriaList().contains(order))
                getDAO().addOrderCriteria(order);
        }
        List<T> temp = (List<T>)getDAO().findByCriteria(this.getActivePage());
        List<T> preservedSelection = null;
        if(preserveSelection){
            preservedSelection = preserveSelection(temp);
        }
        super.clear();
        super.addAll(temp);
        /* add preserved selection */
        if(preservedSelection != null && preserveSelection){
            for(T obj : preservedSelection){
                super.addToSelection(obj);
            }
        }

        }catch (Throwable e){
            Log.getLogger().info("Something wrong on 'loadObjectList' function: " + e.getMessage());
            Log.getLogger().error(e,e); 
            getDAO().clearAllList();
            List<T> temp = (List<T>)getDAO().findByCriteria( this.getActivePage());
            List<T> preservedSelection = null;
            if(preserveSelection){
                preservedSelection = preserveSelection(temp);
            }
            super.clear();
            super.addAll(temp);
            if(preservedSelection != null && preserveSelection){
                for(T obj : preservedSelection){
                    super.addToSelection(obj);
                }
            }
        }
    return new ArrayList<T>(_set);
}

private List <T> preserveSelection(List <T> dataList){
    ArrayList <T> preservedSelectionList = new ArrayList<T>();
    _selection.retainAll(dataList);
    preservedSelectionList.addAll(_selection);
    return preservedSelectionList;
}

public void resetDAOAndPage(){
    activePage = 0;
    getDAO().clearAllList();
}

public void resetPage(){
    activePage = 0;
}

public int getActivePage(){

    return this.activePage;
}


public void setActivePage(int activePage) {
    this.activePage = activePage;
    try {
        this.loadObjList();
    } catch (Throwable e) {
        Log.getLogger().info("Something wrong on 'setActivePage' function: " + e.getMessage());
        Log.getLogger().error(e,e); 
    }
}

public int getTotalSize(){
    return getDAO().count();
}
public ComboAutocompleteModel<T> getSynchComboModel() {
    return synchComboModel;
}
public void setSynchComboModel(ComboAutocompleteModel<T> synchComboModel) {
    this.synchComboModel = synchComboModel;
}
public boolean isPreserveSelection() {
    return preserveSelection;
}

public void setPreserveSelection(boolean preserveSelection) {
    this.preserveSelection = preserveSelection;
}
@Override
public boolean addToSelection(T obj) {
    //Autocomplete combo model synch
    if(synchComboModel != null){
        synchComboModel.clear();
        synchComboModel.add(obj);
        synchComboModel.addToSelection(obj);
    }
    return super.addToSelection(obj);
}
@Deprecated
public void setSearchActive(boolean searchActive) {
    this.searchActive = searchActive;
}
@Deprecated
public boolean isSearchActive() {
    return searchActive;
}
// metodi setSelectedObj e getSelectedObj sono stati introdotti per retrocompatibilità
@Deprecated
public void setSelectedObj( T obj ){
    super.clearSelection();
    super.addToSelection(obj);
}
@Deprecated
public T getSelectedObj(){

    if( ( !this.isMultiple() ) && ( !_selection.isEmpty() ) )
        return _selection.iterator().next();
    else
        return null;

}

@Deprecated
public String getSearchParameter() {
    return searchParameter;
}
@Deprecated
public void setSearchParameter(String searchParameter) {
    this.searchParameter = searchParameter;
}
    }

my example zul file (test_VMPaging)

<zk>
<window title="test_VMPaging" border="normal" apply="org.zkoss.bind.BindComposer" viewModel="@id('tvm') 
@init('vis.eghos.web.ui.viewModel.common.TestVM')">
<listbox>
<listhead>
<listheader label="test flight operator" sort="auto" />
</listhead>
<listitem vflex="true">
<listcell>
<label value="Arrivo"></label>
<combobox id="c1" model="@load(tvm.flightOperatorViewAutocomplete)"
buttonVisible="false" autodrop="true"     selectedItem="@bind(tvm.selectedArrivalObj)"
value="@load(tvm.selectedArrivalObj.iataicao)" constraint="strict">
<template name="model" var="a">
<comboitem label="@load(a.iataicao)" />
</template>
</combobox>
<bandbox id="b1" width="1px">
<bandpopup width="450px" vflex="true">
<listbox vflex="true" height="50%"
model="@load(tvm.arrFlightOperatorViewVMPagingDAO)"
onSelect="b1.close();"
selectedItem="@bind(tvm.selectedArrivalObj)">
<listhead>
<listheader
label="Arrival flight operator" />
</listhead>
<template name="model" var="af">
<listitem label="@load(af.iataicao)" />
</template>
</listbox>
<hbox width="100%" pack="center"
align="center">
<intbox style="text-align:center;"
value="@bind(tvm.arrFlightOperatorViewVMPagingDAO.pageSize)"
format="####" constraint="no negative,no empty" width="30px"
height="100%"
onBlur="@command('listPageChange',vmPagingData =     tvm.arrFlightOperatorViewVMPagingDAO)"
onOK="@command('listPageChange',vmPagingData =     tvm.arrFlightOperatorViewVMPagingDAO)"/>
<paging pageSize="@load(tvm.arrFlightOperatorViewVMPagingDAO.pageSize)"
totalSize="@load(tvm.arrFlightOperatorViewVMPagingDAO.totalSize)"
activePage="@bind(tvm.arrFlightOperatorViewVMPagingDAO.activePage)" />
<label value="@load(c:cat(tvm.arrFlightOperatorViewVMPagingDAO.totalSize,' rows'))"
height="95%" style="text-align:center; border : none;" />
</hbox>
</bandpopup>
</bandbox>
<separator></separator>
<label value="@load(tvm.selectedArrivalObj.iataicao)"></label>
</listcell>
</listitem>
<listitem>
<listcell>
<label value="Partenza"></label>
<combobox id="c2"
model="@load(tvm.depFlightOperatorViewAutocomplete) @template('miot')"
buttonVisible="false" autodrop="true"
selectedItem="@bind(tvm.selectedDepartureObj)"
value="@load(tvm.selectedDepartureObj.iataicao)"
constraint="strict">
<template name="miot" var="d">
<comboitem label="@load(d.iataicao)" />
</template>
</combobox>
<bandbox id="b2" width="1px">
<bandpopup width="450px" vflex="true">
<listbox vflex="true"
model="@load(tvm.depFlightOperatorViewVMPagingDAO) @template('myTem')"
onSelect="b2.close()"
selectedItem="@bind(tvm.selectedDepartureObj)">
<listhead>
<listheader
label="Departure flight operator" />
</listhead>
<template name="myTem">
<listitem
label="@load(each.iataicao)">
</listitem>
</template>
</listbox>
<hbox width="100%" pack="center" align="center">
<intbox style="text-align:center;"
value="@bind(tvm.depFlightOperatorViewVMPagingDAO.pageSize)"
format="####" constraint="no negative,no empty" width="30px"
height="100%" onBlur="@command('listPageChange',flightVMData = tvm,vmPagingData =     tvm.depFlightOperatorViewVMPagingDAO)"
onOK="@command('listPageChange',flightVMData = tvm,vmPagingData =     tvm.depFlightOperatorViewVMPagingDAO)" />
<paging pageSize="@load(tvm.depFlightOperatorViewVMPagingDAO.pageSize)"
totalSize="@load(tvm.depFlightOperatorViewVMPagingDAO.totalSize)"
activePage="@bind(tvm.depFlightOperatorViewVMPagingDAO.activePage)" />
<label value="@load(c:cat(tvm.depFlightOperatorViewVMPagingDAO.totalSize,' rows'))"
height="95%" style="text-align:center; border : none;" />
</hbox>
</bandpopup>
</bandbox>
<separator></separator>
<label
value="@load(tvm.selectedDepartureObj.iataicao)">
</label>
</listcell>
</listitem>
</listbox>
</window>
</zk>

ViewModel (TestVM):

 public class TestVM {
private FlightOperatorViewAutocompleteModel flightOperatorViewAutocomplete;
private FlightOperatorListVMPagingDAO arrFlightOperatorViewVMPagingDAO;
private DepFlightOperatorViewAutocompleteModel depFlightOperatorViewAutocomplete;
private FlightOperatorListVMPagingDAO depFlightOperatorViewVMPagingDAO;
private FlightOperatorViewBean selectedArrivalObj;
private FlightOperatorViewBean selectedDepartureObj;

@Init
public void init() {
    try {
        selectedArrivalObj = new FlightOperatorViewBean();
        selectedDepartureObj = new FlightOperatorViewBean();
        flightOperatorViewAutocomplete = new FlightOperatorViewAutocompleteModel();
        depFlightOperatorViewAutocomplete = new DepFlightOperatorViewAutocompleteModel();


        arrFlightOperatorViewVMPagingDAO = new FlightOperatorListVMPagingDAO();
        arrFlightOperatorViewVMPagingDAO.loadObjList();
        arrFlightOperatorViewVMPagingDAO.setSynchComboModel(flightOperatorViewAutocomplete);
        arrFlightOperatorViewVMPagingDAO.setPreserveSelection(false);

        depFlightOperatorViewVMPagingDAO = new FlightOperatorListVMPagingDAO();
        depFlightOperatorViewVMPagingDAO.loadObjList();
        depFlightOperatorViewVMPagingDAO.setSynchComboModel(depFlightOperatorViewAutocomplete);
        depFlightOperatorViewVMPagingDAO.setPreserveSelection(false);

    } catch (Throwable e) {
        e.printStackTrace();
    }
}
@SuppressWarnings("rawtypes")
@Command("listPageChange")
public void listPageChange( @BindingParam("vmPagingData")ListVMPagingDAO vmpDAO ) throws Throwable{

    if(vmpDAO.getClass().getName().equals(arrFlightOperatorViewVMPagingDAO.getClass().getName())){
        ((FlightOperatorListVMPagingDAO)vmpDAO).loadObjList();
   //BindUtils.postNotifyChange(null,null, this, "arrFlightOperatorViewVMPagingDAO");
   }
   }
   //get and set    
   }
delete flag offensive retag edit
Be the first one to answer this question!
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2014-04-01 10:47:57 +0800

Seen: 10 times

Last updated: Apr 01 '14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More