Please disable your adblock and script blockers to view this page

Search this blog

Wednesday 5 June 2013

Download file from url using Oracle ADF & Java- Download Manager

In this tutorial i will show you how to download a file from its url (web)-
as- http://www.tutorialspoint.com/java/java_tutorial.pdf
This will work as a simple download manager, you can add file url to download it and save it.
This tutorial makes use of FileHandling and java.net.URL class in java.

  • User Interface is very simple to design as it have only one input text and one button, so create a fusion web application , and a bounded taskflow with a page fragment in it
  • Now drag a Input text to enter url and a button to perform action on it from Componenet palette .
  • Bind input text to bean to get its value from page

  •  private RichInputText fileUrlBind;
    
        public void setFileUrlBind(RichInputText fileUrlBind) {
            this.fileUrlBind = fileUrlBind;
        }
    
        public RichInputText getFileUrlBind() {
            return fileUrlBind;
        }
    
  • Now create actionListener to managed bean and write code for downloading file from given url- see code

  •     public void DownloadFileButton(ActionEvent actionEvent) {
            try {
                if (fileUrlBind.getValue() != null) {
                    String fileUrl = fileUrlBind.getValue().toString();
                    if (fileUrl.startsWith("http://")) {
                        String msgNm = fileUrl.substring(7);
    
                        cnctmsgBind.setValue("Connecting to " + msgNm + "....");
    
                        URL url = new URL(fileUrl);
                        url.openConnection();
                        InputStream reader = url.openStream();
    
    
                        FileOutputStream writer =
                            new FileOutputStream("C:/javadrive." + fileUrl.substring(fileUrl.lastIndexOf(".")));
                        byte[] buffer = new byte[153600];
                        int totalBytesRead = 0;
                        int bytesRead = 0;
    
                       
                        dwnldMsgBind.setValue("Reading file 150KB blocks at a time");
                        while ((bytesRead = reader.read(buffer)) > 0) {
                            writer.write(buffer, 0, bytesRead);
                            buffer = new byte[153600];
                            totalBytesRead += bytesRead;
                        }
                     
                        alerMsgBind.setValue("File is downloaded successfully, look at your c drive :-)");
                        writer.close();
                        reader.close();
                    } else {
                        FacesMessage errMsg = new FacesMessage("Something went wrong");
                        errMsg.setSeverity(FacesMessage.SEVERITY_ERROR);
                        errMsg.setDetail("Example- http://www.javadrive.co.in/java.pdf");
                        FacesContext context = FacesContext.getCurrentInstance();
                        context.addMessage(fileUrlBind.getClientId(), errMsg);
                    }
                }
            } catch (MalformedURLException e) {
                FacesMessage errMsg = new FacesMessage("Something went wrong");
                errMsg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, errMsg);
                e.printStackTrace();
            } catch (IOException e) {
                FacesMessage errMsg = new FacesMessage("Something went wrong");
                errMsg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, errMsg);
                e.printStackTrace();
            }
    
    
        }
    
    




  • Now Run your application and see downloaded file in your c drive as path is hard coded in bean


  •  See your downloaded file in c drive


Tuesday 4 June 2013

Fun - Develop Tic Tac Toe game in Oracle ADF/Java

Tic Tac Toe or Naughty Cross is a paper-pencil game for two players and simple in  concept.
I have developed a application on ADF, using JSF page and unbounded adfc-config.xml .
There is nothing to explain, see the sample application at bottom of this post
  • Tic Tac Toe game has 9 square to enter values 0 or X, so for that i have used 9 buttons and images of 0 and X



  • And create a managed bean for all nine buttons, to execute proper conditions for each move
     
  • See managed bean code and conditions for player's move(only Java Code is needed to do this)



  • package ticTacToeADF.view;
    
    import javax.faces.event.ActionEvent;
    
    import oracle.adf.view.rich.component.rich.nav.RichCommandButton;
    
    public class TictactoeBean {
    
        public TictactoeBean() {
        }
        private static String one = "     ";
        private static String two = "     ";
        private static String three = "     ";
        private static String four = "     ";
        private static String five = "     ";
        private static String six = "     ";
        private static String seven = "     ";
        private static String eight = "     ";
        private static String nine = "     ";
        private static boolean check = false;
        private String Result = "";
        private static boolean oneDis = false;
        private static boolean twoDis = false;
        private static boolean threeDis = false;
        private static boolean fourDis = false;
        private static boolean fiveDis = false;
        private static boolean sixDis = false;
        private static boolean sevenDis = false;
        private static boolean eightDis = false;
        private static boolean nineDis = false;
    
    
        public void Button1(ActionEvent actionEvent) {
            if (check == false) {
                this.setOne("X");
    
                check = true;
                this.setOneDis(true);
                System.out.println("Check is--------->" + check);
                if (one.equalsIgnoreCase(two) && two.equalsIgnoreCase(three)) {
                    this.setResult(one + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (one.equalsIgnoreCase(five) && five.equalsIgnoreCase(nine)) {
                    this.setResult(one + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (one.equalsIgnoreCase(four) && four.equalsIgnoreCase(seven)) {
                    this.setResult(one + " wins");
                    finishgame();
                    System.out.println("One wins");
                }
    
    
            } else {
                this.setOne("0");
    
                check = false;
                this.setOneDis(true);
                System.out.println("Check is--------->" + check);
                if (one.equalsIgnoreCase(two) && two.equalsIgnoreCase(three)) {
                    this.setResult(one + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (one.equalsIgnoreCase(five) && five.equalsIgnoreCase(nine)) {
                    this.setResult(one + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (one.equalsIgnoreCase(four) && four.equalsIgnoreCase(seven)) {
                    this.setResult(one + " wins");
                    finishgame();
                    System.out.println("One wins");
                }
    
            }
    
        }
    
        public void Button2(ActionEvent actionEvent) {
            if (check == false) {
                this.setTwo("X");
                check = true;
                this.setTwoDis(true);
                System.out.println("Check is--------->" + check);
                if (two.equalsIgnoreCase(five) && five.equalsIgnoreCase(eight)) {
                    this.setResult(two + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (two.equalsIgnoreCase(one) && one.equalsIgnoreCase(three)) {
                    this.setResult(two + " wins");
                    finishgame();
                    System.out.println("One wins");
                }
            } else {
                this.setTwo("0");
                check = false;
                this.setTwoDis(true);
                System.out.println("Check is--------->" + check);
                if (two.equalsIgnoreCase(five) && five.equalsIgnoreCase(eight)) {
                    this.setResult(two + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (two.equalsIgnoreCase(one) && one.equalsIgnoreCase(three)) {
                    this.setResult(two + " wins");
                    finishgame();
                    System.out.println("One wins");
                }
            }
        }
    
        public void Button3(ActionEvent actionEvent) {
            if (check == false) {
                this.setThree("X");
                check = true;
                this.setThreeDis(true);
                System.out.println("Check is--------->" + check);
    
                if (three.equalsIgnoreCase(six) && six.equalsIgnoreCase(nine)) {
                    this.setResult(three + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (three.equalsIgnoreCase(five) && five.equalsIgnoreCase(seven)) {
                    this.setResult(three + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (three.equalsIgnoreCase(one) && one.equalsIgnoreCase(two)) {
                    this.setResult(three + " wins");
                    finishgame();
                    System.out.println("One wins");
                }
    
            } else {
                this.setThree("0");
                check = false;
                this.setThreeDis(true);
                System.out.println("Check is--------->" + check);
    
                if (three.equalsIgnoreCase(six) && six.equalsIgnoreCase(nine)) {
                    this.setResult(three + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (three.equalsIgnoreCase(five) && five.equalsIgnoreCase(seven)) {
                    this.setResult(three + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (three.equalsIgnoreCase(one) && one.equalsIgnoreCase(two)) {
                    this.setResult(three + " wins");
                    finishgame();
                    System.out.println("One wins");
                }
            }
        }
    
        public void Button4(ActionEvent actionEvent) {
            if (check == false) {
                this.setFour("X");
                check = true;
                this.setFourDis(true);
                System.out.println("Check is--------->" + check);
    
                if (four.equalsIgnoreCase(one) && one.equalsIgnoreCase(seven)) {
                    this.setResult(four + " wins");
                    finishgame();
                    System.out.println("four wins");
                } else if (four.equalsIgnoreCase(five) && five.equalsIgnoreCase(six)) {
                    this.setResult(four + " wins");
                    finishgame();
                    System.out.println("One wins");
                }
    
            } else {
                this.setFour("0");
                check = false;
                this.setFourDis(true);
                System.out.println("Check is--------->" + check);
    
                if (four.equalsIgnoreCase(one) && one.equalsIgnoreCase(seven)) {
                    this.setResult(four + " wins");
                    finishgame();
                    System.out.println("four wins");
                } else if (four.equalsIgnoreCase(five) && five.equalsIgnoreCase(six)) {
                    this.setResult(four + " wins");
                    finishgame();
                    System.out.println("One wins");
                }
            }
        }
    
        public void Button5(ActionEvent actionEvent) {
            if (check == false) {
                this.setFive("X");
                check = true;
                this.setFiveDis(true);
                System.out.println("Check is--------->" + check);
    
                if (one.equalsIgnoreCase(five) && five.equalsIgnoreCase(nine)) {
                    this.setResult(five + " wins");
                    finishgame();
                    System.out.println("One wins");
    
                } else if (two.equalsIgnoreCase(five) && five.equalsIgnoreCase(eight)) {
                    this.setResult(five + " wins");
                    finishgame();
                    System.out.println("One wins");
                }
    
                else if (four.equalsIgnoreCase(five) && five.equalsIgnoreCase(six)) {
                    this.setResult(five + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (five.equalsIgnoreCase(three) && three.equalsIgnoreCase(seven)) {
                    this.setResult(five + " wins");
                    finishgame();
                    System.out.println("One wins");
                }
            } else {
                this.setFive("0");
                check = false;
                this.setFiveDis(true);
                System.out.println("Check is--------->" + check);
    
                if (one.equalsIgnoreCase(five) && five.equalsIgnoreCase(nine)) {
                    this.setResult(five + " wins");
                    finishgame();
                    System.out.println("One wins");
    
                } else if (two.equalsIgnoreCase(five) && five.equalsIgnoreCase(eight)) {
                    this.setResult(five + " wins");
                    finishgame();
                    System.out.println("One wins");
                }
    
                else if (four.equalsIgnoreCase(five) && five.equalsIgnoreCase(six)) {
                    this.setResult(five + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (five.equalsIgnoreCase(three) && three.equalsIgnoreCase(seven)) {
                    this.setResult(five + " wins");
                    finishgame();
                    System.out.println("One wins");
                }
    
            }
        }
    
        public void Button6(ActionEvent actionEvent) {
            if (check == false) {
                this.setSix("X");
                check = true;
                this.setSixDis(true);
                System.out.println("Check is--------->" + check);
    
                if (four.equalsIgnoreCase(five) && five.equalsIgnoreCase(six)) {
                    this.setResult(six + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (three.equalsIgnoreCase(six) && six.equalsIgnoreCase(nine)) {
                    this.setResult(six + " wins");
                    finishgame();
                    System.out.println("One wins");
                }
            } else {
                this.setSix("0");
                check = false;
                this.setSixDis(true);
                System.out.println("Check is--------->" + check);
    
                if (four.equalsIgnoreCase(five) && five.equalsIgnoreCase(six)) {
                    this.setResult(six + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (three.equalsIgnoreCase(six) && six.equalsIgnoreCase(nine)) {
                    this.setResult(six + " wins");
                    finishgame();
                    System.out.println("One wins");
                }
            }
        }
    
        public void Button7(ActionEvent actionEvent) {
            if (check == false) {
                this.setSeven("X");
                check = true;
                this.setSevenDis(true);
                System.out.println("Check is--------->" + check);
    
                if (four.equalsIgnoreCase(one) && one.equalsIgnoreCase(seven)) {
                    this.setResult(four + " wins");
                    finishgame();
                    System.out.println("four wins");
                } else if (five.equalsIgnoreCase(three) && three.equalsIgnoreCase(seven)) {
                    this.setResult(seven + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (seven.equalsIgnoreCase(eight) && eight.equalsIgnoreCase(nine)) {
                    this.setResult(seven + " wins");
                    finishgame();
                    System.out.println("One wins");
                }
            } else {
                this.setSeven("0");
                check = false;
                this.setSevenDis(true);
                System.out.println("Check is--------->" + check);
                if (four.equalsIgnoreCase(one) && one.equalsIgnoreCase(seven)) {
                    this.setResult(four + " wins");
                    finishgame();
                    System.out.println("four wins");
                } else if (five.equalsIgnoreCase(three) && three.equalsIgnoreCase(seven)) {
                    this.setResult(seven + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (seven.equalsIgnoreCase(eight) && eight.equalsIgnoreCase(nine)) {
                    this.setResult(seven + " wins");
                    finishgame();
                    System.out.println("One wins");
                }
            }
        }
    
        public void Button8(ActionEvent actionEvent) {
            if (check == false) {
                this.setEight("X");
                check = true;
                this.setEightDis(true);
                System.out.println("Check is--------->" + check);
                if (eight.equalsIgnoreCase(two) && two.equalsIgnoreCase(five)) {
                    this.setResult(eight + " wins");
                    finishgame();
                    System.out.println("four wins");
                } else if (eight.equalsIgnoreCase(seven) && seven.equalsIgnoreCase(nine)) {
                    this.setResult(eight + " wins");
                    finishgame();
                    System.out.println("One wins");
    
                }
            } else {
                this.setEight("0");
                check = false;
                this.setEightDis(true);
                System.out.println("Check is--------->" + check);
                if (eight.equalsIgnoreCase(two) && two.equalsIgnoreCase(five)) {
                    this.setResult(eight + " wins");
                    finishgame();
                    System.out.println("four wins");
                } else if (eight.equalsIgnoreCase(seven) && seven.equalsIgnoreCase(nine)) {
                    this.setResult(eight + " wins");
                    finishgame();
                    System.out.println("One wins");
    
                }
            }
        }
    
        public void Button9(ActionEvent actionEvent) {
            if (check == false) {
                this.setNine("X");
                check = true;
                this.setNineDis(true);
                System.out.println("Check is--------->" + check);
                if (nine.equalsIgnoreCase(one) && one.equalsIgnoreCase(five)) {
                    this.setResult(nine + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (three.equalsIgnoreCase(six) && six.equalsIgnoreCase(nine)) {
                    this.setResult(six + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (eight.equalsIgnoreCase(seven) && seven.equalsIgnoreCase(nine)) {
                    this.setResult(eight + " wins");
                    finishgame();
                    System.out.println("One wins");
    
                }
            } else {
                this.setNine("0");
                check = false;
                this.setNineDis(true);
                System.out.println("Check is--------->" + check);
    
                if (nine.equalsIgnoreCase(one) && one.equalsIgnoreCase(five)) {
                    this.setResult(nine + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (three.equalsIgnoreCase(six) && six.equalsIgnoreCase(nine)) {
                    this.setResult(six + " wins");
                    finishgame();
                    System.out.println("One wins");
                } else if (eight.equalsIgnoreCase(seven) && seven.equalsIgnoreCase(nine)) {
                    this.setResult(eight + " wins");
                    finishgame();
                    System.out.println("One wins");
    
                }
            }
        }
    
        public void setOne(String one) {
            this.one = one;
        }
    
        public String getOne() {
            return one;
        }
    
        public void setTwo(String two) {
            this.two = two;
        }
    
        public String getTwo() {
            return two;
        }
    
        public void setThree(String three) {
            this.three = three;
        }
    
        public String getThree() {
            return three;
        }
    
        public void setFour(String four) {
            this.four = four;
        }
    
        public String getFour() {
            return four;
        }
    
        public void setFive(String five) {
            this.five = five;
        }
    
        public String getFive() {
            return five;
        }
    
        public void setSix(String six) {
            this.six = six;
        }
    
        public String getSix() {
            return six;
        }
    
        public void setSeven(String seven) {
            this.seven = seven;
        }
    
        public String getSeven() {
            return seven;
        }
    
        public void setEight(String eight) {
            this.eight = eight;
        }
    
        public String getEight() {
            return eight;
        }
    
        public void setNine(String nine) {
            this.nine = nine;
        }
    
        public String getNine() {
            return nine;
        }
    
        public void setResult(String Result) {
            this.Result = Result;
        }
    
        public String getResult() {
            return Result;
        }
    
        public void ResetButton(ActionEvent actionEvent) {
            this.one = "     ";
            this.two = "     ";
            this.three = "     ";
            this.four = "     ";
            this.five = "     ";
            this.six = "     ";
            this.seven = "     ";
            this.eight = "     ";
            this.nine = "     ";
            this.check = false;
            this.oneDis = false;
            this.twoDis = false;
            this.threeDis = false;
            this.fourDis = false;
            this.fiveDis = false;
            this.sixDis = false;
            this.sevenDis = false;
            this.eightDis = false;
            this.nineDis = false;
    
        }
    
        public void finishgame() {
            this.oneDis = true;
            this.twoDis = true;
            this.threeDis = true;
            this.fourDis = true;
            this.fiveDis = true;
            this.sixDis = true;
            this.sevenDis = true;
            this.eightDis = true;
            this.nineDis = true;
        }
    
        public void setOneDis(boolean oneDis) {
            this.oneDis = oneDis;
        }
    
        public boolean isOneDis() {
            return oneDis;
        }
    
        public void setTwoDis(boolean twoDis) {
            this.twoDis = twoDis;
        }
    
        public boolean isTwoDis() {
            return twoDis;
        }
    
        public void setThreeDis(boolean threeDis) {
            this.threeDis = threeDis;
        }
    
        public boolean isThreeDis() {
            return threeDis;
        }
    
        public void setFourDis(boolean fourDis) {
            this.fourDis = fourDis;
        }
    
        public boolean isFourDis() {
            return fourDis;
        }
    
        public void setFiveDis(boolean fiveDis) {
            this.fiveDis = fiveDis;
        }
    
        public boolean isFiveDis() {
            return fiveDis;
        }
    
        public void setSixDis(boolean sixDis) {
            this.sixDis = sixDis;
        }
    
        public boolean isSixDis() {
            return sixDis;
        }
    
        public void setSevenDis(boolean sevenDis) {
            this.sevenDis = sevenDis;
        }
    
        public boolean isSevenDis() {
            return sevenDis;
        }
    
        public void setEightDis(boolean eightDis) {
            this.eightDis = eightDis;
        }
    
        public boolean isEightDis() {
            return eightDis;
        }
    
        public void setNineDis(boolean nineDis) {
            this.nineDis = nineDis;
        }
    
        public boolean isNineDis() {
            return nineDis;
        }
    
    
    }
    

  • Download and Run this sample application-Sample ADF Application