Thank you for visiting! If you liked the site, please add a bookmark, else add a critical comment! Would you like to visit Greece? Traditional Greek flag This real estate site is only available in Greek! The holy mountain

Tuesday 13 July 2010

Getting the current date in your model layer

Besides the standard way of getting the current date as a String, one could come across the need of inserting in a database table column of date datatype. Thus, the well known code snippet won't be suitable:

 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
 
 java.util.Date date = new java.util.Date();
 
 String datetime = dateFormat.format(date);
 
 System.out.println("Current Date Time : " + datetime);
 
In case you use ADF business components for your Model project, you get the 
current date in a slightly different way. Consider for example the following code snippet,
from an adf business components, employees entity object: 
 
import oracle.jbo.domain.Date;
...

              
protected void doDML(int operation, TransactionEvent e) {
                if (operation == DML_UPDATE) {
    ...
               histStartDate = row.getEndDate();
              /*Check whether end date equals current date*/
              if (histStartDate.equals(new Date(Date.getCurrentDate()))){
                  System.out.println("End date equals current date!");
                  }
      
In case you need to convert  oracle.jbo.domain.Date to java.util.Date, please consult this:

http://forums.oracle.com/forums/thread.jspa?threadID=2185902&tstart=15

or

http://www.ecotronics.ch/webdesign/javadate.htm
  

Finally, in case you prefer EJB's for your Model project instead, you might consider the following example:

/*Typical current date for DB inserts */
java.util.Date today = new java.util.Date();
java.sql.Date bidDate = new java.sql.Date(today.getTime());
/* public void setBidDate(Date bidDate)*/
newBid.setBidDate(bidDate);

Which is in a sense, the most paradox way of all, since in order to get the current date, you actually need to call getTime()!

References

More help about: using timestamps
http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

Comments on Oracle XSQL, by M.Thomas

On the bright side, the text is well written, in American English, with a few spelling errors. It provides an introduction to PL/SQL, database design, Oracle Text for Oracle 9i, XML, including XSL, XPath and xsql, which is now considered obsolete(?). Its source code works and could be reused. On the dark side, the book is not an introductory level one, the chapters from the beginning to 14 are more reference like, until the true tutorial building of the web application begins. A javascript or java, introduction is missing, and the paragraphs about XPath are difficult to understand.
All in all, the book can be well used as a reference, provided that several corrections need to be made by the reader, in both its source code and text.

Monday 12 July 2010

JDeveloper 11g:Using Log4j in EJB projects

Apart from the usual web client interface, a EJB 3 application can be accessed also by a plain Java Standard Edition client. This post clarifies the use of Log4j in the latter form of client. To add the Log4j jar library, double click the project, so that the window of the image appears. As for web based (view) client J2EE projects you can read it here:

All that 's written there is still valid. I would like to add a couple of things. First, you need to create the log4j.properties file in the following path:
C:\...\Model\src\
A copy of the log4j.properties file is given here for your convenience:
# **Set root logger level to DEBUG and its only appender to A.
log4j.rootLogger=DEBUG, A
# ***** A is set to be a ConsoleAppender.
log4j.appender.A=org.apache.log4j.ConsoleAppender
# ***** A uses PatternLayout.
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Secondly, when it comes to EJB model projects, you need to add the following code in italics, in order to avoid the expensive use of System.out.println() in your client classes:
...
import org.apache.log4j.Logger;


public class PlaceOrderClient {
static Logger logger =Logger.getLogger("PlaceOrderClient");
/* instead of Logger logger = Logger.getLogger(this.getClass().getSimpleName());*/

public static void main(String [] args) {
try {
final Context context = getInitialContext();
PlaceOrder placeOrder = (PlaceOrder)context.lookup("ejb3inaction-Model-PlaceOrder#actionbazaar.buslogic.PlaceOrder");
if (logger.isDebugEnabled())
             logger.debug("Exercising PlaceOrder EJB...");
// System.out.println("Exercising PlaceOrder EJB...");

The full source code is available on OTN:

http://www.oracle.com/technetwork/indexes/samplecode/jdeveloper-adf-sample-522118.html 
and is given without any guarantee of support whatsoever.
 
Finally, in case you prefer working with Eclipse, the process is quite similar. See for example:
http://snippets.dzone.com/posts/show/3248
If you need more tips about appenders:
http://www.mobilefish.com/developer/log4j/log4j_quickguide_appenders.html

https://logging.apache.org/log4j/2.x/