001package org.kuali.ole.krad;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import org.apache.log4j.Logger;
007import org.kuali.rice.krad.uif.component.Component;
008import org.kuali.rice.krad.uif.container.Group;
009import org.kuali.rice.krad.uif.container.PageGroup;
010import org.kuali.rice.krad.uif.view.FormView;
011
012public class OleFormView extends FormView {
013
014        private static final long serialVersionUID = 153071280080228636L;
015        private static final Logger LOG = Logger.getLogger(OleFormView.class);
016
017        private List<? extends Group> originalItems;
018
019        @Override
020        public List<Component> getComponentsForLifecycle() {
021                List<Component> rv = super.getComponentsForLifecycle();
022
023                if (LOG.isDebugEnabled()) {
024                        StringBuilder sb = new StringBuilder("Components for lifecycle:");
025                        sb.append("\n  currentPageId = ").append(getCurrentPageId());
026                        sb.append("\n  singlePageView? ").append(isSinglePageView());
027                        if (rv == null)
028                                sb.append(" NULL!");
029                        else
030                                for (Component comp : rv)
031                                        if (comp == null)
032                                                sb.append("\n     NULL!");
033                                        else
034                                                sb.append("\n   - ").append(comp.getClass())
035                                                                .append(" ").append(comp.getId());
036                        sb.append("\nMy items are:");
037                        List<? extends Component> items = getItems();
038                        if (items == null)
039                                sb.append(" NULL!");
040                        else
041                                for (Component comp : items)
042                                        if (comp == null)
043                                                sb.append("\n     NULL!");
044                                        else {
045                                                sb.append("\n   - ").append(comp.getClass())
046                                                                .append(" ").append(comp.getId());
047                                                if (rv != null)
048                                                        sb.append(" -> ").append(rv.contains(comp));
049                                        }
050                        LOG.debug(sb);
051                }
052
053                return rv;
054        }
055
056        @Override
057        public void setCurrentPageId(String currentPageId) {
058                if (originalItems != null)
059                        setItems(new ArrayList<Group>());
060                super.setCurrentPageId(currentPageId);
061        }
062
063        private boolean itemsBusy = false;
064
065        @Override
066        public List<? extends Group> getItems() {
067                synchronized (this) {
068                        if (itemsBusy)
069                                return originalItems;
070        
071                        List<? extends Group> rv = super.getItems();
072                        if ((rv == null || rv.isEmpty()) && originalItems != null) {
073                                itemsBusy = true;
074                                try {
075                                        setItems(rv = OleComponentUtils.filterCurrentPage(
076                                                        getCurrentPageId(), originalItems));
077                                } finally {
078                                        itemsBusy = false;
079                                }
080        
081                                if (LOG.isDebugEnabled()) {
082                                        StringBuilder sb = new StringBuilder("Lazy init items:");
083                                        if (rv == null)
084                                                sb.append(" NULL!");
085                                        else
086                                                for (Group comp : rv)
087                                                        if (comp == null)
088                                                                sb.append("\n     NULL!");
089                                                        else
090                                                                sb.append("\n   - ").append(comp.getClass())
091                                                                                .append(" ").append(comp.getId());
092                                        LOG.debug(sb, new Throwable());
093                                }
094                        }
095                        return rv;
096                }
097        }
098
099        /**
100         * Filters {@link PageGroup} instances based on the pageId property if an
101         * active form is available.
102         */
103        @Override
104        protected <T> void copyProperties(T component) {
105                List<? extends Group> srcitems;
106
107                synchronized (this) {
108                        srcitems = getItems();
109                        try {
110                                setItems(null);
111                                super.copyProperties(component);
112                        } finally {
113                                setItems(srcitems);
114                        }
115                }
116
117                OleFormView copyView = (OleFormView) component;
118                copyView.setItems(new ArrayList<Group>());
119                copyView.originalItems = srcitems;
120        }
121
122}
123