Sie sind auf Seite 1von 7

package hr.mobile.bootcamp.mobile.

profile ;

import java.sql.Connection ;
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
import java.sql.SQLException ;

import oracle.adf.model.datacontrols.device.DeviceManager ;
import
oracle.adf.model.datacontrols.device.DeviceManagerFactory ;

import oracle.adfmf.framework.api.AdfmfContainerUtilities ;
import oracle.adfmf.framework.api.AdfmfJavaUtilities ;
import oracle.adfmf.framework.api.AdfmfSlidingWindowOptions ;
import oracle.adfmf.framework.api.AdfmfSlidingWindowUtilities ;
import oracle.adfmf.java.beans.PropertyChangeListener ;
import oracle.adfmf.java.beans.PropertyChangeSupport ;

public class ProfileDC


{

private static UserProfile profile = null ;

private static String windowId = null ;

private PropertyChangeSupport propertyChangeSupport =


new PropertyChangeSupport( this ) ;

public void setProfile ( UserProfile profile )


{
UserProfile oldProfile = ProfileDC.profile ;
ProfileDC.profile = profile ;
propertyChangeSupport.firePropertyChange( "profile" ,
oldProfile ,
profile ) ;
}
public static UserProfile getProfile ()
{

Connection conn = getDatabaseConnection() ;


if ( conn != null )
{
profile = new UserProfile() ;
try
{
PreparedStatement stat =
conn.prepareStatement( "SELECT * FROM
PROFILE" ) ;
ResultSet rs = stat.executeQuery() ;
while ( rs.next() )
{

profile.setFirstName( rs.getString( "FIRST_NAME" ) ) ;

profile.setLastName( rs.getString( "LAST_NAME" ) ) ;


profile.setEmail( rs.getString( "EMAIL" ) ) ;

profile.setMobileNumber( rs.getString( "MOBILE" ) ) ;

profile.setDepartment( rs.getString( "DEPARTMENT" ) ) ;


profile.setPhoto( rs.getString( "IMAGE" ) ) ;
}
rs.close() ;
}
catch ( Exception e )
{
System.err.println( "Error Reading User Profile" ) ;
}
}

return profile ;
}

private static Connection getDatabaseConnection ()


{
// Get Database Connection
String dir =
AdfmfJavaUtilities.getDirectoryPathRoot( AdfmfJavaUtilities.Applica
tionDirectory ) ;

Connection conn = null ;


try
{
conn =
new SQLite.JDBCDataSource( "jdbc:sqlite:" + dir +
"/hr.db" ).getConnection() ;
}
catch ( SQLException e )
{
System.err.print( "Cannot Open Database Connection" ) ;
}

return conn ;
}

public static void chooseExistingPhoto ()


{

getPicture( DeviceManager.CAMERA_SOURCETYPE_PHOTOLIBRARY
);
}

public static void takePhoto ()


{
getPicture( DeviceManager.CAMERA_SOURCETYPE_CAMERA ) ;
}

private static void getPicture ( int sourceType )


{
DeviceManager dm =
DeviceManagerFactory.getDeviceManager() ;
String picture =
dm.getPicture( 100 ,
DeviceManager.CAMERA_DESTINATIONTYPE_FILE_URI ,
sourceType , true ,
DeviceManager.CAMERA_ENCODINGTYPE_JPEG ,
100 ,
100 ) ;
profile.setPhoto( picture ) ;
AdfmfJavaUtilities.flushDataChangeEvent() ;
}

public void save ()


{
Connection conn = getDatabaseConnection() ;

if ( conn != null )
{
try
{
PreparedStatement stat =
conn.prepareStatement( "SELECT * FROM
PROFILE" ) ;
ResultSet rs = stat.executeQuery() ;
// row exists, then update
if ( rs.next() )
{
PreparedStatement stmt =
conn.prepareStatement( "UPDATE PROFILE
SET FIRST_NAME = ? , LAST_NAME = ? , EMAIL = ? , MOBILE = ? ,
IMAGE = ? , DEPARTMENT = ?" ) ;

stmt.setString( 1 , profile.getFirstName() ) ;
stmt.setString( 2 , profile.getLastName() ) ;
stmt.setString( 3 , profile.getEmail() ) ;
stmt.setString( 4 ,
profile.getMobileNumber() ) ;
stmt.setString( 5 , profile.getPhoto() ) ;
stmt.setString( 6 , profile.getDepartment() ) ;

stmt.executeUpdate() ;
conn.commit() ;

}
// row doesn't exists, then create
else
{

PreparedStatement stmt =
conn.prepareStatement( "INSERT INTO
PROFILE ('FIRST_NAME', 'LAST_NAME' , 'EMAIL' , 'MOBILE' , 'IMAGE' ,
'DEPARTMENT') values (?,?,?,?,?,?);" ) ;

stmt.setString( 1 , profile.getFirstName() ) ;
stmt.setString( 2 , profile.getLastName() ) ;
stmt.setString( 3 , profile.getEmail() ) ;
stmt.setString( 4 ,
profile.getMobileNumber() ) ;
stmt.setString( 5 , profile.getPhoto() ) ;
stmt.setString( 6 , profile.getDepartment() ) ;

stmt.executeUpdate() ;
conn.commit() ;

}
}
catch ( Exception e )
{

AdfmfContainerUtilities.invokeContainerJavaScriptFunction( AdfmfJ
avaUtilities.getFeatureId() ,

"navigator.notification.alert" , new Object[] {


"Error..." ,
null ,
"Can't save
profile..." ,
"Ok"
});
return ;
}

hideProfilePage() ;

}
}

public void showProfilePage ()


{

if ( this.windowId == null )
this.setWindowId( AdfmfSlidingWindowUtilities.create( "hr.mobile.bo
otcamp.UserProfile" ) ) ;

AdfmfSlidingWindowOptions windowOptions =
new AdfmfSlidingWindowOptions() ;

windowOptions.setDirection( AdfmfSlidingWindowOptions.DIRECTIO
N_BOTTOM ) ;

windowOptions.setStyle( AdfmfSlidingWindowOptions.STYLE_OVERL
AID ) ;
windowOptions.setDuration( 500 ) ;

windowOptions.setSize( "100%" ) ;
AdfmfSlidingWindowUtilities.show( getWindowId() ,
windowOptions ) ;

public void hideProfilePage ()


{
if ( this.windowId == null )

this.setWindowId( AdfmfSlidingWindowUtilities.create( "hr.mobile.bo


otcamp.UserProfile" ) ) ;

AdfmfSlidingWindowUtilities.hide( getWindowId() ) ;
}

public void setWindowId ( String windowId )


{
String oldWindowId = ProfileDC.windowId ;
ProfileDC.windowId = windowId ;
propertyChangeSupport.firePropertyChange( "windowId" ,
oldWindowId ,
windowId ) ;
}
public static String getWindowId ()
{
return windowId ;
}

public void addPropertyChangeListener ( PropertyChangeListener


l)
{
propertyChangeSupport.addPropertyChangeListener( l ) ;
}

public void removePropertyChangeListener


( PropertyChangeListener l )
{
propertyChangeSupport.removePropertyChangeListener( l ) ;
}

Das könnte Ihnen auch gefallen