Author: bbrossaud Date: 2010-05-06 18:18:00 +0200 (Thu, 06 May 2010) New Revision: 10 Url: http://chorem.org/repositories/revision/bow/10 Log: Added the tags display which almost works Modified: trunk/src/main/java/org/chorem/bow/BookmarkActions.java trunk/src/main/java/org/chorem/bow/ControllerServlet.java trunk/src/main/webapp/search.jsp Modified: trunk/src/main/java/org/chorem/bow/BookmarkActions.java =================================================================== --- trunk/src/main/java/org/chorem/bow/BookmarkActions.java 2010-05-06 09:52:18 UTC (rev 9) +++ trunk/src/main/java/org/chorem/bow/BookmarkActions.java 2010-05-06 16:18:00 UTC (rev 10) @@ -5,20 +5,25 @@ package org.chorem.bow; import java.util.ArrayList; +import java.util.Collection; import java.util.Date; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; /** * * @author bbrossaud */ - public class BookmarkActions { - List<Bookmark> bookmarks = new ArrayList<Bookmark>(); - Map<String, Integer> tagsCloud = new HashMap<String, Integer>(); + protected List<Bookmark> bookmarks = new ArrayList<Bookmark>(); + protected Map<String, Integer> tagsCloud = new HashMap<String, Integer>(); + protected List<String> tagsSearch = new ArrayList<String>(); + protected int tmax = -1; + protected int tmin = -1; public Bookmark createBookmark(String query, User user) { Bookmark bookmark = (Bookmark) new BookmarkImpl(); @@ -35,7 +40,6 @@ query = ""; } } - } else { bookmark.setLink(query); } @@ -49,9 +53,98 @@ } public void createTagsCloud() { - ; + + Iterator itTags = tagsSearch.iterator(); + + tagsCloud.clear(); + while (itTags.hasNext()) { + String tag = (String) itTags.next(); + Iterator it = bookmarks.iterator(); + while (it.hasNext()) { + Bookmark bookmark = (Bookmark) it.next(); + if (bookmark.getTags().contains(tag) == false) { + it.remove(); + } else { + Set<String> tags = bookmark.getTags(); + for (String key : tags) { + if (tagsCloud.containsKey(key)) { + Integer count = tagsCloud.get(key); + ++count; + tagsCloud.put(key, count); + } else { + tagsCloud.put(key, 1); + } + } + } + } + } + this.defineTValues(); } + protected void defineTValues() { + Collection values = tagsCloud.values(); + Iterator it = values.iterator(); + tmax = -1; + tmin = -1; + int value; + + while (it.hasNext()) { + value = (Integer) it.next(); + if (tmax < value) { + tmax = value; + } + if (tmin == -1) { + tmin = value; + } else if (tmin > value) { + tmin = value; + } + } + } + + public void addTag(String tag) { + if (!tagsSearch.contains(tag)) { + tagsSearch.add(tag); + } + } + + public void addTags(String[] tags) { + for (int i = 0; i < tags.length; i++) { + if (!tagsSearch.contains(tags[i])) { + tagsSearch.add(tags[i]); + } + } + } + + public String getSearchLine() { + String line = ""; + + Iterator it = tagsSearch.iterator(); + + while (it.hasNext()) { + String tag = (String) it.next(); + line += tag; + if (it.hasNext()) + line += " "; + } + return line; + } + + public int getFont(int ti) { + int font = 1; + + if (tmax > tmin) { + font = (15 * (ti - tmin)) / (tmax - tmin); + } + return font; + } + + public void reset() { + bookmarks.clear(); + tagsCloud.clear(); + tmax = -1; + tmin = -1; + } + public Map<String, Integer> getTagsCloud() { return tagsCloud; } @@ -59,4 +152,12 @@ public void setBookmarks(List<Bookmark> bookmarksList) { bookmarks = bookmarksList; } + + public int getTmin() { + return tmin; + } + + public int getTmax() { + return tmax; + } } Modified: trunk/src/main/java/org/chorem/bow/ControllerServlet.java =================================================================== --- trunk/src/main/java/org/chorem/bow/ControllerServlet.java 2010-05-06 09:52:18 UTC (rev 9) +++ trunk/src/main/java/org/chorem/bow/ControllerServlet.java 2010-05-06 16:18:00 UTC (rev 10) @@ -8,8 +8,8 @@ * * @author bbrossaud */ - import java.io.IOException; +import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -23,9 +23,9 @@ public class ControllerServlet extends HttpServlet { private static final Log log = LogFactory.getLog(ControllerServlet.class); - private Model model = new Model(); - private BookmarkActions bookmarkActions = new BookmarkActions(); - private User user = null; + protected Model model = new Model(); + protected BookmarkActions bookmarkActions = new BookmarkActions(); + protected User user = null; @Override public void doGet(HttpServletRequest request, HttpServletResponse response) @@ -72,28 +72,47 @@ } } - private void actionAddUrl(HttpServletRequest request, HttpServletResponse response) + protected void actionAddUrl(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + String url = request.getParameter("url"); - if (request.getParameter("url") != null) { + if (url != null) { bookmarkActions.getBookmarks().clear(); - Bookmark bookmark = bookmarkActions.createBookmark(request.getParameter("url"), user); + bookmarkActions.getTagsCloud().clear(); + url = url.replaceAll("\\s*", ""); + Bookmark bookmark = bookmarkActions.createBookmark(url, user); model.getProxy().store(bookmark); + log.debug("URL = {" + url + "}"); log.debug("Adding URL"); - request.getRequestDispatcher("search.jsp").forward(request, response); } + request.setAttribute("bookmarkActions", bookmarkActions); + request.getRequestDispatcher("search.jsp").forward(request, response); } - private void actionSearch(HttpServletRequest request, HttpServletResponse response) + protected void actionSearch(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - if (bookmarkActions.getBookmarks().isEmpty()) { - bookmarkActions.setBookmarks(model.getProxy().findAllByCriteria(Bookmark.class, null).getAll()); - + String words = request.getParameter("words"); + + request.setAttribute("bookmarkActions", bookmarkActions); + if (words != null) { + if (!words.isEmpty()) { + if (bookmarkActions.getBookmarks().isEmpty()) { + bookmarkActions.setBookmarks(new ArrayList( + model.getProxy().findAllByCriteria(Bookmark.class, null).getAll())); + } + log.debug("ListSize before = {" + String.valueOf(bookmarkActions.getBookmarks().size()) + "}"); + String[] tags = words.split("\\s+"); + bookmarkActions.addTags(tags); + bookmarkActions.createTagsCloud(); + log.debug("TabSize = {" + String.valueOf(tags.length) + "}"); + log.debug("ListSize after = {" + String.valueOf(bookmarkActions.getBookmarks().size()) + "}"); + } } - log.debug("ListSize = {"+String.valueOf(bookmarkActions.getBookmarks().size())+"}"); + request.setAttribute("bookmarkActions", bookmarkActions); + request.getRequestDispatcher("search.jsp").forward(request, response); } - private void actionRegister(HttpServletRequest request, HttpServletResponse response) + protected void actionRegister(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { try { if (this.checkRegister(request, response)) { @@ -111,13 +130,12 @@ request.getRequestDispatcher("search.jsp").forward(request, response); } } - } - catch (Exception eee) { + } catch (Exception eee) { request.getRequestDispatcher("error.jsp").forward(request, response); } } - private void actionLogin(HttpServletRequest request, HttpServletResponse response) + protected void actionLogin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { try { User user = this.checkLogin(request, response); @@ -125,6 +143,7 @@ if (user != null) { log.debug("User exists"); this.user = user; + request.setAttribute("bookmarkActions", bookmarkActions); request.getRequestDispatcher("search.jsp").forward(request, response); } else { log.debug("User doesn't exist"); @@ -136,7 +155,7 @@ } - private User checkLogin(HttpServletRequest request, HttpServletResponse response) + protected User checkLogin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String email = request.getParameter("email"); String password = request.getParameter("password"); @@ -151,12 +170,12 @@ return null; } - private boolean checkRegister(HttpServletRequest request, HttpServletResponse response) + protected boolean checkRegister(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String email = request.getParameter("email"); String password = request.getParameter("password"); - log.debug("email={"+email+"}"); + log.debug("email={" + email + "}"); if (!email.isEmpty() && !password.isEmpty()) { WikittyProxy proxy = model.getProxy(); Modified: trunk/src/main/webapp/search.jsp =================================================================== --- trunk/src/main/webapp/search.jsp 2010-05-06 09:52:18 UTC (rev 9) +++ trunk/src/main/webapp/search.jsp 2010-05-06 16:18:00 UTC (rev 10) @@ -1,3 +1,12 @@ +<%@ page import="org.chorem.bow.BookmarkActions" %> +<%@ page import="java.util.HashMap" %> +<%@ page import="java.util.Iterator" %> +<%@ page import="java.util.Map" %> +<%@ page import="java.util.Set" %> +<%@ page import="java.util.ArrayList" %> + +<%BookmarkActions bookmarkActions = (BookmarkActions) request.getAttribute("bookmarkActions");%> + <html> <body> <h1>Search</h1> @@ -8,9 +17,19 @@ </form> <form method="POST" action="search?action=check"> - <input type="text" name="words" size="20"><br /> + <input type="text" name="words" size="20" value=<%=bookmarkActions.getSearchLine()%>><br /> <input type="submit" value="Find"> </form> +<% if (bookmarkActions != null) { + Map<String, Integer> tagsCloud = bookmarkActions.getTagsCloud(); + Set<String> tags = tagsCloud.keySet(); + for (String tag : tags) { + int font = bookmarkActions.getFont(tagsCloud.get(tag)); +%> +<FONT SIZE=<%=font%>><%=tag%></FONT> +<% +}} +%> </body> </html> \ No newline at end of file