Sunday, 25 January 2015

Java : TreeSet

TreeSet is another popular implementation of Set interface along with HashSet and LinkedHashSet. All these implementations of Set interface are required in different scenarios. If you don’t want any order of elements, then you can use HashSet. If you want insertion order of elements to be maintained, then use LinkedHashSet. If you want elements to be ordered according to some Comparator, then use TreeSet. The common thing of these three implementations is that they don’t allow duplicate elements.

In this article, I have tried to explain two examples of Java TreeSet. One example doesn’t use Comparator and another example uses Comparator to order the elements. You can go through some basic properties of TreeSet class here.

Java TreeSet Example With No Comparator :

You already know that if you don’t pass any comparator while creating a TreeSet, elements will be placed in their natural ascending order. In this example, we create a TreeSet of Integers without supplying any Comparator like this,



Java TreeSet Example With Comparator :

In this example, we create one TreeSet by supplying a customized Comparator. In this example, we will try to create a TreeSet of Student objects ordered in the descending order of the percentage of marks they have obtained. That means, student with highest marks will be placed at the top.

Let’s create ‘Student’ class with three fields – id, name and perc_Of_Marks_Obtained.








Monday, 19 January 2015

JQuery : Key Press Validations

// Allow only alphabet keys in textfiled

$(document).ready(function () {
    $("#inputTextBox").keypress(function (event) {
        var inputValue = event.which;
        //if digits or not a space then don't let keypress work.
        if ((inputValue > 64 && inputValue < 91) // uppercase
        ||
        (inputValue > 96 && inputValue < 123) // lowercase
        ||
        inputValue == 32) { // space
            return;
        }
        event.preventDefault();
    });
});

// Allow only numerics in text field

$(document).ready(function () {
    $("#inputTextBox").keypress(function (event) {
       var charCode = (event.which) ? event.which : event.keyCode
         if(charCode==8)//back space
            return true;
         if (charCode < 48 || charCode > 57)//0-9
         {
            //alert("Please Enter Only Numbers.");
            return false;
         }
           
         return true;
    });
});

Wednesday, 7 January 2015

Grails : How to send a mail


Step 1 : create account in https://mandrillapp.com/login/

Mandrill is a scalable and affordable email infrastructure service, with all the marketing-friendly analytics tools you’ve come to expect from MailChimp.

step 2 : goto settings > smtp & API info

Step 3 : select API key. it generates one api key

step 4 : Add compile ":mail:1.0.7" in BuildConfig.groovy at plugins

step 5 : In config.groovy, place below mail settings

grails {
mail {
 host = "smtp.mandrillapp.com"
 port = 587
 username = "your@mail.com"
 password = "apikey"
 props = ["mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"587"]
 
}
}

In above code, mandrill username & api key

Step 6 : In your controller inject mail services like shown in below

def mailService

def yourAction(){

mailService.sendMail {
multipart true
//to "sender1@mail.com","sender2@mail.com"
//to "sender@mail.com"
from "support@mail.com"
subject "Subject"
body 'body content'
//attachBytes "test.zip", " application/x-compressed", new File('D:/delete/test.zip').bytes
attachBytes 'new year.jpg','image/jpg', new File('D:/delete/new year.jpg').readBytes()
html  g.render( template: '/admission/mailTemplate')
//attachBytes 'Penguins.jpg','image/jpg', new File('D:/delete/Penguins.jpg').readBytes()
//attachBytes 'Penguins.jpg','image/jpg', new File('D:/delete/Penguins.jpg').readBytes()
 
  }
}

Step 7 : clean your project

Step 8 : run the application

Step 9 : call yourAction and then mail will be send.

Thank U.. Happy Coding..

How to protect our folder with different passwords

Java : ListIterator and JDBC PreparedStatement

Tuesday, 6 January 2015

Index of release/org/springframework/spring/4.1.4.RELEASE

Java : HashSet


Java HashSet is very powerful Collection type when you want a collection of unique objects. HashSet doesn’t allow duplicate elements. HashSet also gives constant time performance for insertion, removal and retrieval operations. It is also important to note that HashSet doesn’t maintain any order. So, It is recommended to use HashSet if you want a collection of unique elements and order of elements is not so important. If you want your elements to be ordered in some way, you can use LinkedHashSet or TreeSet.

In this Java article, we will see one real time example of HashSet.

Let’s create one HashSet of Student records where each Student record contains three fields – name, rollNo and department. In these, rollNo will be unique for all students.

Let’s create Student class with three fields – name, rollNo and department.



You can notice that hashCode() and equals() methods are overrided in the above class so that two Students objects will be compared solely based on rollNo. That means, two Student objects having same rollNo will be considered as duplicates irrespective of other fields.

Create one HashSet object containing elements of Student type.










How to provide security to your folder with password


Step 1 : create one folder
Step 2 : create one text document with any name ( Ex: secure.txt )
Step 3 : Copy the given code and paste it in your file

@ECHO OFF
title Folder Private
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLOCK
if NOT EXIST Private goto MDLOCKER
:CONFIRM
echo Are you sure you want to lock the folder(Y/N)
set/p "cho=>"
if %cho%==Y goto LOCK
if %cho%==y goto LOCK
if %cho%==n goto END
if %cho%==N goto END
echo Invalid choice.
goto CONFIRM
:LOCK
ren Private "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto End
:UNLOCK
echo Enter password to unlock folder
set/p "pass=>"
if NOT %pass%== ENTER YOUR PASSWORD HERE goto FAIL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" Private
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
goto end
:MDLOCKER
md Private
echo Private created successfully
goto End
:End


Step 4 : in above code, enter your password at "ENTER YOUR PASSWORD HERE"
Step 5 : 'Save As' this with .bat extension ( Ex: secure.bat )
Step 6 : remove that original file
Step 7 : Now the file like given below img 1

img 1

Step 8 : If u double click on this, it shows 'private' folder


Step 9 : in this folder,copy your documents ( Ex: data.txt )


step 10: double click on secure.bat then type 'Y' press 'Enter'. the folder disappear.


step 11: again double click on it shows like this


step 12: enter your password as you are given and press 'Enter'
step 13: it shows private folder. your documents are existed in it.


Advantages:

- It is hides the documents
- these type of securities used in offline war deployment

Disadvantages:

- It can be decode easily with logical effort
- It is not provided 100% security

Java Script : How to sort Javascript arraylist of objects


//How to sort Javascript arraylist of objects
The table like as shown in the figure.

 <script>
$(function(){
        // Intialized table id with datatable jquey
var table = $('#receiptsList').dataTable();

// ArrayList
var feeList = [];

$(table.fnGetNodes()).each(function(i){
var record = new Object();
record.receiptNo = $('button',table.fnGetData(i)[0]).text();
record.admissionNo = table.fnGetData(i)[1];
record.name = table.fnGetData(i)[2];
record.paidDate = table.fnGetData(i)[3];
record.paidAmount = table.fnGetData(i)[4];
record.paidType = table.fnGetData(i)[5];
record.userName = table.fnGetData(i)[6];

// Object added to arrayList
feeList.push(record);
});

// Sorting is called
         feeList.sort(SortByField);

});

function SortByField(a, b){
 var aName = a.receiptNo;
 var bName = b.receiptNo;
 return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
}

</script>

Finally arraylist sorted receiptNo wise.

Grails : How to set default value in domain class


// How to set default value in domain class

package com.gb.vamsi

import com.gb.vamsi.common.BaseEntity

class User extends BaseEntity{

String firstName
String lastName
Long accountId
String displayName
String jobRole
String photoUrl
Date lastLogin
boolean chatStatus

static constraints = {
photoUrl nullable:true
lastLogin nullable:true
}

static mapping = {
chatStatus defaultValue:true
}
}

// Here chatStatus by default true