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.model.bo.*;
8   import org.kuali.ole.docstore.model.enums.DocType;
9   import org.kuali.ole.describe.service.DiscoveryHelperService;
10  import org.kuali.rice.core.api.util.tree.Node;
11  
12  
13  import java.util.*;
14  import java.util.regex.Matcher;
15  import java.util.regex.Pattern;
16  
17  /**
18   * Created with IntelliJ IDEA.
19   * User: pp7788
20   * Date: 12/12/12
21   * Time: 2:30 PM
22   * To change this template use File | Settings | File Templates.
23   */
24  public class EditorFormDataHandler {
25  
26      private static final Logger LOG = Logger.getLogger(EditorFormDataHandler.class);
27  
28      public EditorForm buildLeftPaneData(EditorForm editorForm) {
29          String docId = editorForm.getDocId();
30          List<WorkBibDocument> workBibDocumentList = editorForm.getWorkBibDocumentList();
31  /*
32          if (null == docId) {
33              WorkBibDocument workBibDocument = new WorkBibDocument();
34              //workBibDocument.setId("New Bib");
35              workBibDocument.setTitle("New Bib");
36              workBibDocumentList.add(workBibDocument);
37          }
38          else {
39              String bibUuid = buildBibUuid(editorForm);
40              WorkBibDocument workBibDocument = new WorkBibDocument();
41              workBibDocument.setId(bibUuid);
42              workBibDocumentList.add(workBibDocument);
43              DocstoreHelperService docstoreHelperService = new DocstoreHelperService();
44              workBibDocumentList = docstoreHelperService.getInfoForBibTree(workBibDocumentList);
45          }
46  */
47          /* String treeData = prepareBibTree(editorForm);
48          editorForm.setTreeData(treeData);*/
49  
50          DocumentSelectionTree documentSelectionTree = new DocumentSelectionTree();
51          List<WorkBibDocument> bibDocumentList = editorForm.getWorkBibDocumentList();
52          Collection<String> uuids = null;
53          Map<String, String> uuidList = new HashMap<String, String>();
54          Map<String, String> eInstanceUuidList = new HashMap<String, String>();
55          if (bibDocumentList != null) {
56              for (WorkBibDocument workBibDocument : bibDocumentList) {
57                  editorForm.setBibId(workBibDocument.getId());
58                  if (workBibDocument.getWorkInstanceDocumentList() != null) {
59                      for (WorkInstanceDocument workInstanceDocument : workBibDocument.getWorkInstanceDocumentList()) {
60                          //  if(workInstanceDocument.getInstanceIdentifier()!=null){
61                          uuidList.put(workInstanceDocument.getInstanceIdentifier(), workInstanceDocument.getInstanceIdentifier());
62                          // }
63                      }
64                  }
65                  if (workBibDocument.getWorkEInstanceDocumentList() != null) {
66                      for (WorkEInstanceDocument workEInstanceDocument : workBibDocument.getWorkEInstanceDocumentList()) {
67                          if (workEInstanceDocument.getInstanceIdentifier() != null && workEInstanceDocument.getInstanceIdentifier().length() > 0) {
68                              eInstanceUuidList.put(workEInstanceDocument.getInstanceIdentifier(), workEInstanceDocument.getInstanceIdentifier());
69                          }
70                      }
71                  }
72  
73              }
74          }
75          uuids = uuidList.values();
76          Node<DocumentTreeNode, String> rootNode = null;
77          try {
78              rootNode = documentSelectionTree.add(uuids, DocType.INSTANCE.getCode());
79              if (eInstanceUuidList.size() > 0) {
80                  documentSelectionTree.add(eInstanceUuidList.values(), DocType.EINSTANCE.getCode());
81              }
82          } catch (SolrServerException e) {
83              //e.printStackTrace();
84              LOG.error("buildLeftPaneData Exception:" + e);
85          }
86          //editorForm.setShowLeftTree(true);
87          editorForm.getLeftTree().setRootElement(rootNode);
88          return editorForm;
89      }
90  
91      public String prepareBibTree(EditorForm editorForm) {
92          List<WorkBibDocument> bibDocumentList = editorForm.getWorkBibDocumentList();
93          String editable = editorForm.getEditable();
94          StringBuilder sb = new StringBuilder();
95          String docFormat = editorForm.getDocFormat();
96          if (bibDocumentList != null) {
97              sb.append("<ul id='navigation'>");
98              for (WorkBibDocument workBibDocument : bibDocumentList) {
99                  sb.append("&nbsp;&nbsp;");
100                 if (editable.equals("true") && workBibDocument.getId() != null) {
101                     sb.append(
102                             "<img src='../krms/images/add.png' alt='Add Instance' title='Add Instance' class='addInstance' id='")
103                             .append(workBibDocument.getId() + "_work_instance_oleml").append("'></img>&nbsp;");
104                     sb.append(
105                             "<img src='../krad/images/cancel.png' alt='Delete Bib' title='Delete Bib' class='deleteBib' id='")
106                             .append(workBibDocument.getId() + "_work_bibliographic_marc").append("'></img>");
107                 }
108 
109 /*            sb.append("<li>");
110 //            sb.append("<div id='rightClick'><b>");
111             sb.append("<div align='left' title='View/Edit Bib' class='bibIdentifierClass' id='").append(workBibDocument.getId() + "_work_bibliographic_"+workBibDocument.getDocFormat()+"'");
112             if (workBibDocument.getId() != null && workBibDocument.getId().equals(editorForm.getDocId())) {
113                 sb.append(" style='color:blue'");
114             }
115             sb.append(">");
116             String title = workBibDocument.getTitle();
117             if(title!= null && title.contains("<")){
118                 title = validateTitleForLeftPane(title);
119             }
120           //  sb.append(title);
121             sb.append("&nbsp;&nbsp;");
122             if (editable.equals("true") && workBibDocument.getId() != null) {
123                 sb.append(
124                         "<img src='../krms/images/add.png' alt='Add Instance' title='Add Instance' class='addInstance' id='")
125                         .append(workBibDocument.getId() + "_work_instance_oleml").append("'></img>&nbsp;");
126                 sb.append(
127                         "<img src='../krad/images/cancel.png' alt='Delete Bib' title='Delete Bib' class='deleteBib' id='")
128                         .append(workBibDocument.getId() + "_work_bibliographic_marc").append("'></img>");
129             }
130 
131             sb.append("</div>");*/
132 //            sb.append("</b></div>");
133                 //            sb.append("<li><div class='bibIdentifierClass' id='rightClick'><b>").append(workBibDocument.getTitle())
134                 //                          .append("</b></div>");
135 //            sb.append("<div class='vmenu'>\n");
136 //            sb.append("<div class='add_li'><span>Add Instance</span></div>\n");
137 //            sb.append("<div class='delete_li'><span>Delete Bib</span></div></div>");
138 //            sb.append(
139 //                    "<div id='itmMenu' style='border: 1px solid #aaa; border-left:1px solid #ccc; padding: 10px; display: none; position: absolute; top: 663px; left: 539px; background:#fff;'>");
140 //            sb.append("<table>");
141 //            sb.append("<tr><td class='delete_itm'width='100%' >Delete Item</td></tr> ");
142 //            sb.append("</table>");
143 //            sb.append("</div>");
144                 if (workBibDocument != null) {
145                     List<WorkInstanceDocument> workInstanceDocumentList = workBibDocument.getWorkInstanceDocumentList();
146                     WorkHoldingsDocument workHoldingsDocument = new WorkHoldingsDocument();
147                     List<WorkItemDocument> workItemDocumentList = new ArrayList<WorkItemDocument>();
148                     if (null != workInstanceDocumentList && workInstanceDocumentList.size() > 0) {
149                         sb.append("<ul>");
150                         for (WorkInstanceDocument workInstanceDocument : workInstanceDocumentList) {
151                             workHoldingsDocument = workInstanceDocument.getHoldingsDocument();
152                             if (workHoldingsDocument != null) {
153                                 String holdingIdentifier = workHoldingsDocument.getHoldingsIdentifier()
154                                         + "_work_holdings_oleml_" + workBibDocument.getId();
155                                 String holdingData = getHoldingsLabel(workHoldingsDocument);
156 
157                                 sb.append("<li><div align='left' title='View/Edit Holdings' class='holdingIdentifierClass' id='")
158                                         .append(holdingIdentifier).append("'");
159                                 if (workHoldingsDocument.getHoldingsIdentifier() != null && workHoldingsDocument.getHoldingsIdentifier().equals(editorForm.getDocId())) {
160                                     sb.append(" style='color:blue'");
161                                 }
162 
163                                 sb.append(">").append(holdingData);
164                                 sb.append("&nbsp;&nbsp;");
165                                 if ((editable.equals("true") && (workInstanceDocument.getInstanceIdentifier() != null))) {
166                                     sb.append(
167                                             "<img src='../krms/images/add.png' alt='Add Item' title='Add Item' class='addItem' id='")
168                                             .append(workInstanceDocument.getInstanceIdentifier() + "_work_item_oleml_"
169                                                     + workBibDocument.getId()).append("'></img>");
170                                     sb.append(
171                                             "<img src='../krad/images/cancel.png' alt='Delete Instance' title='Delete Instance' class='deleteBib' id='")
172                                             .append(workInstanceDocument.getInstanceIdentifier() + "_work_instance_oleml" + "_"
173                                                     + workBibDocument.getId()).append("'></img>");
174                                 }
175                                 sb.append("</div>");
176                                 sb.append("<ul>");
177                             }
178                             workItemDocumentList = workInstanceDocument.getItemDocumentList();
179                             if (workItemDocumentList != null) {
180                                 for (int i = 0; i < workItemDocumentList.size(); i++) {
181                                     WorkItemDocument workItemDocument = workItemDocumentList.get(i);
182                                     if (workItemDocument != null) {
183                                         String itemIdentifier = workItemDocument.getItemIdentifier() + "_work_item_oleml_"
184                                                 + workInstanceDocument.getInstanceIdentifier() + "_"
185                                                 + workBibDocument.getId();
186                                         String itemLevelContent = getItemLabel(workHoldingsDocument, workItemDocument);
187 
188                                         sb.append("<li><div align='left' title='View/Edit Item' class='itemIdentifierClass' id='")
189                                                 .append(itemIdentifier).append("'");
190                                         if (workItemDocument.getItemIdentifier() != null && workItemDocument.getItemIdentifier().equals(editorForm.getDocId())) {
191                                             sb.append(" style='color:blue'");
192                                         }
193                                         sb.append(">").append(itemLevelContent);
194                                         sb.append("&nbsp;&nbsp;");
195                                         if ((editable.equals("true")) && (workItemDocument.getItemIdentifier() != null)) {
196                                             sb.append(
197                                                     "<img src='../krad/images/cancel.png' alt='Add Item' title='Delete Item' class='deleteBib' id='")
198                                                     .append(workItemDocument.getItemIdentifier() + "_work_item_oleml" + "_"
199                                                             + workBibDocument.getId()).append("'></img>").append("</div></li>");
200                                         }
201 
202                                     }
203                                 }
204                             }
205                             sb.append("</ul></li>");
206                         }
207                         // sb.append("</ul></li>");
208                     } else {
209                         sb.append("</li>");
210                     }
211                 }
212             }
213             sb.append("</ul>");
214         }
215         return sb.toString();
216     }
217 
218     public String getItemLabel(WorkHoldingsDocument workHoldingsDocument, WorkItemDocument workItemDocument) {
219 
220         String itemLabel = "Item";
221         StringBuffer itemLabelBuffer = new StringBuffer();
222         StringBuffer itemLabelBufferForStaffOnly = new StringBuffer();
223         StringBuffer holdingsCallNumberInformation = new StringBuffer();
224         StringBuffer itemCallNumberInformation = new StringBuffer();
225         //prefix+callNumber for holdings
226         if (workHoldingsDocument != null) {
227             getCallNumberInformation(holdingsCallNumberInformation, workHoldingsDocument.getCallNumberPrefix(),
228                     workHoldingsDocument.getCallNumber());
229         }
230 
231         //prefix+callNumber for item
232         getCallNumberInformation(itemCallNumberInformation, workItemDocument.getCallNumberPrefix(),
233                 workItemDocument.getCallNumber());
234 
235         if (workItemDocument.getLocation() != null) {
236             if ((workHoldingsDocument != null) && (workHoldingsDocument.getLocationName() != null)) {
237                 if (!workItemDocument.getLocation().equalsIgnoreCase(workHoldingsDocument.getLocationName())) {
238                     addDataToLabel(itemLabelBuffer, workItemDocument.getLocation());
239                 }
240             } else {
241                 addDataToLabel(itemLabelBuffer, workItemDocument.getLocation());
242             }
243         }
244 
245         if (!itemCallNumberInformation.toString().equalsIgnoreCase(holdingsCallNumberInformation.toString())) {
246             if (itemLabelBuffer.length() > 0) {
247                 itemLabelBuffer.append("-");
248             }
249             itemLabelBuffer.append(itemCallNumberInformation.toString());
250         }
251 
252         if (workItemDocument.getEnumeration() != null) {
253             addDataToLabel(itemLabelBuffer, workItemDocument.getEnumeration());
254         }
255         if (workItemDocument.getChronology() != null) {
256             addDataToLabel(itemLabelBuffer, workItemDocument.getChronology());
257         }
258         if (workItemDocument.getCopyNumber() != null) {
259             addDataToLabel(itemLabelBuffer, workItemDocument.getCopyNumber());
260         }
261         if (workItemDocument.getBarcode() != null) {
262             addDataToLabel(itemLabelBuffer, workItemDocument.getBarcode());
263         }
264 
265         if (itemLabelBuffer.length() > 0) {
266 
267             if (workItemDocument.getStaffOnlyFlag() != null && workItemDocument.getStaffOnlyFlag().equals("true")) {
268                 itemLabelBufferForStaffOnly.append("<i><font color='red'>");
269                 itemLabelBufferForStaffOnly.append(itemLabelBuffer);
270                 itemLabelBufferForStaffOnly.append("</font></i>");
271             } else {
272                 itemLabelBufferForStaffOnly.append(itemLabelBuffer);
273             }
274             itemLabel = itemLabelBufferForStaffOnly.toString();
275         }
276 
277         return itemLabel;
278     }
279 
280 
281     public String getHoldingsLabel(WorkHoldingsDocument workHoldingsDocument) {
282         StringBuffer holdingsLabelBuffer = new StringBuffer();
283         StringBuffer holdingsLabelBufferForStaffOnly = new StringBuffer();
284         String staffOnlyFlag = workHoldingsDocument.getStaffOnlyFlag();
285         String holdingsLabel = "Holdings";
286         String linkedBibCount = "";
287         String showBibLink = null;
288         if (workHoldingsDocument.getLocationName() != null) {
289             addDataToLabel(holdingsLabelBuffer, workHoldingsDocument.getLocationName());
290         }
291 
292         getCallNumberInformation(holdingsLabelBuffer, workHoldingsDocument.getCallNumberPrefix(),
293                 workHoldingsDocument.getCallNumber());
294 
295         if (workHoldingsDocument.getCopyNumber() != null) {
296             addDataToLabel(holdingsLabelBuffer, workHoldingsDocument.getCopyNumber());
297         }
298         boolean boundWithFlag = false;
299 
300         if (holdingsLabelBuffer.length() > 0) {
301             List<String> bibUUIDList = workHoldingsDocument.getBibUUIDList();
302 
303             if (bibUUIDList != null) {
304                 if ((bibUUIDList.size() > 1)) {
305                     boundWithFlag = true;
306                     linkedBibCount = "<b>BIBS(" + (bibUUIDList.size()) + ")</b>";
307                 } else if ((bibUUIDList.size() == 1)) {
308                     linkedBibCount = "NO MORE BIBS";
309                 }
310 
311                 showBibLink = "<a class='boundWithbibs' style='width:50px;text-decoration:none;display:block;margin-left:25px;padding-top:0px; padding-bottom:0px; padding-right:0px; padding-left:0px;' target='_blank' id ='linkedBibs'" +
312                         " href='editorcontroller?viewId=ShowBibView&amp;methodToCall=showBibs&amp;instanceId="
313                         + workHoldingsDocument.getInstanceIdentifier()
314                         + "&amp;docCategory=work&amp;docType=holdings&amp;docFormat=oleml&amp;editable=true'>"
315                         + linkedBibCount + "</a>";
316             }
317             if (boundWithFlag) {
318                 if (staffOnlyFlag != null && "true".equals(staffOnlyFlag)) {
319                     holdingsLabelBufferForStaffOnly.append("<i><font color='red'>");
320                     holdingsLabelBufferForStaffOnly.append(holdingsLabelBuffer);
321                     holdingsLabelBufferForStaffOnly.append("</font></i>");
322                 } else {
323                     holdingsLabelBufferForStaffOnly.append(holdingsLabelBuffer);
324                 }
325                 holdingsLabel = holdingsLabelBufferForStaffOnly.toString() + showBibLink;
326             } else {
327                 if (staffOnlyFlag != null && "true".equals(staffOnlyFlag)) {
328                     holdingsLabelBufferForStaffOnly.append("<i><font color='red'>");
329                     holdingsLabelBufferForStaffOnly.append(holdingsLabelBuffer);
330                     holdingsLabelBufferForStaffOnly.append("</font></i>");
331                 } else {
332                     holdingsLabelBufferForStaffOnly.append(holdingsLabelBuffer);
333                 }
334 
335                 holdingsLabel = holdingsLabelBufferForStaffOnly.toString();
336             }
337         }
338 
339         return holdingsLabel;
340     }
341 
342     private void getCallNumberInformation(StringBuffer holdingsData, String prefix, String callNumber) {
343 
344         if (prefix != null) {
345             addDataToLabel(holdingsData, prefix);
346         }
347 
348         if (callNumber != null) {
349             addDataToLabel(holdingsData, callNumber);
350         }
351 
352     }
353 
354 
355     private void addDataToLabel(StringBuffer label, String data) {
356         if (data.length() > 0) {
357             if (label.length() > 0) {
358                 label.append("-");
359             }
360             label.append(data);
361         }
362     }
363 
364 
365     private String buildBibUuid(EditorForm editorForm) {
366         String docType = editorForm.getDocType();
367         String bibUuid = null;
368         if (!docType.equalsIgnoreCase("bibliographic")) {
369             List solrResponse = new DiscoveryHelperService().getResponseFromSOLR("id", editorForm.getUuid());
370             for (Iterator iterator = solrResponse.iterator(); iterator.hasNext(); ) {
371                 Map map = (Map) iterator.next();
372                 if (map.containsKey("bibIdentifier")) {
373                     List list = (List) map.get("bibIdentifier");
374                     bibUuid = list != null & list.size() > 0 ? (String) list.get(0) : null;
375                     break;
376                 }
377             }
378         } else {
379             bibUuid = editorForm.getUuid();
380         }
381         LOG.info("bib uuid-->" + bibUuid);
382         return bibUuid;
383     }
384 
385     public String validateTitleForLeftPane(String title) {
386         StringBuffer stringBuffer = new StringBuffer();
387         String regex = "<";
388         String replaceString = " &lt; ";
389         Pattern pattern = Pattern.compile(regex);
390         Matcher matcher = pattern.matcher(title);
391         while (matcher.find()) {
392             matcher.appendReplacement(stringBuffer, replaceString);
393         }
394         matcher.appendTail(stringBuffer);
395         return stringBuffer.toString();
396     }
397 
398 }