Monday, February 5, 2018

Migrate Coneigniter from MySQL to Oracle

Hi,

I was migrating an existed system developed using Codeiginter 2.1.4 and MySQL database into Oracle database.

Definitely there was several things to migrate in DML and queries such as MySQL built-in functions that are not existed in Oracle like (LIMIT, CONCAT, DATE_ADD, DATE_FORMAT etc..) .. but the most annoying thing was case-sensitive issue in tables and columns names.

As an Oracle developer, I was never cared about columns and tables characters case in queries .. but I fond that Codeigniter has another opinion.

For example if I have a database column called "ID" (in capital letters), the following PHP result-set will not work:

$user = $this->db->get_where('users_table', array('username' => $username, 'password' => $password, 'state' => 1, 'deleted' => 0));                
        if($user->num_rows() == 1)
        {
            $user = $user->row();             
            $user_id = $user->id;

        }

Because column name is "ID" not "id" thus I couldn't get the column value, if it changed to $user_id = $user->ID; it will work.

There's two solutions:

  • Either user same case for both database and PHP code.

You can change the Codeigniter system and make all attributes in lower case:
I quote from thread:

To get object attributes lowercase do next:
1. go to db_result.php near line 75
2. find method result_object()
3. change:

Code:
while ($row = $this->_fetch_object())
        {
            $this->result_object[] = $row;
       
        }
with:
Code:
while ($row = $this->_fetch_object())
        {
            $new_row = array();
            foreach ($row as $key => $value) {
                    $new_row[strtolower($key)] = $value;
            }
            $this->result_object[] = (object) $new_row;
       
        }

And for getting result as array:

1. open oci8_result.php
2. find method result_array()
3. change:

Code:
while ($this->_fetch_assoc($row))
        {
           
            $this->result_array[] = $row;
        }

with:

Code:
while ($this->_fetch_assoc($row))
        {
           
            $new_row = array();
            foreach ($row as $key => $value) {
                    $new_row[strtolower($key)] = $value;
            }
           
            $this->result_array[] = $new_row;
        }
 

Wednesday, January 4, 2012

How to send an e-mail using Glassfish JavaMail session

Oracle Glassfish 3 application server offers a support for JavaMail API, which make sending and receiving e-mail quite easy and effective.

First you have to create new JavaMail session at Glass fish, login into Glassfish admin console --> JavaMail Sessions --> Click "New"
Then fill the following data:


JNDI Name should be at this syntax mail/<Something>
Mail Host is the SMTP that you will use to send the mail
Default user is the username that defined at your SMTP

Now if your SMTP require an authentication (need password too), move to the button of the add JavaMail session page and add these three properties:
 
  
mail.smtp.password is your password at the SMTP (for the user you defined above)
mail.smtp.auth is ture (SMTP require authentication)
mail.smtp.port is SMTP port number

Done, now you can use this JavaMail session, make Java class similar to this one:

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class SendMail {

    public void sendMail() throws MessagingException, UnsupportedEncodingException {
        try {
            InitialContext ctx = new InitialContext();
            Session mySession = (Session) ctx.lookup("mail/ozienadden@omniya.sy");
            Message msg = new MimeMessage(mySession);
            msg.setSubject("Hello World !");
            msg.setRecipient(RecipientType.TO, new InternetAddress("ozienadden@omniya.sy", "Osama"));
            msg.setFrom(new InternetAddress("automated@omniya.sy", "Virtual Sender"));
            msg.setSentDate(new Date());
            msg.setSubject("Speed Change Request");
            msg.setText("This is my test");
            Transport.send(msg);
        } catch (NamingException ex) {
            Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE, null,ex);
        }
    }
}

Tuesday, January 3, 2012

How to create Oracle DataSource in Glassfish

Oracle (previously Sun) Glassfish web server is really great Java EE open-source application server for small and midsize applications.

Glassfish is free, supports all Java EE API specifications, rich in features (JDBC, RMI, e-Mail, JMS, web services, JavaBeans, Connectors, servlets, portlets ..etc..)

At other hand, Glassfish has really easy to use and effective admin console, by default, you can access it from server IP address at port 4848:
http://127.0.0.1:4848/

At this post I'll describe a small thing about Glassfish for newbies, It's about how to make JDBC data source with Oracle database and how to use it at Java code.

First Login into Glassfish admin console, other steps are described clearly via images bellow:

1- Make new JDBC connection pool:


2- Select "java.sql.Driver" as resource type, also don't forget to select the driver vendor (Oracle at my case)






3-At step 2, fill the connection URL + user-name and password of your database connection:



4-Finally, access the new pool and make "Ping" enabled, ping at this case means check connection, If you got "Ping Succeeded" message that means you can obtain a connection with the database:


5-Now you have to create JDBC resource uses the pool that we just created:


6-Finished, all what you need now is to use this JDBC resource at your Java code as follow:


Thursday, November 10, 2011

Clone Virtual Box machine

I have existing Virtual Box machine, Rehat 5.5 and Oracle database are installed and configured.

I want to create Oracle Data Guard standby database, so I need to clone the existing one in order to save time and here's how to do it:

Operating System: CentOS 5.5 64 bit
Oracle Virtual Box version: 4.0.6
The first machine name: DataGuard1 existed at /home/osama/VirtualBox VMs

1-Clone the virtual disk:
[root@omniyapc osama]# VBoxManage clonevdi /home/osama/VirtualBox\ VMs/DataGuard1/DataGuard1.vdi /home/osama/VirtualBox\ VMs/DataGuard2/DataGuard2.vdi
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone hard disk created in format 'VDI'. UUID: 4413e40f-f3e5-4aa0-858a-873bb539cf26


Note that Virtual Box will create the needed directories by itself.

2-Create new Virtual machine, and chose the cloned virtual disk.
When tried, I've get this error:
That's because permissions, as you note I cloned the disk using root user, simple change the owner and the group of the "DataGuard2" directory into the right user:

[root@omniyapc osama]# chown osama VirtualBox\ VMs/DataGuard2/ -R
[root@omniyapc osama]# chgrp osama VirtualBox\ VMs/DataGuard2/ -R


Then try using the disk again:

It works actually, but I disappointed again because Redhat didn't start and give me this error:
It could not mount the cloned disk, after double check the difference between it and the original machine, I found that the original machine disks type was IDE, while the newly created one are SATA, after removing the SATA disk and adding it as IDE one it works and Redhat starts successfully.

At other hand I got this dialog at Virtual Box manager:

and this one too after shutting down the new machine:

This message disappeared after closing and re-opening Virtual Box, it seems that it cashing some thing, actually I'm not sure why I got it but that's solve my problem.

Sunday, October 16, 2011

Session timeout at Apache 2.2

I have Oracle BI Publisher 10.1.3.4 deployed over Apache Tomcat 7 using it as a reporting tool.
At other hand, there's Apache web server 2.2 used as proxy server and it redirects the reporting requests to Tomcat.


Today I finished building some new report, actually it consumes much time to execute, when try it from BI Publisher that deployed at Tomcat it works perfectly, but when try to access it via the redirector (httpd 2.2) it gives this error:

Proxy Error

The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /xmlpserver///Dialup_Reports/Geographical_Total_Calls/Geographical_Total_Calls.xdo.
Reason: Error reading from remote server

Apache/2.2.3 (CentOS) Server at 31.193.78.197 Port 80
 
Actually that was because of the session timeout of Apache server 2.2 (by default it's 120 seconds) :


So the problem sovled by increasing the "Timeout" value to 600 (5 minutes) and adding "ProxyTimeout 600" directive.
Finally restart the httpd service and it should work well.

Tuesday, September 13, 2011

Configure VNC at Centos 5.5 Linux


Intro:

As former Windows user, I used to lean on remote desktop service in order to access another Windows computer over the network.
That was the single practical way to deal with remote computer for Windows, because there (at Windows) every thing is done using GUI.

At Linux it's a bit different, because you can almost do everything using secure shell (ssh) connection, just access the target computer shell and enjoy ;)
But some times you need (or prefer) to use Windows way in order to access other Linux machine, and one of the most famous way to do this is using Virtual Networked Computing (VNC).
This is simple tutorial shows how much it's easy and effective to use VNC.

Requirements:

The VNC method is based on using a VNC Server installed and running over the target machine, and using VNC Viewer installed and running over your machine which you could use it to communicate with target machine VNC Server.

At this tutorial, at both machines there's CentOS Linux 5.5 installed, VNC server and viewer are available at CentOS updates repository and you can install them using yum:

# yum install vnc-server
# yum install vnc
At other hand you can check if VNC is installed by using rpm to query VNC installed packages:
# rpm -qa | grep vnc
vnc-4.1.2-14.el5_6.6
vnc-server-4.1.2-14.el5_6.6
At target machine:

As I described, VNC server should be installed at the target machine, after finishing this step you should add a user to this server in order to use it for accessing.

Edit the file /etc/sysconfig/vncservers as root user and add this couple lines to the end of the file:

# nano /etc/sysconfig/vncservers
VNCSERVERS="1:osama"
VNCSERVERARGS[1]="-geometry 800x600 -depth 16 -nolisten tcp -nohttpd -localhost"
The first line sets up a user (osama) for the NVC server, surly you can add any additional number of users here.
Whereas the second line describes the argument (configuration) for each user, ie:
VNCSERVERARGS[1] <=> VNCSERVERS="1:osama"

Geometry argument describes the screen resolution (800 pixel x 600 pixel), Depth argument describes the color depth, nolisten tcp argument used to prevent X connections to your VNC server via TCP, nohttpd argument used to prevent web-based VNC clients connecting., and finally localhost argument used to prevent remote VNC clients connecting except when doing so through a secure tunnel.

Tip about the ports:

When you try to connect using VNC, you need to specify the port number used for this connection, to know the used port you should calculate it using this tricky way:
5900 + (Session number) = VNC Port
The session number is the defined number at “/etc/sysconfig/vncservers” file, for our case it was “1” for the user “osama”, VNCSERVERS="1:osama".
So the used port number for our case is : 5900 + 1 = 5901

Setting a password:

VNC need to specify a password before a connection can be established, you can do so using “vncpasswd” command:
# vncpasswd
Password:
Verify:

Starting and stopping the server:

You can start the VNC server using “vncserver” command followed by the session number that you configured at “/etc/sysconfig/vncservers” file:
# vncserver :1
New 'omniyapc:1 (osama)' desktop is omniyapc:1
Creating default startup script /root/.vnc/xstartup
Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/omniyapc:1.log
Now the server is started and a user could connect.
For stopping the VNC server use the command “vncserver” followed by “kill” parameter and the session number:

# vncserver -kill :1
Killing Xvnc process ID 30857
 
Configuring VNC server

By default, the connected users will get gray desktop because the connection will not start new session for X server, so you need to edit the startup script in the .vnc folder in the home directory:

# nano /root/.vnc/xstartup
Then un-comment these two lines and save:

unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc
But remember please that you have to restart the “vncserver” after this update.

At your machine:

As I described you should install “vnc” package at your machine in order to use it for connecting VNC server, and thus you can use it like this:

# vncviewer 192.168.30.15:5901
Which “192.168.30.15” is the remote machine IP address (surly you can use it defined name at your DNS) and “5901” is the calculated port that we talked about at previous section.

Then, VNC Viewer will ask you to enter the remote VNC server password that we defined at previous steps:


Enter the password and “play” remotely with visual desktop ;)

Sunday, September 11, 2011

The first "Date" with Apache Wicket !

Many JAVA developers didn't hear about Apache Wicket before, I was one of them too, but a happy occasion happened and I met Wicket !


When you are planning to build JAVA web application you have many choices, the first one is JSP and JAVA servlets, but for advanced application you need a framework make things more easy and enhance your productivity.


I've searched about JAVA frameworks before, I've checked Struts2, Spring MVC, JBoss Seam, Apache Tapestry and JSF, but actually I didn't applied them into any application, I've just tested them a little, so I didn't fall in love with these frameworks really !


Last month I decided to begin new "ambitious" project, and I was need a free JAVA framework, at other hand I was afraid of using these frameworks because of "XML tags soup", so Wicket was my decision at last for these reasons:


  1. It's free and open source
  2. It's Component Centric approach
  3. Swing-like Object Oriented Component Model
  4. No XML soup
  5. Just plain Java and HTML 
  6. There's many reuseable and flexible components
I had a look at couple books, Wicket in action and  Wicket Cookbook, the first one aimed for bigginers but I found it somehow "buggy" and confusing, while Wicket Cookbook is more clear and divided to sample with clear and complete description, so I recommend to go for Wicket website tutorials and examples first, then you can read Wicket Cookbook for great and usefull Wicket capabilities.

I'll post a tutorials about Wicket later, but for now if you tried Wicket let'us know your opinion ;)