Submit Form Through Plugin

Thanks to the great help I got from the Atlassian Developer Community here, I am able to connect to a SQL server DB using JDBC.
The connection and displaying values from SQL Server works fine already.

Now comes the next difficulty:
In my Jira Plugin, I would like to have

  • a basic form
  • with a visible text area,
  • some hidden inputs and
  • a submit button

By submitting I would like to

  • INSERT INTO a new record into my DB and then
  • call again the main action of the plugin which displays the values from the DB (including the newly created)

The form itself gets already displayed correctly.
Also to execute the INSERT INTO statement successfully from Eclipse works already.

Questions:
What I am currently struggling to understand is the following:

  1. How to pass values from the form to an action (my INSERT-INTO-action that then executes the SQL query)?
  2. How to redirect from one action to another action (from the INSERT-INTO-action to the main action that displays the content from the SQL DB)?

I’m used to ASP.NET where this is rather straight forward.

My form currently looks like this:

	<form class="aui" name="commentForm" id="commentForm" action="postComment" method="post">
		<input type="hidden" name="jiraKey" id="jiraKey" value=$jiraKey />
		<input type="hidden" name="userName" id="userName" value=$user.displayName />
		<textarea name="commentTextInput" id="commentTextInput" cols="60" rows="5" style="max-width: 100%;" value=$newComment></textarea>
		<br />
		<br />
		<input type="submit" value="Post Comment"/>
	</form>

My postComment Action looks like this:

public class postComment {

	protected String userName;
	protected String jiraKey;
	protected String newComment;
	protected String query;
	
	protected String conUrl;

	protected String timeStamp;
	protected Boolean queryResult;

    public postComment(String userName, String jiraKey, String newComment) {
        
		this.userName = userName;
        this.jiraKey = jiraKey;
		this.newComment = newComment;
		this.timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new java.util.Date());
		
		if(jiraKey != null && !jiraKey.isEmpty()) 
		{ 
			try
			{
				conUrl = ConString.generateConString();	
				
				query = "INSERT INTO [dbo].[Comments] ([JiraKey],[UserID],[UserComment],[UserCommentDate],[UserCommentType]) VALUES (" 
				+ "'" + this.jiraKey + "',"
				+ "'" + this.userName + "',"
				+ "'" + this.newComment + "',"
				+ "'" + this.timeStamp + "',"
				+ "'1');";

				queryResult = CommentToSQL.postComment(conUrl, query);
				
				//YourAction.YourAction();
				
				
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
    }

}

And my main Action like this (ignore the default-naming :D):

public class YourAction extends AbstractIssueAction {

    protected Issue issue;
    protected ApplicationUser remoteUser;
	protected String jiraKey;
	protected ArrayList<Map<String, Object>> queryResult;

    public YourAction(IssueTabPanelModuleDescriptor descriptor, Issue issue, ApplicationUser remoteUser) {
        super(descriptor);
        this.issue = issue;
        this.remoteUser = remoteUser;
		this.jiraKey = issue.getKey();
		try
		{
			this.queryResult = CommentsArrayMap.getComments(this.jiraKey);
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		
    }

    public Date getTimePerformed() {
        return issue.getCreated();
    }

    protected void populateVelocityParams(Map params) {
        params.put("action", this);
        params.put("issue", issue);
        params.put("user", remoteUser);
		params.put("jiraKey", jiraKey);
		params.put("queryResult", queryResult);
    }

}

Can I somehow use a MVC-design pattern?

Edit:
I’m currently having a look at Spring MVC.

Oh my… euhm… yes… well… I would strongly recommend you to move away from the Google for a second as you are now entering a very different kind of rabbit hole.

Apart from the OSGi conundrums, one of the thinks to understand with Jira P2 plugin development is that it leans heavily on WebWork, which has its own dynamic. It does come with a neat sort of MVC framework by itself, with the Controller being of type JiraWebActionSupport. This will automatically map form fields to class properties, and has commands which are performed on submit.

There is really a lot to unpack with Jira WebWork, a bit too much for this post. So you might want to look into the documentation. Another great solution would be to check out open source examples for plugins (for example https://bitbucket.org/mdoar/webwork-sample/src/master/) and download a copy of the Jira source from my.atlassian.com.

Another option, and somewhat recommended, is to use a REST module (https://developer.atlassian.com/server/framework/atlassian-sdk/rest-plugin-module/). This will allow you to move your logic to a REST service and use XmlHttpRequest from the front-end. The benefit of this approach is to have a more loose coupling between your views and your controller.

1 Like

WebWork looks good to me and I guess the neat sort of MVC framework should be enough for my purposes.

In general, is it possible to combine the Issue tab panel module and the Webwork module so that the Webwork MVC functionality can be used within an Issue tab panel?

I haven’t worked with Issue Tab Panel module, but looking at the documentation it looks like this is actually not a webwork action. Sorry, I’m afraid I won’t be of much assistance here.

1 Like

Hi remie,

your input is highly appreciated.
Probably the issue tab module is just not the right module for this purpose. I just thought it would be nice to have this separate comment section within its own issue tab as it is issue specific.
Do you know which other modules are issue specific and could be used for what I plan to do?

Update:
I made good progress with combining a Servlet module with my issue tab panel module. Looks very promising so far.

1 Like