com.mycollab.module.project.view.bug.AssignBugWindow.java Source code

Java tutorial

Introduction

Here is the source code for com.mycollab.module.project.view.bug.AssignBugWindow.java

Source

/**
 * This file is part of mycollab-web.
 *
 * mycollab-web is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * mycollab-web is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with mycollab-web.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.mycollab.module.project.view.bug;

import com.mycollab.common.domain.CommentWithBLOBs;
import com.mycollab.common.i18n.GenericI18Enum;
import com.mycollab.common.service.CommentService;
import com.mycollab.eventmanager.EventBusFactory;
import com.mycollab.module.project.CurrentProjectVariables;
import com.mycollab.module.project.ProjectTypeConstants;
import com.mycollab.module.project.event.BugEvent;
import com.mycollab.module.project.i18n.BugI18nEnum;
import com.mycollab.module.project.view.settings.component.ProjectMemberSelectionField;
import com.mycollab.module.tracker.domain.BugWithBLOBs;
import com.mycollab.module.tracker.domain.SimpleBug;
import com.mycollab.module.tracker.service.BugService;
import com.mycollab.spring.AppContextUtil;
import com.mycollab.vaadin.MyCollabUI;
import com.mycollab.vaadin.UserUIContext;
import com.mycollab.vaadin.ui.AbstractBeanFieldGroupEditFieldFactory;
import com.mycollab.vaadin.ui.AbstractFormLayoutFactory;
import com.mycollab.vaadin.ui.AdvancedEditBeanForm;
import com.mycollab.vaadin.ui.GenericBeanForm;
import com.mycollab.vaadin.web.ui.WebThemes;
import com.mycollab.vaadin.web.ui.grid.GridFormLayoutHelper;
import com.vaadin.event.ShortcutAction;
import com.vaadin.server.FontAwesome;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.ui.*;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;
import org.vaadin.viritin.button.MButton;
import org.vaadin.viritin.layouts.MHorizontalLayout;
import org.vaadin.viritin.layouts.MWindow;

import java.util.GregorianCalendar;

/**
 * @author MyCollab Ltd.
 * @since 1.0
 */
class AssignBugWindow extends MWindow {
    private static final long serialVersionUID = 1L;
    private final SimpleBug bug;

    AssignBugWindow(SimpleBug bug) {
        super(UserUIContext.getMessage(BugI18nEnum.OPT_ASSIGN_BUG, bug.getName()));
        this.bug = bug;

        VerticalLayout contentLayout = new VerticalLayout();
        EditForm editForm = new EditForm();
        contentLayout.addComponent(editForm);
        editForm.setBean(bug);
        contentLayout.setMargin(new MarginInfo(false, false, true, false));

        this.withWidth("750px").withResizable(false).withModal(true).withContent(contentLayout).withCenter();
    }

    private class EditForm extends AdvancedEditBeanForm<BugWithBLOBs> {
        private static final long serialVersionUID = 1L;
        private RichTextArea commentArea;

        @Override
        public void setBean(final BugWithBLOBs item) {
            this.setFormLayoutFactory(new FormLayoutFactory());
            this.setBeanFormFieldFactory(new EditFormFieldFactory(EditForm.this));
            super.setBean(item);
        }

        class FormLayoutFactory extends AbstractFormLayoutFactory {
            private static final long serialVersionUID = 1L;
            private GridFormLayoutHelper informationLayout;

            @Override
            public AbstractComponent getLayout() {
                final VerticalLayout layout = new VerticalLayout();
                this.informationLayout = GridFormLayoutHelper.defaultFormLayoutHelper(2, 2);

                layout.addComponent(this.informationLayout.getLayout());

                final MButton approveBtn = new MButton(UserUIContext.getMessage(GenericI18Enum.BUTTON_ASSIGN),
                        clickEvent -> {
                            if (EditForm.this.validateForm()) {
                                // Save bug status and assignee
                                final BugService bugService = AppContextUtil.getSpringBean(BugService.class);
                                bugService.updateSelectiveWithSession(AssignBugWindow.this.bug,
                                        UserUIContext.getUsername());

                                // Save comment
                                final String commentValue = commentArea.getValue();
                                if (StringUtils.isNotBlank(commentValue)) {
                                    final CommentWithBLOBs comment = new CommentWithBLOBs();
                                    comment.setComment(Jsoup.clean(commentValue, Whitelist.relaxed()));
                                    comment.setCreatedtime(new GregorianCalendar().getTime());
                                    comment.setCreateduser(UserUIContext.getUsername());
                                    comment.setSaccountid(MyCollabUI.getAccountId());
                                    comment.setType(ProjectTypeConstants.BUG);
                                    comment.setTypeid("" + bug.getId());
                                    comment.setExtratypeid(CurrentProjectVariables.getProjectId());

                                    CommentService commentService = AppContextUtil
                                            .getSpringBean(CommentService.class);
                                    commentService.saveWithSession(comment, UserUIContext.getUsername());
                                }

                                close();
                                EventBusFactory.getInstance().post(new BugEvent.BugChanged(this, bug.getId()));
                            }
                        }).withStyleName(WebThemes.BUTTON_ACTION).withIcon(FontAwesome.SHARE);
                approveBtn.setClickShortcut(ShortcutAction.KeyCode.ENTER);

                MButton cancelBtn = new MButton(UserUIContext.getMessage(GenericI18Enum.BUTTON_CANCEL),
                        clickEvent -> close()).withStyleName(WebThemes.BUTTON_OPTION);

                MHorizontalLayout controlsBtn = new MHorizontalLayout(cancelBtn, approveBtn)
                        .withMargin(new MarginInfo(true, true, false, false));
                layout.addComponent(controlsBtn);
                layout.setComponentAlignment(controlsBtn, Alignment.MIDDLE_RIGHT);

                return layout;
            }

            @Override
            protected Component onAttachField(Object propertyId, Field<?> field) {
                if (propertyId.equals("assignuser")) {
                    return informationLayout.addComponent(field,
                            UserUIContext.getMessage(GenericI18Enum.FORM_ASSIGNEE), 0, 0);
                } else if (propertyId.equals("comment")) {
                    return informationLayout.addComponent(field,
                            UserUIContext.getMessage(GenericI18Enum.OPT_COMMENT), 0, 1, 2, "100%");
                }
                return null;
            }
        }

        private class EditFormFieldFactory extends AbstractBeanFieldGroupEditFieldFactory<BugWithBLOBs> {
            private static final long serialVersionUID = 1L;

            public EditFormFieldFactory(GenericBeanForm<BugWithBLOBs> form) {
                super(form);
            }

            @Override
            protected Field<?> onCreateField(final Object propertyId) {
                if (propertyId.equals("assignuser")) {
                    return new ProjectMemberSelectionField();
                } else if (propertyId.equals("comment")) {
                    commentArea = new RichTextArea();
                    return commentArea;
                }

                return null;
            }
        }
    }
}