Thursday, January 19, 2012
ADF 11G : How to handle with User & Role Programatically?
public String getUserName(){
return ADFContext.getCurrent().getSecurityContext().getUserName().toUpperCase();
}
----
public boolean isUserInRole(String role){
return ADFContext.getCurrent().getSecurityContext().isUserInRole(role);
}
---- print the roles of the current user
for ( String role : ADFContext.getCurrent().getSecurityContext().getUserRoles() ) {
System.out.println("role "+role);
}
-- Check Valid User public boolean isAuthenticated() {
return ADFContext.getCurrent().getSecurityContext().isAuthenticated();
}
ADF 11G : How to Check the Existing User, Programatically?
Put the below code to AMImpl.java & Expose it as Client Interface:
------------------------------------------------------------------
public boolean isExistingUser(String userName,String password){
try {
ViewObjectImpl vo = getTestusersView1();
RowSetIterator itr = vo.createRowSetIterator(null);
while (itr.hasNext()) {
Row row = itr.next();
if (row.getAttribute("Username").equals(userName) &&
row.getAttribute("Password").equals(password)) {
return true;
} //end if
} //end while
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
------------------------------------------------------------------
public boolean isExistingUser(String userName,String password){
try {
ViewObjectImpl vo = getTestusersView1();
RowSetIterator itr = vo.createRowSetIterator(null);
while (itr.hasNext()) {
Row row = itr.next();
if (row.getAttribute("Username").equals(userName) &&
row.getAttribute("Password").equals(password)) {
return true;
} //end if
} //end while
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
How to Display a PDF on JSP page as the part of Applet?
Put this below code in .jspx file
-------------------------------------
-------------------------------------
import java.applet.*;
import java.awt.*;
import java.net.URL;
@author Neelmani Jaiswal * @version 1.0 */
public class PDFViewer extends Applet {
Font font = new Font("Dialog", Font.BOLD, 24);
String str = "PDF Viewer";
int xPos = 5;
/** * Returns information about this applet. * @return a string of information about this applet */
public String getAppletInfo() {
return "PDFViewer\n" + "\n" + "This type was created in VisualAge.\n" + "";
}
/** * Draws the text on the drawing area. * @param g the specified Graphics window */
public void paint(Graphics g) {
g.setFont(font);
g.setColor(Color.black);
g.drawString(str, xPos, 50);}
/** * Called to start the applet. You never need to call this method * directly, it is called when the applet's document is visited. * @see #init * @see #stop * @see #destroy */
public void start() {
super.start();
// insert any code to be run when the applet starts here try {
URL url = new URL( "http://xxx.xx.x.x:xxxx/daw/disp/123.pdf" );
this.getAppletContext().showDocument( url, "_blank" );
} catch (Exception e) {
System.err.println( "Error: Could not display PDF document!" );
}
}
}
-------------------------------------
-------------------------------------
import java.applet.*;
import java.awt.*;
import java.net.URL;
@author Neelmani Jaiswal * @version 1.0 */
public class PDFViewer extends Applet {
Font font = new Font("Dialog", Font.BOLD, 24);
String str = "PDF Viewer";
int xPos = 5;
/** * Returns information about this applet. * @return a string of information about this applet */
public String getAppletInfo() {
return "PDFViewer\n" + "\n" + "This type was created in VisualAge.\n" + "";
}
/** * Draws the text on the drawing area. * @param g the specified Graphics window */
public void paint(Graphics g) {
g.setFont(font);
g.setColor(Color.black);
g.drawString(str, xPos, 50);}
/** * Called to start the applet. You never need to call this method * directly, it is called when the applet's document is visited. * @see #init * @see #stop * @see #destroy */
public void start() {
super.start();
// insert any code to be run when the applet starts here try {
URL url = new URL( "http://xxx.xx.x.x:xxxx/daw/disp/123.pdf" );
this.getAppletContext().showDocument( url, "_blank" );
} catch (Exception e) {
System.err.println( "Error: Could not display PDF document!" );
}
}
}
ADF 11G : Create/update Row Programatically(Entity Object)
Put below code inside EOImpl.java
------------------------------------------
public void createDept(String deptId, String deptName){
System.out.println("-1-");
String entityName = "model.Dept";
System.out.println("-2-");
EntityDefImpl deptDef = EntityDefImpl.findDefObject(entityName);
System.out.println("-3-");
EntityImpl newDept = deptDef.createInstance2(getDBTransaction(), null); System.out.println("-4-");
newDept.setAttribute("DeptId", deptId);
newDept.setAttribute("DeptName", deptName);
System.out.println("-5-");
try{
getDBTransaction().commit();
System.out.println("try");
}catch(JboException ex){
System.out.println("try");
throw ex;
}
}
http://jneelmani.blogspot.in/2013/05/adf-11g-how-to-insert-row.html
**************************************************************
public void updateDeptName(String deptId, String deptName){
EntityImpl dept = (findDepartment(deptId);
if(dept != null){
dept.setAttribute("DepartmentName", deptName);
try{ getDBTransaction().commit();
}catch(JboException ex){
getDBTransaction().rollback();
throw ex;
}
}
}
http://jneelmani.blogspot.in/2013/05/adf-11g-how-to-edit-row-programatically.html
http://jneelmani.blogspot.in/2013/05/adf-11g-how-to-remove-row.html
------------------------------------------
public void createDept(String deptId, String deptName){
System.out.println("-1-");
String entityName = "model.Dept";
System.out.println("-2-");
EntityDefImpl deptDef = EntityDefImpl.findDefObject(entityName);
System.out.println("-3-");
EntityImpl newDept = deptDef.createInstance2(getDBTransaction(), null); System.out.println("-4-");
newDept.setAttribute("DeptId", deptId);
newDept.setAttribute("DeptName", deptName);
System.out.println("-5-");
try{
getDBTransaction().commit();
System.out.println("try");
}catch(JboException ex){
System.out.println("try");
throw ex;
}
}
http://jneelmani.blogspot.in/2013/05/adf-11g-how-to-insert-row.html
**************************************************************
public void updateDeptName(String deptId, String deptName){
EntityImpl dept = (findDepartment(deptId);
if(dept != null){
dept.setAttribute("DepartmentName", deptName);
try{ getDBTransaction().commit();
}catch(JboException ex){
getDBTransaction().rollback();
throw ex;
}
}
}
http://jneelmani.blogspot.in/2013/05/adf-11g-how-to-edit-row-programatically.html
http://jneelmani.blogspot.in/2013/05/adf-11g-how-to-remove-row.html
ADF 11G : How to add the ViewCriteria Programatically
public String viewCriteria() {
// Add event code here...
System.out.println("viewCriteria()");
String amDef = "model.AppModule";
String config = "AppModuleLocal";
ApplicationModule am = Configuration.createRootApplicationModule(amDef, config); ViewObject empVO=am.findViewObject("EmpView1");
System.out.println("-1-");
ViewCriteria vc= empVO.createViewCriteria();
ViewCriteriaRow promotionRow = vc.createViewCriteriaRow();
System.out.println("-2-");
promotionRow.setAttribute("Age", ">= '"+11+"'");
System.out.println("-3-");
promotionRow.setAttribute("Salary", "> '"+1000+"'");
System.out.println("-4-");
vc.addElement(promotionRow);
System.out.println("-5-");
empVO.applyViewCriteria(vc);
System.out.println(empVO.getQuery());
System.out.println("-Executing Query-");
empVO.executeQuery();
return null;
}
// Add event code here...
System.out.println("viewCriteria()");
String amDef = "model.AppModule";
String config = "AppModuleLocal";
ApplicationModule am = Configuration.createRootApplicationModule(amDef, config); ViewObject empVO=am.findViewObject("EmpView1");
System.out.println("-1-");
ViewCriteria vc= empVO.createViewCriteria();
ViewCriteriaRow promotionRow = vc.createViewCriteriaRow();
System.out.println("-2-");
promotionRow.setAttribute("Age", ">= '"+11+"'");
System.out.println("-3-");
promotionRow.setAttribute("Salary", "> '"+1000+"'");
System.out.println("-4-");
vc.addElement(promotionRow);
System.out.println("-5-");
empVO.applyViewCriteria(vc);
System.out.println(empVO.getQuery());
System.out.println("-Executing Query-");
empVO.executeQuery();
return null;
}
ADF 11G : How to Validate the email in ADF ?
ADF 11G : How to Validate the email in ADF ?
There are multiple way to handle the validation in ADF framework. Few of them are mentioned here.
APPROACH-1
APPROACH-2
/**
* Validation method for Email.
*/
public boolean validateEmail(String email) {
System.out.println(email);
String expression="^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
CharSequence inputStr=email;
Pattern pattern=Pattern.compile(expression);
Matcher matcher=pattern.matcher(inputStr);
String msg="Email is not in Proper Format";
if(matcher.matches()){
return true;
}
else{
return false;
}
}
----------------------------------------------------------------------------------
APPROACH-3
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_ERROR);
message.setSummary("Email is not valid.");
message.setDetail("Email is not valid.");
facesContext.addMessage("userForm:Email", message);
throw new ValidatorException(message);
}
*********************************************
int dot1pos = mData.indexOf('.');
int dot2pos = mData.lastIndexOf('.');
int ln = mData.length();
if (dot1pos != 3 dot2pos != 7 ln != 12) {
throw new DataCreationException(null,"Invalid phone number - format is xxx.xxx.xxxx",null);
}
There are multiple way to handle the validation in ADF framework. Few of them are mentioned here.
APPROACH-1
APPROACH-2
/**
* Validation method for Email.
*/
public boolean validateEmail(String email) {
System.out.println(email);
String expression="^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
CharSequence inputStr=email;
Pattern pattern=Pattern.compile(expression);
Matcher matcher=pattern.matcher(inputStr);
String msg="Email is not in Proper Format";
if(matcher.matches()){
return true;
}
else{
return false;
}
}
----------------------------------------------------------------------------------
APPROACH-3
public boolean validateEmail(String email) {
int i=0;
i= email.indexOf("@");
if(i >= 0)
return true;
else
return false; }
/** * Validation method for ConfirmEmail. */
public boolean validateConfirmEmail(String confirmemail) {
if(getEmail().equalsIgnoreCase(confirmemail))
return true;
else
return false;
}
-------------------------------------------------------------------------------------
if(!emailId.contains("@")) { FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_ERROR);
message.setSummary("Email is not valid.");
message.setDetail("Email is not valid.");
facesContext.addMessage("userForm:Email", message);
throw new ValidatorException(message);
}
*********************************************
int dot1pos = mData.indexOf('.');
int dot2pos = mData.lastIndexOf('.');
int ln = mData.length();
if (dot1pos != 3 dot2pos != 7 ln != 12) {
throw new DataCreationException(null,"Invalid phone number - format is xxx.xxx.xxxx",null);
}