Skip to content

Commit

Permalink
SAK-47038 Search: Add the content creator's display name to the index (
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianfish authored Mar 18, 2022
1 parent 683a351 commit 9b5f03b
Show file tree
Hide file tree
Showing 14 changed files with 260 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.sakaiproject.tool.api.Session;
import org.sakaiproject.tool.api.Tool;
import org.sakaiproject.tool.api.ToolSession;
import org.sakaiproject.user.api.User;

/**
* @author Adrian Fish ([email protected])
Expand Down Expand Up @@ -59,6 +60,8 @@ public interface SakaiProxy {

public String getDisplayNameForTheUser(String userId);

public User getUser(String userId);

public boolean isCurrentUserAdmin();

public String getPortalUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,18 @@
import lombok.Getter;
import lombok.Setter;

@Getter @Setter
public class Comment implements Entity {

@Getter @Setter
private String id = "";

@Getter
private String content = "";

@Getter
private long createdDate = -1L;

@Getter @Setter
private long modifiedDate = -1L;

@Getter @Setter
private String creatorId;

@Getter @Setter
private String creatorDisplayName;

@Getter @Setter
private String creatorUserName;
private String postId;

@Getter @Setter
private Post post;

@Getter @Setter
private String url;

public Comment() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class Post implements Entity {
private String content = "";
private long createdDate = -1L;
private String creatorDisplayName = null;
private String creatorUserName;
private String creatorId = null;
private String id = "";
private long modifiedDate = -1L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.sakaiproject.commons.api.CommonsFunctions;
import org.sakaiproject.commons.api.CommonsManager;
import org.sakaiproject.commons.api.CommonsReferenceReckoner;
import org.sakaiproject.commons.api.CommonsReferenceReckoner.CommonsReference;
import org.sakaiproject.commons.api.QueryBean;
import org.sakaiproject.commons.api.SakaiProxy;
import org.sakaiproject.commons.api.datamodel.Comment;
Expand Down Expand Up @@ -84,17 +85,20 @@ public void init() {
}
}

@Override
public boolean isContentFromReader(String reference) {
return false;
}

@Override
public Reader getContentReader(String reference) {
return null;
}

@Override
public String getContent(String ref) {

CommonsReferenceReckoner.CommonsReference r = CommonsReferenceReckoner.reckoner().reference(ref).reckon();
CommonsReference r = CommonsReferenceReckoner.reckoner().reference(ref).reckon();

if (CommonsConstants.PostType.COMMENT == r.getType()) {
Optional<Comment> opComment = commonsManager.getComment(r.getCommentId());
Expand All @@ -117,25 +121,26 @@ public String getContent(String ref) {
return "";
}

@Override
public String getTitle(String ref) {

CommonsReferenceReckoner.CommonsReference r = CommonsReferenceReckoner.reckoner().reference(ref).reckon();
CommonsReference r = CommonsReferenceReckoner.reckoner().reference(ref).reckon();
if (r.getType() == CommonsConstants.PostType.POST) {
return rl.getString("search_title_post");
} else {
return rl.getString("search_title_comment");
}
}

@Override
public String getUrl(String ref) {

return transactionTemplate.execute(new TransactionCallback<String>() {

@Override
public String doInTransaction(TransactionStatus status) {

CommonsReferenceReckoner.CommonsReference r
= CommonsReferenceReckoner.reckoner().reference(ref).reckon();
CommonsReference r = CommonsReferenceReckoner.reckoner().reference(ref).reckon();

try {
Site site = siteService.getSite(r.getContext());
Expand All @@ -152,10 +157,12 @@ public String doInTransaction(TransactionStatus status) {
});
}

@Override
public boolean matches(String ref) {
return ref.startsWith(CommonsManager.REFERENCE_ROOT);
}

@Override
public Integer getAction(Event event) {

String evt = event.getEvent();
Expand All @@ -166,20 +173,105 @@ public Integer getAction(Event event) {
return SearchBuilderItem.ACTION_UNKNOWN;
}

@Override
public boolean matches(Event event) {

String evt = event.getEvent();
return addingEvents.contains(evt) || removingEvents.contains(evt);
}

@Override
public String getTool() {
return "commons";
}

@Override
public String getSiteId(String ref) {
return CommonsReferenceReckoner.reckoner().reference(ref).reckon().getContext();
}

@Override
public String getCreatorDisplayName(String ref) {

CommonsReference r = CommonsReferenceReckoner.reckoner().reference(ref).reckon();

switch (r.getType()) {

case POST:
Optional<Post> optPost = commonsManager.getPost(r.getPostId(), false);
if (optPost.isPresent()) {
return optPost.get().getCreatorDisplayName();
}
log.warn("Invalid commons post ref {}. Returning empty creator display name ...", ref);
break;
case COMMENT:
Optional<Comment> optComment = commonsManager.getComment(r.getCommentId());
if (optComment.isPresent()) {
return optComment.get().getCreatorDisplayName();
}
log.warn("Invalid commons comment ref {}. Returning empty creator display name ...", ref);
break;
default:
}

return "";
}

@Override
public String getCreatorId(String ref) {

CommonsReference r = CommonsReferenceReckoner.reckoner().reference(ref).reckon();

switch (r.getType()) {

case POST:
Optional<Post> optPost = commonsManager.getPost(r.getPostId(), false);
if (optPost.isPresent()) {
return optPost.get().getCreatorId();
}
log.warn("Invalid commons post ref {}. Returning empty creator id ...", ref);
break;
case COMMENT:
Optional<Comment> optComment = commonsManager.getComment(r.getCommentId());
if (optComment.isPresent()) {
return optComment.get().getCreatorId();
}
log.warn("Invalid commons comment ref {}. Returning empty creator id ...", ref);
break;
default:
}

return "";
}

@Override
public String getCreatorUserName(String ref) {

CommonsReference r = CommonsReferenceReckoner.reckoner().reference(ref).reckon();

switch (r.getType()) {

case POST:
Optional<Post> optPost = commonsManager.getPost(r.getPostId(), false);
if (optPost.isPresent()) {
return optPost.get().getCreatorUserName();
}
log.warn("Invalid commons post ref {}. Returning empty creator user name ...", ref);
break;
case COMMENT:
Optional<Comment> optComment = commonsManager.getComment(r.getCommentId());
if (optComment.isPresent()) {
return optComment.get().getCreatorUserName();
}
log.warn("Invalid commons comment ref {}. Returning empty creator user name ...", ref);
break;
default:
}

return "";
}

@Override
public Iterator<String> getSiteContentIterator(String siteId) {

List<String> refs = new ArrayList<>();
Expand All @@ -199,13 +291,15 @@ public Iterator<String> getSiteContentIterator(String siteId) {
return refs.iterator();
}

@Override
public boolean isForIndex(String ref) {
return ref.startsWith(CommonsManager.REFERENCE_ROOT);
}

@Override
public boolean canRead(String ref) {

CommonsReferenceReckoner.CommonsReference r = CommonsReferenceReckoner.reckoner().reference(ref).reckon();
CommonsReference r = CommonsReferenceReckoner.reckoner().reference(ref).reckon();
String siteId = r.getContext();
if (r.getType() == CommonsConstants.PostType.POST) {
return sakaiProxy.isAllowedFunction(CommonsFunctions.POST_READ_ANY, siteId);
Expand All @@ -214,30 +308,36 @@ public boolean canRead(String ref) {
}
}

@Override
public Map<String, ?> getCustomProperties(String ref) {
return null;
}

@Override
public String getCustomRDF(String ref) {
return null;
}

@Override
public String getId(String ref) {

CommonsReferenceReckoner.CommonsReference r = CommonsReferenceReckoner.reckoner().reference(ref).reckon();
CommonsReference r = CommonsReferenceReckoner.reckoner().reference(ref).reckon();
return (r.getType() == CommonsConstants.PostType.POST) ? r.getPostId() : r.getCommentId();
}

@Override
public String getType(String ref) {
return "commons";
}

@Override
public String getSubType(String ref) {

CommonsReferenceReckoner.CommonsReference r = CommonsReferenceReckoner.reckoner().reference(ref).reckon();
CommonsReference r = CommonsReferenceReckoner.reckoner().reference(ref).reckon();
return (r.getType() == CommonsConstants.PostType.POST) ? "posts" : "comments";
}

@Override
public String getContainer(String ref) {
return CommonsReferenceReckoner.reckoner().reference(ref).reckon().getContext();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.sakaiproject.commons.api.PersistenceManager;
import org.sakaiproject.commons.api.QueryBean;
import org.sakaiproject.commons.api.SakaiProxy;
import org.sakaiproject.user.api.User;

/**
* @author Adrian Fish ([email protected])
Expand Down Expand Up @@ -163,7 +164,11 @@ public Comment readSqlResultRecord(ResultSet result) {
if (comments.size() > 0) {
Comment comment = comments.get(0);
comment.setPost(comment.getPost());
comment.setCreatorDisplayName(sakaiProxy.getDisplayNameForTheUser(comment.getCreatorId()));
User user = sakaiProxy.getUser(comment.getCreatorId());
if (user != null ) {
comment.setCreatorDisplayName(user.getDisplayName());
comment.setCreatorUserName(user.getEid());
}
return Optional.of(comment);
} else {
log.warn("No comment for id {}", commentId);
Expand Down Expand Up @@ -359,8 +364,13 @@ private Post loadPostFromResult(ResultSet result, boolean loadComments) {

try {
Post post = new Post(result);
post.setCreatorDisplayName(
sakaiProxy.getDisplayNameForTheUser(post.getCreatorId()));

User user = sakaiProxy.getUser(post.getCreatorId());
if (user != null ) {
post.setCreatorDisplayName(user.getDisplayName());
post.setCreatorUserName(user.getEid());
}

if (loadComments) {
List<Comment> comments = sqlService.dbRead(COMMENTS_SELECT
, new Object[] {post.getId()}
Expand All @@ -369,8 +379,11 @@ public Comment readSqlResultRecord(ResultSet commentResult) {

try {
Comment comment = new Comment(commentResult);
comment.setCreatorDisplayName(
sakaiProxy.getDisplayNameForTheUser(comment.getCreatorId()));
User user = sakaiProxy.getUser(comment.getCreatorId());
if (user != null ) {
comment.setCreatorDisplayName(user.getDisplayName());
comment.setCreatorUserName(user.getEid());
}
String toolId = sakaiProxy.getCommonsToolId(post.getSiteId());
String url = sakaiProxy.getPortalUrl() + "/directtool/"
+ toolId + "?state=post&postId=" + post.getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.sakaiproject.tool.api.*;
import org.sakaiproject.user.api.User;
import org.sakaiproject.user.api.UserDirectoryService;
import org.sakaiproject.user.api.UserNotDefinedException;
import org.sakaiproject.util.api.FormattedText;

/**
Expand Down Expand Up @@ -152,6 +153,16 @@ public void setCurrentToolSession(ToolSession toolSession) {
sessionManager.setCurrentToolSession(toolSession);
}

public User getUser(String userId) {

try {
return userDirectoryService.getUser(userId);
} catch (UserNotDefinedException e) {
log.error("No user for id: {}", userId);
}
return null;
}

public String getDisplayNameForTheUser(String userId) {

try {
Expand Down
7 changes: 7 additions & 0 deletions search/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ mapping@org.sakaiproject.search.elasticsearch.ElasticSearchIndexBuilder=
"index": "not_analyzed", \
"store": "yes" \
}, \
"creatordisplayname": { \
"type": "text", \
"store": "true", \
"term_vector" : "with_positions_offsets", \
"search_analyzer": "str_search_analyzer", \
"analyzer": "str_index_analyzer" \
}, \
"title": { \
"type": "string", \
"store": "yes", \
Expand Down
Loading

0 comments on commit 9b5f03b

Please sign in to comment.