001package org.kuali.ole.describe.bo; 002 003import org.apache.commons.lang.StringUtils; 004import org.apache.log4j.Logger; 005import org.apache.solr.client.solrj.SolrServerException; 006import org.kuali.ole.describe.form.EditorForm; 007import org.kuali.ole.docstore.common.document.BibTree; 008import org.kuali.ole.docstore.common.document.Holdings; 009import org.kuali.ole.docstore.common.document.HoldingsTree; 010import org.kuali.ole.docstore.common.document.Item; 011import org.kuali.ole.docstore.common.document.content.instance.LocationLevel; 012import org.kuali.ole.docstore.common.document.content.instance.OleHoldings; 013import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor; 014import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor; 015import org.kuali.ole.docstore.model.enums.DocType; 016import org.kuali.rice.core.api.util.tree.Node; 017import org.kuali.rice.krad.service.BusinessObjectService; 018import org.kuali.rice.krad.service.KRADServiceLocator; 019import org.kuali.ole.docstore.common.document.EHoldings; 020 021import java.util.*; 022import java.util.regex.Matcher; 023import java.util.regex.Pattern; 024 025/** 026 * Created with IntelliJ IDEA. 027 * User: pp7788 028 * Date: 12/12/12 029 * Time: 2:30 PM 030 * To change this template use File | Settings | File Templates. 031 */ 032public class EditorFormDataHandler { 033 034 private static final Logger LOG = Logger.getLogger(EditorFormDataHandler.class); 035 036 public EditorForm buildLeftPaneData(EditorForm editorForm) { 037 DocumentSelectionTree documentSelectionTree = new DocumentSelectionTree(); 038 List<BibTree> bibTreeList = editorForm.getDocumentForm().getBibTreeList(); 039 if (bibTreeList == null) { 040 bibTreeList = editorForm.getBibTreeList(); 041 } 042 Collection<String> uuids = null; 043 List<HoldingsTree> holdingsTrees = new ArrayList<HoldingsTree>(); 044 List<HoldingsTree> eHoldingsTrees = new ArrayList<HoldingsTree>(); 045 046 if (bibTreeList != null) { 047 for (BibTree bibTree : bibTreeList) { 048 if (bibTree != null && bibTree.getBib() != null) { 049 editorForm.setBibId(bibTree.getBib().getId()); 050 if (bibTree.getHoldingsTrees() != null) { 051 Collections.sort(bibTree.getHoldingsTrees()); 052 for (HoldingsTree holdingsTree : bibTree.getHoldingsTrees()) { 053 Collections.sort(holdingsTree.getItems()); 054 if (holdingsTree.getHoldings().getId() != null) { 055 Holdings holdings = holdingsTree.getHoldings(); 056 if (holdings instanceof EHoldings) { 057 eHoldingsTrees.add(holdingsTree); 058 } else { 059 holdingsTrees.add(holdingsTree); 060 } 061 } 062 } 063 } 064 } 065 } 066 } 067 //uuids = uuidList.values(); 068 Node<DocumentTreeNode, String> rootNode = null; 069 try { 070 rootNode = documentSelectionTree.addHoldingsTree(holdingsTrees, DocType.HOLDINGS.getCode()); 071 072 if (eHoldingsTrees.size() > 0) { 073 documentSelectionTree.addHoldingsTree(eHoldingsTrees, DocType.EHOLDINGS.getCode()); 074 } 075 } catch (SolrServerException e) { 076 LOG.error("buildLeftPaneData Exception:" + e); 077 } 078 editorForm.getLeftTree().setRootElement(rootNode); 079 return editorForm; 080 } 081 082 083 public String prepareBibTree(EditorForm editorForm) { 084 085 List<BibTree> bibTreeList = editorForm.getBibTreeList(); 086 String editable = editorForm.getEditable(); 087 StringBuilder sb = new StringBuilder(); 088 String docFormat = editorForm.getDocFormat(); 089 if (bibTreeList != null) { 090 sb.append("<ul id='navigation'>"); 091 for (BibTree bibTree : bibTreeList) { 092 sb.append(" "); 093 if (editable.equals("true") && bibTree.getId() != null) { 094 sb.append( 095 "<img src='../krms/images/add.png' alt='Add Holdings' title='Add Holdings' class='addInstance' id='") 096 .append(bibTree.getId() + "_work_instance_oleml").append("'></img> "); 097 sb.append( 098 "<img src='../krad/images/cancel.png' alt='Delete Bib' title='Delete Bib' class='deleteBib' id='") 099 .append(bibTree.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.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(" "); 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.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.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.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(" "); 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&methodToCall=showAnalyticsSummary&itemId="+ item.getId() 221 + "&docType=item&' 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&methodToCall=showAnalyticsSummary&holdingsId="+ holdingsTree.getHoldings().getId() 279 + "&docType=holdings&' 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&methodToCall=showAnalyticsSummary&itemId="+ itemId 299 + "&docType=item&' 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&methodToCall=showBibs&holdingsId=" 310 + holdingsTree.getHoldings().getId() 311 + "&docCategory=work&docType=holdings&docFormat=oleml&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 = " < "; 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}