Permalink
Browse files

Gestion plus optimale des tags en cas d'un grand nombre

  • Loading branch information...
1 parent 281053c commit 9304156791be1754db42c2e89ec1b2cf966f1173 @nicogiard committed Mar 10, 2012
@@ -17,6 +17,8 @@
import java.io.File;
import java.io.FileNotFoundException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -28,7 +30,7 @@
@Before
static void defaultData() {
- List<Tag> tags = Tag.findAll();
+ List<Tag> tags = Tag.findOrderByMostUsed();
renderArgs.put("tags", tags);
// Récupération de la pagination
@@ -75,13 +77,13 @@ public static void create() {
renderTemplate("Application/update.html");
}
- public static void update(Long torrentId, String keywords) {
+ public static void update(Long torrentId, String keywords, String activeTag) {
Torrent torrent = Torrent.findById(torrentId);
notFoundIfNull(torrent);
- render(torrent, keywords);
+ render(torrent, keywords, activeTag);
}
- public static void save(@Required @Valid Torrent torrent, File file, String tags, String keywords) {
+ public static void save(@Required @Valid Torrent torrent, File file, String tags, String keywords, String activeTag) {
if (torrent.id == null) {
validation.required(file);
}
@@ -96,7 +98,7 @@ public static void save(@Required @Valid Torrent torrent, File file, String tags
if (torrent.id == null) {
create();
}
- update(torrent.id, keywords);
+ update(torrent.id, keywords, activeTag);
}
if (torrent.id == null) {
@@ -127,7 +129,17 @@ public static void save(@Required @Valid Torrent torrent, File file, String tags
}
}
if (!StringUtils.isBlank(keywords)) {
- search(keywords);
+ try {
+ search(URLEncoder.encode(keywords, "UTF-8"));
+ } catch (UnsupportedEncodingException e) {
+ Logger.error(e.getMessage(), e);
+ }
+ } else if (!StringUtils.isBlank(activeTag)) {
+ try {
+ tag(URLEncoder.encode(activeTag, "UTF-8"));
+ } catch (UnsupportedEncodingException e) {
+ Logger.error(e.getMessage(), e);
+ }
} else {
index();
}
View
@@ -1,9 +1,11 @@
package models;
import play.data.validation.Required;
+import play.db.jpa.JPA;
import play.db.jpa.Model;
import javax.persistence.Entity;
+import java.util.List;
@Entity
public class Tag extends Model {
@@ -13,4 +15,8 @@
public static Tag findByName(String tagName) {
return Tag.find("byName", tagName).first();
}
+
+ public static List<Tag> findOrderByMostUsed() {
+ return (List<Tag>) JPA.em().createNativeQuery("SELECT tag.id, tag.name, COUNT(*) AS nombre FROM `Tag` tag INNER JOIN `Torrent_Tag` tt ON tag.id=tt.tags_id GROUP BY tt.tags_id ORDER BY nombre DESC", Tag.class).getResultList();
+ }
}
@@ -21,15 +21,13 @@
<h1 class="screen-title"><i class="big-icon-page"></i>&nbsp;Torrents</h1>
-<div class="subnav">
- <ul class="nav nav-pills">
- <li #{if request.action != "Application.noTag" && !activeTag && !keywords}class="active"#{/if}><a href="@{Application.index}">Tous</a></li>
- <li #{if request.action =~ "Application.noTag"}class="active"#{/if}><a href="@{Application.noTag}">Pas de Tag</a></li>
- #{list tags, as:'tag'}
- <li #{if activeTag && activeTag.name == tag.name}class="active"#{/if}><a href="@{Application.tag(tag.name)}">${tag.name}</a></li>
- #{/list}
- </ul>
-</div>
+<ul class="tagCloud">
+ <li><a href="@{Application.index}" class="btn #{if request.action != "Application.noTag" && !activeTag && !keywords}active#{/if}">Tous</a></li>
+ <li><a href="@{Application.noTag}" class="btn #{if request.action =~ "Application.noTag"}active#{/if}">Pas de Tag</a></li>
+#{list tags, as:'tag'}
+ <li><a href="@{Application.tag(tag.name)}" class="btn #{if activeTag && activeTag.name == tag.name}active#{/if}">${tag.name}</a></li>
+#{/list}
+</ul>
<div class="container">
#{form @Application.search(), class:"form-search pull-left", style:"margin: 18px 0;"}
@@ -69,7 +67,7 @@ <h1 class="screen-title"><i class="big-icon-page"></i>&nbsp;Torrents</h1>
${torrent.creationDate?.format("dd/MM/yyyy")}
</td>
<td class="actions">
- <a href="@{Application.update(torrent.id)}#{if keywords}?keywords=${keywords}#{/if}" class="btn" title="Modifier"><i class="icon-pencil"></i>Modifier</a>
+ <a href="@{Application.update(torrent.id)}#{if keywords}?keywords=${keywords}#{/if}#{if activeTag}?activeTag=${activeTag.name}#{/if}" class="btn" title="Modifier"><i class="icon-pencil"></i>Modifier</a>
#{if controllers.Security.connectedUser() == torrent.uploader || controllers.Security.connectedUser().isAdmin}
<a href="@{Application.delete(torrent.id)}" class="btn btn-danger" onclick="if(!confirm('Etes vous sûr de vouloir supprimer ce torrent ?'))return false;" title="Supprimer"><i class="icon-trash icon-white"></i>Supprimer</a>
#{/if}
@@ -16,6 +16,7 @@ <h2 class="screen-title">
<form action="@{Application.save()}" method="POST" class="form-horizontal" enctype="multipart/form-data">
<input type="hidden" name="keywords" value="${keywords}">
+ <input type="hidden" name="activeTag" value="${activeTag}">
<input type="hidden" name="torrent.id" value="${torrent?.id}">
#{if torrent?.id == null}
@@ -54,6 +55,8 @@ <h2 class="screen-title">
<div class="form-actions">
<button type="submit" class="btn btn-primary"><i class="icon-ok icon-white"></i>&nbsp;Enregistrer</button>
- <a href="#{if keywords}@{Application.search(keywords)}#{/if}@{Application.index}" class="btn"><i class="icon-ban-circle"></i>&nbsp;Annuler</a>
+ <a href="#{if keywords}@{Application.search(keywords)}#{/if}
+ #{elseif activeTag}@{Application.tag(activeTag)}#{/elseif}
+ #{else}@{Application.index}#{/else}" class="btn"><i class="icon-ban-circle"></i>&nbsp;Annuler</a>
</div>
</form>
@@ -46,4 +46,18 @@ li.active > a i[class^="icon-"] {
margin-right: 5px;
text-align: right;
width: 105px;
+}
+ul.tagCloud {
+ list-style: none;
+ margin-left: 0;
+}
+ul.tagCloud li {
+ display: inline;
+ line-height: 35px;
+}
+ul.tagCloud li a {
+ color: #08C;
+}
+ul.tagCloud li a.active {
+ color: #777;
}

0 comments on commit 9304156

Please sign in to comment.