Thursday, August 20, 2015

How to extend an 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);
  }
}

Tuesday, July 2, 2013

Multilevel Inheritance in java with public variables and parameterized Constructor

This is a demonstration of Multi-level inheritance in java using parameterised constructor
package com.inheritance;

public class Class1 {
public int a;
public int b;
public Class1(int a, int b) {
super();
this.a = a;
this.b = b;
System.out.println("I am super class called first--->"+(a+b));
System.out.println(a+b);
}
}
---------------------------------------------
package com.inheritance;
public class Class2 extends Class1 {
public int c;
public Class2(int a, int b, int c) {
super(a, b);
this.c=c;
System.out.println("I am the second level subclass--->" +a%c);
}

}
--------------------------------------------------------------
package com.inheritance;

public class Class3 extends Class2 {
public int d;
public Class3(int a, int b,int c, int d) {
super(a, b, c);
this.d=d;
System.out.println("I am thrid level subclass extends second level class-->"+a*b*c*d);
// TODO Auto-generated constructor stub
}
public static void main(String args[]){
Class3 class3=new Class3(1, 2, 3, 4);
}
}
--------------------------------------------
output:
I am super class called first--->3
3
I am the second level subclass--->1
I am thrid level subclass extends second level class-->24

Wednesday, June 26, 2013

Can a Class implement two interfaces with the same method signature

I created two interfaces with same method signature.
You can actually implement the method only once.

package com.anjana;
public interface Interface2 {
public String printme();
}
package com.anjana;
public interface Interface1 {
public String printme();
}
package com.anjana;

public class Employee implements Interface1,Interface2,Interface3{
@Override
public String printme() {
return "Hello";
}

public static void main(String args[]){
Interface1 emp1=new Employee();
emp1.printme();
System.out.println(emp1.printme());
Interface2 emp2=new Employee();
System.out.println(emp2.printme());
}
This will be misleading if you have to do some operations based on the returned value.