View Javadoc
1   package org.kuali.ole.describe.krad;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.deliver.bo.OLEBibSearchResultDisplayRow;
5   import org.kuali.ole.deliver.bo.OLEHoldingsSearchResultDisplayRow;
6   import org.kuali.ole.deliver.bo.OLEItemSearchResultDisplayRow;
7   import org.kuali.ole.deliver.form.OLEDeliverItemSearchForm;
8   import org.kuali.ole.docstore.common.document.config.DocumentSearchConfig;
9   import org.kuali.ole.docstore.common.search.SearchParams;
10  import org.kuali.rice.core.api.util.ConcreteKeyValue;
11  import org.kuali.rice.core.api.util.KeyValue;
12  import org.kuali.rice.krad.uif.UifConstants;
13  import org.kuali.rice.krad.uif.component.BindingInfo;
14  import org.kuali.rice.krad.uif.component.Component;
15  import org.kuali.rice.krad.uif.container.CollectionGroup;
16  import org.kuali.rice.krad.uif.container.Container;
17  import org.kuali.rice.krad.uif.container.Group;
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  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Map;
30  
31  /**
32   * Created by jayabharathreddy on 6/25/15.
33   */
34  public class OleDeliverSearchLayout extends LayoutManagerBase implements
35          CollectionLayoutManager {
36  
37      private static final long serialVersionUID = 5289870490845303915L;
38      private static final Logger LOG = Logger.getLogger(OleDeliverSearchLayout.class);
39  
40      private List<OleItemSearchLines> searchLines = new ArrayList<>();
41      private int totalLines;
42      private int displayedLines;
43      private int pageSize;
44      private Pager pager;
45      private List<KeyValue> pageSizeOptions;
46      private Group rowDetailsGroup;
47      private List<Component> components = new ArrayList<>();
48  
49      private void setUpPager(OLEDeliverItemSearchForm form, CollectionGroup collection) {
50          pageSize = form.getPageSize();
51          if (pager == null)
52              return;
53  
54          if (pageSize == 0) {
55              pager.setNumberOfPages(0);
56              return;
57          }
58  
59          totalLines = form.getTotalRecordCount();
60  
61          int lastPage = totalLines / pageSize
62                  + (totalLines % pageSize == 0 ? 0 : 1);
63  
64          SearchParams params = form.getSearchParams();
65          int currentPage = ((params == null ? 0 : params.getStartIndex()) / pageSize) + 1;
66          String pageNumber = form.getPageNumber();
67  
68          if (LOG.isDebugEnabled())
69              LOG.debug("Setting up pager with page size " + pageSize
70                      + ", total lines " + totalLines + ", last page " + lastPage
71                      + ", page number " + pageNumber + ", current page "
72                      + currentPage);
73  
74          pager.setCurrentPage(currentPage);
75          pager.setNumberOfPages(lastPage);
76      }
77  
78      @Override
79      public void performApplyModel(View view, Object model, Container container) {
80          OLEDeliverItemSearchForm form = (OLEDeliverItemSearchForm) model;
81          CollectionGroup collection = (CollectionGroup) container;
82          List<Integer> pageSizes = DocumentSearchConfig.getPageSizes();
83          List<KeyValue> pso = new ArrayList<>(pageSizes.size() + 1);
84          for (Integer ps : pageSizes) {
85              String pss = Integer.toString(ps);
86              pso.add(new ConcreteKeyValue(pss, pss));
87          }
88          pageSizeOptions = pso;
89          setUpPager(form, collection);
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         OLEBibSearchResultDisplayRow row = (OLEBibSearchResultDisplayRow) currentLine;
101 
102         BindingInfo bi = new BindingInfo();
103         bi.setBindByNamePrefix(bindingPath);
104 
105         OleItemSearchLines line = new OleItemSearchLines();
106 
107         line.setLineNumber(lineIndex);
108         line.setLineId(collectionGroup.getBaseId() + idSuffix);
109         line.setBindPath(bi.getBindByNamePrefix());
110         line.setDeliverRow(row);
111         line.setRowDetails(getRowDetails((OLEDeliverItemSearchForm) model, idSuffix,
112                 lineIndex, collectionGroup, view, bindingPath, row));
113 
114         synchronized (searchLines) {
115             searchLines.add(line);
116         }
117 
118         if (LOG.isDebugEnabled())
119             LOG.debug("SEARCH LINE: " + line);
120 
121         for (OLEHoldingsSearchResultDisplayRow oleHoldingsSearchResultDisplayRow : row.getOleHoldingsSearchResultDisplayRowList()) {
122             displayedLines = displayedLines + oleHoldingsSearchResultDisplayRow.getOleItemSearchResultDisplayRowList().size();
123         }
124 
125     }
126 
127 
128     private Group getRowDetails(OLEDeliverItemSearchForm form, String idSuffix,
129                                 int lineIndex, CollectionGroup collection, View view,
130                                 String bindingPath, OLEBibSearchResultDisplayRow item) {
131         String lineId = collection.getBaseId() + idSuffix;
132         String selectedRowDetails = form.getSelectRowDetails();
133         String extensionKey = lineId + ".rowDetails";
134         Map<String, Object> extension = form.getExtensionData();
135         if (lineId.equals(selectedRowDetails))
136             synchronized (extension) {
137                 if (form.isShowSelectedRowDetails())
138                     extension.put(extensionKey, true);
139                 else
140                     extension.remove(extensionKey);
141             }
142 
143         if (!Boolean.TRUE.equals(extension.get(extensionKey))) {
144             if (LOG.isDebugEnabled())
145                 LOG.debug("Omitting row details " + extensionKey + ", "
146                         + form.getSelectRowDetails() + " "
147                         + form.isShowSelectedRowDetails());
148             return null;
149         }
150 
151         if (LOG.isDebugEnabled())
152             LOG.debug("Including row details " + extensionKey + ", "
153                     + form.getSelectRowDetails() + " "
154                     + form.isShowSelectedRowDetails());
155 
156         Group rowDetails = ComponentUtils.copy(rowDetailsGroup, idSuffix);
157         rowDetails.setFieldBindByNamePrefix(bindingPath);
158         ComponentUtils.updateContextForLine(rowDetails, item, lineIndex,
159                 idSuffix);
160         view.getViewHelperService().spawnSubLifecyle(view, form, rowDetails,
161                 collection, null, UifConstants.ViewPhases.INITIALIZE);
162 
163         synchronized (components) {
164             components.add(rowDetails);
165         }
166 
167         return rowDetails;
168     }
169 
170     @Override
171     public FieldGroup getSubCollectionFieldGroupPrototype() {
172         return null;
173     }
174 
175     public int getPageSize() {
176         return pageSize;
177     }
178 
179     public int getTotalLines() {
180         return totalLines;
181     }
182 
183     public int getDisplayedLines() {
184         return displayedLines;
185     }
186 
187     public List<OleItemSearchLines> getSearchLines() {
188         return searchLines;
189     }
190 
191     public Pager getPager() {
192         return pager;
193     }
194 
195     public void setPager(Pager pager) {
196         this.pager = pager;
197     }
198 
199     public List<KeyValue> getPageSizeOptions() {
200         return pageSizeOptions;
201     }
202 
203     public Group getRowDetailsGroup() {
204         return rowDetailsGroup;
205     }
206 
207     public void setRowDetailsGroup(Group rowDetailsGroup) {
208         this.rowDetailsGroup = rowDetailsGroup;
209     }
210 
211     public List<Component> getComponents() {
212         return components;
213     }
214 
215     public void setComponents(List<Component> components) {
216         this.components = components;
217     }
218 
219     @Override
220     public List<Component> getComponentsForLifecycle() {
221         List<Component> rv = super.getComponentsForLifecycle();
222         if (components != null)
223             rv.addAll(components);
224         if (pager != null)
225             rv.add(pager);
226         return rv;
227     }
228 
229     @Override
230     protected <T> void copyProperties(T copy) {
231         super.copyProperties(copy);
232         OleDeliverSearchLayout c = (OleDeliverSearchLayout) copy;
233         if (rowDetailsGroup != null)
234             c.setRowDetailsGroup(ComponentUtils.copy(rowDetailsGroup));
235         if (pager != null)
236             c.setPager(ComponentUtils.copy(pager));
237     }
238 
239     @Override
240     public List<Component> getComponentPrototypes() {
241         List<Component> rv = super.getComponentPrototypes();
242         rv.add(rowDetailsGroup);
243         return rv;
244     }
245 
246 }
247