Thursday, June 30, 2016

Capture AJAX response and assign to a variable using JQUERY

function checkforduplicateempname(){
 var message=true;
 /*Assigning AJAX response to a javascript variable*/
 var empId = $.ajax({
   url: 'checkforduplicateempname.relay',
   data: oMyForms,
   cache: false,
   contentType: false,
   processData: false,
   global: false,
   async:false,
   type: 'POST',
   success: function(data){
    return data;
       },error:function(){
    $("#empname").notify("Unable to check for duplicate title.","error");
       }
}).responseText; //end of ajax capturing the response
    if(empId!='0'){
    $("#empaname").notify("Duplicate Title.","error");
    message=false;
    }

return message;
}//end of function

Sunday, January 17, 2016

How to retain the space of the td after hidding it in Jquery


If you use the following code snippet to hide and show tds- the UI will get disturbed.
The next corresponding td will get shifted to the hidden td space.
  var tablevar="#"+myTable;
 $(tablevar+' tr:eq('+i+')').find('td:eq(3)').show();
   $(tablevar+' tr:eq('+i+')').find('td:eq(2)').show();

So how to retain the td space and still achieve the functionality.
use the this code snippet.
  var tablevar="#"+myTable;
$(tablevar+' tr:eq('+i+')').find('td:eq(3)').css("visibility", "hidden");
 $(tablevar+' tr:eq('+i+')').find('td:eq(2)').css("visibility", "hidden");

 $(tablevar+' tr:eq('+i+')').find('td:eq(4)').css("visibility", "visible");


Thursday, August 20, 2015

How to extend a JSON array in Jquery

Suppose you get a JSON  Object like this

var optionsoptions;
 $.getJSON("relaytemplateattributes.json" , function(result){
optionsoptions=result.optionsoptions;
 });

/*JSON LIST FORMAT*/
{"optionsoptions":[{"OPTIONID":-1,"COMBOITEM":"---SELECT--"},{"OPTIONID":1,"COMBOITEM":"-3U0Comp"},
{"OPTIONID":1,"COMBOITEM":""},{"OPTIONID":1,"COMBOITEM":"-U2comp"},
{"OPTIONID":1,"COMBOITEM":"Ipol"},{"OPTIONID":1,"COMBOITEM":"-U2"},
{"OPTIONID":1,"COMBOITEM":"-3U0"},{"OPTIONID":1,"COMBOITEM":"Dual"},
{"OPTIONID":2,"COMBOITEM":"0 deg"},{"OPTIONID":2,"COMBOITEM":"180 deg"}
{"OPTIONID":3,"COMBOITEM":"1 out of 3"},{"OPTIONID":3,"COMBOITEM":"2 out of 3"},
{"OPTIONID":3,"COMBOITEM":"3 out of 3"},{"OPTIONID":4,"COMBOITEM":"4"},
{"OPTIONID":4,"COMBOITEM":"1"},{"OPTIONID":4,"COMBOITEM":"2"},
{"OPTIONID":4,"COMBOITEM":"3"},{"OPTIONID":4,"COMBOITEM":"5"},
{"OPTIONID":5,"COMBOITEM":"2 out of 3"}}

/*JSON LIST FORMAT*/
if you want to append to new value to optionsoptions Object you can extend the optionsoptions Object in jquery.


 var object3 = {
    "OPTIONID":newoptionName,
    "COMBOITEM": $("#newoptionValue3").val()
    };
    $.extend( optionsoptions, object3);

Friday, February 20, 2015

Jquery Table Inline Edititing

https://jsfiddle.net/anjana6/yc1o97kL/5/embedded/result/

The Above link demonstrates inline editing of jquery.
The  idea used to implement  is as follows:

Table consists of two identical rows  first one is Parent row which is non editable
Second one is Editable child row.
By Default the Parent row is visible and on click on edit the parent row is hidden and child row is displayed.
Save data displays all the modified row records and it stores it in a hidden string.

Wednesday, December 24, 2014

Convert Excel to XML in JEXCEL API

package com.relaytemplate.data;

import java.io.*;

import jxl.*;
import jxl.demo.XML;
import jxl.read.biff.BiffException;

import java.util.*;

import java.sql.*;

public class ExceltoXML{


// public void readExcelFile() {
public static void main(String args[]) throws BiffException, IOException{
File f = new File("TestConsolidated.xml");
OutputStream os = (OutputStream) new FileOutputStream(f);
String encoding = "UTF8";
OutputStreamWriter osw = new OutputStreamWriter(os, encoding);
String filename = "relaytemplate.xls";
WorkbookSettings ws = new WorkbookSettings();
ws.setLocale(new Locale("en", "EN"));
Workbook w = Workbook.getWorkbook(new File(filename), ws);
try {
XML myxml=new XML(w,os,filename, false);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Wednesday, December 10, 2014

Spring JDBC Template- To get back the inserted values using Keyholder

public List insert(final Action action){
final String insertIntoSql ="INSERT INTO MIPUSER_ACTIONS(IACTIONID, SACTIONNAME) VALUES (nextval('SEQ_MIPUSER_ACTIONS'),?)";
KeyHolder holder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator() {           

                @Override
                public PreparedStatement createPreparedStatement(Connection connection)
                        throws SQLException {
                    PreparedStatement ps = connection.prepareStatement(insertIntoSql.toString(), Statement.RETURN_GENERATED_KEYS);
                    ps.setString(1, action.getAction_name());
                    return ps;
                }
            }, holder);

KeyHolder newPersonId = holder;
System.out.println(newPersonId.getKeyList());
System.out.println(newPersonId.getKeys());
return newPersonId.getKeyList();
 }

Sunday, May 4, 2014

SpringMailService Utility Class

// This class contains  overloaded methods and has demonstrated all the features of spring mail inclusing velocity integration.
package com.packt.mailservice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.MimeMessagePreparator;
import javax.mail.Message;
import java.util.Map;
import java.util.HashMap;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailSenderService {
@Autowired
private VelocityEngine velocityEngine;
@Autowired
private MailSender mailSender;
@Autowired
private JavaMailSender javaMailSender;
@Autowired
private SimpleMailMessage configured_message;
public void sendmail(String from, String to, String subject, String body) throws MailException{
SimpleMailMessage message=new SimpleMailMessage();
message.setFrom(from);
message.setSubject(subject);
message.setText(body);
mailSender.send(message);
}
public void sendmail(String from, String []to, String subject, String body) throws MailException{
SimpleMailMessage message=new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(body);
mailSender.send(message);
}
public void sendmime_mail(final String from, final String to, final String subject, final String body) throws MailException{
MimeMessagePreparator message = new MimeMessagePreparator() {
               public void prepare(MimeMessage mimeMessage) throws Exception {
              message.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
              message.setFrom(new InternetAddress(from));
              message.setSubject(subject);
              message.setText(msg);
            }

public void prepare(MimeMessage arg0) throws Exception {
// TODO Auto-generated method stub

}

    };
    mailSender.send(message);
}
public void sendmail(String from, String []to, String subject, String body,FileSystemResource file,String filename) throws MailException{
       MimeMessage mimemessage = mailSender.createMimeMessage();
       MimeMessageHelper mimemessagehelper = new MimeMessageHelper(message, true);
       mimemessagehelper.setFrom(from);
       mimemessagehelper.setTo(to);
       mimemessagehelper.setSubject(subject);
       mimemessagehelper.setText(msg);
       mimemessagehelper.addAttachment(filename, file);//image will be sent by this name
       mailSender.send(message);
     
}
public void sendmail(String message) throws MailException{
SimpleMailMessage message=new SimpleMailMessage(configured_message);
        mailSender.send(message);
     
}
public void sendmail(final Customer customer){
MimeMessagePreparator preparator = new MimeMessagePreparator() {
     public void prepare(MimeMessage mimeMessage)
          throws Exception {
       MimeMessageHelper message =
                             new MimeMessageHelper(mimeMessage);
       message.setTo(user.getEmailAddress());
           message.setFrom("webmaster@csonth.gov.uk"); // could be parameterized...
           Map model = new HashMap();
           model.put("customer", customer);
           String text = VelocityEngineUtils.mergeTemplateIntoString(
              velocityEngine, "com/packt/velocity/templates/orderconfirmation.vm", model);
           message.setText(text, true);
        }
     };
     this.mailSender.send(preparator);
  }
}