Monday, December 31, 2012

Create a url link with JasperReports

To make a textField a hyperlink to an external URL, you need to add the attributehyperlinkType="Reference" to the element, and add a  tag within it. The reference expression is where you put the URL.
For example:

    
    
    <![CDATA["Click Here!"]]>
    <![CDATA["http://www.google.com"]]>

The hyperlinkTarget attribute behaves in the same way as the target attribute in HTML.
Note that only textFields, images and charts can be hyperlinked in this way.
For a more in-depth explanation and more examples, see this jasperforge page.

Friday, December 28, 2012

How to calculate average of a column and then include it in a select query in oracle?

Your group by is what aggregates your average, and it is grouping by the whole table (I am assuming you did this to allow the select for everything) Just move your avg into another subquery, remove the overarching group by and that should solve it.
SELECT id, m_name AS "Mobile Name", cost AS Price,
    (SELECT AVG(cost) FROM mobile) AS Average,
    cost-(SELECT AVG(cost) FROM mobile) AS Difference 
FROM mobile;
When you run the basic SELECT AVG(cost) statement it is naturally grouping by the column specified (cost in this case) as that is what you are requesting. I would suggest reading up more on GROUP BY and aggregates to get a better grasp on the concept. That should help you more than just a simple solution.
UPDATE:
The answer below is actually from David's answer. It makes use the analytical functions. Basically, what is happening is that on each AVG call, you are telling the engine what to use for the function (in this case, nothing). A decent writeup on analytical functions can be found here and here and more with a google on the matter.
SELECT id, m_name AS "Mobile Name" cost AS Price, AVG(cost) OVER( ) AS Average, 
    cost - AVG(cost) OVER ( ) AS Difference
    FROM mobile
However, if your SQL engine allows for variables, you could just as easily do the below answer. I actually prefer this for future maintainability/readability. The reason is that a variable with a good name can be very descriptive to future readers of the code, versus an analytical function that does require a little bit more work to read (especially if you do not understand the over function).
Also, this solution duplicates the same query twice, so it might be worth storing your average in a SQL variable. Then you ca change your statement to simply use that global average
This is variables in SQL-Server (you will have to adapt it for your own instance of SQL)
DECLARE @my_avg INT;
SELECT @my_avg = AVG(cost) FROM Mobile;

    SELECT id, m_name AS "Mobile Name", cost AS Price,
        @my_avg AS Average, cost-@my_avg AS Difference 
    FROM mobile;
This solution will read a lot cleaner to future readers of your SQL, too

http://stackoverflow.com/questions/9647693/how-to-calculate-average-of-a-column-and-then-include-it-in-a-select-query-in-or

Thursday, December 27, 2012

Calculating percentage of a row over total sum

You're looking for the analytical function ratio_to_report
select 
  agent,
  round(ratio_to_report(commission) over ()*100) "% Comm."
from  
  commissions;
http://dba.stackexchange.com/questions/653/calculating-percentage-of-a-row-over-total-sum

1.2 Identifying Time Zones and Zone Offsets

[XML Schema] follows the [ISO 8601] standard for its lexical representation. Date and time values in ISO 8601 are field-based using the definitions above and can indicate (or omit) the zone offset from UTC. A zone offset is not the same thing as a time zone, and the difference can be important. XML Schema only supports zone offset, but, confusingly, calls it time zone, see for example section 3.2.8.1, lexical representation in [XML Schema].
Although ISO 8601 is expressed in terms of the Gregorian calendar, it can be used to represent values in any calendar system. The presentation of date and time values to end users using different calendar and timekeeping systems is separate from the lexical representation.
What is a "zone offset"? A zone offset is the difference in hours and minutes between a particular time zone and UTC. In ISO 8601, the particular zone offset can be indicated in a date or time value. The zone offset can be Z for UTC or it can be a value "+" or "-" from UTC. For example, the value 08:00-08:00 represents 8:00 AM in a time zone 8 hours behind UTC, which is the equivalent of 16:00Z (8:00 plus eight hours). The value 08:00+08:00 represents the opposite increment, or midnight (08:00 minus eight hours).
What is a "time zone"? A time zone is an identifier for a specific location or region which translates into a combination of rules for calculating the UTC offset. For example, when a website maintaining a group calendar in the United States schedules a recurring meeting for 08:00 Pacific Time, it is referring to what is sometimes known as wall time (so called because that is the time shown "on the clock (or calendar) on the wall"). This is not equivalent to either 08:00-08:00 or 08:00-07:00, because Pacific Time does not have a fixed offset from UTC; instead, the offset changes during the course of the year. As mentioned before, XML Schema only supports zone offsets, and it does not make the terminological distinction between zone offset and time zone. So a wall time expressed as an XML Schema time value, must choose which zone offset to use. This may have the unintended effect of causing a scheduled event to shift by an hour (or more) when wall time changes to or from Daylight/Summer time.
To complicate matters, the rules for computing when daylight savings takes effect may be somewhat complex and may change from year to year or from location to location. In the United States, the state of Indiana, for example, does not follow daylight savings time, but this will change in April 2006. See: http://www.mccsc.edu/time.html for further information. The Northern and Southern hemispheres perform Daylight/Summer Time adjustments during opposing times during the year (corresponding to seasonal differences in the two hemispheres).
To capture these situations, a calendar system must use an ID for the time zone. The most definitive reference for dealing with wall time is the TZ database (also known as the "Olson time zone database" [tzinfo]), which is used by systems such as various commercial UNIX operating systems, Linux, Java, CLDR, ICU, and many other systems and libraries. In the TZ database, "Pacific Time" is denoted with the ID America/Los_Angeles. The TZ database also supplies aliases among different IDs; for example, Asia/Ulan Bator is equivalent to Asia/Ulaanbaatar. From these alias relations, a canonical identifier can be derived. The Common Locale Data Repository [CLDR] can be used to provide a localized form for the IDs: see Appendix J in [UAX 35].

Tuesday, December 25, 2012

Multiple COUNT SELECTS from the same table in one query

SELECT sum(case when Page LIKE '%AEC%' then 1 end) as tAEC, 
    sum(case when Page LIKE '%TOL%' then 1 end) as tTOL 
FROM someTable 
http://stackoverflow.com/questions/12938332/multiple-count-selects-from-the-same-table-in-one-query

Getting seconds between two Oracle Timestamps

"Best Practice"
Whatever you do, wrap it in a function, e.g. seconds_between (from_date, to_date) - doesn't matter how it does it (choose the most efficient method) - then it will be perfectly obvious what your code is doing.
Performance
I tested the two methods on 11gR1 on my laptop (WinXP) with the test case below. It seems the CAST option is the fastest. (t1 is baseline, t2 used the extract method, t3 used the cast method)
t1 (nothing) 3
t2 (extract) 338
t3 (cast)    101

t1 (nothing) 3
t2 (extract) 336
t3 (cast)    100
Test script
declare
 x TIMESTAMP := SYSTIMESTAMP;
 y TIMESTAMP := TRUNC(SYSDATE);
 n PLS_INTEGER;
 lc CONSTANT PLS_INTEGER := 1000000;
 t1 PLS_INTEGER;
 t2 PLS_INTEGER;
 t3 PLS_INTEGER;
begin
 t1 := DBMS_UTILITY.get_time;
 for i in 1..lc loop
  n := i;
 end loop;
 t1 := DBMS_UTILITY.get_time - t1;
 t2 := DBMS_UTILITY.get_time;
 for i in 1..lc loop
  n := extract(day from (x-y))*24*60*60
     + extract(hour from (x-y))*60*60
     + extract(minute from (x-y))*60
     + extract(second from (x-y));
 end loop;
 t2 := DBMS_UTILITY.get_time - t2;
 t3 := DBMS_UTILITY.get_time;
 for i in 1..lc loop
  n := ( CAST( x AS DATE ) - CAST( y AS DATE ) ) * 86400;
 end loop;
 t3 := DBMS_UTILITY.get_time - t3;
 dbms_output.put_line('t1 (nothing) ' || t1);
 dbms_output.put_line('t2 (extract) ' || t2);
 dbms_output.put_line('t3 (cast)    ' || t3);
end;
http://stackoverflow.com/questions/3957909/getting-seconds-between-two-oracle-timestamps

Monday, December 24, 2012

Webservices - SOAP vs. “XML over HTTP”

SOAP is just a specialization of XML over HTTP and that response you posted does indeed look like a SOAP response (a SOAP fault actually).
This looks like a big misunderstanding, so don't assume they are pulling your leg. Try asking your question in a different way.
As for the WSDL, if this is indeed a 100% SOAP web service, do note that it is not mandatory to have a WSDL for a SOAP web service.
A web service is just an application that exposes a set of operations over the network. In order to call these operations you need to know what their name is, what parameters they expect, what types the parameters have etc, so that you know how to build your client stub.
This means the web service needs to be documented or else you would not know how to write the code that interact with the web service. This documentation can be a Word document or a PDF and you could build the client manually from that (which involves writing a lot of plumbing code for that client stub of yours) OR the documentation could be a WSDL file which unlike a PDF or Word document can be fed to a tool to generate the stub plumbing code for you automatically.
The WSDL describes the web service - and it's good practice to provide one - but the web service exists separately from the WSDL.

Class teardown in junit 3?

Found the answer to my own question :) I had tried this briefly before posting my question, but it wasn't working for me. But I realize now that it's because our test framework is invoking junit differently than the default, so it wasn't calling the "suite" method that is needed in the following solution. In Eclipse, if I use the bundled junit runner to directly execute one Test/TestCase, it runs fine.
A way to do this setup/teardown once per class in junit 3 is to use TestSuite. Here's an example on junit.org:
public static Test suite() {
    return new TestSetup(new TestSuite(YourTestClass.class)) {

        protected void setUp() throws Exception {
            System.out.println(" Global setUp ");
        }
        protected void tearDown() throws Exception {
            System.out.println(" Global tearDown ");
        }
    };
}
http://stackoverflow.com/questions/1646586/class-teardown-in-junit-3

Friday, December 21, 2012

Is there a way to find/replace across an entire project in Eclipse?

Search->File menu, enter the text to search for, hit the Replace... button which will give you another dialog where you can replace text.

http://stackoverflow.com/questions/3426049/is-there-a-way-to-find-replace-across-an-entire-project-in-eclipse

Thursday, December 20, 2012

Should dateTime elements include time zone information in SOAP messages?

Given the following sentences from the w3c schema documentation:
"Local" or untimezoned times are presumed to be the time in the timezone of some unspecified locality as prescribed by the appropriate legal authority;
and
When a timezone is added to a UTC dateTime, the result is the date and time "in that timezone".
it does not sound like there is a definitive answer to this. I would assume that it is the usual ambiguity: Both versions are principally valid, and the question of what version to use depends on the configuration/behavior/expectations of the system one is interfacing with.
And even if there where a definitive answer, I would definitely not rely on it, but rather expect that every other web service and library had its own way of dealing with this :/

 http://stackoverflow.com/questions/1642636/should-datetime-elements-include-time-zone-information-in-soap-messages

xml schema Timezone

I think that your interpretation might be off; I can't see how you ended up with GMT+13:00 from 13:00:00Z.
The XSD spec gives the following example:
2002-10-10T12:00:00+05:00 is 2002-10-10T07:00:00Z 
2002-10-10T00:00:00+05:00 is 2002-10-09T19:00:00Z
A nonnegative duration means that the time zone is ahead; negative is behind.
Assuming that the timestamp was taken at midnight (12:00AM), and it matched 13:00:00Z, then you could offset this either ahead or behind:
Behind: 2012-02-04T00:00:00-13:00 is 2012-02-04T13:00:00Z
Ahead:  2012-02-05T00:00:00+11:00 is 2012-02-04T13:00:00Z
The only one valid is the ahead (there is no -13); As to what is in that time zone, take a look here.

http://stackoverflow.com/questions/9950406/xml-schema-timezone

Wednesday, December 19, 2012

Clustered Timers | JAVA Developer's Journal

Clustered Timers | JAVA Developer's Journal

Determine if a String is an Integer in Java

public static boolean isInteger(String s) {
    try { 
        Integer.parseInt(s); 
    } catch(NumberFormatException e) { 
        return false; 
    }
    // only got here if we didn't return false
    return true;
}
A non exception based method:
public static boolean isInteger(String s) {
    return isInteger(s,10);
}

public static boolean isInteger(String s, int radix) {
    if(s.isEmpty()) return false;
    for(int i = 0; i < s.length; i++) {
        if(i == 0 && s.charAt(i) == '-') continue;
        if(Character.digit(s.charAt(i),radix) < 0) return false;
    }
    return true;
}
http://stackoverflow.com/questions/5439529/determine-if-a-string-is-an-integer-in-java 

Monday, December 17, 2012

Java.util.Calendar - milliseconds since Jan 1, 1970

The dates you print from Calendar are local to your timezone, whereas the epoch is defined to be midnight of 1970-01-01 in UTC. So if you live in a timezone west of UTC, then your date will show up as 1969-12-31, even though (in UTC) it's still 1970-01-01.

        Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(1970, 0, 1, 0, 0, 0);
        System.out.println(cal.getTimeInMillis());

http://stackoverflow.com/questions/263376/java-util-calendar-milliseconds-since-jan-1-1970

Sunday, December 16, 2012

How to convert string to int in Java?

int foo = Integer.parseInt("1234");
See the Javadoc for more information.
(If you have it in a StringBuffer, you'll need to do Integer.parseInt(myBuffer.toString());instead).

Thursday, December 13, 2012

How to keep the servlet continously running?

27 down vote accepted
Your problem is that you misunderstand the purpose of the servlet. It's intented to act on HTTP requests, nothing more. You want just a background task which runs once on daily basis. For that Quartz or even ScheduledExecutorService is perfect. This needs to be initiated by a ServletContextListener. Here's a kickoff example:
public class Config implements ServletContextListener {

    private ScheduledExecutorService scheduler;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        scheduler = Executors.newSingleThreadScheduledExecutor();
        scheduler.scheduleAtFixedRate(new UpdateCounts(), 0, 1, TimeUnit.DAYS);
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        scheduler.shutdownNow();
    }

}
Where the class UpdateCounts look like this:
public class UpdateCounts implements Runnable {

    @Override
    public void run() {
        // Do your job here.
    }

}
To get the ServletContextListener to run, just register it in web.xml as follows:
<listener>
    <listener-class>com.example.Config</listener-class>
</listener>
Or, when you're already on Servlet 3.0, just annotate it with @WebListener
@WebListener
public class Config implements ServletContextListener {}
 

 http://www.blogger.com/blogger.g?blogID=2583984764735864664#editor