Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to set value for variables in velocity file

vishwas l November 23, 2011
inputdate.vm file:

<form name="form1" method="post" action="/jira/secure/datePlannerUpdateData!upload.jspa">
<td><input type="hidden" name="keyHolder" value="$key"/></td>
    <b>From Date:</b> <input name="fromDt" id="fromDt"/>
<input type=button value="select" onclick="displayDatePicker('fromDt', this);"/>

  <b>To Date:</b> <input name="toDt" id="toDt"/>
<input type=button value="select" onclick="displayDatePicker('toDt', this);"/>
   <input type="button" value="GO" onclick="CompareDates()"/>
</form>

atlassian-plugin.xml file:

   <project-tabpanel key="Monthly Data" name="Monthly Data" class="com.moog.jira.plugins.MonthlyDataCalc">
      <label key="Monthly Data">Monthly Data:</label>
        <description>Calculated monthly data</description>
        <order>20</order>
        <resource type="velocity" name="view" location="templates/inputdate.vm" />
    </project-tabpanel>


    <webwork1 key="com.moog.jira.plugins.UpdateDataAction" class="java.lang.Object">
      <actions>
          <action name="com.moog.jira.plugins.UpdateDataAction!upload" alias="datePlannerUpdateData">
               <view name="success">/templates/CalculationsView.vm</view>
               <view name="input">/templates/inputdate.vm</view>
          </action>
      </actions>
    </webwork1>

UpdateDataAction.java:

package com.moog.jira.plugins;

import com.atlassian.jira.web.action.JiraWebActionSupport;
import net.sourceforge.jtds.jdbc.Driver;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

import java.io.StringWriter;
import java.io.Writer;
import java.sql.*;

/**
 * Created by IntelliJ IDEA.
 * User: hsumathi
 * Date: Nov 11, 2011
 * Time: 8:58:34 AM
 * To change this template use File | Settings | File Templates.
 */
public class UpdateDataAction extends JiraWebActionSupport{


    MonthlyDataBean mdb = new MonthlyDataBean();
    
    public String getFromDt() {
        return fromDt;
    }

    public void setFromDt(String fromDt) {
        this.fromDt = fromDt;
    }

    public String getToDt() {
        return toDt;
    }

    public void setToDt(String toDt) {
        this.toDt = toDt;
    }

    private String fromDt;
    private String toDt;

 
    public String getKeyHolder() {
        return keyHolder;
    }

    public void setKeyHolder(String keyHolder) {
        this.keyHolder = keyHolder;
    }

    private String keyHolder;

    public String doUpload() throws Exception{
         String fromdate = getFromDt();
         String todate = getToDt();
         String ikey = getKeyHolder();
	 setValueForEffortEstimation(ikey, fromdate, todate);
	 return SUCCESS;
    }

    public void setValueForEffortEstimation(String key,String fromDate,String toDate){
        Statement st = null;
        ResultSet rs = null;
        conn = null;
        try{
            conn = connect();
            st = conn.createStatement();
            if(fromDate.length() == 0 && toDate.length() == 0){
                   rs = st.executeQuery("select sum(TIMESPENT) as ActualTime, sum(TIMEORIGINALESTIMATE) as EstimatedTime from jiraissue  where pkey like '%" + key + "%' and "+
                   previousMonth);
            }else{
                   rs = st.executeQuery("select sum(TIMESPENT) as ActualTime, sum(TIMEORIGINALESTIMATE) as EstimatedTime from jiraissue  where pkey like '%" + key + "%' and "+
                   givenMonth + fromDate + " and " + toDate);
            }

            //rs.next();
            Float f = new Float(0.0D);
            if (rs.next())
            {
                f = new Float(((double)(rs.getLong(1)-rs.getLong(2))/(double)rs.getLong(2)));
                mdb.setpEffortEstimation(Math.round(f.doubleValue()*100)+"%");
            }else{
                mdb.setpEffortEstimation(Math.round(f.doubleValue())+"%");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        finally{
             try {
                rs.close();
                st.close();
                conn.close();
             } catch (SQLException ex) {
        }
        }
    }

}

MonthlyDataBean.java

public class MonthlyDataBean {
    private String pEffortEstimation;
    public String getpEffortEstimation() {
        return pEffortEstimation;
    }

    public void setpEffortEstimation(String pEffortEstimation) {
        this.pEffortEstimation = pEffortEstimation;
    }
}


Calculationsview.vm:

<form>
	<div>
	<table cellpadding="5">
	<b>Management Review Data</b>
	<tr><td>
	Effort Estimation Conformance:</td>
	<td>$pEffortEstimation</td>
	</tr>
</form>

This is the complete flow that I have given to set the value in Calculationsview.vm file from java class but value is not getting set. 
Do I have to set velocityparameters, is yes how? Can anybody help me out in this. Its very urgent

5 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Raju
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.
November 30, 2011

Vishwas,

As Nic mentioned, you need to have getter that will get called from velocity.

So assuming pEffortEstimation is method that you need to call from your velocity, then $action.pEffortEstimation will be the call that you need to put into your Velocity template. And in your action class (UpdateDataAction), you need to add a method getpEffortEstimation().

hope this helps,

Raju

0 votes
vishwas l December 5, 2011

Yes I had set, getter method as public

public String getpEffortEstimation() {
return pEffortEstimation;
}

public void setpEffortEstimation(String pEffortEstimation) {
this.pEffortEstimation = pEffortEstimation;
}

still value is not getting showed up. Just if we have getter method will it be enough to publish the value to the velocity file?

Raju
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.
December 5, 2011

Vishwas,

Do $action.pEffortEstimation to get the value. Remember this method is defined in Action class so you need to pass the reference to that object.

And yes you can just create getter method, no need to have setter method if you are not using it.

Hope this helps,

Raju

0 votes
MattS
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.
November 30, 2011

And make sure the getter method you add is public or it won't be seen.


~Matt

0 votes
vishwas l November 24, 2011

Is there anybody who knows answer for this? Do I have to set velocity context for this, if yes how to set it because currently I am not able to see the value for $pEffortEstimation instead it displays $pEffortEstimation.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 24, 2011

There's nothing in your code to publish any variables into the velocity.

0 votes
vishwas l November 23, 2011

Can anyone help me out in this?

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events