Monday, March 23, 2015

Adding a date range filter to Primefaces 4 DataTable

This post is an addendum to this other post 

This is some sort of a hack, but it seems to work.

First, the xhtml

                    <f:facet name="header">

                        <p:outputLabel
                            for="from"
                            value="Filter from:" />
                        <p:calendar
                            id="from"
                            value="#{sitePasswordHistoryMB.filterFrom}"
                            pattern="MM/dd/yyyy HH:mm:ss">
                            <p:ajax
                                event="dateSelect"
                                listener="#{sitePasswordHistoryMB.handleUserFilterSelection}"
                                update=":am:pas" />
                        </p:calendar>


                        <p:outputLabel
                            for="to"
                            value="&#160;Filter until:" />
                        <p:calendar
                            id="to"
                            value="#{sitePasswordHistoryMB.filterTo}"
                            pattern="MM/dd/yyyy HH:mm:ss">
                            <p:ajax
                                event="dateSelect"
                                listener="#{sitePasswordHistoryMB.handleUserFilterSelection}"
                                update=":am:pas" />
                        </p:calendar>

                    </f:facet>


The Managed Bean

    private Date filterFrom;
    private Date filterTo;

    @PostConstruct
    public void init() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
       
        filterFrom = cal.getTime(); //yesterday
        filterTo = new Date(); //today
       
        refresh();
    }

    private void refresh() {
        try {
            this.model = new LazyDataModel<SitePasswordHistory>(){
                private static final long    serialVersionUID    = 1L;
                @Override
                public List<SitePasswordHistory> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
                   
                    filters.put("from", String.valueOf(filterFrom.getTime()));
                    filters.put("to", String.valueOf(filterTo.getTime()));
                   
                    List<SitePasswordHistory> result = sitePasswordHistoryEJB.getResultList(first, pageSize, sortField, sortOrder, filters);
                   
                    filters.put("from", String.valueOf(filterFrom.getTime()));
                    filters.put("to", String.valueOf(filterTo.getTime()));

                    model.setRowCount((int)sitePasswordHistoryEJB.count(filters));
                   
                    return result;
                }
            };
        } catch (Exception e) {
            jsfUtilEJB.addErrorMessage(e,"Could not list");
        }
    }


and down to the DAO

    public Collection<SitePasswordHistory> getAll(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
       
        Date from = null;
        if (filters != null && filters.get("from") != null) {
            from = new Date(Long.parseLong(filters.get("from")));
            filters.remove("from");
        }
       
        Date to = null;
        if (filters != null && filters.get("to") != null) {
            to = new Date(Long.parseLong(filters.get("to")));
            filters.remove("to");
        }
(...)


        Predicate filterCondition = cb.conjunction();
       
        if (from != null) {
            filterCondition = cb.and(filterCondition, cb.greaterThan(sitePasswordHistory.get(SitePasswordHistory_.ts), from));
        }
       
        if (to != null) {
            filterCondition = cb.and(filterCondition, cb.lessThan(sitePasswordHistory.get(SitePasswordHistory_.ts), to));
        }
       
        for (Map.Entry<String, String> filter : filters.entrySet()) {
(...)


I haven't tried yet, but I believe PrimeFaces 5 has a better support for these kind of filters, since the LazyDataModel API has been changed (thus making this post incompatible with the new API...) 

Sunday, March 8, 2015

Using Oracle OIM API (simplified from OIM doc)

import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;

import javax.security.auth.login.LoginException;

import oracle.iam.identity.exception.UserSearchException;
import oracle.iam.identity.usermgmt.api.UserManager;
import oracle.iam.identity.usermgmt.api.UserManagerConstants.AttributeName;
import oracle.iam.identity.usermgmt.vo.User;
import oracle.iam.platform.OIMClient;
import oracle.iam.platform.authz.exception.AccessDeniedException;
import oracle.iam.platform.entitymgr.vo.SearchCriteria;

public class NEASOIMClient {
    private static OIMClient oimClient;

    private void init() throws LoginException {
        System.setProperty("java.security.auth.login.config", "/mypathto/conf/authwl.conf");
        System.setProperty("APPSERVER_TYPE","wls");

        System.out.println("Creating client....");
        String ctxFactory = "weblogic.jndi.WLInitialContextFactory";
        String serverURL = "t3://127.0.0.1:14000";
        String username = "xelsysadm";
        String password = "password";
        Hashtable<String,String> env = new Hashtable<String,String>();
        env.put(OIMClient.JAVA_NAMING_FACTORY_INITIAL, ctxFactory);
        env.put(OIMClient.JAVA_NAMING_PROVIDER_URL, serverURL);
        oimClient = new OIMClient(env);
        System.out.println("Logging in");
        oimClient.login(username, password.toCharArray());
        System.out.println("Log in successful");
    }

    private void getUserLogin(String psFirstName) {
        UserManager userService = oimClient.getService(UserManager.class);
        Set<String> retAttrs = new HashSet<String>();
        retAttrs.add(AttributeName.USER_LOGIN.getId());
        SearchCriteria criteria = new SearchCriteria(AttributeName.FIRSTNAME.getId(), psFirstName, SearchCriteria.Operator.EQUAL);
        try {
            List<User> users = userService.search(criteria, retAttrs, null);
            for (int i = 0; i < users.size(); i++) {
                System.out.println("First Name : " + psFirstName + "--" + users.get(i).getLogin());
            }
        } catch (AccessDeniedException ade) {
            ade.printStackTrace();
        } catch (UserSearchException use) {
            use.printStackTrace();
        }
    }

    public static void main(String args[]) {
        try {
            NEASOIMClient oimSample = new NEASOIMClient();
            oimSample.init();
            oimSample.getUserLogin("Leo*");
            oimClient.logout();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}