Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label retrieve. Show all posts
Showing posts with label retrieve. Show all posts

Sunday 16 June 2013

Using Java API for getting Weather information in Oracle ADF-wunderground-core

hello everyone, here is nice tutorial for all ADF techies-
i have developed an application in ADF that retrieves weather information of a country's weather stations.
as-temperature, humidity, dew, rain rate, wind speed etc using wunderground-core 
API . This API fetch weather data for a weater station by given month and year or data for all the stations of a country.

Feature of this wunderground-core-
  1. Very simple to use
  2. Date Listener support
  3. Fetch weather data for all stations of country
  4. can be used with desktop or web application
  5. Ajax supported
  6. Real Time weather values
Snap-
 Change Country-

and download jar distribution with all dependencies

  • Now start , create a fusion web application and add jar file to  classpath
  • Now create a bounded taskflow and a .jsff page inside this , now see the simple code to get all weather station of acountry and their weather data

  •         /*  create an instance of WeatherStationService */
            WeatherStationService weatherStationService = new WeatherStationService();
    
            /*  find all weather stations */
            List<WeatherStation> stations = weatherStationService.findAllWeatherStationsByCountry("INDIA");
    
            
            for (WeatherStation weatherStation : stations) {
                System.out.println(weatherStation.getStationId() + "\t" + "\t" + weatherStation.getCity() + "\t" +
                                   weatherStation.getCountry());
    
                HttpDataReaderService dataReader = new HttpDataReaderService();
                dataReader.setWeatherStation(weatherStation);
                DataSet current = dataReader.getCurrentData();
    
                System.out.println(current.getDateTime() + "Temperature- " + current.getTemperature() + "Humidity-" +
                                   current.getHumidity());
    

  • I have used this code on my af:commandButton for getting weather data, and a table on page to show this information on page using list (arrayList) , rest is same and simple like other ADF Application
  • Download Sample Application and see it, you will also learn that how to show a table using List(Java) Sample ADF Application




  • See source of .jsff page for tables'use and values-

  • <?xml version='1.0' encoding='UTF-8'?>
    <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:af="http://xmlns.oracle.com/adf/faces/rich"
              xmlns:f="http://java.sun.com/jsf/core">
        <af:panelBox text="Weather Report App-www.javadrive.co.in" id="pb1" showDisclosure="false">
            <f:facet name="toolbar"/>
            <af:panelFormLayout id="pfl1">
                <af:inputText label="Country Name" id="it1"
                              contentStyle="text-transform:upperCase;color:green;font-weight:bold;"
                              binding="#{WeatherReportBean.cntryNameBind}"/>
                <af:commandButton text="Get Report" id="cb1" actionListener="#{WeatherReportBean.getWeatherReportButton}"
                                  inlineStyle="font-weight:bold;"/>
            </af:panelFormLayout>
            <af:popup childCreation="deferred" autoCancel="disabled" id="p1">
                <af:dialog id="d1" title="User-User4" type="none">
                    <f:facet name="buttonBar"/>
                    <af:message id="m1" message="You are not authorize to make an order of 25000, your limit is 20000 !!!"
                                messageType="error"/>
                </af:dialog>
            </af:popup>
            <af:panelGroupLayout id="pgl1" layout="horizontal">
                <af:table var="row" rowBandingInterval="1" id="t1" value="#{WeatherReportBean.stations}"
                          partialTriggers="::cb1" contentDelivery="immediate" width="500">
                    <af:column sortable="false" headerText="Country" id="c3">
                        <af:spacer width="10" height="10" id="s2"/>
                        <af:outputText value="#{row.country}" id="ot3" inlineStyle="font-weight:bold;color:red;"/>
                        <af:spacer width="10" height="10" id="s1"/>
                    </af:column>
                    <af:column sortable="false" headerText="Locality" id="c4" width="150">
                        <af:outputText value="#{row.neighborhood}" id="ot4"/>
                    </af:column>
                    <af:column sortable="false" headerText="City" id="c2">
                        <af:outputText value="#{row.city}" id="ot2" inlineStyle="font-weight:bold;color:darkgreen;"/>
                    </af:column>
                    <af:column sortable="false" headerText="Station Id" id="c1">
                        <af:outputText value="#{row.stationId}" id="ot1" inlineStyle="font-weight:bold;color:darkblue;"/>
                    </af:column>
                </af:table>
                <af:table value="#{WeatherReportBean.reportL}" var="row" rowBandingInterval="1" id="t2"
                          contentDelivery="immediate" partialTriggers="::t1 ::cb1" styleClass="AFStretchWidth" width="800">
                    <af:column sortable="false" headerText="Date" align="start" id="c5">
                        <af:spacer width="10" height="10" id="s4"/>
                        <af:outputText value="#{row.currDate}" id="ot5" inlineStyle="font-weight:bold;">
                            <af:convertDateTime pattern="dd/MMM/yyyy"/>
                        </af:outputText>
                        <af:spacer width="10" height="10" id="s3"/>
                    </af:column>
                    <af:column sortable="false" headerText="Temperature" align="end" id="c6" width="70">
                        <af:outputText value="#{row.temp} C" id="ot6" inlineStyle="font-weight:bold;color:teal;"/>
                    </af:column>
                    <af:column id="c7" headerText="Humidity" align="right" width="70">
                        <af:outputText value="#{row.humid}" id="ot7" inlineStyle="font-weight:bold;color:orange;"/>
                    </af:column>
                    <af:column id="c8" headerText="Dew Point" align="right" width="70">
                        <af:outputText value="#{row.dewR}" id="ot8" inlineStyle="font-weight:bold;color:green;"/>
                    </af:column>
                    <af:column id="c9" headerText="Wind Direction" align="right" width="70">
                        <af:outputText value="#{row.windDirec}" id="ot9" inlineStyle="font-weight:bold; color:Olive;"/>
                    </af:column>
                    <af:column id="c10" headerText="Wind Speed (Km/H)" align="right">
                        <af:outputText value="#{row.windSpd}" id="ot10"
                                       inlineStyle="color:ActiveCaption;font-weight:bold;"/>
                    </af:column>
                    <af:column id="c11" headerText="Rain Rate" align="right" width="70">
                        <af:outputText value="#{row.rainRt}" id="ot11" inlineStyle="font-weight:bold;color:maroon;"/>
                    </af:column>
                    <af:column id="c12">
                        <af:image shortDesc="Weather" id="i1"
                                  source="#{row.temp >35 ? resource['images:sun.png'] : resource['images:sun_slush.png']}"/>
                    </af:column>
                </af:table>
            </af:panelGroupLayout>
        </af:panelBox>
    </jsp:root>
    

  • And see managed bean code to populate data in tables on page, other than manged bean i have used a java bean to populate value in list and then in table
  • WeatherReportBean.java

  • package weather.view.bean;
    
    import de.mbenning.weather.wunderground.api.domain.DataSet;
    import de.mbenning.weather.wunderground.api.domain.WeatherStation;
    import de.mbenning.weather.wunderground.impl.services.HttpDataReaderService;
    import de.mbenning.weather.wunderground.impl.services.WeatherStationService;
    
    import java.io.Serializable;
    
    import java.sql.SQLException;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.faces.event.ActionEvent;
    
    import oracle.adf.view.rich.component.rich.input.RichInputText;
    
    import oracle.jbo.domain.Date;
    
    public class WeatherReportBean implements Serializable {
        private RichInputText cntryNameBind;
    
        public WeatherReportBean() {
        }
        WeatherStationService weatherStationService = new WeatherStationService();
    
        List<WeatherStation> stations;
        List<Report> reportL;
    
    
        public void getWeatherReportButton(ActionEvent actionEvent) throws SQLException {
            if (cntryNameBind.getValue() != null) {
                String cntryName = cntryNameBind.getValue().toString();
    
                stations = weatherStationService.findAllWeatherStationsByCountry(cntryName);
                reportL = new ArrayList<Report>(50);
                for (WeatherStation weatherStation : stations) {
    
                    HttpDataReaderService dataReader = new HttpDataReaderService();
                    dataReader.setWeatherStation(weatherStation);
                    DataSet current = dataReader.getCurrentData();
    
                    java.util.Date b = new java.util.Date();
                    Double c = new Double(0);
                    Integer humidity = 0;
                    Double dew = new Double(0);
                    String windDir = "";
                    Double windSp = new Double(0);
                    Double rainRate = new Double(0);
    
                    if (current != null) {
                        if (current.getDateTime() != null) {
                            b = current.getDateTime();
                            c = current.getTemperature();
                            humidity = current.getHumidity();
                            dew = current.getDewPoint();
                            windDir = current.getWindDirection();
                            windSp = current.getWindSpeedKmh();
                            rainRate = current.getRainRateHourlyMm();
                            System.out.println("Humidity is-->" + current.getHumidity() + "dew point-->" +
                                               current.getDewPoint() + "wind -->" + current.getWindDirection() +
                                               "speed--" + current.getWindSpeedKmh());
    
    
                        }
                    }
    
                    reportL.add(new Report(b, c, humidity, dew, windDir, windSp, rainRate));
    
                }
    
    
            }
    
        }
    
        public void setStations(List<WeatherStation> stations) {
            this.stations = stations;
        }
    
        public List<WeatherStation> getStations() {
            return stations;
        }
    
        public void setReportL(List<Report> reportL) {
            this.reportL = reportL;
        }
    
        public List<Report> getReportL() {
            return reportL;
        }
    
        public void setCntryNameBind(RichInputText cntryNameBind) {
            this.cntryNameBind = cntryNameBind;
        }
    
        public RichInputText getCntryNameBind() {
            return cntryNameBind;
        }
    }
    

  • Report.java- bean class to populate list

  • package weather.view.bean;
    
    import java.util.Date;
    
    public class Report {
    
        private Date CurrDate;
        private double temp;
        Integer humid;
        Double dewR;
        String windDirec;
        Double windSpd;
        Double rainRt;
    
        public Report(Date date, Double double1, Integer hum, Double de, String windD, Double windS, Double Rain) {
            System.out.println("date is-->" + date + "and temp is-->" + double1);
    
            this.CurrDate = date;
            setTemp(double1);
            this.humid = hum;
            this.dewR = de;
            this.windDirec = windD;
            this.windSpd = windS;
            this.rainRt = Rain;
    
    
        }
    
    
        public void setCurrDate(Date CurrDate) {
            this.CurrDate = CurrDate;
        }
    
        public Date getCurrDate() {
            return CurrDate;
        }
    
        public void setTemp(double temp) {
            this.temp = temp;
        }
    
        public double getTemp() {
            return temp;
        }
    
        public void setHumid(Integer humid) {
            this.humid = humid;
        }
    
        public Integer getHumid() {
            return humid;
        }
    
        public void setDewR(Double dewR) {
            this.dewR = dewR;
        }
    
        public Double getDewR() {
            return dewR;
        }
    
        public void setWindDirec(String windDirec) {
            this.windDirec = windDirec;
        }
    
        public String getWindDirec() {
            return windDirec;
        }
    
        public void setWindSpd(Double windSpd) {
            this.windSpd = windSpd;
        }
    
        public Double getWindSpd() {
            return windSpd;
        }
    
        public void setRainRt(Double rainRt) {
            this.rainRt = rainRt;
        }
    
        public Double getRainRt() {
            return rainRt;
        }
    }