1 40 package org.dspace.app.webui.jsptag; 41 42 import org.dspace.app.webui.util.UIUtil; 43 44 import org.dspace.authorize.AuthorizeManager; 45 46 import org.dspace.content.Bitstream; 47 import org.dspace.content.Bundle; 48 import org.dspace.content.DCDate; 49 import org.dspace.content.DCValue; 50 import org.dspace.content.Item; 51 52 import org.dspace.core.ConfigurationManager; 53 import org.dspace.core.Constants; 54 import org.dspace.core.Context; 55 import org.dspace.core.Utils; 56 57 import org.dspace.storage.bitstore.BitstreamStorageManager; 58 59 import java.awt.image.BufferedImage ; 60 61 import java.io.IOException ; 62 import java.io.InputStream ; 63 import java.io.UnsupportedEncodingException ; 64 65 import java.sql.SQLException ; 66 import java.util.StringTokenizer ; 67 68 import javax.imageio.ImageIO ; 69 70 import javax.servlet.http.HttpServletRequest ; 71 import javax.servlet.jsp.JspException ; 72 import javax.servlet.jsp.JspWriter ; 73 import javax.servlet.jsp.jstl.fmt.LocaleSupport; 74 import javax.servlet.jsp.tagext.TagSupport ; 75 76 82 public class ItemListTag extends TagSupport 83 { 84 85 private Item[] items; 86 87 88 private int highlightRow = -1; 89 90 91 private String emphColumn; 92 93 94 private boolean showThumbs; 95 96 97 private int thumbItemListMaxWidth; 98 99 private int thumbItemListMaxHeight; 100 101 102 private boolean linkToBitstream = false; 103 104 105 private static String listFields = "dc.date.issued(date), dc.title, dc.contributor.*"; 106 107 108 private static String dateField = "dc.date.issued"; 109 110 111 private static String titleField = "dc.title"; 112 113 public ItemListTag() 114 { 115 super(); 116 getThumbSettings(); 117 } 118 119 public int doStartTag() throws JspException 120 { 121 JspWriter out = pageContext.getOut(); 122 HttpServletRequest hrq = (HttpServletRequest ) pageContext.getRequest(); 123 124 boolean emphasiseDate = false; 125 boolean emphasiseTitle = false; 126 127 if (emphColumn != null) 128 { 129 emphasiseDate = emphColumn.equalsIgnoreCase("date"); 130 emphasiseTitle = emphColumn.equalsIgnoreCase("title"); 131 } 132 133 String configLine = ConfigurationManager.getProperty("webui.itemlist.columns"); 135 if (configLine != null) 136 { 137 listFields = configLine; 138 } 139 140 String dateLine = ConfigurationManager.getProperty("webui.browse.index.date"); 142 if (dateLine != null) 143 { 144 dateField = dateLine; 145 } 146 147 String titleLine = ConfigurationManager.getProperty("webui.browse.index.title"); 148 if (titleLine != null) 149 { 150 titleField = titleLine; 151 } 152 153 StringTokenizer st = new StringTokenizer (listFields, ","); 154 155 int columns = st.countTokens(); 157 String [] frags = new String [columns * items.length]; 158 159 try 160 { 161 out.println("<table align=\"center\" class=\"miscTable\" summary=\"This table browse all dspace content\">"); 162 out.println("<tr>"); 163 164 int colCount = 1; 166 boolean isDate = false; 167 boolean emph = false; 168 169 while (st.hasMoreTokens()) 170 { 171 String field = st.nextToken().toLowerCase().trim(); 172 String cOddOrEven = ((colCount % 2) == 0 ? "Odd" : "Even"); 173 174 if (field.indexOf("(date)") > 0) 176 { 177 field = field.replaceAll("\\(date\\)", ""); 178 isDate = true; 179 } 180 181 StringTokenizer eq = new StringTokenizer (field, "."); 188 189 String [] tokens = { "", "", "" }; 190 int k = 0; 191 while(eq.hasMoreTokens()) 192 { 193 tokens[k] = eq.nextToken().toLowerCase().trim(); 194 k++; 195 } 196 String schema = tokens[0]; 197 String element = tokens[1]; 198 String qualifier = tokens[2]; 199 200 if ((field.equals(dateField) && emphasiseDate) || 202 (field.equals(titleField) && emphasiseTitle)) 203 { 204 emph = true; 205 } 206 207 String id = "t" + Integer.toString(colCount); 209 String css = "oddRow" + cOddOrEven + "Col"; 210 String message = "itemlist." + field; 211 212 out.print("<th id=\"" + id + "\" class=\"" + css + "\">" 214 + (emph ? "<strong>" : "") 215 + LocaleSupport.getLocalizedMessage(pageContext, message) 216 + (emph ? "</strong>" : "") + "</th>"); 217 218 for (int i = 0; i < items.length; i++) 220 { 221 DCValue[] metadataArray; 223 if (qualifier.equals("*")) 224 { 225 metadataArray = items[i].getMetadata(schema, element, Item.ANY, Item.ANY); 226 } 227 else if (qualifier.equals("")) 228 { 229 metadataArray = items[i].getMetadata(schema, element, null, Item.ANY); 230 } 231 else 232 { 233 metadataArray = items[i].getMetadata(schema, element, qualifier, Item.ANY); 234 } 235 236 String metadata = "-"; 238 if (metadataArray.length > 0) 239 { 240 if (isDate) 242 { 243 String thumbs = ""; 246 if (showThumbs) 247 { 248 thumbs = getThumbMarkup(hrq, items[i]); 249 } 250 DCDate dd = new DCDate(metadataArray[0].value); 251 metadata = UIUtil.displayDate(dd, false, false) + thumbs; 252 } 253 else if (field.equals(titleField)) 255 { 256 metadata = "<a HREF=\"" + hrq.getContextPath() + "/handle/" 257 + items[i].getHandle() + "\">" 258 + Utils.addEntities(metadataArray[0].value) 259 + "</a>"; 260 } 261 else 263 { 264 StringBuffer sb = new StringBuffer (); 265 for (int j = 0; j < metadataArray.length; j++) 266 { 267 sb.append(Utils.addEntities(metadataArray[j].value)); 268 if (j < (metadataArray.length - 1)) 269 { 270 sb.append("; "); 271 } 272 } 273 metadata = "<em>" + sb.toString() + "</em>"; 274 } 275 } 276 277 String rOddOrEven; 279 if (i == highlightRow) 280 { 281 rOddOrEven = "highlight"; 282 } 283 else 284 { 285 rOddOrEven = ((i % 2) == 1 ? "odd" : "even"); 286 } 287 288 String extras = ""; 290 if (isDate) 291 { 292 extras = "nowrap=\"nowrap\" align=\"right\""; 293 } 294 295 int idx = ((i + 1) * columns) - columns + colCount - 1; 296 frags[idx] = "<td headers=\"" + id + "\" class=\"" 297 + rOddOrEven + "Row" + cOddOrEven + "Col\" " + extras + ">" 298 + (emph ? "<strong>" : "") + metadata + (emph ? "</strong>" : "") 299 + "</td>"; 300 301 } 302 303 colCount++; 304 isDate = false; 305 emph = false; 306 } 307 308 out.println("</tr>"); 309 310 for (int i = 0; i < frags.length; i++) 312 { 313 if ((i + 1) % columns == 1) 314 { 315 out.println("<tr>"); 316 } 317 out.println(frags[i]); 318 if ((i + 1) % columns == 0) 319 { 320 out.println("</tr>"); 321 } 322 } 323 324 out.println("</table>"); 326 } 327 catch (IOException ie) 328 { 329 throw new JspException (ie); 330 } 331 332 return SKIP_BODY; 333 } 334 335 340 public Item[] getItems() 341 { 342 return items; 343 } 344 345 351 public void setItems(Item[] itemsIn) 352 { 353 items = itemsIn; 354 } 355 356 361 public String getHighlightrow() 362 { 363 return String.valueOf(highlightRow); 364 } 365 366 372 public void setHighlightrow(String highlightRowIn) 373 { 374 if ((highlightRowIn == null) || highlightRowIn.equals("")) 375 { 376 highlightRow = -1; 377 } 378 else 379 { 380 try 381 { 382 highlightRow = Integer.parseInt(highlightRowIn); 383 } 384 catch (NumberFormatException nfe) 385 { 386 highlightRow = -1; 387 } 388 } 389 } 390 391 396 public String getEmphcolumn() 397 { 398 return emphColumn; 399 } 400 401 407 public void setEmphcolumn(String emphColumnIn) 408 { 409 emphColumn = emphColumnIn; 410 } 411 412 public void release() 413 { 414 highlightRow = -1; 415 emphColumn = null; 416 items = null; 417 } 418 419 420 private void getThumbSettings() 421 { 422 showThumbs = ConfigurationManager 423 .getBooleanProperty("webui.browse.thumbnail.show"); 424 425 if (showThumbs) 426 { 427 thumbItemListMaxHeight = ConfigurationManager 428 .getIntProperty("webui.browse.thumbnail.maxheight"); 429 430 if (thumbItemListMaxHeight == 0) 431 { 432 thumbItemListMaxHeight = ConfigurationManager 433 .getIntProperty("thumbnail.maxheight"); 434 } 435 436 thumbItemListMaxWidth = ConfigurationManager 437 .getIntProperty("webui.browse.thumbnail.maxwidth"); 438 439 if (thumbItemListMaxWidth == 0) 440 { 441 thumbItemListMaxWidth = ConfigurationManager 442 .getIntProperty("thumbnail.maxwidth"); 443 } 444 } 445 446 String linkBehaviour = ConfigurationManager 447 .getProperty("webui.browse.thumbnail.linkbehaviour"); 448 449 if (linkBehaviour != null) 450 { 451 if (linkBehaviour.equals("bitstream")) 452 { 453 linkToBitstream = true; 454 } 455 } 456 } 457 458 465 private String getScalingAttr(HttpServletRequest hrq, Bitstream bitstream) 466 throws JspException 467 { 468 BufferedImage buf; 469 470 try 471 { 472 Context c = UIUtil.obtainContext(hrq); 473 474 InputStream is = BitstreamStorageManager.retrieve(c, bitstream 475 .getID()); 476 477 buf = ImageIO.read(is); 480 is.close(); 481 } 482 catch (SQLException sqle) 483 { 484 throw new JspException (sqle.getMessage()); 485 } 486 catch (IOException ioe) 487 { 488 throw new JspException (ioe.getMessage()); 489 } 490 491 float xsize = (float) buf.getWidth(null); 493 float ysize = (float) buf.getHeight(null); 494 495 if (xsize > (float) thumbItemListMaxWidth) 497 { 498 float scale_factor = (float) thumbItemListMaxWidth / xsize; 500 501 xsize = xsize * scale_factor; 503 ysize = ysize * scale_factor; 504 } 505 506 if (ysize > (float) thumbItemListMaxHeight) 508 { 509 float scale_factor = (float) thumbItemListMaxHeight / ysize; 510 511 xsize = xsize * scale_factor; 514 ysize = ysize * scale_factor; 515 } 516 517 StringBuffer sb = new StringBuffer ("width=\"").append(xsize).append( 518 "\" height=\"").append(ysize).append("\""); 519 520 return sb.toString(); 521 } 522 523 524 private String getThumbMarkup(HttpServletRequest hrq, Item item) 525 throws JspException 526 { 527 Bundle[] original = null; 528 529 try 530 { 531 original = item.getBundles("ORIGINAL"); 532 } 533 catch(SQLException sqle) 534 { 535 throw new JspException (sqle.getMessage()); 536 } 537 538 if (original.length == 0) 539 { 540 return ""; 541 } 542 543 boolean html = false; 544 545 if (original[0].getBitstreams().length > 1) 547 { 548 Bitstream[] bitstreams = original[0].getBitstreams(); 549 550 for (int i = 0; (i < bitstreams.length) && !html; i++) 551 { 552 if (bitstreams[i].getID() == original[0] 553 .getPrimaryBitstreamID()) 554 { 555 html = bitstreams[i].getFormat().getMIMEType().equals( 556 "text/html"); 557 } 558 } 559 } 560 561 try 562 { 563 Bundle[] thumbs = item.getBundles("THUMBNAIL"); 564 565 if ((thumbs.length > 0) && !html) 568 { 569 Context c = UIUtil.obtainContext(hrq); 570 571 Bitstream thumbnailBitstream; 572 Bitstream originalBitstream; 573 574 if ((original[0].getBitstreams().length > 1) 575 && (original[0].getPrimaryBitstreamID() > -1)) 576 { 577 originalBitstream = Bitstream.find(c, original[0] 578 .getPrimaryBitstreamID()); 579 thumbnailBitstream = thumbs[0] 580 .getBitstreamByName(originalBitstream.getName() 581 + ".jpg"); 582 } 583 else 584 { 585 originalBitstream = original[0].getBitstreams()[0]; 586 thumbnailBitstream = thumbs[0].getBitstreams()[0]; 587 } 588 589 if ((thumbnailBitstream != null) 590 && (AuthorizeManager.authorizeActionBoolean(c, 591 thumbnailBitstream, Constants.READ))) 592 { 593 StringBuffer thumbLink; 594 595 if (linkToBitstream) 596 { 597 thumbLink = new StringBuffer ( 598 "<br/><a target=\"_blank\" HREF=\"").append( 599 hrq.getContextPath()).append("/bitstream/") 600 .append(item.getHandle()).append("/").append( 601 originalBitstream.getSequenceID()) 602 .append("/").append( 603 UIUtil.encodeBitstreamName(originalBitstream 604 .getName(), 605 Constants.DEFAULT_ENCODING)); 606 } 607 else 608 { 609 thumbLink = new StringBuffer ("<br/><a HREF=\"").append( 610 hrq.getContextPath()).append("/handle/") 611 .append(item.getHandle()); 612 } 613 614 thumbLink.append("\"><img SRC=\"").append( 615 hrq.getContextPath()).append("/retrieve/").append( 616 thumbnailBitstream.getID()).append("/").append( 617 UIUtil.encodeBitstreamName(thumbnailBitstream.getName(), 618 Constants.DEFAULT_ENCODING)).append( 619 "\" alt=\"").append(thumbnailBitstream.getName()) 620 .append("\" ").append( 621 getScalingAttr(hrq, thumbnailBitstream)) 622 .append("/></a>"); 623 624 return thumbLink.toString(); 625 } 626 } 627 } 628 catch (SQLException sqle) 629 { 630 throw new JspException (sqle.getMessage()); 631 } 632 catch (UnsupportedEncodingException e) 633 { 634 throw new JspException ( 635 "Server does not support DSpace's default encoding. ", 636 e); 637 } 638 639 return ""; 640 } 641 } 642 | Popular Tags |