Getting erors in .java file while creating days-left-for-jira version plugin?

jira vikram October 31, 2017

Hi Team,

I am trying to create days-left-for-jira version plugin. I have imported my code into eclipse. But I am getting errors in my .java file. I am getting errors at the following lines:
import com.atlassian.plugins.rest.common.security.AnonymousAllowed;(The import com.atlassian.plugins.rest cannot be resolved)

import static com.atlassian.jira.rest.v1.util.CacheControl.NO_CACHE;(The import com.atlassian.jira.rest.v1 cannot be resolved)

@AnonymousAllowed(AnonymousAllowed cannot be resolved to a type)

  return Response.ok(new VersionList(versionList)).cacheControl(NO_CACHE).build();(NO_CACHE cannot be resolved to a variable)

I am unable to resolve above mentioned errors. 

My pom.xml :

<?xml version="1.0" encoding="UTF-8"?>

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">

<modelVersion>4.0.0</modelVersion>

<groupId>com.atlassian.plugin.tutorial</groupId>

<artifactId>jira-gadget-tutorial-plugin</artifactId>

<version>1.0-SNAPSHOT</version>


<organization>

<name>Example Company</name>

<url>http://www.example.com/</url>

</organization>

<name>jira-gadget-tutorial-plugin</name>

<description>This is the com.atlassian.plugin.tutorial:jira-gadget-tutorial-plugin plugin for Atlassian JIRA.</description>

<packaging>atlassian-plugin</packaging>


<dependencies>


<dependency>

<groupId>com.atlassian.gadgets</groupId>

<artifactId>atlassian-gadgets-api</artifactId>

<version>${atlasisan.gadgets.version}</version>

</dependency>


<dependency>

<groupId>com.atlassian.gadgets</groupId>

<artifactId>atlassian-gadgets-spi</artifactId>

<version>${atlasisan.gadgets.version}</version>

</dependency>


<dependency>

<groupId>com.atlassian.jira</groupId>

<artifactId>jira-api</artifactId>

<version>${jira.version}</version>

<scope>provided</scope>

</dependency>


<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.10</version>

<scope>test</scope>

</dependency>


<dependency>

<groupId>com.atlassian.jira</groupId>

<artifactId>jira-func-tests</artifactId>

<version>${jira.version}</version>

<scope>test</scope>

</dependency>


<dependency>

<groupId>javax.ws.rs</groupId>

<artifactId>jsr311-api</artifactId>

<version>1.1</version>

<scope>provided</scope>

</dependency>


<dependency>

<groupId>com.atlassian.plugins.rest</groupId>

<artifactId>atlassian-rest-common</artifactId>

<version>1.1.0.beta6</version>

<type>jar</type>

</dependency>


<dependency>

<groupId>com.atlassian.jira</groupId>

<artifactId>jira-rest-plugin</artifactId>

<version>${jira.version}</version>

<scope>provided</scope>

</dependency>

<!--testing-->

 

<dependency>

<groupId>javax.xml.bind</groupId>

<artifactId>jaxb-api</artifactId>

<version>2.1</version>

<scope>provided</scope>

</dependency>


<dependency>

<groupId>com.atlassian.plugins.rest</groupId>

<artifactId>atlassian-rest-common</artifactId>

<version>1.0.2</version>

<scope>provided</scope>

</dependency>


<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>2.3</version>

<scope>provided</scope>

</dependency>


<dependency>

<groupId>com.atlassian.sal</groupId>

<artifactId>sal-api</artifactId>

<version>2.1.beta4</version>

</dependency>

</dependencies>


<build>


<plugins>


<plugin>

<groupId>com.atlassian.maven.plugins</groupId>

<artifactId>maven-jira-plugin</artifactId>

<version>${amps.version}</version>

<extensions>true</extensions>


<configuration>

<productVersion>${jira.version}</productVersion>

<productDataVersion>${jira.version}</productDataVersion>

</configuration>

</plugin>


<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.6.2</version>


<configuration>

<source>1.6</source>

<target>1.6</target>

</configuration>

</plugin>

</plugins>

</build>


<properties>

<jira.version>7.2.2</jira.version>

<amps.version>6.2.11</amps.version>

<atlasisan.gadgets.version>4.2.21</atlasisan.gadgets.version>

</properties>

</project>

My .java file:

package com.atlassian.plugins.tutorial;

import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.version.Version;
import com.atlassian.jira.project.version.VersionManager;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.util.velocity.VelocityRequestContextFactory;
import com.atlassian.jira.web.util.OutlookDate;
import com.atlassian.plugins.rest.common.security.AnonymousAllowed;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import static com.atlassian.jira.rest.v1.util.CacheControl.NO_CACHE;

/**
* REST endpoint for days left in iteration gadget.
*
* @since v4.0
*/
@Path ("/days-left-in-iteration")
@AnonymousAllowed
@Produces ({ MediaType.APPLICATION_JSON })
public class DaysLeftInVersionResource
{

static final int MILLISECONDS_IN_SEC = 1000;
static final int SECONDS_IN_MIN = 60;
static final int MINUTES_IN_DAY = 60;
static final int HOURS_IN_DAY = 24;
private static final ToStringStyle TO_STRING_STYLE = ToStringStyle.SHORT_PREFIX_STYLE;

private final VersionManager versionManager;
private final JiraAuthenticationContext authenticationContext;
private final SearchService searchService;
private VelocityRequestContextFactory velocityRequestContextFactory;

public DaysLeftInVersionResource(final SearchService searchService, final JiraAuthenticationContext authenticationContext, final VelocityRequestContextFactory velocityRequestContextFactory, final VersionManager versionManager)
{
this.searchService = searchService;
this.authenticationContext = authenticationContext;
this.velocityRequestContextFactory = velocityRequestContextFactory;
this.versionManager = versionManager;
}

@GET
@Path ("/getVersions")

public Response getVersionsForProject(@QueryParam ("projectId") String projectIdString)
{
Long projectId = Long.valueOf(projectIdString.substring("project-".length()));
List<Version> versions = getVersionList(projectId);

 

final OutlookDate outlookDate = authenticationContext.getOutlookDate();
long daysRemaining;
List<VersionInfo> versionList = new ArrayList<VersionInfo>();

String releaseDate;
for (Version v : versions){
releaseDate = formatDate(v.getReleaseDate());
Project srcProj = v.getProjectObject();
ProjectInfo targetProj = new ProjectInfo(srcProj.getId(), srcProj.getKey(), srcProj.getName());
if("".equals(releaseDate)){
daysRemaining = 0;
}
else {
daysRemaining = calculateDaysLeftInVersion(v.getReleaseDate());
}
versionList.add(new VersionInfo(v.getId(),v.getName(), v.getDescription(),releaseDate,targetProj, daysRemaining));
}


return Response.ok(new VersionList(versionList)).cacheControl(NO_CACHE).build();

}

public static long calculateDaysLeftInVersion(Date targetDate){
Date currentDate = new Date(System.currentTimeMillis());
Date releaseDate = targetDate; //TO DO need to write convert string to date FUNCTION
long currentTime = currentDate.getTime();
long targetTime = releaseDate.getTime();

long remainingTime = targetTime - currentTime; //remaining time in milliseconds
long hoursRemaining = remainingTime/(MILLISECONDS_IN_SEC* SECONDS_IN_MIN * MINUTES_IN_DAY);
long daysRemaining = remainingTime/(MILLISECONDS_IN_SEC* SECONDS_IN_MIN * MINUTES_IN_DAY * HOURS_IN_DAY); //
if(hoursRemaining % HOURS_IN_DAY > 0 ) {
daysRemaining++; //the days remaining includes today should be updated for different time z
}
return daysRemaining;
}

public String formatDate(Date date){
if(date == null) {
return "";
} else {
OutlookDate outlookDate = authenticationContext.getOutlookDate();
return outlookDate.formatDMY(date);
}
}
public List<Version> getVersionList(Long projectId)
{
List<Version> versions = new ArrayList<Version>();

versions.addAll(versionManager.getVersionsUnreleased(projectId, false));

Collections.sort(versions, new Comparator<Version>()
{
public int compare(Version v1, Version v2)
{
if(v1.getReleaseDate()== null)
{
return 1;
}
else if (v2.getReleaseDate() == null)
{
return 0;
}
else {
return v1.getReleaseDate().compareTo(v2.getReleaseDate());

}
}
});
return versions;
}
///CLOVER:OFF


/**
* The data structure of the days left in iteration
* <p/>
* It contains the a collection of versionData about all the versions of a particular project
*/
@XmlRootElement
public static class VersionList
{
@XmlElement
Collection<VersionInfo> versionsForProject;

@SuppressWarnings ({ "UnusedDeclaration", "unused" })
private VersionList()
{ }

VersionList(final Collection<VersionInfo> versionsForProject)
{
this.versionsForProject = versionsForProject;
}

public Collection<VersionInfo> getVersionsForProject()
{
return versionsForProject;

}

}
@XmlRootElement
public static class ProjectInfo
{

@XmlElement
private long id;

@XmlElement
private String key;

@XmlElement
private String name;

@SuppressWarnings ({ "UnusedDeclaration", "unused" })
private ProjectInfo()
{}

ProjectInfo(final long id, String key, String name)
{
this.id = id;
this.key = key;
this.name = name;
}

public long getId()
{
return id;
}

public String getKey()
{
return key;
}

public String getName()
{
return name;
}

@Override
public int hashCode()
{
return HashCodeBuilder.reflectionHashCode(this);
}

@Override
public boolean equals(final Object o)
{
return EqualsBuilder.reflectionEquals(this, o);
}

@Override
public String toString()
{
return ToStringBuilder.reflectionToString(this, TO_STRING_STYLE);
}
}
@XmlRootElement
public static class VersionInfo
{
@XmlElement
private long id;

@XmlElement
private String name;

@XmlElement
private String description;

@XmlElement
private String releaseDate;

@XmlElement
private long daysRemaining;

@XmlElement
private boolean isOverdue;

@XmlElement
private ProjectInfo owningProject;


@SuppressWarnings ({ "UnusedDeclaration", "unused" })
private VersionInfo()
{ }

VersionInfo(final long id, final String name, final String description, final String releaseDate, final ProjectInfo owningProject, final long daysRemaining)
{
this.id = id;
this.name = name;
this.description = description;
this.releaseDate = releaseDate;
this.isOverdue = isOverdue();
this.owningProject = owningProject;
this.daysRemaining = daysRemaining;


}

public long getId()
{
return id;
}

public String getName()
{
return name;
}

public String getDescription()
{
return description;
}

public String getReleaseDate()
{
return releaseDate;
}

public long getDaysRemaining()
{
return daysRemaining;
}

public boolean isOverdue ()
{
if (daysRemaining < 0 )
{
isOverdue = true;
}
else
{
isOverdue = false;
}
return isOverdue;
}

public ProjectInfo getOwningProject()
{
return owningProject;
}

 

}


}

My atlassian-plugin.xml:

<?xml version="1.0"?>

<atlassian-plugin plugins-version="2" name="${project.artifactId}" key="${project.groupId}.${project.artifactId}">


<plugin-info>

<description>${project.description}</description>

<version>${project.version}</version>

<vendor name="${project.organization.name}" url="${project.organization.url}"/>

</plugin-info>

<gadget key="test" location="days-left-gadget.xml"/>

<resource name="i18n" location="i18n/i18n" type="i18n"/>


-<rest key="tutorial-gadget-rest-resources" version="1.0" path="/tutorial-gadget">

<description>Provides the REST resource for the project list.</description>

</rest>

</atlassian-plugin>

 

I am using java version 1.8, maven version 3.2.1, atlas version 6.2.1 and jira version is 7.2.2. Please help me.


Thanks in advance.

 

1 answer

0 votes
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 31, 2017

Hello,

I created a project according to your pom.xml, atlassian-plugin.xml and DaysLeftInVersionResource.java. I am able to package the jar file without any modifications of your code.

Please, make sure that all dependecies from your pom.xml are present in your project. You need to use maven from atlassian-sdk for the project. If you use a built-in in Eclipse Maven then there is a big change you will not be able to download all dependecies and you will have compilation errors. 

You can read an article about configuring external maven in Eclipse:

http://www.yourfriendsdatadiary.info/configure-external-maven-to-run-within-eclipse/

Suggest an answer

Log in or Sign up to answer