ar.com.zauber.garfio.modules.mantis.model.actions.AddNoteToIssueAction.java Source code

Java tutorial

Introduction

Here is the source code for ar.com.zauber.garfio.modules.mantis.model.actions.AddNoteToIssueAction.java

Source

/**
 * Copyright (c) 2007-2009 Zauber S.A. <http://www.zauber.com.ar/>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.zauber.garfio.modules.mantis.model.actions;

import java.math.BigInteger;
import java.util.List;

import org.apache.commons.collections15.Predicate;
import org.apache.commons.lang.UnhandledException;
import org.mantisbt.connect.JMTException;
import org.mantisbt.connect.service.AccountData;
import org.mantisbt.connect.service.IssueData;
import org.mantisbt.connect.service.ObjectRef;

import ar.com.zauber.garfio.modules.mantis.model.MantisIssue;
import ar.com.zauber.garfio.modules.mantis.model.MantisTrackerSession;
import ar.com.zauber.garfio.modules.model.Action;
import ar.com.zauber.garfio.modules.model.MustNotExecuteException;

/**
 * Adds a comment to an issue.
 * 
 * @author Fernando Zunino
 * @author Juan F. Codagnone
 * @since 09/02/2007
 */
public class AddNoteToIssueAction extends AbstractIssueAction {
    /**  note to add */
    private final String note;

    /**
     * Creates the AddNoteToIssueAction.
     *
     * @param issue involved issue 
     * @param note note to add
     * @param session tracker session
     */
    public AddNoteToIssueAction(final MantisIssue issue, final MantisTrackerSession session, final String note) {
        super(issue, session);

        this.note = note;
    }

    /** @see Action#execute() */
    public final void execute() {
        try {
            session.getSession().addNote(mantisIssue.getIssueData().getId().longValue(),
                    session.getSession().newNote(note));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**  @see Object#equals(Object) */
    @Override
    public final boolean equals(final Object o) {
        boolean ret = false;

        if (o == this) {
            ret = true;
        } else if (o instanceof AddNoteToIssueAction) {
            AddNoteToIssueAction action = (AddNoteToIssueAction) o;
            ret = super.equals(action) && note.equals(action.note);
        }

        return ret;
    }

    /**  @see Object#hashCode() */
    @Override
    public final int hashCode() {
        return super.hashCode() + note.hashCode();
    }

    /**
     * Returns the note.
     * 
     * @return <code>String</code> with the note.
     */
    public final String getNote() {
        return note;
    }

    /** @see AbstractIssueAction#preExecutionValidation(List) */
    @Override
    public final void preExecutionValidation(final List<Action> allActions) throws MustNotExecuteException {
        super.preExecutionValidation(allActions);

        try {
            boolean canComment = false;

            if (mantisIssue.isFixed()) {
                final BigInteger projId = mantisIssue.getIssueData().getProject().getId();

                final ObjectRef manager = session.getManagerAccess();
                final AccountData[] accounts = session.getSession().getProjectUsers(projId.longValue(),
                        manager.getId().intValue());

                for (final AccountData account : accounts) {
                    if (account.getName().equals(session.getUsername())) {
                        canComment = true;
                        break;
                    }
                }

                if (!canComment) {
                    throw new MustNotExecuteException(
                            "only managers users can add " + "a comment on a closed issue...");
                }
            } else {
                canComment = true;
            }
        } catch (final JMTException e) {
            throw new UnhandledException(e);
        }

    }
}