ar.com.zauber.garfio.modules.mantis.model.MantisTrackerSession.java Source code

Java tutorial

Introduction

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

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.mantisbt.connect.Enumeration;
import org.mantisbt.connect.ISession;
import org.mantisbt.connect.JMTException;
import org.mantisbt.connect.service.ObjectRef;
import org.xml.sax.SAXException;

import ar.com.zauber.garfio.modules.model.Issue;
import ar.com.zauber.garfio.modules.model.TrackerSession;
import ar.com.zauber.garfio.modules.services.ActionFactory;

/**
 * {@link TrackerSession} for MantisBT.
 * 
 * 
 * @author Juan F. Codagnone
 * @since Oct 7, 2007
 */
public class MantisTrackerSession implements TrackerSession {
    private final ActionFactory actionFactory;
    private final ISession session;
    private final String username;

    /** constructor 
     * @param username */
    public MantisTrackerSession(final ActionFactory actionFactory, final ISession session, final String username) {
        Validate.notNull(actionFactory, "actionFactory is null");
        Validate.notNull(session, "session is null");
        Validate.isTrue(!StringUtils.isBlank(username), "username is blank");

        this.actionFactory = actionFactory;
        this.session = session;
        this.username = username;
    }

    /** @return <code>ISession</code> with the session. */
    public final ISession getSession() {
        return session;
    }

    /** @see TrackerSession#getActionFactory() */
    public final ActionFactory getActionFactory() {
        return actionFactory;
    }

    /** @see TrackerSession#getIssue(long) */
    public final Issue getIssue(final String id) {
        validateIssueId(id);
        try {
            return new MantisIssue(session.getIssue(Long.parseLong(id)), this);
        } catch (final JMTException e) {
            throw new RuntimeException(e);
        }

    }

    /** @see TrackerSession#issueExists(long) */
    public final boolean issueExists(final String issueId) {
        validateIssueId(issueId);
        boolean ret = false;
        try {
            final long issue = Long.parseLong(issueId);
            ret = session.issueExists(issue);
            if (ret) {
                // issueExists no siempre funciona bien para verificar que
                // se tiene permiso para accceder
                session.getIssue(issue);
            }

        } catch (final JMTException e) {
            if ("Access Denied".equals(e.getMessage())) {
                ret = false;
            } else {
                Throwable t = e;
                while (t.getCause() != null) {
                    t = t.getCause();
                }
                if (t instanceof SAXException && t.getMessage().startsWith("Bad envelope tag:")) {
                    ret = false;
                } else {
                    throw new RuntimeException(e);
                }

            }
        }
        return ret;
    }

    /** throws {@link IllegalArgumentException} if issueid doesnt look
     *  like a mantis issue id */
    private void validateIssueId(final String issueid) {
        try {
            Long.parseLong(issueid);
        } catch (final NumberFormatException e) {
            throw new IllegalArgumentException(
                    "Matis only suports issuesid that are numeric. " + issueid + " is not numeric!.");
        }
    }

    /** constant */
    private static final String RESOLVED = "resolved";
    /** constant */
    private static final String FIXED = "fixed";

    private ObjectRef resolved;

    /**
     *  obtiene la referencia a la resolucin "resolved"
     *  @throws JMTException on error. 
     */
    public final ObjectRef getResolvedStatus() throws JMTException {
        if (this.resolved == null) {
            ObjectRef resolvedRef = null;
            final ObjectRef[] refs = session.getEnum(Enumeration.STATUS);
            for (final ObjectRef ref : refs) {
                if (ref.getName().equals(RESOLVED)) {
                    resolvedRef = ref;
                    break;
                }
            }
            Validate.notNull(resolvedRef);
            this.resolved = resolvedRef;
        }
        return this.resolved;
    }

    private ObjectRef fixed;

    /**
     *  obtiene la referencia a la resolucin "fixed"
     *  @throws JMTException on error. 
     */
    public final ObjectRef getFixedResolution() throws JMTException {
        if (this.fixed == null) {
            ObjectRef fixedRef = null;
            final ObjectRef[] resolutions = session.getEnum(Enumeration.RESOLUTIONS);
            for (final ObjectRef res : resolutions) {
                if (res.getName().equals(FIXED)) {
                    fixedRef = res;
                    break;
                }
            }
            Validate.notNull(fixedRef);
            this.fixed = fixedRef;
        }
        return this.fixed;
    }

    private ObjectRef developerAccess;

    /**
     *  obtiene la referencia al nivel de acceso developer
     *  @throws JMTException on error. 
     */
    public final ObjectRef getDeveloperAccess() throws JMTException {
        if (this.developerAccess == null) {
            ObjectRef developerAccessRef = null;
            final ObjectRef[] refs = session.getEnum(Enumeration.ACCESS_LEVELS);
            for (final ObjectRef ref : refs) {
                if (ref.getName().equals("developer")) {
                    developerAccessRef = ref;
                    break;
                }
            }
            Validate.notNull(developerAccessRef);
            this.developerAccess = developerAccessRef;
        }
        return this.developerAccess;
    }

    private ObjectRef managerAccess;

    /**
     *  obtiene la referencia al nivel de acceso manager
     *  @throws JMTException on error. 
     */
    public final ObjectRef getManagerAccess() throws JMTException {
        if (this.managerAccess == null) {
            ObjectRef managerAccessRef = null;
            final ObjectRef[] refs = session.getEnum(Enumeration.ACCESS_LEVELS);
            for (final ObjectRef ref : refs) {
                if (ref.getName().equals("manager")) {
                    managerAccessRef = ref;
                    break;
                }
            }
            Validate.notNull(managerAccessRef);
            this.managerAccess = managerAccessRef;
        }

        return this.managerAccess;
    }

    public final String getUsername() {
        return username;
    }

}