View Javadoc
1   package org.kuali.ole.describe.krad;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.apache.log4j.Logger;
7   import org.kuali.ole.describe.bo.SearchResultDisplayFields;
8   import org.kuali.ole.describe.bo.SearchResultDisplayRow;
9   import org.kuali.ole.describe.form.OLESearchForm;
10  import org.kuali.ole.docstore.common.document.config.DocumentSearchConfig;
11  import org.kuali.ole.docstore.common.search.SearchParams;
12  import org.kuali.rice.core.api.util.ConcreteKeyValue;
13  import org.kuali.rice.core.api.util.KeyValue;
14  import org.kuali.rice.krad.uif.component.BindingInfo;
15  import org.kuali.rice.krad.uif.component.Component;
16  import org.kuali.rice.krad.uif.container.CollectionGroup;
17  import org.kuali.rice.krad.uif.container.Container;
18  import org.kuali.rice.krad.uif.element.Action;
19  import org.kuali.rice.krad.uif.field.Field;
20  import org.kuali.rice.krad.uif.field.FieldGroup;
21  import org.kuali.rice.krad.uif.layout.CollectionLayoutManager;
22  import org.kuali.rice.krad.uif.layout.LayoutManagerBase;
23  import org.kuali.rice.krad.uif.util.ComponentUtils;
24  import org.kuali.rice.krad.uif.view.View;
25  import org.kuali.rice.krad.uif.widget.Pager;
26  
27  public class OleSearchLayout extends LayoutManagerBase implements
28  		CollectionLayoutManager {
29  
30  	private static final long serialVersionUID = 5289870490845303915L;
31  	private static final Logger LOG = Logger.getLogger(OleSearchLayout.class);
32  
33  	private List<OleSearchLine> searchLines = new ArrayList<OleSearchLine>();
34  	private int totalLines;
35  	private int displayedLines;
36  
37  	private List<KeyValue> pageSizeOptions;
38  	private int pageSize;
39  
40  	private SearchResultDisplayFields searchResultDisplayFields;
41  
42  	private Pager pager;
43  
44  	private void setUpPager(OLESearchForm form, CollectionGroup collection) {
45  		pageSize = form.getPageSize();
46  		if (pager == null)
47  			return;
48  		
49  		if (pageSize == 0) {
50  			pager.setNumberOfPages(0);
51  			return;
52  		}
53  
54  		totalLines = form.getTotalRecordCount();
55  
56  		int lastPage = totalLines / pageSize
57  				+ (totalLines % pageSize == 0 ? 0 : 1);
58  
59  		SearchParams params = form.getSearchParams();
60  		int currentPage = ((params == null ? 0 : params.getStartIndex()) / pageSize) + 1;
61  		String pageNumber = form.getPageNumber();
62  
63  		if (LOG.isDebugEnabled())
64  			LOG.debug("Setting up pager with page size " + pageSize
65  					+ ", total lines " + totalLines + ", last page " + lastPage
66  					+ ", page number " + pageNumber + ", current page "
67  					+ currentPage);
68  
69  		pager.setCurrentPage(currentPage);
70  		pager.setNumberOfPages(lastPage);
71  	}
72  
73  	@Override
74  	public void performApplyModel(View view, Object model, Container container) {
75  		OLESearchForm form = (OLESearchForm) model;
76  		CollectionGroup collection = (CollectionGroup) container;
77  
78  		searchResultDisplayFields = form.getSearchResultDisplayFields();
79  
80  		List<Integer> pageSizes = DocumentSearchConfig.getPageSizes();
81  		List<KeyValue> pso = new ArrayList<>(pageSizes.size() + 1);
82  		for (Integer ps : pageSizes) {
83  			String pss = Integer.toString(ps);
84  			pso.add(new ConcreteKeyValue(pss, pss));
85  		}
86  		pageSizeOptions = pso;
87  
88  		setUpPager(form, collection);
89  
90  		super.performApplyModel(view, model, container);
91  	}
92  
93  	@Override
94  	public void buildLine(View view, Object model,
95  			CollectionGroup collectionGroup, List<Field> lineFields,
96  			List<FieldGroup> subCollectionFields, String bindingPath,
97  			List<Action> actions, String idSuffix, Object currentLine,
98  			int lineIndex) {
99  
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