Friday 30 November 2012

JavaScript Calculator


JavaScript Calculator
  • Calculator should have 0 to 9 digits , clear , backspace , . , and = buttons.
  • Operators buttons are : + , - , / , *.
  • Layout should be div based. No tables should be used.
  • It should perform all valid calculations.
  • Use CSS to design buttons & not images or html button element.
  • Button should change in given states ( i. e. normal , disabled, hover and clicked ). Use CSS effect for each state

style.css
body{
overflow: scroll;
}
INPUT{
padding: 2px;
margin: 10px 0px 0px 5px;
}
.calci{
background-color: #aaa;
width: 200px;
height: 250px;
border: 5px !important;
border-color: #000 !important;
}
.btn{
background-color: #ccc;
margin: 1px;
padding: 2px;
min-width: 30px;
text-align: center;
float: left;
border: 2px !important; 
border-color: black;
border-style: solid;
}
.btn:HOVER {
background-color: #cdd;
cursor: pointer;
}

Index.html


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Calculator</title>
<link rel="stylesheet" type="text/css" href="css/styles.css" />
<script type="text/javascript" src="js/Calculations.js"></script>
</head>
<body onload="loadComponents()">
<noscript>
Javascript not enabled
</noscript>

<div class="calci" >
<div style="margin-left: 10px">
<div>
<input id="text_input" value="" type="text" readonly="readonly" style="left:0px"/>
</div><br>
<div style="width: 100%">
<div id="clear_btn" class="btn" > Clear </div>
<div id="del_btn" class="btn">Backspace</div>
</div>
<br><br>
<div style="width: 100%">
<div id="btn_one" class="btn"> 1 </div>
<div id="btn_two" class="btn">2</div>
<div id="btn_three" class="btn">3</div>
<div id="btn_div" class="btn">/</div>
</div>
<br><br>
<div style="width: 100%">
<div id="btn_four" class="btn"> 4 </div>
<div id="btn_five" class="btn">5</div>
<div id="btn_six" class="btn">6</div>
<div id="btn_mul" class="btn">*</div>
</div>
<br><br>
<div style="width: 100%">
<div id="btn_seven" class="btn">7</div>
<div id="btn_eight" class="btn">8</div>
<div id="btn_nine" class="btn">9</div>
<div id="btn_sub" class="btn">-</div>
</div>
<br><br>
<div style="width: 100%">
<div id="btn_dot" class="btn">.</div>
<div id="btn_zero" class="btn">0</div>
<div id="btn_eq" class="btn">=</div>
<div id="btn_plus" class="btn">+</div>
</div>
</div>
</div>

</body>
</html>

Calculations.js

function numberClicked(num){
if(newNumberFlag == true){
document.getElementById("text_input").value = num;
newNumberFlag = false;
}else {
var value = document.getElementById("text_input").value;
value += num;
document.getElementById("text_input").value = value;
}
}
function oneClicked(){
numberClicked(1);
}

function twoClicked(){
numberClicked(2);
}
function threeClicked(){
numberClicked(3);
}
function fourClicked(){
numberClicked(4);
}
function fiveClicked(){
numberClicked(5);
}
function sixClicked(){
numberClicked(6);
}
function operatorClicked(op){
operator = op;
newNumberFlag = true;
result = parseFloat(document.getElementById("text_input").value);
}
function sevenClicked(){
numberClicked(7);
}
function eightClicked(){
numberClicked(8);
}
function nineClicked(){
numberClicked(9);
}
function zeroClicked(){
numberClicked(0);
}

function dotClicked(){
var value = document.getElementById("text_input").value;
if(value == null || value == undefined || value == "" ){
value = "0.";
}else if(value.indexOf(".")!= -1){
return;
}else {
value += ".";
}
document.getElementById("text_input").value = value;
}
function loadComponents(){
clear();
var clearBtn=document.getElementById("clear_btn");
clearBtn.onclick = clear;
document.getElementById("del_btn").onclick = backspaceClicked;
document.getElementById("btn_one").onclick = oneClicked;
document.getElementById("btn_two").onclick = twoClicked;
document.getElementById("btn_three").onclick = threeClicked;
document.getElementById("btn_four").onclick = fourClicked;
document.getElementById("btn_five").onclick = fiveClicked;
document.getElementById("btn_six").onclick = sixClicked;
document.getElementById("btn_seven").onclick = sevenClicked;
document.getElementById("btn_eight").onclick = eightClicked;
document.getElementById("btn_nine").onclick = nineClicked;
document.getElementById("btn_zero").onclick = zeroClicked;
document.getElementById("btn_dot").onclick = dotClicked;
document.getElementById("btn_plus").onclick = function(){
operatorClicked("+");
};
document.getElementById("btn_sub").onclick = function(){
operatorClicked("-");
};
document.getElementById("btn_div").onclick = function(){
operatorClicked("/");
};
document.getElementById("btn_mul").onclick = function(){
operatorClicked("*");
};
document.getElementById("btn_eq").onclick = calculate;
}

function clear(){
document.getElementById("text_input").value="";
result = 0;
operator = "";
// num1 = "";
newNumberFlag = true;
}
function backspaceClicked (){
var value = document.getElementById("text_input").value;
if(null != value && value != undefined && value != ""){
var origLen = value.length , newValue;
newValue = value.substring(0,origLen-1);
document.getElementById("text_input").value = newValue;
}
}

function calculate(){
var num2 = parseFloat(document.getElementById("text_input").value);
if(result == 0 || operator == ""){
result = num2;
}else {
if(operator == "+"){
result = result + num2;
}else if(operator == "-"){
result = result - num2;
}else if(operator == "/"){
result = result / num2;
}else if(operator == "*"){
result = result * num2;
}
}
document.getElementById("text_input").value = result;
}


Thursday 11 October 2012

Component Frameworks


Welcome to the world of components!!!

Today, we are heading towards the component world. Everything we get in terms of component.Rather we are building everything as a component. Why I am saying this is because the kind of frameworks I came across.

I started my carrier with Struts framework. This is most popular MVC framework of that time. As it is today as well.But there were very few other frameworks competing with Struts.

At that time i didn't heard anything in terms of component.But later on I moved to other organizations and  started working on JSF.At that time JSF was the emerging framework. It was known as the component framework for fast web application development.It also had in built support for Ajax.

After that I came across another framework Apache Wicket. This is another frame that I learnt. This framework was quite different from JSF. As a Java developer u need not have to write anything on the HTML side or in JSP. There is a VERY nice separation of presentation and logic. Here HTML will remain as HTML. I enjoyed working on this framework.

Currently I am using the ExtJS which is another JavaScript framework which is also a component framework.

Yesterday I read about some other framework like "Vaadin", "KendoUI".These are the other emerging component based framework.There are many more in the market which are known as component framework. By using which u can fasten the development and it eases the maintenance of software.

I m not comparing these frameworks here. Just wanted share and highlight that the Technology world is moving fast and everything in coming to us in terms of "Component"...

Wednesday 26 September 2012

MultiKeyMap

It uses multiple keys to store the value.

This MultiKeyMap is not synchronized and is not thread-safe.

Add the jar "commons-collections-3.2.1.jar".

Example is :


MultiKeyMap multiKeyMap = new MultiKeyMap();
multiKeyMap.put("DocumentName","DocumentRef","DocumentAuthor");
multiKeyMap.put("DocumentName1","DocumentRef1","DocumentAuthor1");
multiKeyMap.put("DocumentName2","DocumentRef2","DocumentAuthor2");
multiKeyMap.put("DocumentName3","DocumentRef3","DocumentAuthor3");

// later retireve the value
System.out.println(multiKeyMap.get("DocumentName","DocumentRef"));  
System.out.println(multiKeyMap.get("DocumentName1","DocumentRef1"));
System.out.println(multiKeyMap.get("DocumentName2","DocumentRef2"));
System.out.println(multiKeyMap.get("DocumentName3","DocumentRef3"));

the output is :


DocumentAuthor
DocumentAuthor1
DocumentAuthor2
DocumentAuthor3

Tuesday 22 May 2012

How to index data in solr from database automatically?

As most of the application store the data in relational databases and once the data size gets larger the option comes to solr. Index the data of database using solr DIH. It requires you to configure the data-config.xml. All the required info about setting up solr and data-config.xml is available at
http://abhijitbashetti.blogspot.in/2011/09/apache-solr-set-up-for-tomcat-on-linux.html and

the other useful link is http://abhijitbashetti.blogspot.in/2011/09/indexing-database-with-solr-34-from.html

Now the question comes how to automize the same. I have used the JBoss for scheduling the same.

In the data-config.xml use the last_index_time as the variable resolver. Or add a new column to the table from where you are fetching the data for indexing e.g last_modification_date.

For the next scheduling which will be invoked for updating the index for the updated data in your database. It will check the last_modification_date with last_index_time and update the index accordingly.

Here you can use the jboss scheduling mechanism by firing different url to your solr server.

i.e. Add the variable resolver in your query and send those data in the http URL to solr.

Your database query would be like ...



select  doc.document_id as id,
        doc.name as name,
        doc.author as author,
        from   ds_document_c doc
        where doc.last_modification_date >= to_date(${dataimporter.request.last_index_time}, 'DD/MM/YYYY HH24:MI:SS');


and the http url for the solr would like

http://localhost:8080/solr/select?qt=/dataimport&command=full-import&clean=false&commit=true&verbose=true&last_index_time='12/05/2012'


This will help you to automate your database indexing ....











Friday 4 May 2012

modify an existing check constraint in oracle?

Drop and Recreate it.



Oracle "alter table" syntax to drop constraints.


ALTER TABLE TEST_TABLE DROP constraint TEST_TABLE_CC1;


Oracle "alter table" syntax to add a check constraint.


ALTER TABLE TEST_TABLE ADD CONSTRAINT TEST_TABLE_CC1 CHECK ( PAYMENT_MODE IN ( 'CASH' , 'NET_BANKING' , 'CHEQUE' ) );

Tuesday 17 April 2012

IFRAME Example


The <iframe> tag specifies an inline frame.
An inline frame is used to embed another document within the current HTML document.

I wanted to have the "src" to be changed dynamically. 

Here is the example of IFRAME where in source can be changed though TextField. 
Whatever you enter into the text box will be given to the Iframe and iframe window will render that page.


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>TEST Page</title>
</head>
<script type="text/javascript" >
function iframeMethod() {
var srcValue =  document.getElementById("text");
var iframe   =  document.getElementById("frame");
iframe.setAttribute('src', srcValue.value);
}
</script>
<body>
<iframe src="http://www.google.com" name="frame" id="frame"
frameborder=0 style="overflow:visible; width:100%; height:600; scrolling="no">
</iframe>
<input type="text" name="text" id="text" target="frame"/>
<input type="submit" name="submit" id="submit" value="Submit" onclick="javascript:iframeMethod();"/>
</body>
</html>

Monday 9 April 2012

How to set JAVA_HOME environment variable in Ubuntu


    Open /etc/bash.bashrc using sudo command
    e.g. sudo gedit /etc/bash.bashrc.
   
   go to the end of the file and add following lines:

    JAVA_HOME=/usr/lib/jvm/name_of_your_java_folder(e.g. java,open-6-jdk etc.)
    export JAVA_HOME
   
    PATH=$PATH:$JAVA_HOME/bin
    export PATH

   Done. Just reboot your computer.


    echo $JAVA_HOME
    it shows something like this:/usr/lib/jvm/name_of_your_java_folder(e.g. java,open-6-jdk etc.)
 
    echo $PATH
    it shows something like this::/usr/lib/jvm/name_of_your_java_folder(e.g. java,open-6-jdk etc.)/bin 

Thursday 22 March 2012

Unique constraint and index relationship in Oracle

Dropping Unique Key does not drop the index created for it in Oracle.

I am not very sure for which all versions it happens.

But when I explicitly run the

Drop Index <indexName>;

After execution the same , my unique constraint violation exception issue is fixed.


Tuesday 7 February 2012

Updating the Cookie for specific time interval


If you want to update the cookies after some time interval using the time then call the below function.
Timer should run with the interval of less than 11 minutes.

updateCookieValue  :  function () {
    var cookies = document.cookie.split(";");
if (cookies.length > 0) {
var nTime = new Date();
//add 11 mins to current time so that the recursive call to update cookie
             (with interval 10 mins)
//gets executed before the cookie gets expired ( 11 * 1000 * 60)
             // time added in millisecond
nTime.setTime(nTime.getTime()+660000);
for (var i = 0; i < cookies.length; i++){
var name = cookies[i].split("=")[0];
name = name.trim();
var value = cookies[i].split("=")[1];
if((name == " cookie_0") || (name == " cookie_1")){
document.cookie = name + "=" +escape( value ) +";expires="
                      + nTime.toGMTString()+";path=/;domain=";
}
        }
}
}

Sunday 5 February 2012

Encryption and Decryption Technique


import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class EncryptDecryptUtil {

    private static final String UNICODE_FORMAT = "UTF8";
    public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
    private KeySpec keySpec;
    private SecretKeyFactory secretKeyFactory;
    private Cipher cipher;
    byte[] keyAsBytes;
    private String encryptionKey;
    private String encryptionScheme;
    private SecretKey secretKey;

    public EncryptDecryptUtil() throws Exception
    {
        encryptionKey = "EncryptionKeyForEncryption";
        encryptionScheme = DESEDE_ENCRYPTION_SCHEME;
        keyAsBytes = encryptionKey.getBytes(UNICODE_FORMAT);
        keySpec = new DESedeKeySpec(keyAsBytes);
        secretKeyFactory = SecretKeyFactory.getInstance(encryptionScheme);
        cipher = Cipher.getInstance(encryptionScheme);
        secretKey = secretKeyFactory.generateSecret(keySpec);
    }

    public String encrypt(String unencryptedString) {
        String encryptedString = null;
        try {
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
            byte[] encryptedText = cipher.doFinal(plainText);
            BASE64Encoder base64encoder = new BASE64Encoder();
            encryptedString = base64encoder.encode(encryptedText);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encryptedString;
    }
 
    public String decrypt(String encryptedString) {
        String decryptedText=null;
        try {
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            BASE64Decoder base64decoder = new BASE64Decoder();
            byte[] encryptedText = base64decoder.decodeBuffer(encryptedString);
            byte[] plainText = cipher.doFinal(encryptedText);
            decryptedText= bytes2String(plainText);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return decryptedText;
    }
 
    private static String bytes2String(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            stringBuffer.append((char) bytes[i]);
        }
        return stringBuffer.toString();
    }
 
     /**
     * Testing The Encryption And Decryption Technique
     */
    public static void main(String args []) throws Exception
    {
    EncryptDecryptUtil myEncryptor = new EncryptDecryptUtil();
    String stringToEncrypt= "theherald";
        String encrypted=myEncryptor.encrypt(stringToEncrypt);
        String decrypted=myEncryptor.decrypt(encrypted);

        System.out.println("The String To be Encrypted : "+stringToEncrypt);
        System.out.println("Encrypted Value of the String : " + encrypted);
        System.out.println("Decrypted Value of the String : "+decrypted);

    }
}  


The output is ::::::

The String To be Encrypted : theherald
Encrypted Value of the String :b6WaWLvUO7XznWLKxDzGXw==
Decrypted Value of the String :theherald