package jpaoletti.jpm.struts.converter;

import java.util.List;
import jpaoletti.jpm.converter.ConverterException;
import jpaoletti.jpm.core.Entity;
import jpaoletti.jpm.core.ListFilter;
import jpaoletti.jpm.core.ListSort;
import jpaoletti.jpm.core.PMException;
import jpaoletti.jpm.struts.PMStrutsContext;

/**
 *
 * @author jpaoletti
 */
public abstract class AbstractCollectionConverter extends StrutsEditConverter {

    public void saveList(PMStrutsContext ctx, String eid) {
        try {
            ctx.getSession().setAttribute(ctx.getTmpName(), getList(ctx, null));
        } catch (PMException ex) {
            ctx.getPresentationManager().error(ex);
        }
    }

    public static List<?> recoverList(PMStrutsContext ctx, String eid, boolean remove) {
        try {
            final List<?> r = (List<?>) ctx.getSession().getAttribute(ctx.getTmpName());
            if (remove) {
                ctx.getSession().removeAttribute(ctx.getTmpName());
            }
            return r;
        } catch (PMException ex) {
            ctx.getPresentationManager().error(ex);
            return null;
        }
    }

    public List<?> getList(PMStrutsContext ctx, String entity_id) throws PMException {
        final String filter = getConfig("filter");
        final String eid = (entity_id == null) ? getConfig("entity") : entity_id;
        final String sortField = getConfig("sord-field");
        final String sortd = getConfig("sord-direction");
        final ListSort sort = new ListSort(sortField, (sortd != null) ? ListSort.SortDirection.ASC : ListSort.SortDirection.DESC);

        ListFilter lfilter = null;
        if (filter != null && filter.compareTo("null") != 0 && filter.compareTo("") != 0) {
            lfilter = (ListFilter) ctx.getPresentationManager().newInstance(filter);
        }
        Entity e = ctx.getPresentationManager().getEntity(eid);
        List<?> list = null;
        if (e == null) {
            throw new ConverterException("Cannot find entity " + eid);
        }
        synchronized (e) {
            ListFilter tmp = e.getListfilter();
            e.setListfilter(lfilter);
            list = e.getList(ctx, null, sort, null, null);
            e.setListfilter(tmp);
        }
        return list;
    }
}

@@ -429,12 +429,15 @@
 lic 
-void
+List%3C?%3E
  sav
@@ -483,32 +483,85 @@
 %7B%0A        try %7B%0A
+            final List%3C?%3E list = getList(ctx, null);%0A
             ctx.
@@ -608,27 +608,38 @@
 (), 
-getList(ctx, null))
+list);%0A            return list
 ;%0A  
@@ -715,32 +715,57 @@
 er().error(ex);%0A
+            return null;%0A
         %7D%0A    %7D%0A

save list return list