• Some Modifications to CategoryBean class

    By Ulrich Krause 1 decade ago

    I have made acouple of modifications:

    • made the code independend from extLib
    • added categoryTotals
    • added sorting ( ascending/ descending )


    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Iterator;
    import java.util.List;
    import java.util.TreeMap;

    import javax.faces.context.FacesContext;

    import lotus.domino.Database;
    import lotus.domino.NotesException;
    import lotus.domino.Session;
    import lotus.domino.View;
    import lotus.domino.ViewEntry;
    import lotus.domino.ViewNavigator;

    public class CategoryBean {

        private String viewName;
        private String server;
        private String databasePathAndName;
        private int categoryColumn;
        private boolean isInitialized;
        private List entries;
        private List categoryValues;
        private TreeMap categoryTotals;
        private FacesContext fc = FacesContext.getCurrentInstance();
        private Session session = (Session) fc.getApplication()
        .getVariableResolver().resolveVariable(fc, "session");

        public int getCategoryColumn() {
            return categoryColumn;
        }

        public void setCategoryColumn(int categoryColumn) {
            this.categoryColumn = categoryColumn;
        }

        public String getViewName() {
            return viewName;
        }

        public void setViewName(String viewName) {
            this.viewName = viewName;
        }

        public String getServer() {
            return server;
        }

        public void setServer(String server) {
            this.server = server;
        }

        public String getDatabasePathAndName() {
            return databasePathAndName;
        }

        public void setDatabasePathAndName(String databasePathAndName) {
            this.databasePathAndName = databasePathAndName;
        }

        public CategoryBean() {
        }

        public List getAllEntries() {
            if (!isInitialized)
                init();
            return entries;
        }

        @SuppressWarnings("unchecked")
        public List getEntries(String categoryValue) {
            if (!isInitialized)
                init();
            ArrayList entriesOfCategory = new ArrayList();

            if (entries != null) {
                Iterator it = entries.iterator();
                for (; it.hasNext();) {
                    Entry e = (Entry) it.next();
                    List columnValues = e.getColumnValues();
                    String catValue = (String) columnValues.get(categoryColumn);
                    if (catValue.equalsIgnoreCase(categoryValue))
                        entriesOfCategory.add(e);
                }
            }

            return entriesOfCategory;
        }

        public List getCategoryValues() {
            if (!isInitialized)
                init();
            return categoryValues;
        }

        public List getCategoryValuesAsc() {
            if (!isInitialized)
                init();
            Collections.sort(categoryValues, new StringAsc());
            return categoryValues;
        }

        public List getCategoryValuesDesc() {
            if (!isInitialized)
                init();
            Collections.sort(categoryValues, new StringDesc());
            return categoryValues;
        }

        public TreeMap getCategoryTotals() {
            return categoryTotals;
        }

        @SuppressWarnings("unchecked")
        private void init() {
            entries = new ArrayList();
            categoryValues = new ArrayList();
            categoryTotals = new TreeMap();
            Database db = null;
            View view = null;
            ViewNavigator navigator = null;
            try {
                if (databasePathAndName.equalsIgnoreCase("")) {
                    db = (Database) fc.getApplication().getVariableResolver()
                    .resolveVariable(fc, "database");
                } else {
                    db = session.getDatabase(server, databasePathAndName);
                }
                if (!db.isOpen())
                    db.open();
                view = db.getView(viewName);
                view.setAutoUpdate(false);
                navigator = view.createViewNav();
                ViewEntry tmpEntry;
                ViewEntry entry = navigator.getFirst();
                while (entry != null) {
                    try {
                        entry.setPreferJavaDates(true);
                        List columnValues = entry.getColumnValues();
                        Entry e = new Entry(columnValues);
                        entries.add(e);
                        String categoryValue = (String) columnValues
                        .get(categoryColumn);
                        if (!categoryValues.contains(categoryValue))
                            categoryValues.add(categoryValue);
                        /**
                         * @author Ulrich Krause
                         */
                        if (!categoryTotals.containsKey(categoryValue)) {
                            categoryTotals.put(categoryValue, 1);
                        } else {
                            categoryTotals.put(categoryValue, categoryTotals
                                    .get(categoryValue) + 1);
                        }

                    } catch (Exception e) {
                    }

                    tmpEntry = navigator.getNext();
                    entry.recycle();
                    entry = tmpEntry;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return;
            } catch (Throwable t) {
                t.printStackTrace();
                return;
            } finally {
                try {
                    if (navigator != null) {
                        navigator.recycle();
                    }
                    if (view != null) {
                        view.recycle();
                    }
                    if (db != null) {
                        db.recycle();
                    }
                } catch (NotesException e) {
                    e.printStackTrace();
                    return;
                }
            }
            isInitialized = true;
        }

        /**
         * @author Ulrich Krause
         *
         */
        private class StringAsc implements Comparator {

            public int compare(String s1, String s2) {
                int result = s1.compareTo(s2);
                int ret = 0;
                if (result < 0)// less than
                {
                    ret = -1;
                } else if (result == 0)// equals
                {
                    ret = 0;
                } else// greater than
                {
                    ret = 1;
                }
                return ret;
            }
        }

        /**s
         * @author Ulrich Krause
         *
         */
        private class StringDesc implements Comparator {

            public int compare(String s1, String s2) {
                int result = s1.compareTo(s2);
                int ret = 0;
                if (result < 0)// less than
                {
                    ret = 1;
                } else if (result == 0)// equals
                {
                    ret = 0;
                } else// greater than
                {
                    ret = -1;
                }
                return ret;
            }
        }
    }