Showing posts with label ADF. Show all posts
Showing posts with label ADF. Show all posts

Tuesday, 23 July 2019

Uploaded Image file to a File object compression using Graphics2D

Uploaded Image file to a File object compression using Graphics2D:

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import org.apache.myfaces.trinidad.model.UploadedFile;

public static File compressUploadedFile(UploadedFile file) {
OutputStream newFile1 = null;
File file1 = null;
try {
BufferedImage img = ImageIO.read(file.getInputStream());
file1 = uploadedFileToFileConverter(file);

newFile1 = new FileOutputStream(file1);                  
int width = img.getWidth();
int height = img.getHeight();
int imgWidth, imgHeight;
if (height > width) {
imgWidth = (img.getWidth() * 300)/ img.getHeight(); // 300 is the desired max image size
imgHeight = 300;
} else {
imgHeight = (img.getHeight() * 300)/ img.getWidth();
imgWidth = 300;
}
final BufferedImage bufferedImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);        
final Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.setComposite(AlphaComposite.Src);

graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

graphics2D.drawImage(img, 0, 0, imgWidth, imgHeight, null);
graphics2D.dispose();

ImageIO.write(bufferedImage, "jpg", newFile1); // Desired compress image type as jpg

} catch (IOException e) {
logger.severe(e);
}

return file1;                

}

public static File uploadedFileToFileConverter(UploadedFile uf) {
InputStream inputStream = null;
OutputStream outputStream = null;
//Add you expected file encoding here:
System.setProperty("file.encoding", "UTF-8");
File newFile = new File(uf.getFilename());
try {
inputStream = uf.getInputStream();
outputStream = new FileOutputStream(newFile);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
} catch (IOException e) {
 logger.severe(e);
}
return newFile;
}  

Image compression in Java for ADF/OAF/EBS application

There are multiple approaches to achieve this purpose as listed below:

1. Graphics2D - Uploaded File to File compression

2. Graphics2D - BlobDomain to BlobDomain

3. ImageWriter - Uploaded File to File

Of these method, I prefer Graphics2D for image compression through java better image quality and control over final image size. Using ImageWriter, I got compression to a percentage of original size but it was not exact and even the image quality was deteriorating.

Uploaded Image file to a File object compression using ImageWriter

Uploaded Image file to a File object compression using ImageWriter:

import java.awt.image.BufferedImage;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.ImageWriteParam;
import org.apache.myfaces.trinidad.model.UploadedFile;

public static File compressUploadedFile(UploadedFile file) {
OutputStream newFile1 = null;
File file1 = null;

try{  
BufferedImage image = ImageIO.read(file.getInputStream());

file1 = uploadedFileToFileConverter(file);

newFile1 = new FileOutputStream(file1);

Iterator<ImageWriter>writers =  ImageIO.getImageWritersByFormatName("jpg"); // Desired compress image type as jpg
ImageWriter writer = (ImageWriter) writers.next();

ImageOutputStream ios = ImageIO.createImageOutputStream(newFile1);
writer.setOutput(ios);

ImageWriteParam param = writer.getDefaultWriteParam();

param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.05f);   // Compress to 5% of the original size
writer.write(null, new IIOImage(image, null, null), param);

ios.close();
writer.dispose();            
} catch (IOException e) {
logger.severe(e);
}

return file1;                
} 

public static File uploadedFileToFileConverter(UploadedFile uf) {
InputStream inputStream = null;
OutputStream outputStream = null;
//Add you expected file encoding here:
System.setProperty("file.encoding", "UTF-8");
File newFile = new File(uf.getFilename());
try {
inputStream = uf.getInputStream();
outputStream = new FileOutputStream(newFile);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
} catch (IOException e) {
logger.severe(e);
}
return newFile;
}  

BlobDomain object compression using Graphics2D

BlobDomain object compression using Graphics2D:

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import java.io.OutputStream;

import javax.imageio.ImageIO;

public static BlobDomain compressBlobImage(BlobDomain blobD) {
OutputStream out = null;
BlobDomain blobDomain = new BlobDomain();

try{ 
BufferedImage img = ImageIO.read(blobD.getInputStream());

out = blobDomain.getBinaryOutputStream();
 
int width = img.getWidth();
int height = img.getHeight();
int imgWidth, imgHeight;
if (height > width) {
imgWidth = (img.getWidth() * 300)/ img.getHeight(); // 300 is the desired image size
imgHeight = 300;
} else {
imgHeight = (img.getHeight() * 300)/ img.getWidth();
imgWidth = 300;
}
final BufferedImage bufferedImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);       
final Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.setComposite(AlphaComposite.Src);

graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

graphics2D.drawImage(img, 0, 0, imgWidth, imgHeight, null);
graphics2D.dispose();

ImageIO.write(bufferedImage, "jpg", out);  // Desired compress image type as jpg

} catch (IOException e) {
logger.severe(e);
} catch (SQLException e) {
logger.severe(e);
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
logger.severe(e);
}
}
return blobDomain;             
}

Friday, 8 September 2017

ADF - Case free LOV Search

While typing a search criteria in a LOV in ADF, if you want to make the search free of Upper or Lower case characters then you can add the following method in the VOImpl java file - 

public CriteriaClauses buildViewCriteriaClauses (ViewCriteria viewCriteria){
viewCriteria.setUpperColumns(true);
CriteriaClauses criteriaClauses =  super.buildViewCriteriaClauses(viewCriteria);        

return criteriaClauses;    }

Friday, 10 June 2016

ADF - Topics for competency building

Below is the initial list of topics which can be covered to build strong competency in ADF –

1.       Worked on unbounded and bounded task flows
2.       ADF search region with View Criteria and Business Services methods
3.       Create /Update new Record
4.       Entity level and attribute level validations
5.       Master details pages with Search  and Insert update functionality in ADF
6.       Use java script in ADF pages
7.       Call PL/SQL Stored procedure
8.       Upload file data in View object.
9.       Implement popup
10.   Print page

11.   Export to excel

Sunday, 1 May 2016

ADF Interview Questions - Set 11

Question: How to connect standalone weblogic instance from jdeveloper? How to deploy application to standalone weblogic instance from jdeveloper?

Answer: To demonstrate deployment directly from JDeveloper, let's create a sample Fusion Web Application (Web) based on the HR schema. Let's create an Employee entity from the EMPLOYEES table and an Employees view based on the Employee entity. Finally, let's bundle the Employees view in the HrAppModule application module.

For the ViewController let’s create a View Activity called Employees directly in the unbounded adfc-config task flow and anEmployees.jspx JSF page for the Employees View Activity. Finally, let’s drag the Employees view from theHrAppModuleDataControl in the Data Controls and drop it onto the Employees.jspx JSF page as an ADF Form.

In order to deploy our application, we first need to create an Application Server Connection to the standalone WebLogic server. First let’s start WebLogic if not yet started. Then, in the Resource Palette (View Resource Palette) let’s create a new Application Server connection. 



In the Create Application Server Connection wizard Name and Type page specify the Connection Name, ensure that the Connection Type is set to WebLogic 10.3 and click Next. 


  
In the Authentication page specify the WebLogic server Username and Password. 


  
In the Configuration page specify the WebLogic Hostname – localhost for our development machine, the server Port – defaults to 7001 - and the Weblogic Domain name that we will be deploying on. 

  

  
In the Test page click on the Test Connection button to test the connection and verify that all tests are successful. 
  
Click Finish to save the connection which will appear in the IDE Connections in the Resource Palette. 
  
Now we are ready to deploy our application. From the Application Menu – at the top left next to the application selection – select Deploy… and then the name of the deployment. 

  
The first time around a Deployment wizard will be displayed. In the Deployment Action page select Deploy to Application Server and click Next.

In the Select Server page select the application server connection we created earlier from the listed Application Servers. Uncheck the Deploy to all server instances in the domain option if you want to deploy on a specific server instance. Leave the Deploy as a standalone Application option selected and press Next.


If you unchecked the Deploy to all server instances in the domain option in the previous page, a Server Instances page will be displayed. Select the Server instance that you will be deploying – ensure that the status of the server instance is RUNNING– and click Next.

In the Summary page verify the Deployment Summary details and click Finish to start the deployment process. The status of the deployment process is displayed in the Deployment – Log window. The deployment process first creates the Web Application Module (war), then the Enterprise Application Module (ear) and finally deploys the application onto the server instance(s). If all goes well the message Application Deployed Successfully is shown in the log window.
  

That’s it. Our application has been deployed into the standalone WebLogic server. To verify that this is the case, log in into the WebLogic administrator console – http://localhost:7001/console - and click Deployments in the Domain Structure tree. In the Summary of Deployments you should see the application we’ve just deployed.



We can access our deployed application by entering the application URL –http://127.0.0.1:7001/JDeveloperFAQNo3/faces/Employees - on the browser. At this point the application won't initialize and the Employees page won’t load until we enable security in our application. We will see how to do that some other time.

Conclusion: Once we install and configure the standalone WebLogic server in our development machine, deploying an ADF application directly onto it from JDeveloper is a piece of cake. Just make a connection to the server in JDeveloper and deploy! 



ADF Interview Questions - Set 10

Question: How to run application with -Djbo.debugoutput=console enabled in JAVA options?
Answer: Right click on project -> open project properties -> Select Run/Debug/Profile option and click on Edit -> Give -Djbo.debugoutput=console  in Java Options -> Click Ok.

Question: Can you use view object inside custom component definition?
Answer: Use of datamodel inside the Declarative components is not allowed by adf. But there is work around for that you can pass you iterator to Declarative component. You have to write the logic to get data from iterator and display on component same logic you can use to retrieve data from component and set that to iterator.

Question: What is view object?
Answer: A view object is a model object used specifically in the presentation tier. It contains the data that must display in the view layer and the logic to validate user input, handle events, and interact with the business-logic tier. The backing bean is the view object in a JSF-based application. Backing bean and view object are interchangeable terms

Question: What is a view scope?
Answer: A view-state allocates a new viewScope when it enters. This scope may be referenced within the view-state to assign variables that should live for the duration of the state. This scope is useful for manipulating objects over a series of requests from the same view.

Question: What do you mean by Bean Scope?
Answer: Bean Scope typically holds beans and other objects that need to be available in the different components of a web application.

Question: Can you use view object inside custom component definition?
Answer: Use of data model inside the Declarative components is not allowed by adf. But there is work around for that you can pass you iterator to Declarative component. You have to write the logic to get data from iterator and display on component same logic you can use to retrieve data from component and set that to iterator.

Question: Which file holds the details of users and groups in adf?
Answer: Users and Groups we usually create in Security part at the time of enabling security on your application by default some of the files gets created inside Application Resources, Inside that we will find jazn-data.xml file, once we open that file we can have access to create the user profiles, Application Roles and Enterprise roles, So your application level roles is assigned to the users whomever are created in your user profile, So each and everything from user roles to enterprise roles you are giving inside your jazn-data.xml file.

Question: Explain about JSF lifecycle.
Answer: A typical JSF lifecycle flows as follows:
1.     JSF interrogates the request to extract the incoming parameters.
2.     JSF converts these parameters from strings to their corresponding data types.
3.     JSF validates the presence of required parameters, the correctness of entered values, etc.
4.     JSF uses the incoming URL to determine who should process the request. This is usually done using post-backs.
5.     JSF will execute any custom logic that you provide it. Your logic will store work products (or Transfer Objects for the view) in the appropriate scope. JSF defines interfaces for event handlers and clear guidelines as to when a particular handler will be invoked.
6.     JSF determines the view that will display the response to the user. JSF uses the return value of your application handler to look up the desired view in a mapping file that you provide.
7.     JSF will execute any custom logic that you provide to render your response, or will forward to the desired view.

During all of this, JSF provides utilities for errors to be exposed to the user, for internationalization using resource bundles, etc. JSF does not provide a native composition facility - so you'll need to include Tiles or Facelets.


Question: How to declare the page navigation (navigation rules) in faces-config.xml file in ADF 10g?
Answer: Navigation rules tells JSF implementation which page to send back to the browser after a form has been submitted. We can declare the page navigation as follows:
<naviagation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/welcome.jsp</to-view-id>
</navigation-case>
</naviagation-rule>
This declaration states that the login action navigates to /welcome.jsp, if it occurred inside /index.jsp.

Question: How to set the range of a table?
Answer: <af:table rows=”#{bindings.LoggedInUserServiceRequests.rangeSize}”…/>

Question: What is region in Task Flow?
Answer: You can render a bounded task flow in a JSF page or page fragment (.jsff) by using an ADF region. An ADF region comprises the following. You create an ADF region by dragging and dropping a bounded task flow that contains at least one view activity or one task flow call activity to the page where you want to render the ADF region. This makes sure that the ADF region you create has content to display at runtime

Question: What is Backing Bean?
Answer: Backing beans are JavaBeans components associated with UI components used in a page. Backing-bean management separates the definition of UI component objects from objects that perform application-specific processing and hold data.
Backing beans are those managed beans which have 1:1 mapping with a page. They have getters and setters for all the components in the related page.

Backing bean is about the role a particular managed bean plays. This is a role to be a server-side representation of the components located on the page. Usually, the backing beans have a request scope, but it is not a restriction.
The backing bean defines properties and handling-logics associated with the UI components used on the page. Each backing-bean property is bound to either a component instance or its value. A backing bean also defines a set of methods that perform functions for the component, such as validating the component’s data, handling events that the component fires and performing processing associated with navigation when the component activates.

ADF Interview Questions - Set 9

Question: What is user customization?
Answer: Term personalization means user can perform changes visible only to that user. Term customization means user with admin privileges can perform global changes, visible to all users. You can achieve personalization functionality within standard ADF framework enabled with MDS. For customization functionality need to use Oracle WebCenter component - Composer.
UserCC class retrieve current user name from security context:



As per documentation, I have created all required Java classes to support Edit and View layers:


User customization and personalization are enabled to be stored across sessions with MDS. This option is needed to store changes for ADF components; Oracle WebCenter Composer is working without this option as well:


I have change customization class from SiteCC to UserCC, this enables personalization:


Session options factory is class must be configured in adf-config.xml to enable customization:


As per documentation, additionally you need to declare Oracle WebCenter Composer servlet in web.xml.
ADF Security defines two application level roles - admin and customer. Admin role is granted with Customization permission:


Session options factory class as per documentation defines two layers - Edit and View. You can see that Edit layer is applied on global level (it’s when Oracle WebCenter Composer is invoked to perform customization). View layer is applied on global and user levels (this means each user will see global customizations and their own personalization):

If Oracle WebCenter Composer is On, Edit layer is enabled, otherwise - View layer: