1 40 package org.dspace.app.webui.servlet; 41 42 import java.io.IOException ; 43 import java.net.URLEncoder ; 44 import java.sql.SQLException ; 45 46 import javax.servlet.ServletException ; 47 import javax.servlet.http.HttpServletRequest ; 48 import javax.servlet.http.HttpServletResponse ; 49 50 import org.apache.log4j.Logger; 51 import org.dspace.app.webui.util.JSPManager; 52 import org.dspace.app.webui.util.UIUtil; 53 import org.dspace.authorize.AuthorizeException; 54 import org.dspace.browse.Browse; 55 import org.dspace.browse.BrowseInfo; 56 import org.dspace.browse.BrowseScope; 57 import org.dspace.content.Collection; 58 import org.dspace.content.Community; 59 import org.dspace.content.Item; 60 import org.dspace.core.Constants; 61 import org.dspace.core.Context; 62 import org.dspace.core.LogManager; 63 import org.dspace.handle.HandleManager; 64 65 82 public class BrowseServlet extends DSpaceServlet 83 { 84 85 private static Logger log = Logger.getLogger(BrowseServlet.class); 86 87 88 private boolean browseAuthors; 89 90 91 private boolean browseTitles; 92 93 94 private boolean browseDates; 95 96 97 private boolean browseSubjects; 98 99 public void init() 100 { 101 String browseWhat = getInitParameter("browse"); 103 104 browseAuthors = ((browseWhat != null) && browseWhat 105 .equalsIgnoreCase("authors")); 106 browseDates = ((browseWhat != null) && browseWhat 107 .equalsIgnoreCase("dates")); 108 browseSubjects = ((browseWhat != null) && browseWhat 109 .equalsIgnoreCase("subjects")); 110 111 browseTitles = ((!browseAuthors && !browseDates)&& !browseSubjects ); 112 } 113 114 protected void doDSGet(Context context, HttpServletRequest request, 115 HttpServletResponse response) throws ServletException , IOException , 116 SQLException , AuthorizeException 117 { 118 BrowseScope scope = new BrowseScope(context); 120 121 boolean highlight = false; 123 124 String logInfo = ""; 126 127 String flipOrderingQuery = ""; 131 132 String focus = request.getParameter("focus"); 134 String startsWith = request.getParameter("starts_with"); 135 String top = request.getParameter("top"); 136 String bottom = request.getParameter("bottom"); 137 138 String month = request.getParameter("month"); 140 String year = request.getParameter("year"); 141 String order = request.getParameter("order"); 142 143 boolean oldestFirst = false; 145 146 if ((order != null) && order.equalsIgnoreCase("oldestfirst")) 147 { 148 oldestFirst = true; 149 } 150 151 if (browseDates && (year != null) && !year.equals("") 152 && ((startsWith == null) || startsWith.equals(""))) 153 { 154 startsWith = year; 159 160 if ((month != null) & !month.equals("-1")) 161 { 162 if (month.length() == 1) 164 { 165 month = "0" + month; 167 } 168 169 startsWith = year + "-" + month; 170 } 171 } 172 173 if (focus != null) 175 { 176 if (browseAuthors||browseSubjects) 180 { 181 scope.setFocus(focus); 183 } 184 else 185 { 186 Item item = (Item) HandleManager 188 .resolveToObject(context, focus); 189 190 if (item == null) 191 { 192 JSPManager.showInvalidIDError(request, response, focus, 194 Constants.ITEM); 195 196 return; 197 } 198 199 scope.setFocus(item); 200 } 201 202 highlight = true; 204 205 logInfo = "focus=" + focus + ","; 206 207 if (browseDates) 208 { 209 flipOrderingQuery = "focus=" 211 + URLEncoder.encode(focus, Constants.DEFAULT_ENCODING) 212 + "&"; 213 } 214 } 215 else if (startsWith != null) 216 { 217 if (browseDates) 221 { 222 flipOrderingQuery = "starts_with=" 224 + URLEncoder.encode(startsWith, 225 Constants.DEFAULT_ENCODING) + "&"; 226 227 249 if (!oldestFirst) 250 { 251 startsWith = startsWith + "-32"; 252 } 253 } 254 255 scope.setFocus(startsWith); 256 highlight = true; 257 logInfo = "starts_with=" + startsWith + ","; 258 } 259 else if ((top != null) || (bottom != null)) 260 { 261 String val = bottom; 266 boolean isTop = false; 267 268 if (top != null) 269 { 270 val = top; 271 isTop = true; 272 } 273 274 if (browseAuthors || browseSubjects) 275 { 276 scope.setFocus(val); 278 } 279 else 280 { 281 Item item = (Item) HandleManager.resolveToObject(context, val); 283 284 if (item == null) 285 { 286 JSPManager.showInvalidIDError(request, response, focus, 288 Constants.ITEM); 289 290 return; 291 } 292 293 scope.setFocus(item); 294 } 295 296 scope.setNumberBefore(isTop ? 0 : 20); 299 300 logInfo = (isTop ? "top" : "bottom") + "=" + val + ","; 301 302 if (browseDates) 303 { 304 if (top != null) 308 { 309 flipOrderingQuery = "bottom=" 310 + URLEncoder 311 .encode(top, Constants.DEFAULT_ENCODING) 312 + "&"; 313 } 314 else 315 { 316 flipOrderingQuery = "top=" 317 + URLEncoder.encode(bottom, 318 Constants.DEFAULT_ENCODING) + "&"; 319 } 320 } 321 } 322 323 Community community = UIUtil.getCommunityLocation(request); 329 Collection collection = UIUtil.getCollectionLocation(request); 330 331 if (collection != null) 332 { 333 logInfo = logInfo + ",collection_id=" + collection.getID() + ","; 334 scope.setScope(collection); 335 } 336 else if (community != null) 337 { 338 logInfo = logInfo + ",community_id=" + community.getID() + ","; 339 scope.setScope(community); 340 } 341 342 BrowseInfo browseInfo; 343 344 try 345 { 346 if (browseAuthors) 348 { 349 browseInfo = Browse.getAuthors(scope); 350 } 351 else if (browseDates) 352 { 353 browseInfo = Browse.getItemsByDate(scope, oldestFirst); 354 } 355 else if (browseSubjects) 356 { 357 browseInfo = Browse.getSubjects(scope); 358 } 359 else 360 { 361 browseInfo = Browse.getItemsByTitle(scope); 362 } 363 } 364 catch (SQLException sqle) 365 { 366 JSPManager.showIntegrityError(request, response); 368 return; 369 } 370 371 String what = "title"; 373 374 if (browseAuthors) 375 { 376 what = "author"; 377 } 378 else if (browseSubjects) 379 { 380 what = "subject"; 381 } 382 else if (browseDates) 383 { 384 what = "date"; 385 } 386 387 log.info(LogManager.getHeader(context, "browse_" + what, logInfo 388 + "results=" + browseInfo.getResultCount())); 389 390 if (browseInfo.getResultCount() == 0) 391 { 392 request.setAttribute("community", community); 394 request.setAttribute("collection", collection); 395 396 JSPManager.showJSP(request, response, "/browse/no-results.jsp"); 397 } 398 else 399 { 400 if (!browseInfo.isFirst()) 403 { 404 String s; 408 409 if (browseAuthors || browseSubjects) { 411 s = (browseInfo.getStringResults())[0]; 412 } 413 else 414 { 415 Item firstItem = (browseInfo.getItemResults())[0]; 416 s = firstItem.getHandle(); 417 } 418 419 if (browseDates && oldestFirst) 420 { 421 request.setAttribute("previous.query", 424 "order=oldestfirst&bottom=" 425 + URLEncoder.encode(s, 426 Constants.DEFAULT_ENCODING)); 427 } 428 else 429 { 430 request.setAttribute("previous.query", "bottom=" 431 + URLEncoder.encode(s, Constants.DEFAULT_ENCODING)); 432 } 433 } 434 435 if (!browseInfo.isLast()) 436 { 437 String s; 441 442 if (browseAuthors) 443 { 444 String [] authors = browseInfo.getStringResults(); 445 s = authors[authors.length - 1]; 446 } 447 else if (browseSubjects) 448 { 449 String [] subjects = browseInfo.getStringResults(); 450 s = subjects[subjects.length - 1]; 451 } 452 else 453 { 454 Item[] items = browseInfo.getItemResults(); 455 Item lastItem = items[items.length - 1]; 456 s = lastItem.getHandle(); 457 } 458 459 if (browseDates && oldestFirst) 460 { 461 request.setAttribute("next.query", "order=oldestfirst&top=" 464 + URLEncoder.encode(s, Constants.DEFAULT_ENCODING)); 465 } 466 else 467 { 468 request.setAttribute("next.query", "top=" 469 + URLEncoder.encode(s, Constants.DEFAULT_ENCODING)); 470 } 471 } 472 473 request.setAttribute("community", community); 475 request.setAttribute("collection", collection); 476 request.setAttribute("browse.info", browseInfo); 477 request.setAttribute("highlight", new Boolean (highlight)); 478 479 if (browseAuthors) 480 { 481 JSPManager.showJSP(request, response, "/browse/authors.jsp"); 482 } 483 else if (browseSubjects) 484 { 485 JSPManager.showJSP(request, response, "/browse/subjects.jsp"); 486 } 487 else if (browseDates) 488 { 489 request.setAttribute("oldest.first", new Boolean (oldestFirst)); 490 request.setAttribute("flip.ordering.query", flipOrderingQuery); 491 JSPManager.showJSP(request, response, 492 "/browse/items-by-date.jsp"); 493 } 494 else 495 { 496 JSPManager.showJSP(request, response, 497 "/browse/items-by-title.jsp"); 498 } 499 } 500 } 501 } 502 | Popular Tags |