Tuesday, 21 March 2017

Scheduler JOB in java

package com..scheduler;

import static org.quartz.CronScheduleBuilder.dailyAtHourAndMinute;
import java.util.Calendar;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;


public class SchedulerTest implements Job {

    public static void main(String[] args) {

        int HOUR_OF_DAY = Integer.parseInt("20");
        int MINUTE_OF_HOUR = Integer.parseInt("10");


        Trigger trigger1 = TriggerBuilder.newTrigger().withIdentity("test trigger1", "group1").withSchedule(dailyAtHourAndMinute(08, 00)).build();
        Trigger trigger2 = TriggerBuilder.newTrigger().withIdentity("test trigger2", "group1").withSchedule(dailyAtHourAndMinute(20, 01)).build();
        Trigger trigger3 = TriggerBuilder.newTrigger().withIdentity("test trigger3", "group1").withSchedule(dailyAtHourAndMinute(23, 10)).build();

        try {
            Scheduler scheduler = new StdSchedulerFactory().getScheduler();
            scheduler.start();

            JobDetail job1 = JobBuilder.newJob(NewsLetterSchedulerTest.class).withIdentity("test job1", "group1").build();
            scheduler.scheduleJob(job1, trigger1);

            JobDetail job2 = JobBuilder.newJob(NewsLetterSchedulerTest.class).withIdentity("test job2", "group1").build();
            scheduler.scheduleJob(job2, trigger2);

            JobDetail job3 = JobBuilder.newJob(NewsLetterSchedulerTest.class).withIdentity("test job3", "group1").build();
            scheduler.scheduleJob(job3, trigger3);

        } catch (SchedulerException e) {
            e.printStackTrace();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
public boolean getValue(){
  System.out.println("Simple Method invoked from Scheduler");
return true;
}
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        SchedulerTest  scheduler=new SchedulerTest ();
scheduler.getValue();
    }
}

PS: Make sure that required jars your have to add to run above code

How to write Scheduler in Java



import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * This Class is Used to Run Every after 15 min
 *
 * @author ananddw
 **/
public class TestScheduler{

    public static void main(String[] args) {
        new TestScheduler().start();
    }

    public void start() {

        System.out.println(" scheduler starts");

        Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                try {
                    //Need to Call Your Method which will execute every after 15 min
                    System.out.println(new java.util.Date());
                } catch (Throwable e) {
                    System.out.println("--Error in  Schduler--");
                    e.printStackTrace();
                }
            }
        }, 0, 15, TimeUnit.MINUTES);
    }
}

Thursday, 2 March 2017

how to get src value from normal Text in javascript

<html>
<head>
</head>
<title>|| Video Uploader || </title>
<body>
<input type="text" id="video_url" name="video_url" value="<iframe   width='420' height='315'    src='https://www.youtube.com/embed/XGSy3_Czz8k'></iframe>"  />
<button id = "opener" onclick="insertURL()">Show URL </button>

<script type="text/javascript">
<!-- If User's Provide direct URL or Full iframe innerhtml-->
    function insertURL(){
        var ishare_url = document.getElementById("video_url").value;
       
        if(undefined!==ishare_url.split('src=')[1]){
        var src = ishare_url.split('src=')[1].split(/[ >]/)[0];
        if(src){
            var decode_ishare_url = decodeURI(src.trim());
            var replace_String = decode_ishare_url.replace(/%22/g, '');
            var replaceQuotes = replace_String.replace(/['"]+/g, '');
            document.getElementById("video_url").value = replaceQuotes;//Assign updated value
           
            }
        }else {
            if (ishare_url){
            var decode_ishare_url = decodeURI(ishare_url.trim());
            var replace_String = decode_ishare_url.replace(/%22/g, '');
            var replaceQuotes = replace_String.replace(/['"]+/g, '');

            document.getElementById("video_url").value = replaceQuotes;//Assign updated value
            }
            }
            alert("SRC ::    "+document.getElementById("video_url").value);
    }   
   
    function clearFieldValue() {
    // Clear urls Fields here
    document.getElementById("video_url").value = "";
}
window.onload = clearFieldValue;       
</script>
</body>
</html>

Thursday, 9 February 2017

How to Add Javascript,CSS inside Javascript

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
var CSSURL="https://fonts.googleapis.com/css?family=Hind";
$(document).ready(function(){
    $("p").click(function(){
       alert("Hi");
  $.getScript("https://code.jquery.com/ui/1.12.1/jquery-ui.js", function(){

   alert("Script loaded but not necessarily executed.");

});
    });
});

var linkElem = document.createElement('link');
document.getElementsByTagName('head')[0].appendChild(linkElem);
linkElem.rel = 'stylesheet';
linkElem.type = 'text/css';
linkElem.href = CSSURL;
</script>
</head>
<body>


<p>Click Here to load JS and CSS As well !</p>

</body>
</html>

Saturday, 7 January 2017

How to Read XLSX File in Java Using apache Poi API

Here we will implement the code to read XLSX file using apache POI API :
and download required jars based on below snapshot






Code : -


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.test.XLS;

import java.io.*;
import java.util.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

/**
 *
 * @author anand
 */
public class XLSReader {

    static final String fileName = "FriendsZone.xlsx";
    FileInputStream file = null;

    public void readXLSFile() {
        try {
            file = new FileInputStream(new File(fileName));

            XSSFWorkbook workbook = new XSSFWorkbook(file);

            XSSFSheet sheet = workbook.getSheetAt(0);

            Iterator<Row> rowIterator = sheet.iterator();

            while (rowIterator.hasNext()) {

                Row row = rowIterator.next();

                Iterator<Cell> cellIterator = row.cellIterator();

                while (cellIterator.hasNext()) {

                    Cell cell = cellIterator.next();

                    switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_NUMERIC:

                            System.out.print(cell.getNumericCellValue() + "\t");

                            break;

                        case Cell.CELL_TYPE_STRING:

                            System.out.print(cell.getStringCellValue() + "\t");

                            break;
                    }
                }

                System.out.println("");
            }

            file.close();

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            if (file != null) {

                try {

                    file.close();

                } catch (IOException ex) {

                    //Logger.getLogger(XLSReader.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public static void main(String[] args){
     
            new XLSReader().readXLSFile();
         
     
    }
}



For any Doubt please comment it . Thanks 

Tuesday, 20 December 2016

Converting JSONArray into CSV file in java

package com.loginController;

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.json.CDL;
import org.json.JSONArray;
import org.json.JSONObject;
/**
 *
 * @author anand
 *
 */
public class ConvertArray {
    public static void main(String myHelpers[]){
        String jsonArrayString = "{\"fileName\": [{\"name\": \"Anand\",\"last\": \"Dwivedi\",\"place\": \"Bangalore\"}]}";

        JSONObject output;
        try {
            output = new JSONObject(jsonArrayString);


            JSONArray docs = output.getJSONArray("fileName");

            File file=new File("JSONSEPERATOR.csv");
            String csv = CDL.toString(docs);
            FileUtils.writeStringToFile(file, csv);
            System.out.println("Data has been Sucessfully Writeen to "+file);
        } catch (Exception e) {
            e.printStackTrace();
        }    
    }

}

Sunday, 18 December 2016

Custom Sorting in Java

How Custom Sorting will work in java

# Some time we may got the requirement to Sort generic Class level list into Default Sorting order in that case we will have to write own custom logic for that . below is the code to perform Custom logic in java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com;

import java.util.*;

/**
 *
 * @author anand
 */
public class CustomSorting {

    private String username;
    private String userplace;
    private String userdesignation;

    static List<CustomSorting> details = null;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUserplace() {
        return userplace;
    }

    public void setUserplace(String userplace) {
        this.userplace = userplace;
    }

    public String getUserdesignation() {
        return userdesignation;
    }

    public void setUserdesignation(String userdesignation) {
        this.userdesignation = userdesignation;
    }

    public CustomSorting(String username, String userplace, String userdesignation) {
        this.username = username;
        this.userplace = userplace;
        this.userdesignation = userdesignation;
    }

    public static void main(String[] args) {
        details = new ArrayList<CustomSorting>();

        CustomSorting customDetails = new CustomSorting("Anand", "Bangalore", "Software Developer");
        CustomSorting customDetails1 = new CustomSorting("Ajeet", "Pune", "Testing Lead");
        CustomSorting customDetails3 = new CustomSorting("Antrish", "Chennai", "C# Lead");
        CustomSorting customDetails5 = new CustomSorting("Sachin", "Rewa", "SBI Manager");
        CustomSorting customDetails6 = new CustomSorting("Anand", "Mumbai", "Software Developer");

        details.add(customDetails);
        details.add(customDetails1);
        details.add(customDetails3);
        details.add(customDetails5);
        details.add(customDetails6);

        System.out.println("==========Before Sorting==============");
        for (CustomSorting custom : details) {
            System.out.println(custom.getUsername());
        }

        Collections.sort(details, new Comparator<CustomSorting>() {

            @Override
            public int compare(CustomSorting t, CustomSorting t1) {
                if (t.getUsername().equals(t1.getUsername())) {
                    return t.getUserplace().compareTo(t1.getUserplace());
                } else {

                    return t.getUsername().compareTo(t1.getUsername());
                }

            }

        });

        System.out.println("===========After Sorting =============");
        for (CustomSorting custom : details) {
            System.out.println(custom.getUsername()+"\t"+custom.getUserplace());
        }
    }

}

Map class field to map

 import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; public class AutogeneratedClassMapper {     public static M...