Thursday, April 7, 2016

UPLOAD FILE TO DROPBOX BY CALLING ITS WEBSERVICE IN JAVA



METHOD 1

USING ACCESS TOKEN GENERATED IN DROPBOX DEVELOPER WEBSITE


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;

import com.dropbox.core.DbxAppInfo;
import com.dropbox.core.DbxAuthFinish;
import com.dropbox.core.DbxEntry;
import com.dropbox.core.DbxException;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.DbxClient;
import com.dropbox.core.DbxWebAuthNoRedirect;
import com.dropbox.core.DbxWriteMode;

public class Upload_Data
{
    private static final String ACCESS_TOKEN = "<ACCESS TOKEN>";

    public static void main(String args[]) throws DbxException, IOException {
        
        DbxRequestConfig config = new DbxRequestConfig(
            "JavaTutorial/1.0", Locale.getDefault().toString());
            
        //YOU CAN GET THIS ACCESS TOKEN FROM DROPBOX DEVELOPER WEBSITE     FOR YOUR APP       
       String accessToken="<aCCESSTOKEN>";

       DbxClient client = new DbxClient(config, accessToken);
        System.out.println("Linked account: " +client.getAccountInfo().displayName);
        File inputFile = new File("C:\\Users\\USER\\Desktop\\dat.txt");
        
        
        FileInputStream inputStream = new FileInputStream(inputFile);
        try 
        {
          client.delete("/dat.txt");
          System.out.println("DAT DELETED SUCCESSFULLY");
            DbxEntry.File uploadedFile = client.uploadFile("/dat.txt",
                DbxWriteMode.add(), inputFile.length(), inputStream);
            System.out.println("Uploaded: " + uploadedFile.toString());
        }   
        finally
        {
            inputStream.close();
            
        }
        FileOutputStream outputStream = new FileOutputStream("DAT.txt");
        try {
            DbxEntry.File downloadedFile = client.getFile("/DAT.txt", null,
                outputStream);
            
            System.out.println("Metadata: " + downloadedFile.toString());
        } 
        finally 
        {
            outputStream.close();
        }
      
        
    }
}

METHOD 2

USING ACCESS KEY AND SECRET KEY GENERATED IN DROPBOX DEVELOPER WEBSITE:


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;

import com.dropbox.core.DbxAppInfo;
import com.dropbox.core.DbxAuthFinish;
import com.dropbox.core.DbxEntry;
import com.dropbox.core.DbxException;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.DbxClient;
import com.dropbox.core.DbxWebAuthNoRedirect;
import com.dropbox.core.DbxWriteMode;

public class Upload_Data
{
    private static final String ACCESS_TOKEN = "<ACCESS TOKEN>";

    public static void main(String args[]) throws DbxException, IOException {
        // Create Dropbox client
        //YOU CAN GET APP KEY AND SECRET KEY FOR YOUR APP IN DROPBOX   DEVELOPER.COM
     final String APP_KEY = "<APP KEY>";
        final String APP_SECRET = "SECRET KEY";
        DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET);
        DbxRequestConfig config = new DbxRequestConfig(
            "JavaTutorial/1.0", Locale.getDefault().toString());
       DbxWebAuthNoRedirect webAuth = new DbxWebAuthNoRedirect(config, appInfo);
       String authorizeUrl = webAuth.start();
      System.out.println("1. Go to: " + authorizeUrl);
       System.out.println("2. Click \"Allow\" (you might have to log in first)");
       System.out.println("3. Copy the authorization code.");
      String code = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();
       System.out.println("Authen code.."+code);
       DbxAuthFinish authFinish = webAuth.finish(code);
        String accessToken = authFinish.accessToken;
             DbxClient client = new DbxClient(config, accessToken);
        System.out.println("Linked account: " +client.getAccountInfo().displayName);
        File inputFile = new File("C:\\Users\\USER\\Desktop\\dat.txt");
        
        
        FileInputStream inputStream = new FileInputStream(inputFile);
        try 
        {
          client.delete("/dat.txt");
          System.out.println("DAT DELETED SUCCESSFULLY");
            DbxEntry.File uploadedFile = client.uploadFile("/dat.txt",
                DbxWriteMode.add(), inputFile.length(), inputStream);
            System.out.println("Uploaded: " + uploadedFile.toString());
            
        }   
        finally
        {
            inputStream.close();
            
        }
        FileOutputStream outputStream = new FileOutputStream("DAT.txt");
        try {
            DbxEntry.File downloadedFile = client.getFile("/DAT.txt", null,
                outputStream);
            
            System.out.println("Metadata: " + downloadedFile.toString());
        } 
        finally 
        {
            outputStream.close();
        }
      
        
    }
}
 

TABLE VIEW IN JAVAFX


TABLE VIEW IN JAVAFX


import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;


public class CalculationView extends VBox
{
    private TableView table;
    private TableColumn sno,room_no,name,no_ofdays,extra,guest,penalty,test;
    private MainView mv;
    private Label title;
     private final ObservableList<Person> data =FXCollections.observableArrayList();
    public CalculationView()
    {
        //this.mv=main;
     instanceCreation();
     setdata();
     designform();
   
    }
    private void instanceCreation()
    {
        //MainTable
        table=new TableView();
         //table.setEditable(true);
       
        //Table Column
        sno=new TableColumn();
        sno.setMinWidth(100);
        sno.setCellValueFactory(
                new PropertyValueFactory<Person, String>("sNo"));
       
        room_no=new TableColumn();
        room_no.setMinWidth(100);
        room_no.setCellValueFactory(
                new PropertyValueFactory<Person, String>("roomNo"));
       
        name=new TableColumn();
        name.setMinWidth(200);
        name.setCellValueFactory(
                new PropertyValueFactory<Person, String>("fname"));
   
        no_ofdays=new TableColumn();
        no_ofdays.setMinWidth(180);
        no_ofdays.setCellValueFactory(new PropertyValueFactory<Person, TextField>("noDays"));
       
        extra=new TableColumn();
        extra.setMinWidth(150);
        extra.setCellValueFactory(new PropertyValueFactory<Person, TextField>("extraChrg"));
       
        guest=new TableColumn();
        guest.setMinWidth(150);
        guest.setCellValueFactory(new PropertyValueFactory<Person, TextField>("guestChrg"));
       
        penalty=new TableColumn();
        penalty.setMinWidth(150);
        penalty.setCellValueFactory(new PropertyValueFactory<Person, TextField>("penaltyChrg"));
       
        test=new TableColumn();
        test.setMinWidth(100);
        test.setCellValueFactory(new PropertyValueFactory<Person, String>("test"));
       
        title=new Label();   
    }
    private void setdata()
    {
          for(int i=0;i<100;i++){
              //  data.add(new Person(String.valueOf(i),"test"+i,""+i+i+"", "name"+i));
              data.add(new Person(String.valueOf(i),""+i+i+"","name"+i,"i","i","i","i","tset1"));
           
            }
        //columns data
        sno.setText("S.NO");
        room_no.setText("ROOM NO");
        name.setText("NAME");
        no_ofdays.setText("NO OF DAYS FOOD TAKEN");
        extra.setText("EXTRA MESS CHARGE");
        guest.setText("GUEST CHARGE");
        penalty.setText("PENALTY CHARGE");
        test.setText("Test col");
        //titles
        title.setText("CHARGES TABLE");
        title.setFont(new Font("Arialblack", 20));
       
    }
    private void designform()
    {       
        table.getColumns().addAll(sno,room_no,name,no_ofdays,extra,guest,penalty,test);
        this.setStyle("-fx-border-color: red");
        //this.setPadding(new Insets(0,0,0,99));
        this.getChildren().addAll(title,table);
        table.setItems(data);
   
    }
   

}

Person CLASS FOR TO RETRIVE TABLE DATA(GETTER AND SETTER METHODS)

 

 

import javafx.beans.property.SimpleStringProperty;
import javafx.scene.control.TextField;


public class Person
{
    private TextField noDays,extraChrg,guestChrg,penaltyChrg;
    private SimpleStringProperty sNo;
    private SimpleStringProperty roomNo;
    private SimpleStringProperty fname;
    private SimpleStringProperty test;
   
   
    public Person(String sNo,String roomno,String fname,String no,String ex,String gs,String pen,String test)
    {
        this.roomNo=new SimpleStringProperty(roomno);
        System.out.println(sNo);
        this.sNo= new SimpleStringProperty(sNo);
        this.fname=new SimpleStringProperty(fname);
        this.test=new SimpleStringProperty(test);
        this.noDays=new TextField(no);
        this.extraChrg=new TextField(ex);
        this.guestChrg=new TextField(gs);
        this.penaltyChrg=new TextField(pen);
      
      
    }
    public TextField getNoDays() {
        return noDays;
    }
    public void setNoDays(TextField noDays) {
        this.noDays = noDays;
    }
    public TextField getExtraChrg() {
        return extraChrg;
    }
    public void setExtraChrg(TextField extraChrg) {
        this.extraChrg = extraChrg;
    }
    public TextField getGuestChrg() {
        return guestChrg;
    }
    public void setGuestChrg(TextField guestChrg) {
        this.guestChrg = guestChrg;
    }
    public TextField getPenaltyChrg() {
        return penaltyChrg;
    }
    public void setPenaltyChrg(TextField penaltyChrg) {
        this.penaltyChrg = penaltyChrg;
    }


    public String getSNo() {
        return sNo.get();
    }
    public void setsNo(String sNo)
    {
        this.sNo.set(sNo);
    }
   


    public String getRoomNo() {
        return roomNo.get();
    }
    public String gettest()
    {
        return test.get();
    }
    public void settest(String testval)
    {
        test.set(testval);
    }

   
    public String getFname() {
        return fname.get();
    }
    
   

}

 


 
 


NOTE:


According to the PropertyValueFactorydocumentation..,
Make sure to type correct getter and setter method with respect to variable name.
If variable name is abc means,your methods should be like this getAbc(),setAbc().

EVENT HANDLING OF MULTIPLE FIELDS IN JAVAFX WINDOWS APPLICATION



EVENT HANDLING OF MULTIPLE FIELDS IN JAVAFX 


import javax.swing.JOptionPane;
import javafx.event.EventHandler;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;


public class EventController 
{
private MouseHandler mh;
private HomeView homeview;
private MainView mv;
Public EventController(HomeView homeview,MainView main)
{
this.homeview=homeview;
mh = new MouseHandler();
this.mv=main;
this.addMouseListener(mh);
}
private void mouseHandler(MouseEvent handle
{
Object obj = handle.getSource();
if (obj == null) {
return;
}
if (handle.getEventType() == MouseEvent.MOUSE_CLICKED) {
if (handle.getButton() == MouseButton.PRIMARY) {
if (obj.equals(homeview.getCalc())) 
{
                  JOptionPane.showMessageDialog(null,"Triggered 1");
} else if (obj.equals(homeview.getRegis())) {
 JOptionPane.showMessageDialog(null,"Triggered 2");
 mv.getmainborder().setCenter(null);
 mv.getmainborder().setCenter(new RegisterView());

}
}
}
 class MouseHandler implements EventHandler<MouseEvent> {
public void handle(MouseEvent me) {
mouseHandler(me);
}
}
private void addMouseListener(EventHandler<MouseEvent> eh) {

homeview.getCalc().addEventHandler(MouseEvent.MOUSE_CLICKED, eh);
homeview.getRegis().addEventHandler(MouseEvent.MOUSE_CLICKED, eh);
}

}



Monday, April 4, 2016

AUTO INCREMENT AND INSERTION OF RECORD IN J2EE USING HIBERNATE FRAMEWORK:



 AUTO INCREMENT AND INSERTION OF RECORD IN J2EE USING HIBERNATE FRAMEWORK:


import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Projections;


 String fname=request.getParameter("fname");
 String sname=request.getParameter("sname");

//CREATING SESSION   

 SessionFactory sdf=new Configuration().configure().buildSessionFactory();
            Session s=sdf.openSession();

  //TO GET MAX ID

Criteria criteria = s.createCriteria(Datahandler.class).setProjection(Projections.max("id"));
Integer maxid = (Integer)criteria.uniqueResult()+1;
Transaction tr=s.beginTransaction();

 //DATA INSERTION

 Datahandler obj=new Datahandler(maxid,fname,sname);

//SAVING AND RELEASING RESOURCES

 s.save(obj);
 tr.commit();
 s.close();
 out.println("DATA INSERTED....");