implemented tag cloud

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-10-15 15:33:01 +02:00
parent 91feb33f5f
commit bda42eb15a
10 changed files with 87 additions and 10 deletions

View File

@@ -6,6 +6,7 @@ public class Constants {
public static final String COMMENT_HASH = "comment_hash";
public static final String CONFIG_DATABASE = "umbrella.modules.tags.database";
public static final String COUNT = "count";
public static final String TABLE_COMMENTS = "comments";
public static final String TABLE_TAGS = "tags";
public static final String TABLE_TAGS_NEW = "tags_new";

View File

@@ -17,6 +17,7 @@ import static java.lang.System.Logger.Level.*;
import static java.text.MessageFormat.format;
import static java.time.ZoneOffset.UTC;
import de.srsoftware.tools.Tuple;
import de.srsoftware.tools.jdbc.Query;
import de.srsoftware.umbrella.bookmarks.BookmarkDb;
import de.srsoftware.umbrella.core.BaseDb;
@@ -269,6 +270,24 @@ CREATE TABLE IF NOT EXISTS {0} (
}
}
@Override
public Collection<Tuple<String,Long>> list(long userId) {
try {
var rs = select("tag, COUNT(tag) as count").from(TABLE_TAGS).where(USER_ID,equal(userId)).groupBy(TAG).sort(TAG).exec(db);
var list = new ArrayList<Tuple<String,Long>>();
while (rs.next()) {
var tag = rs.getString(TAG);
if (tag.isBlank()) continue;;
var count = rs.getLong("count");
list.add(Tuple.of(tag,count));
}
rs.close();
return list;
} catch (SQLException e) {
throw databaseException("Failed to load tags for user {0}",userId);
}
}
@Override
public Map<Long, ? extends Collection<String>> list(long userId, String module, Collection<Long> entityIds) {
try {

View File

@@ -2,6 +2,7 @@
package de.srsoftware.umbrella.tags;
import de.srsoftware.tools.Tuple;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -14,6 +15,8 @@ public interface TagDB {
Map<String, List<Long>> getUses(String tag, long id);
Collection<Tuple<String, Long>> list(long userId);
Set<String> list(long userId, String module, long entityId);
/**

View File

@@ -7,8 +7,7 @@ import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_UNPROCESSABLE;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.unprocessable;
import static de.srsoftware.umbrella.tags.Constants.CONFIG_DATABASE;
import static de.srsoftware.umbrella.tags.Constants.TAG;
import static de.srsoftware.umbrella.tags.Constants.*;
import com.sun.net.httpserver.HttpExchange;
import de.srsoftware.configuration.Configuration;
@@ -67,7 +66,7 @@ public class TagModule extends BaseHandler implements TagService {
var user = userService().refreshSession(ex);
if (user.isEmpty()) return unauthorized(ex);
var module = path.pop();
if (module == null) throw unprocessable("Module missing in path.");
if (module == null) return getUserTags(ex, user.get());
var head = path.pop();
if (USES.equals(module)) return getTagUses(ex,head,user.get());
long entityId = Long.parseLong(head);
@@ -79,10 +78,6 @@ public class TagModule extends BaseHandler implements TagService {
}
}
private boolean getTagUses(HttpExchange ex, String tag, UmbrellaUser user) throws IOException {
return sendContent(ex,tagDb.getUses(tag,user.id()));
}
@Override
public boolean doPost(Path path, HttpExchange ex) throws IOException {
addCors(ex);
@@ -117,6 +112,10 @@ public class TagModule extends BaseHandler implements TagService {
}
}
private boolean getTagUses(HttpExchange ex, String tag, UmbrellaUser user) throws IOException {
return sendContent(ex,tagDb.getUses(tag,user.id()));
}
public Collection<String> getTags(String module, long entityId, UmbrellaUser user) throws UmbrellaException{
return tagDb.list(user.id(),module,entityId);
}
@@ -126,6 +125,11 @@ public class TagModule extends BaseHandler implements TagService {
return tagDb.list(user.id(),module,entityIds);
}
private boolean getUserTags(HttpExchange ex, UmbrellaUser user) throws IOException {
var tuples = tagDb.list(user.id()).stream().filter(t -> t.a != null && t.b != null);
return sendContent(ex,tuples.map(t -> Map.of(TAG,t.a,COUNT,t.b)));
}
@Override
public void save(String module, long entityId, Collection<Long> userIds, Collection<String> tags) {
tagDb.save(userIds,module,entityId,tags);