View Javadoc
1   package org.kuali.ole.describe.bo;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.apache.solr.client.solrj.SolrServerException;
6   import org.kuali.ole.describe.form.EditorForm;
7   import org.kuali.ole.docstore.common.document.BibTree;
8   import org.kuali.ole.docstore.common.document.Holdings;
9   import org.kuali.ole.docstore.common.document.HoldingsTree;
10  import org.kuali.ole.docstore.common.document.Item;
11  import org.kuali.ole.docstore.common.document.content.instance.LocationLevel;
12  import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
13  import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
14  import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
15  import org.kuali.ole.docstore.model.enums.DocType;
16  import org.kuali.rice.core.api.util.tree.Node;
17  import org.kuali.rice.krad.service.BusinessObjectService;
18  import org.kuali.rice.krad.service.KRADServiceLocator;
19  import org.kuali.ole.docstore.common.document.EHoldings;
20  
21  import java.util.*;
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  /**
26   * Created with IntelliJ IDEA.
27   * User: pp7788
28   * Date: 12/12/12
29   * Time: 2:30 PM
30   * To change this template use File | Settings | File Templates.
31   */
32  public class EditorFormDataHandler {
33  
34      private static final Logger LOG = Logger.getLogger(EditorFormDataHandler.class);
35  
36      public EditorForm buildLeftPaneData(EditorForm editorForm) {
37          DocumentSelectionTree documentSelectionTree = new DocumentSelectionTree();
38          List<BibTree> bibTreeList = editorForm.getDocumentForm().getBibTreeList();
39          if (bibTreeList == null) {
40              bibTreeList = editorForm.getBibTreeList();
41          }
42          Collection<String> uuids = null;
43          List<HoldingsTree> holdingsTrees = new ArrayList<HoldingsTree>();
44          List<HoldingsTree> eHoldingsTrees = new ArrayList<HoldingsTree>();
45  
46          if (bibTreeList != null) {
47              for (BibTree bibTree : bibTreeList) {
48                  if (bibTree != null && bibTree.getBib() != null) {
49                      editorForm.setBibId(bibTree.getBib().getId());
50                      if (bibTree.getHoldingsTrees() != null) {
51                          Collections.sort(bibTree.getHoldingsTrees());
52                          for (HoldingsTree holdingsTree : bibTree.getHoldingsTrees()) {
53                              Collections.sort(holdingsTree.getItems());
54                              if (holdingsTree.getHoldings().getId() != null) {
55                                  Holdings holdings = holdingsTree.getHoldings();
56                                  if (holdings instanceof EHoldings) {
57                                      eHoldingsTrees.add(holdingsTree);
58                                  } else {
59                                      holdingsTrees.add(holdingsTree);
60                                  }
61                              }
62                          }
63                      }
64                  }
65              }
66          }
67          //uuids = uuidList.values();
68          Node<DocumentTreeNode, String> rootNode = null;
69          try {
70              rootNode = documentSelectionTree.addHoldingsTree(holdingsTrees, DocType.HOLDINGS.getCode());
71  
72              if (eHoldingsTrees.size() > 0) {
73                  documentSelectionTree.addHoldingsTree(eHoldingsTrees, DocType.EHOLDINGS.getCode());
74              }
75          } catch (SolrServerException e) {
76              LOG.error("buildLeftPaneData Exception:" + e);
77          }
78          editorForm.getLeftTree().setRootElement(rootNode);
79          return editorForm;
80      }
81  
82  
83      public String prepareBibTree(EditorForm editorForm) {
84  
85          List<BibTree> bibTreeList = editorForm.getBibTreeList();
86          String editable = editorForm.getEditable();
87          StringBuilder sb = new StringBuilder();
88          String docFormat = editorForm.getDocFormat();
89          if (bibTreeList != null) {
90              sb.append("<ul id='navigation'>");
91              for (BibTree bibTree : bibTreeList) {
92                  sb.append("&nbsp;&nbsp;");
93                  if (editable.equals("true") && bibTree.getBib().getId() != null) {
94                      sb.append(
95                              "<img src='../krms/images/add.png' alt='Add Holdings' title='Add Holdings' class='addInstance' id='")
96                              .append(bibTree.getBib().getId() + "_work_instance_oleml").append("'></img>&nbsp;");
97                      sb.append(
98                              "<img src='../krad/images/cancel.png' alt='Delete Bib' title='Delete Bib' class='deleteBib' id='")
99                              .append(bibTree.getBib().getId() + "_work_bibliographic_marc").append("'></img>");
100                 }
101                 if (bibTree != null) {
102                     List<HoldingsTree> holdingsTreeList = bibTree.getHoldingsTrees();
103                     Holdings holdings = new Holdings();
104                     List<Item> itemList = new ArrayList<Item>();
105                     if (null != holdingsTreeList && holdingsTreeList.size() > 0) {
106                         sb.append("<ul>");
107                         for (HoldingsTree holdingsTree : holdingsTreeList) {
108                             holdings = holdingsTree.getHoldings();
109                             if (holdings != null) {
110                                 String holdingIdentifier = holdings.getId()
111                                         + "_work_holdings_oleml_" + bibTree.getBib().getId();
112                                 //String holdingData = getHoldingsLabel(workHoldingsDocument);
113 
114                                 sb.append("<li><div align='left' title='View/Edit Holdings' class='holdingIdentifierClass' id='")
115                                         .append(holdingIdentifier).append("'");
116                                 if (holdings.getId() != null && holdings.getId().equals(editorForm.getDocId())) {
117                                     sb.append(" style='color:blue'");
118                                 }
119 
120                                 //sb.append(">").append(holdingData);
121                                 sb.append("&nbsp;&nbsp;");
122                                 if ((editable.equals("true") && (holdingsTree.getHoldings().getId() != null))) {
123                                     sb.append(
124                                             "<img src='../krms/images/add.png' alt='Add Item' title='Add Item' class='addItem' id='")
125                                             .append(holdingsTree.getHoldings().getId() + "_work_item_oleml_"
126                                                     + bibTree.getBib().getId()).append("'></img>");
127                                     sb.append(
128                                             "<img src='../krad/images/cancel.png' alt='Delete Instance' title='Delete Instance' class='deleteBib' id='")
129                                             .append(holdingsTree.getHoldings().getId() + "_work_instance_oleml" + "_"
130                                                     + bibTree.getBib().getId()).append("'></img>");
131                                 }
132                                 sb.append("</div>");
133                                 sb.append("<ul>");
134                             }
135                             itemList = holdingsTree.getItems();
136                             if (itemList != null) {
137                                 for (int i = 0; i < itemList.size(); i++) {
138                                     Item item = itemList.get(i);
139                                     if (item != null) {
140                                         String itemIdentifier = item.getId() + "_work_item_oleml_"
141                                                 + holdingsTree.getHoldings().getId() + "_"
142                                                 + bibTree.getBib().getId();
143                                         //  String itemLevelContent = getItemLabel(holdings, workItemDocument);
144 
145                                         sb.append("<li><div align='left' title='View/Edit Item' class='itemIdentifierClass' id='")
146                                                 .append(itemIdentifier).append("'");
147                                         if (item.getId() != null && item.getId().equals(editorForm.getDocId())) {
148                                             sb.append(" style='color:blue'");
149                                         }
150                                         // sb.append(">").append(itemLevelContent);
151                                         sb.append("&nbsp;&nbsp;");
152                                         if ((editable.equals("true")) && (item.getId() != null)) {
153                                             sb.append(
154                                                     "<img src='../krad/images/cancel.png' alt='Add Item' title='Delete Item' class='deleteBib' id='")
155                                                     .append(item.getId() + "_work_item_oleml" + "_"
156                                                             + item.getId()).append("'></img>").append("</div></li>");
157                                         }
158 
159                                     }
160                                 }
161                             }
162                             sb.append("</ul></li>");
163                         }
164                         // sb.append("</ul></li>");
165                     } else {
166                         sb.append("</li>");
167                     }
168                 }
169             }
170             sb.append("</ul>");
171         }
172         return sb.toString();
173     }
174 
175     public String getItemLabel(Holdings holdings, Item item) {
176 
177         String itemLabel = "";
178         StringBuffer itemLabelBuffer = new StringBuffer();
179         StringBuffer itemLabelBufferForStaffOnly = new StringBuffer();
180         StringBuffer holdingsCallNumberInformation = new StringBuffer();
181         OleHoldings oleHoldings = new HoldingOlemlRecordProcessor().fromXML(holdings.getContent());
182         org.kuali.ole.docstore.common.document.content.instance.Item oleItem = new ItemOlemlRecordProcessor().fromXML(item.getContent());
183         //prefix+callNumber for holdings
184         if (oleHoldings != null && oleHoldings.getCallNumber() != null) {
185             getCallNumberInformation(holdingsCallNumberInformation, oleHoldings.getCallNumber().getPrefix(),
186                     oleHoldings.getCallNumber().getNumber());
187         }
188 
189 //        //prefix+callNumber for item
190 //        if (oleItem.getLocation() != null) {
191 //            if ((oleHoldings != null) && oleHoldings.getLocation() != null && oleHoldings.getLocation().getLocationLevel() != null && (oleHoldings.getLocation().getLocationLevel().getName() != null)) {
192 //                if ((oleItem != null) && (oleItem.getLocation().getLocationLevel() != null)) {
193 //                    addDataToLabel(itemLabelBuffer, getLocationCode(oleItem.getLocation().getLocationLevel()));
194 //                }
195 //            } else if (oleItem.getLocation() != null) {
196 //                addDataToLabel(itemLabelBuffer, getLocationCode(oleItem.getLocation().getLocationLevel()));
197 //            }
198 //        }
199 //        if (oleItem.getEnumeration() != null) {
200 //            addDataToLabel(itemLabelBuffer, oleItem.getEnumeration());
201 //        }
202 //        if (oleItem.getChronology() != null) {
203 //            addDataToLabel(itemLabelBuffer, oleItem.getChronology());
204 //        }
205 //        if (oleItem.getCopyNumber() != null) {
206 //            addDataToLabel(itemLabelBuffer, oleItem.getCopyNumber());
207 //        }
208 //        if (oleItem.getBarcodeARSL() != null) {
209 //            addDataToLabel(itemLabelBuffer, oleItem.getBarcodeARSL());
210 //        }
211 //        if (oleItem.getAccessInformation() != null && oleItem.getAccessInformation().getBarcode() != null) {
212 //            addDataToLabel(itemLabelBuffer, oleItem.getAccessInformation().getBarcode());
213 //        }
214         itemLabelBuffer.append(item.getDisplayLabel());
215         if (StringUtils.isEmpty(itemLabelBuffer.toString().trim())) {
216             itemLabelBuffer = itemLabelBuffer.append("Item");
217         }
218         if (itemLabelBuffer.length() > 0) {
219             if (item.isAnalytic()) {
220                 itemLabelBuffer.append("<a href='analyticsController?viewId=AnalyticsSummaryView&amp;methodToCall=showAnalyticsSummary&amp;itemId="+ item.getId()
221                         + "&amp;docType=item&amp' class='analytics' target='_blank'>AI</a>");
222             }
223             if (item.isStaffOnly()) {
224                 itemLabelBufferForStaffOnly.append("<i><font color='red'>");
225                 itemLabelBufferForStaffOnly.append(itemLabelBuffer);
226                 itemLabelBufferForStaffOnly.append("</font></i>");
227             } else {
228                 itemLabelBufferForStaffOnly.append(itemLabelBuffer);
229             }
230             itemLabel = itemLabelBufferForStaffOnly.toString();
231         }
232 
233         return itemLabel;
234     }
235 
236 
237     public String getHoldingsLabel(HoldingsTree holdingsTree) {
238         StringBuffer holdingsLabelBuffer = new StringBuffer();
239         StringBuffer holdingsLabelBufferForStaffOnly = new StringBuffer();
240         String holdingsLabel = "";
241         //OleHoldings oleHoldings = new HoldingOlemlRecordProcessor().fromXML(holdings.getContent());
242         Boolean staffOnlyFlag = holdingsTree.getHoldings().isStaffOnly();
243         String linkedBibCount = "";
244         boolean analyticHoldingFlag = false;
245         String itemId = "";
246         //String analyticHoldingLabel = "";
247 //        if (oleHoldings.getLocation() != null  && oleHoldings.getLocation().getLocationLevel()!=null ) {
248 //            addDataToLabel(holdingsLabelBuffer, getLocationCode(oleHoldings.getLocation().getLocationLevel()));
249 //        }
250 //        if(oleHoldings!=null && oleHoldings.getCallNumber()!=null)  {
251 //            getCallNumberInformation(holdingsLabelBuffer,oleHoldings.getCallNumber().getPrefix(),
252 //                oleHoldings.getCallNumber().getNumber());
253 //        }
254 //
255 //        if (oleHoldings.getCopyNumber() != null) {
256 //            addDataToLabel(holdingsLabelBuffer, oleHoldings.getCopyNumber());
257 //        }
258         if (StringUtils.isNotBlank(holdingsTree.getHoldings().getDisplayLabel())) {
259             if (staffOnlyFlag != null && staffOnlyFlag) {
260                 holdingsLabelBuffer.append("<i><font color='red'>");
261                 holdingsLabelBuffer.append(holdingsTree.getHoldings().getDisplayLabel());
262                 holdingsLabelBuffer.append("</font></i>");
263             } else {
264                 holdingsLabelBuffer.append(holdingsTree.getHoldings().getDisplayLabel());
265             }
266         }
267 
268         boolean boundWithFlag = false;
269 
270         if (holdingsLabelBuffer.length() > 0) {
271             int bibUUIDS = 0;
272             String showBibLink = null;
273             String showSHLink = null;
274             if(holdingsTree.getHoldings().isBoundWithBib()){
275                 bibUUIDS = holdingsTree.getHoldings().getBibs().getBibs().size();
276             }
277             if (holdingsTree.getHoldings().isSeries()) {
278                 showSHLink = "<a href='analyticsController?viewId=AnalyticsSummaryView&amp;methodToCall=showAnalyticsSummary&amp;holdingsId="+ holdingsTree.getHoldings().getId()
279                         + "&amp;docType=holdings&amp' class='analytics' target='_blank'>SH</a>";
280                 //holdingsLabel = holdingsLabelBuffer + showBibLink;
281                 holdingsLabelBuffer.append(showSHLink);
282             }
283             String holdingsId = "";
284             for (Item item : holdingsTree.getItems()) {
285                 if (item.isAnalytic()) {
286                     for (Holdings holdings : item.getHoldings()) {
287                             holdingsId = holdings.getId();
288                     }
289                     if(!(holdingsId.equals(holdingsTree.getHoldings().getId()))){
290                         //analyticHoldingLabel = item.getHolding().getDisplayLabel();
291 //                        analyticHoldingLabel = holdingsTree.getHoldings().getDisplayLabel();
292                         analyticHoldingFlag = true;
293                         itemId = item.getId();
294                     }
295                 }
296             }
297             if(analyticHoldingFlag == true) {
298                 showSHLink = "<a href='analyticsController?viewId=AnalyticsSummaryView&amp;methodToCall=showAnalyticsSummary&amp;itemId="+ itemId
299                         + "&amp;docType=item&amp' class='analytics' target='_blank'>AH</a>" ;
300 //                holdingsLabelBuffer.append(analyticHoldingLabel);
301                 holdingsLabelBuffer.append(showSHLink);
302             }
303             if (bibUUIDS > 0) {
304                 if ((bibUUIDS > 1)) {
305                     boundWithFlag = true;
306                     linkedBibCount = "BIBS(" + (bibUUIDS) + ")";
307                 }
308                 showBibLink =
309                         "<a href='editorcontroller?viewId=ShowBibView&amp;methodToCall=showBibs&amp;holdingsId="
310                                 + holdingsTree.getHoldings().getId()
311                                 + "&amp;docCategory=work&amp;docType=holdings&amp;docFormat=oleml&amp;editable=true' class='boundWithbibs' target='_blank'>"
312                                 + linkedBibCount + "</a>";
313             }
314             if (boundWithFlag) {
315                 //holdingsLabel = holdingsLabelBuffer + showBibLink;
316                 holdingsLabelBuffer.append(showBibLink);
317             }
318             holdingsLabel = holdingsLabelBuffer.toString();
319         }
320         if(StringUtils.isEmpty(holdingsLabel.trim()))   {
321              holdingsLabel ="Holdings";
322         }
323         return holdingsLabel;
324     }
325 
326 
327     private String getLocationCode(LocationLevel locationLevel) {
328         String locationCode = "";
329         while (locationLevel != null) {
330             String name = locationLevel.getName();
331             if (name != null) {
332                 BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
333                 Map parentCriteria = new HashMap();
334                 parentCriteria.put("locationCode", name);
335                 OleLocation oleLocationCollection = businessObjectService.findByPrimaryKey(OleLocation.class, parentCriteria);
336                 if (oleLocationCollection != null) {
337                     String code = oleLocationCollection.getLocationCode();
338                     if (locationCode.equalsIgnoreCase("")) {
339                         locationCode = code;
340                     } else {
341                         locationCode = locationCode + "/" + code;
342                     }
343                 }
344             }
345             locationLevel = locationLevel.getLocationLevel();
346         }
347         return locationCode;
348     }
349 
350     private void getCallNumberInformation(StringBuffer holdingsData, String prefix, String callNumber) {
351 
352         if (prefix != null) {
353             addDataToLabel(holdingsData, prefix);
354         }
355 
356         if (callNumber != null) {
357             addDataToLabel(holdingsData, callNumber);
358         }
359 
360     }
361 
362 
363     private void addDataToLabel(StringBuffer label, String data) {
364         if (data.length() > 0) {
365             if (label.length() > 0) {
366                 label.append("-");
367             }
368             label.append(data);
369         }
370     }
371 
372 
373     public String validateTitleForLeftPane(String title) {
374         StringBuffer stringBuffer = new StringBuffer();
375         String regex = "<";
376         String replaceString = " &lt; ";
377         Pattern pattern = Pattern.compile(regex);
378         Matcher matcher = pattern.matcher(title);
379         while (matcher.find()) {
380             matcher.appendReplacement(stringBuffer, replaceString);
381         }
382         matcher.appendTail(stringBuffer);
383         return stringBuffer.toString();
384     }
385 
386 }