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

Java tutorial

Introduction

Here is the source code for ar.com.zauber.garfio.modules.mantis.model.actions.FixInVersionAction.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.util.List;

import org.apache.commons.collections15.CollectionUtils;
import org.apache.commons.collections15.Predicate;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.mantisbt.connect.JMTException;
import org.mantisbt.connect.service.ProjectVersionData;

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;

/**
 * Writes the artifact id where the issue is resolved. 
 * 
 * @author Juan F. Codagnone
 * @since Mar 25, 2007
 */
public class FixInVersionAction extends AbstractIssueAction {
    /**  version to add */
    private String version;

    /**
     * Creates the AddNoteToIssueAction.
     *
     * @param issue involved issue 
     * @param session tracker session
     * @throws JMTException on i/o error.
     */
    public FixInVersionAction(final MantisIssue issue, final MantisTrackerSession session, final String aversion)
            throws JMTException {
        super(issue, session);
        Validate.isTrue(!StringUtils.isBlank(aversion), "you must set a version number");

        final String versionNumber = StringUtils.trim(aversion);
        final ProjectVersionData[] versions = session.getSession()
                .getVersions(mantisIssue.getIssueData().getProject().getId().longValue());
        String tmp = null;
        for (final ProjectVersionData v : versions) {
            if (v.getName().equalsIgnoreCase(versionNumber)) {
                if (v.getReleased()) {
                    throw new IllegalArgumentException("you specified version `" + versionNumber
                            + "' but it has already been released. " + "Ignoring action.");
                } else {
                    tmp = v.getName();
                    break;
                }
            }
        }
        if (tmp == null) {
            final StringBuilder sb = new StringBuilder();
            sb.append("you specified version `");
            sb.append(versionNumber);
            sb.append("' but posible versions are: ");
            for (final ProjectVersionData v : versions) {
                if (!v.getReleased()) {
                    sb.append(v.getName());
                    sb.append(' ');
                }
            }
            sb.append(". Ignoring action.");
            throw new IllegalArgumentException(sb.toString());
        }
        this.version = tmp;
    }

    /** 
     * {@link AbstractIssueAction}{@link #preExecutionValidation(List)}
     * @throws MustNotExecuteException on error 
     */
    @Override
    public final void preExecutionValidation(final List<Action> allActions) throws MustNotExecuteException {
        if (mantisIssue.isFixed()) {
            Action resolveIssueAction = CollectionUtils.find(allActions, new Predicate<Action>() {
                public boolean evaluate(final Action action) {
                    return action instanceof ResolveIssueAction;
                }
            });
            if (resolveIssueAction == null) {
                throw new MustNotExecuteException("version specified to an issue that is not fixed. You "
                        + "must also use the resolved action");
            }
        }
    }

    /**  @see Action#execute() */
    public final void execute() {
        if (version != null) {
            mantisIssue.getIssueData().setFixed_in_version(version);
            updateIssue();
        }
    }
}