Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame^] | 1 | package eu.mulk.mulkcms2.benki.bookmarks; |
| 2 | |
| 3 | import static javax.ws.rs.core.MediaType.TEXT_HTML; |
| 4 | |
| 5 | import eu.mulk.mulkcms2.benki.accesscontrol.Role; |
| 6 | import eu.mulk.mulkcms2.benki.users.User; |
| 7 | import io.quarkus.panache.common.Sort; |
| 8 | import io.quarkus.qute.Template; |
| 9 | import io.quarkus.qute.TemplateExtension; |
| 10 | import io.quarkus.qute.TemplateInstance; |
| 11 | import io.quarkus.qute.api.ResourcePath; |
| 12 | import io.quarkus.security.identity.SecurityIdentity; |
| 13 | import java.time.format.DateTimeFormatter; |
| 14 | import java.time.format.FormatStyle; |
| 15 | import java.time.temporal.TemporalAccessor; |
| 16 | import java.util.List; |
| 17 | import javax.inject.Inject; |
| 18 | import javax.json.spi.JsonProvider; |
| 19 | import javax.ws.rs.GET; |
| 20 | import javax.ws.rs.Path; |
| 21 | import javax.ws.rs.Produces; |
| 22 | import org.jboss.logging.Logger; |
| 23 | |
| 24 | @Path("/bookmarks") |
| 25 | public class BookmarkResource { |
| 26 | |
| 27 | private static Logger log = Logger.getLogger(BookmarkResource.class); |
| 28 | |
| 29 | private static DateTimeFormatter htmlDateFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME; |
| 30 | |
| 31 | private static DateTimeFormatter humanDateFormatter = |
| 32 | DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.SHORT); |
| 33 | |
| 34 | private static JsonProvider jsonProvider = JsonProvider.provider(); |
| 35 | |
| 36 | @ResourcePath("benki/bookmarks/bookmarkList.html") |
| 37 | @Inject |
| 38 | Template bookmarkList; |
| 39 | |
| 40 | @Inject SecurityIdentity identity; |
| 41 | |
| 42 | @GET |
| 43 | @Produces(TEXT_HTML) |
| 44 | public TemplateInstance getPage() { |
| 45 | List<Bookmark> bookmarks; |
| 46 | if (identity.isAnonymous()) { |
| 47 | Role world = Role.find("from Role r join r.tags tag where tag = 'world'").singleResult(); |
| 48 | bookmarks = |
| 49 | Bookmark.find( |
| 50 | "select bm from Bookmark bm join bm.targets target left join fetch bm.owner where target = ?1", |
| 51 | Sort.by("date").descending(), |
| 52 | world) |
| 53 | .list(); |
| 54 | } else { |
| 55 | var userName = identity.getPrincipal().getName(); |
| 56 | User user = |
| 57 | User.find("from BenkiUser u join u.nicknames n where ?1 = n", userName).singleResult(); |
| 58 | bookmarks = |
| 59 | Bookmark.find( |
| 60 | "select bm from BenkiUser u inner join u.visibleBookmarks bm left join fetch bm.owner where u.id = ?1", |
| 61 | Sort.by("date").descending(), |
| 62 | user.id) |
| 63 | .list(); |
| 64 | } |
| 65 | return bookmarkList.data("bookmarks", bookmarks); |
| 66 | } |
| 67 | |
| 68 | @TemplateExtension |
| 69 | static String humanDateTime(TemporalAccessor x) { |
| 70 | return humanDateFormatter.format(x); |
| 71 | } |
| 72 | |
| 73 | @TemplateExtension |
| 74 | static String htmlDateTime(TemporalAccessor x) { |
| 75 | return htmlDateFormatter.format(x); |
| 76 | } |
| 77 | } |