Wiki source code of Like Java API
Last modified by Eleni Cojocariu on 2026/08/12 13:56
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | ##org.xwiki.like.LikeManager## is the Component role behind the whole feature: inject it to read or write likes from Java. A user is always passed as a ##UserReference## and a liked entity as an ##EntityReference##. Every method that reads or writes likes throws a ##LikeException## when the store fails or when the user lacks the ##like## right; ##getLikeRight()## and the two ##clearCache## methods do not throw. | ||
| 2 | |||
| 3 | |=Method|=Returns|=Description | ||
| 4 | |##saveLike(source, target)##|##long##|Records a like and returns the new like count of the target. | ||
| 5 | |##removeLike(source, target)##|##boolean##|Removes a like and returns whether one was actually removed. | ||
| 6 | |##isLiked(source, target)##|##boolean##|Whether that user already liked that entity. | ||
| 7 | |##getEntityLikes(target)##|##long##|The number of likes of an entity. | ||
| 8 | |##getLikers(target, offset, limit)##|##List<UserReference>##|The users who liked an entity, most recently updated first. | ||
| 9 | |##getUserLikes(source, offset, limit)##|##List<EntityReference>##|The entities a user liked, most recently updated first. | ||
| 10 | |##countUserLikes(source)##|##long##|How many likes a user performed. | ||
| 11 | |##getLikeRight()##|##Right##|The ##like## right, as registered when the Component initialized. | ||
| 12 | |##clearCache(target)##|##void##|Drops the cached count and the per-user entries of one entity. | ||
| 13 | |##clearCache()##|##void##|Empties both caches. | ||
| 14 | |||
| 15 | {{code language="java"}} | ||
| 16 | @Inject | ||
| 17 | private LikeManager likeManager; | ||
| 18 | |||
| 19 | @Inject | ||
| 20 | @Named("document") | ||
| 21 | private UserReferenceResolver<DocumentReference> userReferenceResolver; | ||
| 22 | |||
| 23 | public long like(DocumentReference user, DocumentReference page) throws LikeException | ||
| 24 | { | ||
| 25 | return this.likeManager.saveLike(this.userReferenceResolver.resolve(user), page); | ||
| 26 | } | ||
| 27 | {{/code}} | ||
| 28 | |||
| 29 | == Events == | ||
| 30 | |||
| 31 | ##LikeEvent## and ##UnlikeEvent##, both in ##org.xwiki.like.events##, are sent through the Observation Manager once a like has been stored or removed. | ||
| 32 | |||
| 33 | |=Listener argument|=Value | ||
| 34 | |##source##|The ##UserReference## of the user who liked or unliked. | ||
| 35 | |##data##|The ##EntityReference## that was liked or unliked. | ||
| 36 | |||
| 37 | {{code language="java"}} | ||
| 38 | @Component | ||
| 39 | @Named("MyLikeListener") | ||
| 40 | @Singleton | ||
| 41 | public class MyLikeListener extends AbstractEventListener | ||
| 42 | { | ||
| 43 | public MyLikeListener() | ||
| 44 | { | ||
| 45 | super("MyLikeListener", new LikeEvent(), new UnlikeEvent()); | ||
| 46 | } | ||
| 47 | |||
| 48 | @Override | ||
| 49 | public void onEvent(Event event, Object source, Object data) | ||
| 50 | { | ||
| 51 | UserReference liker = (UserReference) source; | ||
| 52 | EntityReference liked = (EntityReference) data; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | {{/code}} | ||
| 56 | |||
| 57 | == Configuration == | ||
| 58 | |||
| 59 | ##org.xwiki.like.LikeConfiguration## reads the settings an administrator edits in the [[Like preferences>>documentation.xs.admin.like.administer-like-preferences.WebHome]]: ##isEnabled()##, ##alwaysDisplayButton()## and ##getLikeCacheCapacity()##. |