use of com.helger.peppol.comment.domain.ICommentIterationCallback in project peppol-practical by phax.
the class CommentUI method getCommentList.
@Nonnull
public static IHCNode getCommentList(@Nonnull final ILayoutExecutionContext aLEC, @Nonnull final ITypedObject<String> aObject, @Nonnull final CommentAction aCommentAction, @Nullable final CommentFormErrors aFormErrors, @Nullable final IHCNode aMessageBox, final boolean bShowCreateComments) {
ValueEnforcer.notNull(aLEC, "LEC");
ValueEnforcer.notNull(aObject, "Object");
ValueEnforcer.notNull(aCommentAction, "CommentAction");
final Locale aDisplayLocale = aLEC.getDisplayLocale();
final IRequestWebScopeWithoutResponse aRequestScope = aLEC.getRequestScope();
final HCDiv ret = new HCDiv();
final String sResultDivID = ret.ensureID().getID();
final boolean bUserCanCreateComments = CommentSecurity.canCurrentUserPostComments();
final boolean bIsAdmin = aLEC.isLoggedInUserAdministrator();
// Get all existing comments
final List<ICommentThread> aComments = CommentThreadManager.getInstance().getAllCommentThreadsOfObject(aObject);
if (CollectionHelper.isNotEmpty(aComments)) {
final IUserManager aUserMgr = PhotonSecurityManager.getUserMgr();
final boolean bIsCommentModerator = CommentSecurity.isCurrentUserCommentModerator();
// Container for all threads
final HCDiv aAllThreadsContainer = new HCDiv().addClass(CCommentCSS.CSS_CLASS_COMMENT_CONTAINER);
for (final ICommentThread aCommentThread : CollectionHelper.getSorted(aComments, Comparator.comparing(ICommentThread::getInitialCommentCreationDateTime))) {
// Container for this thread
final HCDiv aThreadContainer = new HCDiv();
aThreadContainer.addClass(CCommentCSS.CSS_CLASS_COMMENT_THREAD);
final NonBlockingStack<AbstractHCDiv<?>> aStack = new NonBlockingStack<>();
aStack.push(aThreadContainer);
aCommentThread.iterateAllComments(new ICommentIterationCallback() {
public void onCommentStart(final int nLevel, @Nullable final IComment aParentComment, @Nonnull final IComment aComment) {
// Show only approved comments
final boolean bIsApproved = aComment.getState().isApproved();
if (bIsApproved || bIsCommentModerator) {
// Get author name and determine if it is a registered user
boolean bRegisteredUser = false;
String sAuthor = null;
if (StringHelper.hasText(aComment.getUserID())) {
final IUser aUser = aUserMgr.getUserOfID(aComment.getUserID());
if (aUser != null) {
sAuthor = aUser.getDisplayName();
bRegisteredUser = true;
}
}
if (sAuthor == null)
sAuthor = aComment.getCreatorName();
// Fill panel header
final BootstrapCard aCommentPanel = new BootstrapCard();
final BootstrapCardHeader aHeader = aCommentPanel.createAndAddHeader();
final BootstrapCardBody aBody = aCommentPanel.createAndAddBody();
if (!bIsApproved)
aHeader.addClass(CBootstrapCSS.BG_DANGER);
// Is comment deleted?
if (aComment.isDeleted())
aHeader.addChild(new HCStrong().addChild(ECommentText.MSG_IS_DELETED.getDisplayText(aDisplayLocale)));
// Creation date
aHeader.addChild(new HCSpan().addChild(PDTToString.getAsString(aComment.getCreationDateTime(), aDisplayLocale)).addClass(CCommentCSS.CSS_CLASS_COMMENT_CREATIONDT));
// Author
aHeader.addChild(ECommentText.MSG_BY.getDisplayText(aDisplayLocale));
final HCSpan aAuthor = new HCSpan().addChild(sAuthor).addClass(CCommentCSS.CSS_CLASS_COMMENT_AUTHOR);
if (bRegisteredUser)
aAuthor.addClass(CCommentCSS.CSS_CLASS_COMMENT_REGISTERED_USER);
if (bIsAdmin)
aAuthor.addChild(bRegisteredUser ? " [registered]" : " [not-registered]");
aHeader.addChild(aAuthor);
// Title
if (StringHelper.hasText(aComment.getTitle())) {
aHeader.addChild(ECommentText.MSG_SEPARATOR_AUTHOR_TITLE.getDisplayText(aDisplayLocale));
aHeader.addChild(new HCSpan().addChild(aComment.getTitle()).addClass(CCommentCSS.CSS_CLASS_COMMENT_TITLE));
}
// Toolbar
final HCSpan aCommentToolbar = new HCSpan().addClass(CCommentCSS.CSS_CLASS_COMMENT_TOOLBAR);
HCDiv aCommentResponseContainer = null;
// Respond to a comment - at maximum 6 levels
if (bShowCreateComments && bUserCanCreateComments && !aComment.isDeleted() && nLevel < 6) {
aCommentResponseContainer = new HCDiv();
final BootstrapButton aResponseButton = new BootstrapButton(EBootstrapButtonSize.SMALL).setIcon(EDefaultIcon.ADD);
aCommentToolbar.addChild(aResponseButton);
aCommentToolbar.addChild(new BootstrapTooltip(aResponseButton).setTitle(ECommentText.TOOLTIP_RESPONSE.getDisplayText(aDisplayLocale)));
if (aCommentAction.isMatching(ECommentAction.ADD_COMMENT, aCommentThread, aComment) && aFormErrors != null && aFormErrors.isReplyTo(aCommentThread, aComment)) {
// Upon adding a response
if (aMessageBox == null || !aFormErrors.isEmpty()) {
// Show the input form again
aCommentResponseContainer.addChild(getCreateComment(aLEC, sResultDivID, aObject, aCommentThread, aComment, aFormErrors, aMessageBox));
} else {
// Show the success or error message
aBody.addChild(aMessageBox);
}
} else {
// Add the JS to show the input form
final JSAnonymousFunction aOnSuccess = new JSAnonymousFunction();
final JSVar aJSData = aOnSuccess.param("data");
aOnSuccess.body().add(JQuery.idRef(aCommentResponseContainer).empty().append(aJSData.ref(PhotonUnifiedResponse.HtmlHelper.PROPERTY_HTML)));
final JQueryInvocation aResponseAction = new JQueryAjaxBuilder().url(CAjax.COMMENT_SHOW_INPUT.getInvocationURL(aRequestScope)).data(new JSAssocArray().add(AjaxExecutorCommentShowInput.PARAM_OBJECT_TYPE, aObject.getObjectType().getName()).add(AjaxExecutorCommentShowInput.PARAM_OBJECT_ID, aObject.getID()).add(AjaxExecutorCommentShowInput.PARAM_COMMENT_THREAD_ID, aCommentThread.getID()).add(AjaxExecutorCommentShowInput.PARAM_COMMENT_ID, aComment.getID()).add(AjaxExecutorCommentShowInput.PARAM_RESULT_DIV_ID, sResultDivID)).success(JSJQueryHelper.jqueryAjaxSuccessHandler(aOnSuccess, null)).build();
aResponseButton.setOnClick(aResponseAction);
}
}
if (bIsCommentModerator) {
if (aCommentAction.isMatching(ECommentAction.DELETE_COMMENT, aCommentThread, aComment))
aBody.addChild(aMessageBox);
// Can the comment be deleted?
if (!aComment.isDeleted()) {
final BootstrapButton aDeleteButton = new BootstrapButton(EBootstrapButtonSize.SMALL).setIcon(EDefaultIcon.DELETE);
aCommentToolbar.addChild(aDeleteButton);
aCommentToolbar.addChild(new BootstrapTooltip(aDeleteButton).setTitle(ECommentText.TOOLTIP_DELETE.getDisplayText(aDisplayLocale)));
final JSAnonymousFunction aOnSuccess = new JSAnonymousFunction();
final JSVar aJSData = aOnSuccess.param("data");
aOnSuccess.body().add(JQuery.idRef(sResultDivID).replaceWith(aJSData.ref(PhotonUnifiedResponse.HtmlHelper.PROPERTY_HTML)));
final JQueryInvocation aDeleteAction = new JQueryAjaxBuilder().url(CAjax.COMMENT_DELETE.getInvocationURL(aRequestScope)).data(new JSAssocArray().add(AjaxExecutorCommentDelete.PARAM_OBJECT_TYPE, aObject.getObjectType().getName()).add(AjaxExecutorCommentDelete.PARAM_OBJECT_ID, aObject.getID()).add(AjaxExecutorCommentDelete.PARAM_COMMENT_THREAD_ID, aCommentThread.getID()).add(AjaxExecutorCommentDelete.PARAM_COMMENT_ID, aComment.getID())).success(JSJQueryHelper.jqueryAjaxSuccessHandler(aOnSuccess, null)).build();
aDeleteButton.setOnClick(aDeleteAction);
}
// Show source host and further info
aCommentToolbar.addChild(BootstrapSimpleTooltip.createSimpleTooltip(ECommentText.TOOLTIP_HOST.getDisplayTextWithArgs(aDisplayLocale, aComment.getHost())));
}
if (aCommentToolbar.hasChildren())
aHeader.addChild(aCommentToolbar);
// Last modification
if (aComment.getLastModificationDateTime() != null) {
final String sLastModDT = PDTToString.getAsString(aComment.getLastModificationDateTime(), aDisplayLocale);
final String sLastModText = aComment.getEditCount() > 0 ? ECommentText.MSG_EDITED_AND_LAST_MODIFICATION.getDisplayTextWithArgs(aDisplayLocale, Integer.valueOf(aComment.getEditCount()), sLastModDT) : ECommentText.MSG_LAST_MODIFICATION.getDisplayTextWithArgs(aDisplayLocale, sLastModDT);
aHeader.addChild(new HCDiv().addChild(sLastModText).addClass(CCommentCSS.CSS_CLASS_COMMENT_LAST_MODIFICATION));
}
// Show the main comment text
aBody.addClass(CCommentCSS.CSS_CLASS_SINGLE_COMMENT);
// Always put the text as the first part of the body
aBody.addChildAt(0, new HCDiv().addChildren(HCExtHelper.nl2brList(aComment.getText())).addClass(CCommentCSS.CSS_CLASS_COMMENT_TEXT));
// the dummy container for new comment form
aBody.addChild(aCommentResponseContainer);
aStack.peek().addChild(aCommentPanel);
aStack.push(aBody);
} else {
// Don't display - push the previous item
aStack.push(aStack.peek());
}
}
public void onCommentEnd(final int nLevel, @Nullable final IComment aParentComment, @Nonnull final IComment aComment) {
aStack.pop();
}
});
// Show only thread panels which contain at least one comment
if (aThreadContainer.hasChildren())
aAllThreadsContainer.addChild(aThreadContainer);
}
ret.addChild(aAllThreadsContainer);
}
if (bShowCreateComments) {
// Create comment only for logged in users
if (bUserCanCreateComments) {
// Add "create comment" button
final boolean bIsForCreateThread = aCommentAction.isMatching(ECommentAction.CREATE_THREAD);
ret.addChild(getCreateComment(aLEC, sResultDivID, aObject, null, null, bIsForCreateThread ? aFormErrors : null, bIsForCreateThread ? aMessageBox : null));
} else
ret.addChild(new BootstrapBadge(EBootstrapBadgeType.INFO).addChild(ECommentText.MSG_LOGIN_TO_COMMENT.getDisplayText(aDisplayLocale)));
}
return ret;
}
Aggregations