001package org.kuali.ole.describe.krad;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import org.apache.log4j.Logger;
007import org.kuali.ole.describe.bo.SearchResultDisplayFields;
008import org.kuali.ole.describe.bo.SearchResultDisplayRow;
009import org.kuali.ole.describe.form.OLESearchForm;
010import org.kuali.ole.docstore.common.document.config.DocumentSearchConfig;
011import org.kuali.ole.docstore.common.search.SearchParams;
012import org.kuali.rice.core.api.util.ConcreteKeyValue;
013import org.kuali.rice.core.api.util.KeyValue;
014import org.kuali.rice.krad.uif.component.BindingInfo;
015import org.kuali.rice.krad.uif.component.Component;
016import org.kuali.rice.krad.uif.container.CollectionGroup;
017import org.kuali.rice.krad.uif.container.Container;
018import org.kuali.rice.krad.uif.element.Action;
019import org.kuali.rice.krad.uif.field.Field;
020import org.kuali.rice.krad.uif.field.FieldGroup;
021import org.kuali.rice.krad.uif.layout.CollectionLayoutManager;
022import org.kuali.rice.krad.uif.layout.LayoutManagerBase;
023import org.kuali.rice.krad.uif.util.ComponentUtils;
024import org.kuali.rice.krad.uif.view.View;
025import org.kuali.rice.krad.uif.widget.Pager;
026
027public class OleSearchLayout extends LayoutManagerBase implements
028                CollectionLayoutManager {
029
030        private static final long serialVersionUID = 5289870490845303915L;
031        private static final Logger LOG = Logger.getLogger(OleSearchLayout.class);
032
033        private List<OleSearchLine> searchLines = new ArrayList<OleSearchLine>();
034        private int totalLines;
035        private int displayedLines;
036
037        private List<KeyValue> pageSizeOptions;
038        private int pageSize;
039
040        private SearchResultDisplayFields searchResultDisplayFields;
041
042        private Pager pager;
043
044        private void setUpPager(OLESearchForm form, CollectionGroup collection) {
045                pageSize = form.getPageSize();
046                if (pager == null)
047                        return;
048                
049                if (pageSize == 0) {
050                        pager.setNumberOfPages(0);
051                        return;
052                }
053
054                totalLines = form.getTotalRecordCount();
055
056                int lastPage = totalLines / pageSize
057                                + (totalLines % pageSize == 0 ? 0 : 1);
058
059                SearchParams params = form.getSearchParams();
060                int currentPage = ((params == null ? 0 : params.getStartIndex()) / pageSize) + 1;
061                String pageNumber = form.getPageNumber();
062
063                if (LOG.isDebugEnabled())
064                        LOG.debug("Setting up pager with page size " + pageSize
065                                        + ", total lines " + totalLines + ", last page " + lastPage
066                                        + ", page number " + pageNumber + ", current page "
067                                        + currentPage);
068
069                pager.setCurrentPage(currentPage);
070                pager.setNumberOfPages(lastPage);
071        }
072
073        @Override
074        public void performApplyModel(View view, Object model, Container container) {
075                OLESearchForm form = (OLESearchForm) model;
076                CollectionGroup collection = (CollectionGroup) container;
077
078                searchResultDisplayFields = form.getSearchResultDisplayFields();
079
080                List<Integer> pageSizes = DocumentSearchConfig.getPageSizes();
081                List<KeyValue> pso = new ArrayList<>(pageSizes.size() + 1);
082                for (Integer ps : pageSizes) {
083                        String pss = Integer.toString(ps);
084                        pso.add(new ConcreteKeyValue(pss, pss));
085                }
086                pageSizeOptions = pso;
087
088                setUpPager(form, collection);
089
090                super.performApplyModel(view, model, container);
091        }
092
093        @Override
094        public void buildLine(View view, Object model,
095                        CollectionGroup collectionGroup, List<Field> lineFields,
096                        List<FieldGroup> subCollectionFields, String bindingPath,
097                        List<Action> actions, String idSuffix, Object currentLine,
098                        int lineIndex) {
099
100                SearchResultDisplayRow row = (SearchResultDisplayRow) currentLine;
101
102                BindingInfo bi = new BindingInfo();
103                bi.setBindByNamePrefix(bindingPath);
104
105                OleSearchLine line = new OleSearchLine();
106                line.setLineNumber(lineIndex);
107                line.setLineId(collectionGroup.getBaseId() + idSuffix);
108                line.setBindPath(bi.getBindByNamePrefix());
109                line.setRow(row);
110
111                synchronized (searchLines) {
112                        searchLines.add(line);
113                }
114
115                if (LOG.isDebugEnabled())
116                        LOG.debug("SEARCH LINE: " + line);
117
118                displayedLines++;
119        }
120
121        @Override
122        public FieldGroup getSubCollectionFieldGroupPrototype() {
123                return null;
124        }
125
126        public SearchResultDisplayFields getSearchResultDisplayFields() {
127                return searchResultDisplayFields;
128        }
129
130        public int getPageSize() {
131                return pageSize;
132        }
133
134        public List<KeyValue> getPageSizeOptions() {
135                return pageSizeOptions;
136        }
137
138        public int getTotalLines() {
139                return totalLines;
140        }
141
142        public int getDisplayedLines() {
143                return displayedLines;
144        }
145
146        public List<OleSearchLine> getSearchLines() {
147                return searchLines;
148        }
149
150        public Pager getPager() {
151                return pager;
152        }
153
154        public void setPager(Pager pager) {
155                this.pager = pager;
156        }
157
158        @Override
159        public List<Component> getComponentsForLifecycle() {
160                List<Component> rv = super.getComponentsForLifecycle();
161                if (pager != null)
162                        rv.add(pager);
163                return rv;
164        }
165
166        @Override
167        protected <T> void copyProperties(T copy) {
168                super.copyProperties(copy);
169
170                OleSearchLayout c = (OleSearchLayout) copy;
171                if (pager != null)
172                        c.setPager(ComponentUtils.copy(pager));
173        }
174
175}
176