Search in sources :

Example 1 with CommentFormErrors

use of com.helger.peppol.comment.ui.CommentFormErrors in project peppol-practical by phax.

the class AjaxExecutorCommentAdd method handleRequest.

public void handleRequest(@Nonnull final IRequestWebScopeWithoutResponse aRequestScope, @Nonnull final PhotonUnifiedResponse aAjaxResponse) throws Exception {
    final LayoutExecutionContext aLEC = LayoutExecutionContext.createForAjaxOrAction(aRequestScope);
    final Locale aDisplayLocale = aLEC.getDisplayLocale();
    final String sObjectType = aRequestScope.params().getAsString(PARAM_OBJECT_TYPE);
    final String sObjectID = aRequestScope.params().getAsString(PARAM_OBJECT_ID);
    final String sCommentThreadID = aRequestScope.params().getAsString(PARAM_COMMENT_THREAD_ID);
    final String sCommentID = aRequestScope.params().getAsString(PARAM_COMMENT_ID);
    String sAuthor = aRequestScope.params().getAsString(PARAM_AUTHOR);
    final String sTitle = aRequestScope.params().getAsString(PARAM_TITLE);
    final String sText = aRequestScope.params().getAsString(PARAM_TEXT);
    // Get info on current user
    final IUser aCurrentUser = LoggedInUserManager.getInstance().getCurrentUser();
    final String sCurrentUserID = aCurrentUser != null ? aCurrentUser.getID() : null;
    if (aCurrentUser != null)
        sAuthor = aCurrentUser.getDisplayName();
    if (StringHelper.hasText(sObjectType) && StringHelper.hasText(sObjectID) && StringHelper.hasText(sCommentThreadID) && StringHelper.hasText(sCommentID) && CommentSecurity.canCurrentUserPostComments()) {
        // Create a dummy object
        final ITypedObject<String> aOwner = TypedObject.create(new ObjectType(sObjectType), sObjectID);
        final ICommentThread aCommentThread = CommentThreadManager.getInstance().getCommentThreadOfID(aOwner, sCommentThreadID);
        if (aCommentThread != null) {
            final IComment aParentComment = aCommentThread.getCommentOfID(sCommentID);
            if (aParentComment != null) {
                final CommentFormErrors aFormErrors = CommentFormErrors.createForReply(aCommentThread, aParentComment);
                if (StringHelper.hasNoText(sAuthor)) {
                    // No author provided
                    aFormErrors.addFieldError(PARAM_AUTHOR, ECommentText.MSG_ERR_COMMENT_NO_AUTHOR.getDisplayText(aDisplayLocale));
                }
                if (StringHelper.hasNoText(sText)) {
                    // No text provided
                    aFormErrors.addFieldError(PARAM_TEXT, ECommentText.MSG_ERR_COMMENT_NO_TEXT.getDisplayText(aDisplayLocale));
                }
                IHCNode aMessageBox = null;
                if (aFormErrors.isEmpty()) {
                    // Go ahead and save
                    final ESuccess eSuccess = CommentThreadManager.getInstance().addCommentToThread(aOwner, sCommentThreadID, sCommentID, new Comment(aRequestScope.getRemoteHost(), ECommentState.APPROVED, sCurrentUserID, sAuthor, sTitle, sText));
                    if (eSuccess.isSuccess())
                        aMessageBox = success(ECommentText.MSG_COMMENT_SAVE_SUCCESS.getDisplayText(aDisplayLocale));
                    else
                        aMessageBox = error(ECommentText.MSG_COMMENT_SAVE_FAILURE.getDisplayText(aDisplayLocale));
                }
                // List of exiting comments + message box
                aAjaxResponse.html(CommentUI.getCommentList(aLEC, aOwner, CommentAction.createForComment(ECommentAction.ADD_COMMENT, aCommentThread, aParentComment), aFormErrors, aMessageBox, true));
                return;
            }
        }
    }
    // Somebody played around with the API
    LOGGER.warn("Failed to resolve comment object type '" + sObjectType + "' and/or object ID '" + sObjectID + "' for adding to comment '" + sCommentID + "' in thread '" + sCommentThreadID + "'");
    aAjaxResponse.createNotFound();
}
Also used : Locale(java.util.Locale) ESuccess(com.helger.commons.state.ESuccess) LayoutExecutionContext(com.helger.photon.core.execcontext.LayoutExecutionContext) ObjectType(com.helger.commons.type.ObjectType) CommentFormErrors(com.helger.peppol.comment.ui.CommentFormErrors) IComment(com.helger.peppol.comment.domain.IComment) Comment(com.helger.peppol.comment.domain.Comment) IComment(com.helger.peppol.comment.domain.IComment) IUser(com.helger.photon.security.user.IUser) ICommentThread(com.helger.peppol.comment.domain.ICommentThread) IHCNode(com.helger.html.hc.IHCNode)

Example 2 with CommentFormErrors

use of com.helger.peppol.comment.ui.CommentFormErrors in project peppol-practical by phax.

the class AjaxExecutorCommentCreateThread method handleRequest.

public void handleRequest(@Nonnull final IRequestWebScopeWithoutResponse aRequestScope, @Nonnull final PhotonUnifiedResponse aAjaxResponse) throws Exception {
    final LayoutExecutionContext aLEC = LayoutExecutionContext.createForAjaxOrAction(aRequestScope);
    final Locale aDisplayLocale = aLEC.getDisplayLocale();
    final String sObjectType = aRequestScope.params().getAsString(PARAM_OBJECT_TYPE);
    final String sObjectID = aRequestScope.params().getAsString(PARAM_OBJECT_ID);
    String sAuthor = aRequestScope.params().getAsString(PARAM_AUTHOR);
    final String sTitle = aRequestScope.params().getAsString(PARAM_TITLE);
    final String sText = aRequestScope.params().getAsString(PARAM_TEXT);
    // Get info on current user
    final IUser aCurrentUser = LoggedInUserManager.getInstance().getCurrentUser();
    final String sCurrentUserID = aCurrentUser != null ? aCurrentUser.getID() : null;
    if (aCurrentUser != null)
        sAuthor = aCurrentUser.getDisplayName();
    if (StringHelper.hasText(sObjectType) && StringHelper.hasText(sObjectID) && CommentSecurity.canCurrentUserPostComments()) {
        // Create a dummy object
        final ITypedObject<String> aOwner = TypedObject.create(new ObjectType(sObjectType), sObjectID);
        final CommentFormErrors aFormErrors = CommentFormErrors.createForNewThread();
        if (StringHelper.hasNoText(sAuthor)) {
            // No author provided
            aFormErrors.addFieldError(PARAM_AUTHOR, ECommentText.MSG_ERR_COMMENT_NO_AUTHOR.getDisplayText(aDisplayLocale));
        }
        if (StringHelper.hasNoText(sText)) {
            // No text provided
            aFormErrors.addFieldError(PARAM_TEXT, ECommentText.MSG_ERR_COMMENT_NO_TEXT.getDisplayText(aDisplayLocale));
        }
        IHCNode aMessageBox = null;
        if (aFormErrors.isEmpty()) {
            // Go ahead and save
            final ICommentThread aNewThread = CommentThreadManager.getInstance().createNewThread(aOwner, new Comment(aRequestScope.getRemoteHost(), ECommentState.APPROVED, sCurrentUserID, sAuthor, sTitle, sText));
            if (aNewThread != null)
                aMessageBox = success(ECommentText.MSG_COMMENT_SAVE_SUCCESS.getDisplayText(aDisplayLocale));
            else
                aMessageBox = error(ECommentText.MSG_COMMENT_SAVE_FAILURE.getDisplayText(aDisplayLocale));
        }
        // List of exiting comments + message box
        aAjaxResponse.html(CommentUI.getCommentList(aLEC, aOwner, CommentAction.createGeneric(ECommentAction.CREATE_THREAD), aFormErrors, aMessageBox, true));
        return;
    }
    // Somebody played around with the API
    LOGGER.warn("Failed to resolve comment object type '" + sObjectType + "' and/or object ID '" + sObjectID + "'");
    aAjaxResponse.createNotFound();
}
Also used : Locale(java.util.Locale) LayoutExecutionContext(com.helger.photon.core.execcontext.LayoutExecutionContext) ObjectType(com.helger.commons.type.ObjectType) CommentFormErrors(com.helger.peppol.comment.ui.CommentFormErrors) Comment(com.helger.peppol.comment.domain.Comment) IUser(com.helger.photon.security.user.IUser) IHCNode(com.helger.html.hc.IHCNode) ICommentThread(com.helger.peppol.comment.domain.ICommentThread)

Example 3 with CommentFormErrors

use of com.helger.peppol.comment.ui.CommentFormErrors in project peppol-practical by phax.

the class AppPageViewExternal method fillContent.

@Override
protected final void fillContent(@Nonnull final WebPageExecutionContext aWPEC) {
    super.fillContent(aWPEC);
    if (AppConfig.isWebPageCommentingEnabled()) {
        // Show comments and "add comment"
        final HCNodeList aNodeList = aWPEC.getNodeList();
        final TypedObject<String> aTO = TypedObject.create(CPPApp.OT_PAGE, getID());
        aNodeList.addChild(CommentUI.getCommentList(aWPEC, aTO, CommentAction.createGeneric(ECommentAction.NONE), (CommentFormErrors) null, (IHCNode) null, true));
    }
}
Also used : CommentFormErrors(com.helger.peppol.comment.ui.CommentFormErrors) HCNodeList(com.helger.html.hc.impl.HCNodeList) IHCNode(com.helger.html.hc.IHCNode)

Aggregations

IHCNode (com.helger.html.hc.IHCNode)3 CommentFormErrors (com.helger.peppol.comment.ui.CommentFormErrors)3 ObjectType (com.helger.commons.type.ObjectType)2 Comment (com.helger.peppol.comment.domain.Comment)2 ICommentThread (com.helger.peppol.comment.domain.ICommentThread)2 LayoutExecutionContext (com.helger.photon.core.execcontext.LayoutExecutionContext)2 IUser (com.helger.photon.security.user.IUser)2 Locale (java.util.Locale)2 ESuccess (com.helger.commons.state.ESuccess)1 HCNodeList (com.helger.html.hc.impl.HCNodeList)1 IComment (com.helger.peppol.comment.domain.IComment)1